-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcache.H
636 lines (511 loc) · 16.3 KB
/
dcache.H
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
* Copyright 2002-2020 Intel Corporation.
*
* This software is provided to you as Sample Source Code as defined in the accompanying
* End User License Agreement for the Intel(R) Software Development Products ("Agreement")
* section 1.L.
*
* This software and the related documents are provided as is, with no express or implied
* warranties, other than those that are expressly stated in the License.
*/
/*! @file
* This file contains a configurable cache class
*/
#ifndef PIN_CACHE_H
#define PIN_CACHE_H
#define KILO 1024
#define MEGA (KILO*KILO)
#define GIGA (KILO*MEGA)
typedef UINT64 CACHE_STATS; // type of cache hit/miss counters
#include <sstream>
//#include <bits/stdc++.h>
using std::string;
using std::ostringstream;
/*! RMR (rodric@gmail.com)
* - temporary work around because decstr()
* casts 64 bit ints to 32 bit ones
*/
static string mydecstr(UINT64 v, UINT32 w)
{
ostringstream o;
o.width(w);
o << v;
string str(o.str());
return str;
}
/*!
* @brief Checks if n is a power of 2.
* @returns true if n is power of 2
*/
static inline bool IsPower2(UINT32 n)
{
return ((n & (n - 1)) == 0);
}
/*!
* @brief Computes floor(log2(n))
* Works by finding position of MSB set.
* @returns -1 if n == 0.
*/
static inline INT32 FloorLog2(UINT32 n)
{
INT32 p = 0;
if (n == 0) return -1;
if (n & 0xffff0000) { p += 16; n >>= 16; }
if (n & 0x0000ff00) { p += 8; n >>= 8; }
if (n & 0x000000f0) { p += 4; n >>= 4; }
if (n & 0x0000000c) { p += 2; n >>= 2; }
if (n & 0x00000002) { p += 1; }
return p;
}
/*!
* @brief Computes floor(log2(n))
* Works by finding position of MSB set.
* @returns -1 if n == 0.
*/
static inline INT32 CeilLog2(UINT32 n)
{
return FloorLog2(n - 1) + 1;
}
/*!
* @brief Cache tag - self clearing on creation
*/
class CACHE_TAG
{
private:
ADDRINT _tag;
public:
CACHE_TAG(ADDRINT tag = 0) { _tag = tag; }
bool operator==(const CACHE_TAG &right) const { return _tag == right._tag; }
operator ADDRINT() const { return _tag; }
};
/*!
* Everything related to cache sets
*/
namespace CACHE_SET
{
/*!
* @brief Cache set direct mapped
*/
class DIRECT_MAPPED
{
private:
CACHE_TAG _tag;
public:
DIRECT_MAPPED(UINT32 associativity = 1) { ASSERTX(associativity == 1); }
VOID SetAssociativity(UINT32 associativity) { ASSERTX(associativity == 1); }
UINT32 GetAssociativity(UINT32 associativity) { return 1; }
UINT32 Find(CACHE_TAG tag) { return(_tag == tag); }
VOID Replace(CACHE_TAG tag) { _tag = tag; }
};
/*!
* @brief Cache set with round robin replacement
*/
template <UINT32 MAX_ASSOCIATIVITY = 4>
class ROUND_ROBIN
{
private:
CACHE_TAG _tags[MAX_ASSOCIATIVITY];
UINT32 _tagsLastIndex;
UINT32 _nextReplaceIndex;
public:
ROUND_ROBIN(UINT32 associativity = MAX_ASSOCIATIVITY)
: _tagsLastIndex(associativity - 1)
{
ASSERTX(associativity <= MAX_ASSOCIATIVITY);
_nextReplaceIndex = _tagsLastIndex;
for (INT32 index = _tagsLastIndex; index >= 0; index--)
{
_tags[index] = CACHE_TAG(0);
}
}
VOID SetAssociativity(UINT32 associativity)
{
ASSERTX(associativity <= MAX_ASSOCIATIVITY);
_tagsLastIndex = associativity - 1;
_nextReplaceIndex = _tagsLastIndex;
}
UINT32 GetAssociativity(UINT32 associativity) { return _tagsLastIndex + 1; }
UINT32 Find(CACHE_TAG tag)
{
bool result = true;
for (INT32 index = _tagsLastIndex; index >= 0; index--)
{
// this is an ugly micro-optimization, but it does cause a
// tighter assembly loop for ARM that way ...
if(_tags[index] == tag) goto end;
}
result = false;
end: return result;
}
VOID Replace(CACHE_TAG tag)
{
// g++ -O3 too dumb to do CSE on following lines?!
const UINT32 index = _nextReplaceIndex;
_tags[index] = tag;
// condition typically faster than modulo
_nextReplaceIndex = (index == 0 ? _tagsLastIndex : index - 1);
}
};
/*!
* @brief Cache set with LRU replacement
*/
template <UINT32 MAX_ASSOCIATIVITY = 4>
class LRU
{
private:
CACHE_TAG _tags[MAX_ASSOCIATIVITY];
UINT64 _repl[MAX_ASSOCIATIVITY];
BOOL _dirty[MAX_ASSOCIATIVITY];
BOOL _valid[MAX_ASSOCIATIVITY];
UINT32 _tagsLastIndex;
UINT64 _timestamp;
BOOL _isDirty;
CACHE_TAG _wbAddr;
public:
LRU(UINT32 associativity = MAX_ASSOCIATIVITY)
: _tagsLastIndex(associativity - 1)
{
_timestamp = 0;
ASSERTX(associativity <= MAX_ASSOCIATIVITY);
for (INT32 index = _tagsLastIndex; index >= 0; index--)
{
_tags[index] = CACHE_TAG(0);
_repl[index] = 0; //initially, it gets replaced in order
_dirty[index] = 0; //everything clean initially
_valid[index] = 0; //nothing valid initially : empty cache
}
}
VOID SetAssociativity(UINT32 associativity)
{
ASSERTX(associativity <= MAX_ASSOCIATIVITY);
_tagsLastIndex = associativity - 1;
}
UINT32 GetAssociativity(UINT32 associativity) { return _tagsLastIndex + 1; }
BOOL isDirty() { return _isDirty; }
BOOL SetDirty(CACHE_TAG tag) {
for (INT32 index = _tagsLastIndex; index >= 0; index--)
{
if((_tags[index] == tag) && _valid[index]) {
_dirty[index] = 1;
}
}
return true;
}
CACHE_TAG getDirtyLine() { return _wbAddr; }
UINT32 Find(CACHE_TAG tag, BOOL isStore)
{
bool result = true;
for (INT32 index = _tagsLastIndex; index >= 0; index--)
{
// this is an ugly micro-optimization, but it does cause a
// tighter assembly loop for ARM that way ...
if((_tags[index] == tag) && _valid[index]) {
_repl[index] = _timestamp++ ;
if(isStore) { _dirty[index] = 1; }
goto end;
}
}
result = false;
end: return result;
}
VOID Replace(CACHE_TAG tag, BOOL isStore)
{
INT32 min_index = 0;
UINT64 min_timestamp = _repl[min_index];
for (INT32 index = _tagsLastIndex; index >= 0; index--)
{
if(_repl[index] < min_timestamp) {
min_timestamp = _repl[index];
min_index = index;
}
if(_valid[index] == 0) {
//First remove invalid data
min_index = index;
break ;
}
}
_isDirty = _dirty[min_index] ? 1 : 0;
_wbAddr = _tags[min_index]; //same tag will be used for L1 and L2
_tags[min_index] = tag; //replace LRU - the one touched least recently
_repl[min_index] = _timestamp++ ;
_valid[min_index] = 1;
if(isStore) {
_dirty[min_index] = 1;
} else { _dirty[min_index] = 0; }
}
};
} // namespace CACHE_SET
namespace CACHE_ALLOC
{
typedef enum
{
STORE_ALLOCATE,
STORE_NO_ALLOCATE
} STORE_ALLOCATION;
}
/*!
* @brief Generic cache base class; no allocate specialization, no cache set specialization
*/
class CACHE_BASE
{
public:
// types, constants
typedef enum
{
ACCESS_TYPE_LOAD,
ACCESS_TYPE_STORE,
ACCESS_TYPE_WB,
ACCESS_TYPE_NUM
} ACCESS_TYPE;
typedef enum
{
CACHE_TYPE_ICACHE,
CACHE_TYPE_DCACHE,
CACHE_TYPE_NUM
} CACHE_TYPE;
protected:
static const UINT32 HIT_MISS_NUM = 2;
CACHE_STATS _access[ACCESS_TYPE_NUM][HIT_MISS_NUM];
private: // input params
const std::string _name;
const UINT32 _cacheSize;
const UINT32 _lineSize;
const UINT32 _associativity;
// computed params
const UINT32 _lineShift;
const UINT32 _setIndexMask;
CACHE_STATS SumAccess(bool hit) const
{
CACHE_STATS sum = 0;
for (UINT32 accessType = 0; accessType < ACCESS_TYPE_NUM; accessType++)
{
sum += _access[accessType][hit];
}
return sum;
}
protected:
UINT32 NumSets() const { return _setIndexMask + 1; }
public:
// constructors/destructors
CACHE_BASE(std::string name, UINT32 cacheSize, UINT32 lineSize, UINT32 associativity);
// accessors
UINT32 CacheSize() const { return _cacheSize; }
UINT32 LineSize() const { return _lineSize; }
UINT32 Associativity() const { return _associativity; }
//
CACHE_STATS Hits(ACCESS_TYPE accessType) const { return _access[accessType][true];}
CACHE_STATS Misses(ACCESS_TYPE accessType) const { return _access[accessType][false];}
CACHE_STATS Accesses(ACCESS_TYPE accessType) const { return Hits(accessType) + Misses(accessType);}
CACHE_STATS Hits() const { return SumAccess(true);}
CACHE_STATS Misses() const { return SumAccess(false);}
CACHE_STATS Accesses() const { return Hits() + Misses();}
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex) const
{
tag = addr >> _lineShift;
setIndex = tag & _setIndexMask;
}
VOID SplitAddress(CACHE_TAG tag, UINT32 & setIndex) const
{
setIndex = tag & _setIndexMask;
}
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex, UINT32 & lineIndex) const
{
const UINT32 lineMask = _lineSize - 1;
lineIndex = addr & lineMask;
SplitAddress(addr, tag, setIndex);
}
string StatsLong(string prefix = "", CACHE_TYPE = CACHE_TYPE_DCACHE) const;
};
CACHE_BASE::CACHE_BASE(std::string name, UINT32 cacheSize, UINT32 lineSize, UINT32 associativity)
: _name(name),
_cacheSize(cacheSize),
_lineSize(lineSize),
_associativity(associativity),
_lineShift(FloorLog2(lineSize)),
_setIndexMask((cacheSize / (associativity * lineSize)) - 1)
{
ASSERTX(IsPower2(_lineSize));
ASSERTX(IsPower2(_setIndexMask + 1));
for (UINT32 accessType = 0; accessType < ACCESS_TYPE_NUM; accessType++)
{
_access[accessType][false] = 0;
_access[accessType][true] = 0;
}
}
/*!
* @brief Stats output method
*/
string CACHE_BASE::StatsLong(string prefix, CACHE_TYPE cache_type) const
{
const UINT32 headerWidth = 19;
const UINT32 numberWidth = 12;
string out;
out += prefix + _name + ":" + "\n";
if (cache_type != CACHE_TYPE_ICACHE) {
for (UINT32 i = 0; i < ACCESS_TYPE_NUM; i++)
{
const ACCESS_TYPE accessType = ACCESS_TYPE(i);
std::string type(accessType == ACCESS_TYPE_LOAD ? "Load" : accessType == ACCESS_TYPE_STORE ? "Store" : "WB");
out += prefix + ljstr(type + "-Hits: ", headerWidth)
+ mydecstr(Hits(accessType), numberWidth) +
" " +fltstr(100.0 * Hits(accessType) / Accesses(accessType), 2, 6) + "%\n";
out += prefix + ljstr(type + "-Misses: ", headerWidth)
+ mydecstr(Misses(accessType), numberWidth) +
" " +fltstr(100.0 * Misses(accessType) / Accesses(accessType), 2, 6) + "%\n";
out += prefix + ljstr(type + "-Accesses: ", headerWidth)
+ mydecstr(Accesses(accessType), numberWidth) +
" " +fltstr(100.0 * Accesses(accessType) / Accesses(accessType), 2, 6) + "%\n";
out += prefix + "\n";
}
}
out += prefix + ljstr("Total-Hits: ", headerWidth)
+ mydecstr(Hits(), numberWidth) +
" " +fltstr(100.0 * Hits() / Accesses(), 2, 6) + "%\n";
out += prefix + ljstr("Total-Misses: ", headerWidth)
+ mydecstr(Misses(), numberWidth) +
" " +fltstr(100.0 * Misses() / Accesses(), 2, 6) + "%\n";
out += prefix + ljstr("Total-Accesses: ", headerWidth)
+ mydecstr(Accesses(), numberWidth) +
" " +fltstr(100.0 * Accesses() / Accesses(), 2, 6) + "%\n";
out += "\n";
return out;
}
/*!
* @brief Templated cache class with specific cache set allocation policies
*
* All that remains to be done here is allocate and deallocate the right
* type of cache sets.
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
class CACHE : public CACHE_BASE
{
private:
SET _sets[MAX_SETS];
public:
// constructors/destructors
CACHE(std::string name, UINT32 cacheSize, UINT32 lineSize, UINT32 associativity)
: CACHE_BASE(name, cacheSize, lineSize, associativity)
{
ASSERTX(NumSets() <= MAX_SETS);
for (UINT32 i = 0; i < NumSets(); i++)
{
_sets[i].SetAssociativity(associativity);
}
}
// modifiers
/// Cache access from addr to addr+size-1
bool Access(ADDRINT addr, UINT32 size, ACCESS_TYPE accessType);
/// Cache access at addr that does not span cache lines
bool AccessSingleLine(ADDRINT addr, ACCESS_TYPE accessType);
bool isDirty(ADDRINT addr);
CACHE_TAG getDirtyLine(ADDRINT addr);
bool WriteBackDirty(CACHE_TAG wbTag);
bool WriteToMemory();
};
/*!
* @return true if all accessed cache lines hit
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
bool CACHE<SET,MAX_SETS,STORE_ALLOCATION>::Access(ADDRINT addr, UINT32 size, ACCESS_TYPE accessType)
{
const ADDRINT highAddr = addr + size;
bool allHit = true;
const ADDRINT lineSize = LineSize();
const ADDRINT notLineMask = ~(lineSize - 1);
do
{
CACHE_TAG tag;
UINT32 setIndex;
SplitAddress(addr, tag, setIndex);
SET & set = _sets[setIndex];
bool localHit = set.Find(tag, accessType == ACCESS_TYPE_STORE);
allHit &= localHit;
// on miss, loads always allocate, stores optionally
if ( (! localHit) && (accessType == ACCESS_TYPE_LOAD || STORE_ALLOCATION == CACHE_ALLOC::STORE_ALLOCATE))
{
set.Replace(tag, accessType == ACCESS_TYPE_STORE);
}
addr = (addr & notLineMask) + lineSize; // start of next cache line
}
while (addr < highAddr);
_access[accessType][allHit]++;
return allHit;
}
/*!
* @return true if accessed cache line hits
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
bool CACHE<SET,MAX_SETS,STORE_ALLOCATION>::AccessSingleLine(ADDRINT addr, ACCESS_TYPE accessType)
{
CACHE_TAG tag;
UINT32 setIndex;
SplitAddress(addr, tag, setIndex);
SET & set = _sets[setIndex];
bool hit = set.Find(tag, accessType == ACCESS_TYPE_STORE);
// on miss, loads always allocate, stores optionally
if ( (! hit) && (accessType == ACCESS_TYPE_LOAD || STORE_ALLOCATION == CACHE_ALLOC::STORE_ALLOCATE))
{
set.Replace(tag, accessType == ACCESS_TYPE_STORE);
}
_access[accessType][hit]++;
return hit;
}
/*!
* @return true if the last replaced line was dirty
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
bool CACHE<SET,MAX_SETS,STORE_ALLOCATION>::isDirty(ADDRINT addr)
{
CACHE_TAG tag;
UINT32 setIndex;
SplitAddress(addr, tag, setIndex);
SET & set = _sets[setIndex];
bool isDirty = set.isDirty();
return isDirty;
}
/*!
* @return TAG of last dirty line
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
CACHE_TAG CACHE<SET,MAX_SETS,STORE_ALLOCATION>::getDirtyLine(ADDRINT addr)
{
CACHE_TAG tag;
UINT32 setIndex;
//Used to get address of SET to query. This is the line which replaced the dirty line
SplitAddress(addr, tag, setIndex);
SET & set = _sets[setIndex];
CACHE_TAG wbAddr = set.getDirtyLine();
return wbAddr;
}
/*!
* @return none. Used to WB line from l1 to l2
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
bool CACHE<SET,MAX_SETS,STORE_ALLOCATION>::WriteBackDirty(CACHE_TAG wbTag)
{
//Need to update Dirty bit though
UINT32 setIndex;
SplitAddress(wbTag, setIndex);
SET & set = _sets[setIndex];
set.SetDirty(wbTag);
// Just count it in accesses that hit, nothing else. No need to update replacement policy
bool hit = true;
_access[ACCESS_TYPE_STORE][hit]++;
return hit;
}
/*!
* @return none. Used to WB line from l2 to memory
*/
template <class SET, UINT32 MAX_SETS, UINT32 STORE_ALLOCATION>
bool CACHE<SET,MAX_SETS,STORE_ALLOCATION>::WriteToMemory()
{
// Just count it in accesses that miss in L2
bool miss = false;
_access[ACCESS_TYPE_WB][miss]++;
return miss;
}
// define shortcuts
#define CACHE_DIRECT_MAPPED(MAX_SETS, ALLOCATION) CACHE<CACHE_SET::DIRECT_MAPPED, MAX_SETS, ALLOCATION>
#define CACHE_ROUND_ROBIN(MAX_SETS, MAX_ASSOCIATIVITY, ALLOCATION) CACHE<CACHE_SET::ROUND_ROBIN<MAX_ASSOCIATIVITY>, MAX_SETS, ALLOCATION>
#define CACHE_LRU(MAX_SETS, MAX_ASSOCIATIVITY, ALLOCATION) CACHE<CACHE_SET::LRU<MAX_ASSOCIATIVITY>, MAX_SETS, ALLOCATION>
#endif // PIN_CACHE_H