-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcachebench.cpp
177 lines (137 loc) · 3.78 KB
/
cachebench.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <sys/time.h>
#include "CacheCore.h"
#include "Report.h"
#include "SescConf.h"
class SampleState : public StateGeneric<long> {
public:
int32_t id;
SampleState(int32_t lineSize) {
id = 0;
}
bool operator==(SampleState s) const {
return id == s.id;
}
};
typedef CacheGeneric<SampleState, long> MyCacheType;
MyCacheType *cache;
timeval stTime;
timeval endTime;
double nAccess;
double nMisses;
void startBench() {
nAccess = 0;
nMisses = 0;
gettimeofday(&stTime, 0);
}
void endBench(const char *str) {
gettimeofday(&endTime, 0);
double usecs = (endTime.tv_sec - stTime.tv_sec) * 1000000 + (endTime.tv_usec - stTime.tv_usec);
fprintf(stderr, "%s: %8.2f Maccesses/s %7.3f%%\n", str, nAccess / usecs, 100 * nMisses / nAccess);
}
#define MSIZE 256
double A[MSIZE][MSIZE];
double B[MSIZE][MSIZE];
double C[MSIZE][MSIZE];
void benchMatrix(const char *str) {
MSG("Benchmark a code like a matrix multiply: %s", str);
startBench();
MyCacheType::CacheLine *line;
for(int32_t i = 0; i < MSIZE; i++) {
for(int32_t j = 0; j < MSIZE; j++) {
// A[i][j]=0;
// A[i][j]=...
line = cache->writeLine((long)&A[i][j]);
nAccess++;
if(line == 0) {
cache->fillLine((long)&A[i][j]);
nAccess++;
nMisses++;
}
for(int32_t k = 0; k < MSIZE; k++) {
// A[i][j] += B[i][j]*C[j][k];
// = ... A[i][j]
line = cache->readLine((long)&A[i][j]);
nAccess++;
if(line == 0) {
cache->fillLine((long)&A[i][j]);
nAccess++;
nMisses++;
}
// = ... B[i][j]
line = cache->readLine((long)&B[i][j]);
nAccess++;
if(line == 0) {
cache->fillLine((long)&B[i][j]);
nAccess++;
nMisses++;
}
// = ... C[i][j]
line = cache->readLine((long)&C[i][j]);
nAccess++;
if(line == 0) {
cache->fillLine((long)&C[i][j]);
nAccess++;
nMisses++;
}
// A[i][j]=...;
line = cache->writeLine((long)&A[i][j]);
nAccess++;
if(line == 0) {
cache->fillLine((long)&A[i][j]);
nAccess++;
nMisses++;
}
}
}
}
endBench(str);
}
int main(int32_t argc, const char **argv) {
if(argc != 2) {
MSG("use: CacheSample <cfg_file>");
exit(0);
}
Report::openFile("report.log");
setenv("ESESC_tradCORE_DL1", "DL1_core DL1", 1);
SescConf = new SConfig(argc, argv);
unsetenv("ESESC_tradCore_DL1");
cache = MyCacheType::create("DL1_core", "", "tst1");
int32_t assoc = SescConf->getInt("DL1_core", "assoc");
for(int32_t i = 0; i < assoc; i++) {
ulong addr = (i << 8) + 0xfa;
MyCacheType::CacheLine *line = cache->findLine(addr);
if(line) {
fprintf(stderr, "ERROR: Line 0x%lX (0x%lX) found\n", cache->calcAddr4Tag(line->getTag()), addr);
exit(-1);
}
line = cache->fillLine(addr);
line->id = i;
}
for(int32_t i = 0; i < assoc; i++) {
ulong addr = (i << 8) + 0xFa;
MyCacheType::CacheLine *line = cache->findLine(addr);
if(line == 0) {
fprintf(stderr, "ERROR: Line (0x%lX) NOT found\n", addr);
exit(-1);
}
if(line->id != i) {
fprintf(stderr, "ERROR: Line 0x%lX (0x%lX) line->id %d vs id %d (bad LRU policy)\n", cache->calcAddr4Tag(line->getTag()),
addr, line->id, i);
exit(-1);
}
}
// cache = MyCacheType::create("PerCore_TLB","","TLB");
// benchMatrix("PerCore_TLB");
cache = MyCacheType::create("DL1_core", "", "L1");
benchMatrix("DL1_core");
#if 0
cache = MyCacheType::create("BTB","","BTB");
benchMatrix("BTB");
cache = MyCacheType::create("DM","","DM");
benchMatrix("DM");
#endif
GStats::report("Cache Stats");
Report::close();
// cache->destroy();
return 0;
}