-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTLB.h
611 lines (515 loc) · 16.5 KB
/
TLB.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
typedef uint32_t TLB_STATS;
#ifndef TLB_H
#define TLB_H
#include <string>
#include <assert.h>
/*!
* @brief Checks if n is a power of 2.
* @returns true if n is power of 2
*/
static inline bool IsPower2(uint32_t 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 uint32_t FloorLog2(uint32_t n)
{
uint32_t 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 uint32_t CeilLog2(uint32_t n)
{
return FloorLog2(n - 1) + 1;
}
/*!
* @brief TLB tag - self clearing on creation
*/
class TLB_TAG
{
private:
uint64_t _tag;
public:
TLB_TAG(uint64_t tag = 0) { _tag = tag; }
bool operator==(const TLB_TAG &right) const { return _tag == right._tag; }
operator uint64_t() const { return _tag; }
};
/*!
* Everything related to Tlb sets
*/
namespace TLB_SET
{
/*!
* @brief Tlb set direct mapped
*/
class DIRECT_MAPPED
{
private:
TLB_TAG _tag;
int32_t _ASID;
uint64_t _PPN;
int32_t _valid;
public:
DIRECT_MAPPED(uint32_t associativity = 1)
{
assert(associativity == 1);
_tag = -1;
_ASID = -1;
_PPN = NULL;
_valid = 0;
}
void SetAssociativity(uint32_t associativity) { assert(associativity == 1); }
uint32_t GetAssociativity(uint32_t associativity) { return 1; }
uint32_t Find_And_Access(TLB_TAG tag, int32_t ASID, uint64_t &paddr)
{
if (_valid == 1 && _tag == tag && _ASID == ASID)
{
paddr = _PPN;
return true;
}
else
{
paddr = NULL;
return false;
}
}
void Replace(TLB_TAG tag, int32_t ASID, uint64_t paddr)
{
_tag = tag;
_ASID = ASID;
_PPN = paddr;
_valid = 1;
}
void Invalidate(TLB_TAG tag, int32_t ASID)
{
if (_valid == 1 && _tag == tag && _ASID == ASID)
_valid = 0;
}
void Flush()
{
_tag = 0;
_ASID = 0;
_valid = 0;
_PPN = NULL;
}
};
/*!
* @brief Tlb set with round robin replacement
*/
template <uint32_t MAX_ASSOCIATIVITY = 4>
class ROUND_ROBIN
{
private:
TLB_TAG _tags[MAX_ASSOCIATIVITY];
uint32_t _tagsLastIndex;
uint32_t _nextReplaceIndex;
int32_t _ASID[MAX_ASSOCIATIVITY];
uint64_t _PPN[MAX_ASSOCIATIVITY];
int32_t _valid[MAX_ASSOCIATIVITY];
public:
ROUND_ROBIN(uint32_t associativity = MAX_ASSOCIATIVITY)
: _tagsLastIndex(associativity - 1)
{
assert(associativity <= MAX_ASSOCIATIVITY);
_nextReplaceIndex = _tagsLastIndex;
for (int32_t index = _tagsLastIndex; index >= 0; index--)
{
_tags[index] = TLB_TAG(0);
_ASID[index] = 0;
_PPN[index] = NULL;
_valid[index] = 0;
}
}
void SetAssociativity(uint32_t associativity)
{
assert(associativity <= MAX_ASSOCIATIVITY);
_tagsLastIndex = associativity - 1;
_nextReplaceIndex = _tagsLastIndex;
}
uint32_t GetAssociativity(uint32_t associativity) { return _tagsLastIndex + 1; }
uint32_t Find_and_Access(TLB_TAG tag, int32_t ASID, uint64_t &paddr)
{
// std::cout<<tag<<" "<<ASID<<" "<<paddr;
bool result = true;
for (int32_t 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 && _ASID[index] == ASID && _valid[index] == 1)
{
paddr = _PPN[index];
goto end;
}
}
result = false;
paddr = NULL;
end:
return result;
}
void Replace(TLB_TAG tag, int32_t ASID, uint64_t paddr, uint64_t &victim_paddr, uint64_t victim_tag)
{
// g++ -O3 too dumb to do CSE on following lines?!
const uint32_t index = _nextReplaceIndex;
victim_paddr=_PPN[index];
victim_tag=_tags[index];
_tags[index] = tag;
_ASID[index] = ASID;
_PPN[index] = paddr;
_valid[index] = 1;
// condition typically faster than modulo
_nextReplaceIndex = (index == 0 ? _tagsLastIndex : index - 1);
}
void Invalidate(TLB_TAG tag, int32_t ASID)
{
bool result = true;
for (int32_t 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 && _ASID[index] == ASID && _valid[index] == 1)
{
_valid[index] = 0;
}
}
}
void Flush()
{
for (int32_t index = _tagsLastIndex; index >= 0; index--)
{
_tags[index] = 0;
_ASID[index] = 0;
_PPN[index] = NULL;
_valid[index] = 0;
}
_nextReplaceIndex = _tagsLastIndex;
}
};
} // namespace TLB_SET
namespace TLB_ALLOC
{
typedef enum
{
STORE_ALLOCATE,
STORE_NO_ALLOCATE
} STORE_ALLOCATION;
}
/*!
* @brief Generic tlb base class; no allocate specialization, no tlb set specialization
*/
class TLB_BASE
{
public:
// types, constants
typedef enum
{
ACCESS_TYPE_LOAD,
ACCESS_TYPE_STORE,
ACCESS_TYPE_NUM
} ACCESS_TYPE;
protected:
static const uint32_t HIT_MISS_NUM = 2;
TLB_STATS _access[ACCESS_TYPE_NUM][HIT_MISS_NUM];
private:
// input params
const std::string _name;
const uint32_t _tlbSize;
const uint32_t _lineSize;
const uint32_t _associativity;
uint32_t _numberOfFlushes;
uint32_t _numberOfResets;
// computed params
const uint32_t _lineShift;
const uint32_t _setIndexMask;
TLB_STATS SumAccess(bool hit) const
{
TLB_STATS sum = 0;
for (uint32_t accessType = 0; accessType < ACCESS_TYPE_NUM; accessType++)
{
sum += _access[accessType][hit];
}
return sum;
}
protected:
uint32_t NumSets() const { return _setIndexMask + 1; }
public:
uint32_t get_linesize()
{
return _lineShift;
}
// constructors/destructors
TLB_BASE(std::string name, uint32_t tlbSize, uint32_t lineSize, uint32_t associativity);
// accessors
uint32_t TlbSize() const { return _tlbSize; }
uint32_t LineSize() const { return _lineSize; }
uint32_t Associativity() const { return _associativity; }
//
TLB_STATS Hits(ACCESS_TYPE accessType) const { return _access[accessType][true]; }
TLB_STATS Misses(ACCESS_TYPE accessType) const { return _access[accessType][false]; }
TLB_STATS Accesses(ACCESS_TYPE accessType) const { return Hits(accessType) + Misses(accessType); }
TLB_STATS Hits() const { return SumAccess(true); }
TLB_STATS Misses() const { return SumAccess(false); }
TLB_STATS Accesses() const { return Hits() + Misses(); }
TLB_STATS Flushes() const { return _numberOfFlushes; }
TLB_STATS Resets() const { return _numberOfResets; }
void SplitAddress(const uint64_t addr, TLB_TAG &tag, uint32_t &setIndex) const
{
tag = addr >> _lineShift;
setIndex = tag & _setIndexMask;
}
void SplitAddress(const uint64_t addr, TLB_TAG &tag, uint32_t &setIndex, uint32_t &lineIndex) const
{
const uint32_t lineMask = _lineSize - 1;
lineIndex = addr & lineMask;
SplitAddress(addr, tag, setIndex);
}
void IncFlushCounter()
{
_numberOfFlushes += 1;
}
void IncResetCounter()
{
_numberOfResets += 1;
}
std::string GetName()
{
return _name;
}
std::ostream &StatsLong(std::ostream &out) const;
};
TLB_BASE::TLB_BASE(std::string name, uint32_t tlbSize, uint32_t lineSize, uint32_t associativity)
: _name(name),
_tlbSize(tlbSize),
_lineSize(lineSize),
_associativity(associativity),
_lineShift(FloorLog2(lineSize)),
_setIndexMask((tlbSize / (associativity * lineSize)) - 1)
{
assert(IsPower2(_lineSize));
assert(IsPower2(_setIndexMask + 1));
for (uint32_t accessType = 0; accessType < ACCESS_TYPE_NUM; accessType++)
{
_access[accessType][false] = 0;
_access[accessType][true] = 0;
}
}
/*!
* @brief Stats output method
*/
std::ostream &TLB_BASE::StatsLong(std::ostream &out) const
{
const uint32_t headerWidth = 19;
const uint32_t numberWidth = 10;
out << _name << ":" << std::endl;
for (uint32_t i = 0; i < ACCESS_TYPE_NUM; i++)
{
const ACCESS_TYPE accessType = ACCESS_TYPE(i);
std::string type(accessType == ACCESS_TYPE_LOAD ? "Load" : "Store");
out << StringString(type + " Hits: ", headerWidth)
<< StringInt(Hits(accessType), numberWidth) << std::endl;
out << StringString(type + " Misses: ", headerWidth)
<< StringInt(Misses(accessType), numberWidth) << std::endl;
out << StringString(type + " Accesses: ", headerWidth)
<< StringInt(Accesses(accessType), numberWidth) << std::endl;
out << StringString(type + " Miss Rate: ", headerWidth)
<< StringFlt(100.0 * Misses(accessType) / Accesses(accessType), 2, numberWidth - 1) << "%" << std::endl;
out << std::endl;
}
out << StringString("Total Hits: ", headerWidth, ' ')
<< StringInt(Hits(), numberWidth) << std::endl;
out << StringString("Total Misses: ", headerWidth, ' ')
<< StringInt(Misses(), numberWidth) << std::endl;
out << StringString("Total Accesses: ", headerWidth, ' ')
<< StringInt(Accesses(), numberWidth) << std::endl;
out << StringString("Total Miss Rate: ", headerWidth, ' ')
<< StringFlt(100.0 * Misses() / Accesses(), 2, numberWidth - 1) << "%" << std::endl;
out << StringString("Flushes: ", headerWidth, ' ')
<< StringInt(Flushes(), numberWidth) << std::endl;
out << StringString("Stat Resets: ", headerWidth, ' ')
<< StringInt(Resets(), numberWidth) << std::endl;
out << std::endl;
return out;
}
/// ostream operator for TLB_BASE
std::ostream &operator<<(std::ostream &out, const TLB_BASE &tlbBase)
{
return tlbBase.StatsLong(out);
}
/*!
* @brief Templated tlb class with specific tlb set allocation policies
*
* All that remains to be done here is allocate and deallocate the right
* type of tlb sets.
*/
template <class SET, uint32_t MAX_SETS, uint32_t STORE_ALLOCATION>
class TLB : public TLB_BASE
{
private:
SET _sets[MAX_SETS];
public:
// constructors/destructors
TLB(std::string name, uint32_t tlbSize, uint32_t lineSize, uint32_t associativity)
: TLB_BASE(name, tlbSize, lineSize, associativity)
{
assert(NumSets() <= MAX_SETS);
for (uint32_t i = 0; i < NumSets(); i++)
{
_sets[i].SetAssociativity(associativity);
}
}
// modifiers
/// Tlb access at addr
bool AccessTLB(uint64_t addr, ACCESS_TYPE accessType, int32_t ASID, uint64_t &paddr);
void ReplaceTLB(uint64_t addr, ACCESS_TYPE accessType, int32_t ASID, uint64_t &paddr, uint64_t &victim_paddr, uint64_t &victim_vaddr);
void InvalidateTLB(uint64_t addr, int32_t ASID);
void Flush();
void ResetStats();
};
/*!
* @return true if accessed tlb line hits
*/
template <class SET, uint32_t MAX_SETS, uint32_t STORE_ALLOCATION>
bool TLB<SET, MAX_SETS, STORE_ALLOCATION>::AccessTLB(uint64_t addr, ACCESS_TYPE accessType, int32_t ASID, uint64_t &paddr)
{
TLB_TAG tag;
uint32_t setIndex;
SplitAddress(addr, tag, setIndex);
SET &set = _sets[setIndex];
bool hit = set.Find_and_Access(tag, ASID, paddr);
// on miss, loads always allocate, stores optionally
_access[accessType][hit]++;
return hit;
}
/*!
* @return true if accessed tlb line hits
*/
template <class SET, uint32_t MAX_SETS, uint32_t STORE_ALLOCATION>
void TLB<SET, MAX_SETS, STORE_ALLOCATION>::ReplaceTLB(uint64_t addr, ACCESS_TYPE accessType, int32_t ASID, uint64_t &paddr, uint64_t &victim_paddr, uint64_t &victim_vaddr)
{
TLB_TAG tag;
uint32_t setIndex;
uint64_t victim_tag;
SplitAddress(addr, tag, setIndex);
SET &set = _sets[setIndex];
// bool hit = set.Find(tag,ASID,paddr);
// on miss, loads always allocate, stores optionally
if ((accessType == ACCESS_TYPE_LOAD || STORE_ALLOCATION == TLB_ALLOC::STORE_ALLOCATE))
{
set.Replace(tag, ASID, paddr, victim_paddr, victim_tag);
victim_vaddr=victim_tag<<(get_linesize());
}
//_access[accessType][hit]++;
// return hit;
}
template <class SET, uint32_t MAX_SETS, uint32_t STORE_ALLOCATION>
void TLB<SET, MAX_SETS, STORE_ALLOCATION>::InvalidateTLB(uint64_t addr, int32_t ASID)
{
TLB_TAG tag;
uint32_t setIndex;
SplitAddress(addr, tag, setIndex);
SET &set = _sets[setIndex];
// bool hit = set.Find(tag,ASID,paddr);
// on miss, loads always allocate, stores optionally
set.Invalidate(tag, ASID);
// if ((accessType == ACCESS_TYPE_LOAD || STORE_ALLOCATION == TLB_ALLOC::STORE_ALLOCATE))
// {
// set.Replace(tag,ASID,paddr);
// }
//_access[accessType][hit]++;
// return hit;
}
template <class SET, uint32_t MAX_SETS, uint32_t STORE_ALLOCATION>
void TLB<SET, MAX_SETS, STORE_ALLOCATION>::Flush()
{
for (int32_t index = NumSets(); index >= 0; index--)
{
SET &set = _sets[index];
set.Flush();
}
IncFlushCounter();
}
template <class SET, uint32_t MAX_SETS, uint32_t STORE_ALLOCATION>
void TLB<SET, MAX_SETS, STORE_ALLOCATION>::ResetStats()
{
for (uint32_t accessType = 0; accessType < ACCESS_TYPE_NUM; accessType++)
{
_access[accessType][false] = 0;
_access[accessType][true] = 0;
}
IncResetCounter();
}
// define shortcuts
#define TLB_DIRECT_MAPPED(MAX_SETS, ALLOCATION) TLB<TLB_SET::DIRECT_MAPPED, MAX_SETS, ALLOCATION>
#define TLB_ROUND_ROBIN(MAX_SETS, MAX_ASSOCIATIVITY, ALLOCATION) \
TLB<TLB_SET::ROUND_ROBIN<MAX_ASSOCIATIVITY>, MAX_SETS, ALLOCATION>
// TLB Data Structures For 'm' nodes each with 'n' cores
namespace ITLB
{
// instruction TLB: 4 kB pages, 128 entries, 8-way associative
const uint32_t lineSize = 4 * KILO;
const uint32_t tlbSize = 512 * lineSize;
const uint32_t associativity = 8;
const TLB_ALLOC::STORE_ALLOCATION allocation = TLB_ALLOC::STORE_ALLOCATE;
const uint32_t max_sets = tlbSize / (lineSize * associativity);
const uint32_t max_associativity = associativity;
typedef TLB_ROUND_ROBIN(max_sets, max_associativity, allocation) TLB;
}
extern ITLB::TLB itlbs[core_count * num_nodes];
#define BOOST_PP_LOCAL_LIMITS (0, ((core_count * num_nodes) - 1))
#define BOOST_PP_LOCAL_MACRO(n) ITLB::TLB("ITLB" #n, ITLB::tlbSize, ITLB::lineSize, ITLB::associativity),
ITLB::TLB itlbs[] =
{
#include "boost/preprocessor/iteration/detail/local.hpp"
};
namespace DTLB
{
// data TLB: 4 kB pages, 64 entries, 4-way associative
const uint32_t lineSize = 4 * KILO;
const uint32_t tlbSize = 1024 * lineSize;
const uint32_t associativity = 4;
const TLB_ALLOC::STORE_ALLOCATION allocation = TLB_ALLOC::STORE_ALLOCATE;
const uint32_t max_sets = tlbSize / (lineSize * associativity);
const uint32_t max_associativity = associativity;
typedef TLB_ROUND_ROBIN(max_sets, max_associativity, allocation) TLB;
}
extern DTLB::TLB dtlbs[core_count * num_nodes];
#define BOOST_PP_LOCAL_LIMITS (0, ((core_count * num_nodes) - 1))
#define BOOST_PP_LOCAL_MACRO(n) DTLB::TLB("DTLB" #n, DTLB::tlbSize, DTLB::lineSize, DTLB::associativity),
DTLB::TLB dtlbs[] =
{
#include "boost/preprocessor/iteration/detail/local.hpp"
};
#endif // PIN_TLB_H