-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExclusive.cpp
More file actions
114 lines (110 loc) · 2.24 KB
/
Copy pathExclusive.cpp
File metadata and controls
114 lines (110 loc) · 2.24 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
#ifndef EXCLUSIVE
#define EXCLUSIVE
#include "LRU.hpp"
class L1Exclusive : public LRUCache
{
public:
L1Exclusive(ulong cacheSize, ulong blockSize) : LRUCache(cacheSize, blockSize) {}
void handleEviction(Cache *upperCache, Cache *, page replacedPage)
{
//evictions from L1 are put in L2, which will always have space
upperCache->insert(replacedPage.addr, replacedPage.state);
}
void read(ulong address)
{
if(!inCache(address))
{
missCount++;
upperCache->read(address);
}
else
{
readCount++;
replace(address, CLEAN);
}
}
void write(ulong address)
{
//std::cout << "L1 write" << std::endl;
if(!inCache(address))
{
//std::cout << "miss?" << std::endl;
missCount++;
upperCache->write(address);
}
else
{
//if it's in L1, update the LRU and mark as messy
writeCount++;
replace(address, MESSY);
}
}
};
class L2Exclusive : public LRUCache
{
public:
L2Exclusive(ulong cacheSize, ulong blockSize) : LRUCache(cacheSize, blockSize) {}
virtual void read(ulong address)
{
//std::cout << "L2 read" << std::endl;
readCount++;
if(!inCache(address))
{
//read from next level
upperCache->read(address);
replace(address, CLEAN);
}
else
{
hitCount++;
evict(address);
lowerCache->insert(address, CLEAN);
}
}
virtual void write(ulong address)
{
//std::cout << "L2 write" << std::endl;
if(!inCache(address))
{
writeCount++;
replace(address, MESSY);
//TODO: Invalidate L3 cache for this address if necessary
}
else
{
//std::cout << "in L2" << std::endl;
evict(address);
hitCount++;
//std::cout << "after evict" << std::endl;
lowerCache->insert(address, MESSY);
}
}
};
class L2ExclusivePrefetch : public L2Exclusive
{
public:
L2ExclusivePrefetch(ulong cacheSize, ulong blockSize) : L2Exclusive(cacheSize, blockSize) {}
void read(ulong address)
{
bool cacheFound = inCache(address);
L2Exclusive::read(address);
//for prefetch, we grab the next cache line to prepare for L1
if(cacheFound)
{
if(!lowerCache->inCache(address+blockSize_))
{
if(!inCache(address+blockSize_))
{
upperCache->read(address+blockSize_);
missCount--;
}
else
{
hitCount--;
}
replace(address+blockSize_, CLEAN);
}
}
}
};
#endif