-
Notifications
You must be signed in to change notification settings - Fork 16
/
MultiCacheSim.cpp
142 lines (112 loc) · 3.52 KB
/
MultiCacheSim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "MultiCacheSim.h"
MultiCacheSim::MultiCacheSim(FILE *cachestats, int size, int assoc, int bsize, CacheFactory c){
cacheFactory = c;
CacheStats = cachestats;
num_caches = 0;
cache_size = size;
cache_assoc = assoc;
cache_bsize = bsize;
#ifndef PIN
pthread_mutex_init(&allCachesLock, NULL);
#else
PIN_InitLock(&allCachesLock);
#endif
}
SMPCache *MultiCacheSim::findCacheByCPUId(unsigned int CPUid){
std::vector<SMPCache *>::iterator cacheIter = allCaches.begin();
std::vector<SMPCache *>::iterator cacheEndIter = allCaches.end();
for(; cacheIter != cacheEndIter; cacheIter++){
if((*cacheIter)->CPUId == CPUid){
return (*cacheIter);
}
}
return NULL;
}
void MultiCacheSim::dumpStatsForAllCaches(bool concise){
std::vector<SMPCache *>::iterator cacheIter = allCaches.begin();
std::vector<SMPCache *>::iterator cacheEndIter = allCaches.end();
for(; cacheIter != cacheEndIter; cacheIter++){
if(!concise){
(*cacheIter)->dumpStatsToFile(CacheStats);
}else{
fprintf(CacheStats,"CPUId, numReadHits, numReadMisses, numReadOnInvalidMisses, numReadRequestsSent, numReadMissesServicedByOthers, numReadMissesServicedByShared, numReadMissesServicedByModified, numWriteHits, numWriteMisses, numWriteOnSharedMisses, numWriteOnInvalidMisses, numInvalidatesSent\n");
(*cacheIter)->conciseDumpStatsToFile(CacheStats);
}
}
}
void MultiCacheSim::createNewCache(){
#ifndef PIN
pthread_mutex_lock(&allCachesLock);
#else
PIN_GetLock(&allCachesLock,1);
#endif
SMPCache * newcache;
newcache = this->cacheFactory(num_caches++, &allCaches, cache_size, cache_assoc, cache_bsize, 1, "LRU", false);
allCaches.push_back(newcache);
#ifndef PIN
pthread_mutex_unlock(&allCachesLock);
#else
PIN_ReleaseLock(&allCachesLock);
#endif
}
void MultiCacheSim::readLine(unsigned long tid, unsigned long rdPC, unsigned long addr){
#ifndef PIN
pthread_mutex_lock(&allCachesLock);
#else
PIN_GetLock(&allCachesLock,1);
#endif
SMPCache * cacheToRead = findCacheByCPUId(tidToCPUId(tid));
if(!cacheToRead){
return;
}
cacheToRead->readLine(rdPC,addr);
#ifndef PIN
pthread_mutex_unlock(&allCachesLock);
#else
PIN_ReleaseLock(&allCachesLock);
#endif
return;
}
void MultiCacheSim::writeLine(unsigned long tid, unsigned long wrPC, unsigned long addr){
#ifndef PIN
pthread_mutex_lock(&allCachesLock);
#else
PIN_GetLock(&allCachesLock,1);
#endif
SMPCache * cacheToWrite = findCacheByCPUId(tidToCPUId(tid));
if(!cacheToWrite){
return;
}
cacheToWrite->writeLine(wrPC,addr);
#ifndef PIN
pthread_mutex_unlock(&allCachesLock);
#else
PIN_ReleaseLock(&allCachesLock);
#endif
return;
}
int MultiCacheSim::getStateAsInt(unsigned long tid, unsigned long addr){
SMPCache * cacheToWrite = findCacheByCPUId(tidToCPUId(tid));
if(!cacheToWrite){
return -1;
}
return cacheToWrite->getStateAsInt(addr);
}
int MultiCacheSim::tidToCPUId(int tid){
//simple for now, perhaps we want to be fancier
return tid % num_caches;
}
char *MultiCacheSim::Identify(){
SMPCache *c = findCacheByCPUId(0);
if(c != NULL){
return c->Identify();
}
return 0;
}
MultiCacheSim::~MultiCacheSim(){
std::vector<SMPCache *>::iterator cacheIter = allCaches.begin();
std::vector<SMPCache *>::iterator cacheEndIter = allCaches.end();
for(; cacheIter != cacheEndIter; cacheIter++){
delete (*cacheIter);
}
}