-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cpp
More file actions
490 lines (410 loc) · 14.3 KB
/
Copy pathCache.cpp
File metadata and controls
490 lines (410 loc) · 14.3 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include "Cache.h"
#ifndef DEBUG_CACHE
#define debug(...)
#else
#define debug(...) do { \
printf("\033[36m[DEBUG] %s ", __FUNCTION__); \
printf(__VA_ARGS__); \
printf("\033[0m\n"); \
} while (0)
#endif
namespace ramulator
{
Cache::Cache(int size, int assoc, int block_size,
int mshr_entry_num, Level level,
std::shared_ptr<CacheSystem> cachesys):
level(level), cachesys(cachesys), higher_cache(0),
lower_cache(nullptr), size(size), assoc(assoc),
block_size(block_size), mshr_entry_num(mshr_entry_num) {
debug("level %d size %d assoc %d block_size %d\n",
int(level), size, assoc, block_size);
if (level == Level::L1) {
level_string = "L1";
} else if (level == Level::L2) {
level_string = "L2";
} else if (level == Level::L3) {
level_string = "L3";
}
is_first_level = (level == cachesys->first_level);
is_last_level = (level == cachesys->last_level);
// Check size, block size and assoc are 2^N
assert((size & (size - 1)) == 0);
assert((block_size & (block_size - 1)) == 0);
assert((assoc & (assoc - 1)) == 0);
assert(size >= block_size);
// Initialize cache configuration
block_num = size / (block_size * assoc);
index_mask = block_num - 1;
index_offset = calc_log2(block_size);
tag_offset = calc_log2(block_num) + index_offset;
prefetcher = nullptr;
debug("index_offset %d", index_offset);
debug("index_mask 0x%x", index_mask);
debug("tag_offset %d", tag_offset);
// regStats
cache_read_miss.name(level_string + string("_cache_read_miss"))
.desc("cache read miss count")
.precision(0)
;
cache_write_miss.name(level_string + string("_cache_write_miss"))
.desc("cache write miss count")
.precision(0)
;
cache_prefetch_miss.name(level_string + string("_cache_prefetch_miss"))
.desc("cache prefetch miss count")
.precision(0)
;
cache_prefetch_hit.name(level_string + string("_cache_prefetch_hit"))
.desc("prefetch requests that were already in the cache")
.precision(0)
;
cache_total_miss.name(level_string + string("_cache_total_miss"))
.desc("cache total miss count")
.precision(0)
;
cache_eviction.name(level_string + string("_cache_eviction"))
.desc("number of evict from this level to lower level")
.precision(0)
;
cache_read_access.name(level_string + string("_cache_read_access"))
.desc("cache read access count")
.precision(0)
;
cache_write_access.name(level_string + string("_cache_write_access"))
.desc("cache write access count")
.precision(0)
;
cache_prefetch_access.name(level_string + string("_cache_prefetch_access"))
.desc("cache prefetch access count")
.precision(0)
;
cache_total_access.name(level_string + string("_cache_total_access"))
.desc("cache total access count")
.precision(0)
;
cache_mshr_hit.name(level_string + string("_cache_mshr_hit"))
.desc("cache mshr hit count")
.precision(0)
;
cache_mshr_unavailable.name(level_string + string("_cache_mshr_unavailable"))
.desc("cache mshr not available count")
.precision(0)
;
cache_set_unavailable.name(level_string + string("_cache_set_unavailable"))
.desc("cache set not available")
.precision(0)
;
}
bool Cache::send(Request req) {
debug("level %d req.addr %lx req.type %d, index %d, tag %ld",
int(level), req.addr, int(req.type), get_index(req.addr),
get_tag(req.addr));
cache_total_access++;
if (req.type == Request::Type::WRITE) {
cache_write_access++;
} else if (req.type == Request::Type::READ) {
cache_read_access++;
} else {
assert(req.type == Request::Type::PREFETCH);
cache_prefetch_access++;
}
// If there isn't a set, create it.
auto& lines = get_lines(req.addr);
std::list<Line>::iterator line;
if (is_hit(lines, req.addr, &line)) {
if(req.type == Request::Type::PREFETCH) {
cache_prefetch_hit++;
return true;;
}
lines.push_back(Line(req.addr, get_tag(req.addr), false,
line->dirty || (req.type == Request::Type::WRITE)));
lines.erase(line);
cachesys->hit_list.push_back(
make_pair(cachesys->clk + latency[int(level)], req));
debug("hit, update timestamp %ld", cachesys->clk);
debug("hit finish time %ld",
cachesys->clk + latency[int(level)]);
if(prefetcher)
prefetcher->hit(req.addr, cachesys->clk);
return true;
} else {
debug("miss @level %d", int(level));
cache_total_miss++;
if (req.type == Request::Type::WRITE) {
cache_write_miss++;
} else if (req.type == Request::Type::READ) {
cache_read_miss++;
} else {
assert(req.type == Request::Type::PREFETCH);
cache_prefetch_miss++;
}
// The dirty bit will be set if this is a write request and @L1
bool dirty = (req.type == Request::Type::WRITE);
// Modify the type of the request to lower level
if (req.type == Request::Type::WRITE) {
req.type = Request::Type::READ;
}
// Look it up in MSHR entries
assert((req.type == Request::Type::READ) || (req.type == Request::Type::PREFETCH));
auto mshr = hit_mshr(req.addr);
if (mshr != mshr_entries.end()) {
debug("hit mshr");
cache_mshr_hit++;
mshr->second->dirty = dirty || mshr->second->dirty;
// FIXME: Shall we train the prefetcher on MSHR hit???
// if(prefetcher)
// prefetcher->miss(req.addr, cachesys->clk);
// upgrade the previous prefetch request to demand request so the
// processor will be informed on completion of the request
if (prefetcher && (req.type == Request::Type::READ) && mshr->second->is_prefetch){
bool is_upgraded = cachesys->upgrade_prefetch_req(align(req.addr));
if(!is_upgraded)
printf("Address of the request failed to upgrade: %ld\n", align(req.addr));
assert(is_upgraded && "ERROR: Failed to upgrade a PREFETCH request to READ!");
mshr->second->is_prefetch = false;
}
return true;
}
// All requests come to this stage will be READ, so they
// should be recorded in MSHR entries.
if (mshr_entries.size() == mshr_entry_num) {
// When no MSHR entries available, the miss request
// is stalling.
cache_mshr_unavailable++;
debug("no mshr entry available");
return false;
}
// Check whether there is a line available
if (all_sets_locked(lines)) {
cache_set_unavailable++;
return false;
}
auto newline = allocate_line(lines, req.addr);
if (newline == lines.end()) {
return false;
}
newline->dirty = dirty;
// Add to MSHR entries
mshr_entries.push_back(make_pair(req.addr, newline));
// Send the request to next level;
if (!is_last_level) {
lower_cache->send(req);
} else {
cachesys->wait_list.push_back(
make_pair(cachesys->clk + latency[int(level)], req));
}
if (prefetcher) {
if (req.type == Request::Type::READ) {
prefetcher->miss(req.addr, cachesys->clk);
} else {
assert(req.type == Request::Type::PREFETCH);
newline->is_prefetch = true;
}
}
return true;
}
}
void Cache::evictline(long addr, bool dirty) {
auto it = cache_lines.find(get_index(addr));
assert(it != cache_lines.end()); // check inclusive cache
auto& lines = it->second;
auto line = find_if(lines.begin(), lines.end(),
[addr, this](Line l){return (l.tag == get_tag(addr));});
assert(line != lines.end());
// Update LRU queue. The dirty bit will be set if the dirty
// bit inherited from higher level(s) is set.
lines.push_back(Line(addr, get_tag(addr), false,
dirty || line->dirty));
lines.erase(line);
}
std::pair<long, bool> Cache::invalidate(long addr) {
long delay = latency_each[int(level)];
bool dirty = false;
auto& lines = get_lines(addr);
if (lines.size() == 0) {
// The line of this address doesn't exist.
return make_pair(0, false);
}
auto line = find_if(lines.begin(), lines.end(),
[addr, this](Line l){return (l.tag == get_tag(addr));});
// If the line is in this level cache, then erase it from
// the buffer.
if (line != lines.end()) {
assert(!line->lock);
debug("invalidate %lx @ level %d", addr, int(level));
lines.erase(line);
} else {
// If it's not in current level, then no need to go up.
return make_pair(delay, false);
}
if (higher_cache.size()) {
long max_delay = delay;
for (auto hc : higher_cache) {
auto result = hc->invalidate(addr);
if (result.second) {
max_delay = max(max_delay, delay + result.first * 2);
} else {
max_delay = max(max_delay, delay + result.first);
}
dirty = dirty || line->dirty || result.second;
}
delay = max_delay;
} else {
dirty = line->dirty;
}
return make_pair(delay, dirty);
}
void Cache::evict(std::list<Line>* lines,
std::list<Line>::iterator victim) {
debug("level %d miss evict victim %lx", int(level), victim->addr);
cache_eviction++;
long addr = victim->addr;
long invalidate_time = 0;
bool dirty = victim->dirty;
// First invalidate the victim line in higher level.
if (higher_cache.size()) {
for (auto hc : higher_cache) {
auto result = hc->invalidate(addr);
invalidate_time = max(invalidate_time,
result.first + (result.second ? latency_each[int(level)] : 0));
dirty = dirty || result.second || victim->dirty;
}
}
debug("invalidate delay: %ld, dirty: %s", invalidate_time,
dirty ? "true" : "false");
if (!is_last_level) {
// not LLC eviction
assert(lower_cache != nullptr);
lower_cache->evictline(addr, dirty);
} else {
// LLC eviction
if (dirty) {
Request write_req(addr, Request::Type::WRITE);
cachesys->wait_list.push_back(make_pair(
cachesys->clk + invalidate_time + latency[int(level)],
write_req));
debug("inject one write request to memory system "
"addr %lx, invalidate time %ld, issue time %ld",
write_req.addr, invalidate_time,
cachesys->clk + invalidate_time + latency[int(level)]);
}
}
lines->erase(victim);
}
std::list<Cache::Line>::iterator Cache::allocate_line(
std::list<Line>& lines, long addr) {
// See if an eviction is needed
if (need_eviction(lines, addr)) {
// Get victim.
// The first one might still be locked due to reorder in MC
auto victim = find_if(lines.begin(), lines.end(),
[this](Line line) {
bool check = !line.lock;
if (!is_first_level) {
for (auto hc : higher_cache) {
if(!check) {
return check;
}
check = check && hc->check_unlock(line.addr);
}
}
return check;
});
if (victim == lines.end()) {
return victim; // doesn't exist a line that's already unlocked in each level
}
assert(victim != lines.end());
evict(&lines, victim);
}
// Allocate newline, with lock bit on and dirty bit off
lines.push_back(Line(addr, get_tag(addr)));
auto last_element = lines.end();
--last_element;
return last_element;
}
bool Cache::is_hit(std::list<Line>& lines, long addr,
std::list<Line>::iterator* pos_ptr) {
auto pos = find_if(lines.begin(), lines.end(),
[addr, this](Line l){return (l.tag == get_tag(addr));});
*pos_ptr = pos;
if (pos == lines.end()) {
return false;
}
return !pos->lock;
}
void Cache::concatlower(Cache* lower) {
lower_cache = lower;
assert(lower != nullptr);
lower->higher_cache.push_back(this);
};
bool Cache::need_eviction(const std::list<Line>& lines, long addr) {
if (find_if(lines.begin(), lines.end(),
[addr, this](Line l){
return (get_tag(addr) == l.tag);})
!= lines.end()) {
// Due to MSHR, the program can't reach here. Just for checking
assert(false);
} else {
if (lines.size() < assoc) {
return false;
} else {
return true;
}
}
}
void Cache::callback(Request& req) {
debug("level %d", int(level));
auto it = find_if(mshr_entries.begin(), mshr_entries.end(),
[&req, this](std::pair<long, std::list<Line>::iterator> mshr_entry) {
return (align(mshr_entry.first) == align(req.addr));
});
if (it != mshr_entries.end()) {
it->second->lock = false;
mshr_entries.erase(it);
}
if (req.type != Request::Type::PREFETCH) // to prefetch only to LLC
if (higher_cache.size()) {
for (auto hc : higher_cache) {
hc->callback(req);
}
}
}
void CacheSystem::tick() {
debug("clk %ld", clk);
++clk;
// Sends ready waiting request to memory
auto it = wait_list.begin();
while (it != wait_list.end() && clk >= it->first) {
if (!send_memory(it->second)) {
++it;
} else {
debug("complete req: addr %lx", (it->second).addr);
it = wait_list.erase(it);
}
}
// hit request callback
it = hit_list.begin();
while (it != hit_list.end()) {
if (clk >= it->first) {
it->second.callback(it->second);
debug("finish hit: addr %lx", (it->second).addr);
it = hit_list.erase(it);
} else {
++it;
}
}
}
bool CacheSystem::upgrade_prefetch_req(long addr) {
if(wait_list.size() != 0){
auto pref_req = find_if(wait_list.begin(), wait_list.end(),
[addr](pair<long, Request>& preq) {
return (addr == preq.second.addr) && preq.second.type == Request::Type::PREFETCH;});
if (pref_req != wait_list.end()) {
(pref_req->second).type = Request::Type::READ;
(pref_req->second).callback = (pref_req->second).proc_callback; // FIXME: proc_callback is an ugly workaround
return true;
}
}
return upgrade_prefetch_req_in_mem(addr);
}
} // namespace ramulator