-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.cpp
241 lines (214 loc) · 5.74 KB
/
Cache.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include "Cache.h"
#include "CPU.h"
Cache::Cache(const char *name)
: MemObj(name)
,readHits("readHits")
,readMisses("readMisses")
,writeHits("writeHits")
,writeMisses("writeMisses")
,writeBacks("writeBacks")
{
GError *error = NULL;
// Get hit delay from config file
hitDelay = g_key_file_get_integer(config->keyfile, name, "hitDelay", NULL);
if(error != NULL) g_error (error->message);
// Create cacheCore after parsing config file
int size = g_key_file_get_integer(config->keyfile, name, "size", &error);
if(error != NULL) g_error (error->message);
int assoc = g_key_file_get_integer(config->keyfile, name, "assoc", NULL);
if(error != NULL) g_error (error->message);
int bsize = g_key_file_get_integer(config->keyfile, name, "bsize", NULL);
if(error != NULL) g_error (error->message);
gchar* pStr = g_key_file_get_string(config->keyfile, name, "replPolicy", NULL);
if(error != NULL) g_error (error->message);
assert(size > 0);
assert(assoc > 0);
assert(bsize > 0);
assert(pStr != NULL);
cacheCore = new CacheCore(size, assoc, bsize, pStr);
g_free(pStr);
}
Cache::~Cache()
{
delete cacheCore;
}
void Cache::access(MemRequest *mreq)
{
mreq->addLatency(hitDelay);
if(verbose) {
const char *memOp;
switch(mreq->getMemOperation()){
case MemRead: memOp = "MemRead"; break;
case MemWrite: memOp = "MemWrite"; break;
case MemWriteBack: memOp = "MemWriteBack"; break;
default: assert(0); break;
}
printf("%s->access(%s, addr: %u, latency: %u)\n", getName().c_str(), memOp, mreq->getAddr(), mreq->getLatency());
}
switch(mreq->getMemOperation()){
case MemRead:
read(mreq);
break;
case MemWrite:
write(mreq);
break;
case MemWriteBack:
writeBack(mreq);
break;
default:
assert(0);
break;
}
}
// Get string that describes MemObj
std::string Cache::toString() const
{
std::string ret;
ret += "[" + getName() + "]\n";
ret += "device type = cache\n";
ret += "write policy = " + getWritePolicy() + "\n";
ret += "hit time = " + std::to_string(hitDelay) + "\n";
ret += cacheCore->toString();
ret += "lower level = " + getLowerLevel() + "\n";
return ret;
}
// Get string that summarizes access statistics
std::string Cache::getStatString() const
{
std::string ret;
ret += getName() + ":";
ret += readHits.toString() + ":";
ret += readMisses.toString() + ":";
ret += writeHits.toString() + ":";
ret += writeMisses.toString() + ":";
ret += writeBacks.toString();
return ret;
}
// Get string that dumps all valid lines in cache
std::string Cache::getContentString() const
{
std::string ret;
ret += "[" + getName() + "]\n";
ret += cacheCore->getContentString();
return ret;
}
// WBCache: Write back cache. Allocates a dirty block on write miss.
WBCache::WBCache(const char *name)
: Cache(name)
{
// nothing to do
}
WBCache::~WBCache()
{
// nothing to do
}
void WBCache::read(MemRequest *mreq)
{
uint32_t *rplcAddr = 0;
CacheLine* line;
line = cacheCore->accessLine(mreq->getAddr());
if(line != NULL){
readHits.inc();
return;
}
readMisses.inc();
getLowerLevelMemObj()->access(mreq);
/*
line = cacheCore->accessLine(mreq->getAddr());
if(line != NULL){
return;
}
readMisses.inc();
getLowerLevelMemObj()->access(mreq);
*/
cacheCore->allocateLine(mreq->getAddr(), rplcAddr);
// For now, the read always misses and memory request is passed on to
// lower level memory. Of course, you'd want to change this behavior. :)
}
void WBCache::write(MemRequest *mreq)
{
// TODO: Implement
uint32_t *rplcAddr = 0;
CacheLine* line;
line = cacheCore->accessLine(mreq->getAddr());
if(line != NULL){
line->makeDirty();
writeHits.inc();
return;
}
writeMisses.inc();
mreq->mutateWriteToRead();
getLowerLevelMemObj()->access(mreq);
line = cacheCore->allocateLine(mreq->getAddr(), rplcAddr);
line->makeDirty();
// For now, the write always misses and memory request is passed on to
// lower level memory. Of course, you'd want to change this behavior. :)
}
void WBCache::writeBack(MemRequest *mreq)
{
// TODO: Implement
uint32_t *rplcAddr = 0;
CacheLine* line;
line = cacheCore->accessLine(mreq->getAddr());
if(line != NULL){
line->makeDirty();
//writeHits.inc();
return;
}
writeMisses.inc();
getLowerLevelMemObj()->access(mreq);
line = cacheCore->allocateLine(mreq->getAddr(), rplcAddr);
line->makeDirty();
}
// WTCache: Write through cache. Always propagates writes down.
WTCache::WTCache(const char *name)
: Cache(name)
{
// nothing to do
}
WTCache::~WTCache()
{
// nothing to do
}
void WTCache::read(MemRequest *mreq)
{
// TODO: Implement
// For now, the read always misses and memory request is passed on to
// lower level memory. Of course, you'd want to change this behavior. :)
uint32_t *rplcAddr = 0;
CacheLine* line;
line = cacheCore->accessLine(mreq->getAddr());
if(line != NULL){
readHits.inc();
return;
}
readMisses.inc();
getLowerLevelMemObj()->access(mreq);
cacheCore->allocateLine(mreq->getAddr(), rplcAddr);
}
void WTCache::write(MemRequest *mreq)
{
// TODO: Implement
// For now, the write always misses and memory request is passed on to
// lower level memory. Of course, you'd want to change this behavior. :)
uint32_t *rplcAddr = 0;
CacheLine* line;
line = cacheCore->accessLine(mreq->getAddr());
if(line != NULL){
writeHits.inc();
return;
}
writeMisses.inc();
getLowerLevelMemObj()->access(mreq);
//cacheCore->allocateLine(mreq->getAddr(), rplcAddr);
}
void WTCache::writeBack(MemRequest *mreq)
{
// No reasonable design will have a WB cache on top of a WT cache
assert(0);
}