-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCache.h
executable file
·153 lines (132 loc) · 2.97 KB
/
Cache.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
#ifndef CACHE_H
#define CACHE_H
#include <cstddef>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <pthread.h>
//#include <memory>
//using std::unique_ptr;
using std::string;
#define MAX_WAYS 32
#define MAX_SLICES 32
#define SLICE0 0x1B5F575440ULL
#define SLICE1 0x2EB5FAA880ULL
#define SLICE2 0x3CCCC93100ULL
class CacheMemory;
typedef enum { MISS = false, HIT = true } CLState;
struct Access {
uint64_t addr;
uint64_t secret = 0;
bool write = false;
bool instruction = false;
uint8_t thread = 0;
};
struct CacheLine {
uint64_t addr;
int time;
bool valid;
bool used;
};
struct CacheConfig {
unsigned line_bits;
unsigned way_bits;
unsigned slices;
unsigned line_size_bits;
};
struct AccessResult {
CLState hit;
bool evicted;
uint64_t evicted_addr;
};
struct CacheSet {
uint32_t index[MAX_WAYS];
};
class Cache
{
public:
virtual void clearCache() = 0;
virtual CLState isCached(size_t addr, size_t secret) = 0;
virtual AccessResult access(Access mem_access) = 0;
virtual void resetUsage(int slice) = 0;
virtual void flush(Access mem_access) = 0;
virtual uint32_t getUsage(int slice)
{
return occupied_lines_[slice];
};
virtual uint64_t hits(int slice)
{
return cache_hits_[slice];
};
virtual uint64_t misses(int slice)
{
return cache_misses_[slice];
};
virtual uint64_t accesses(int slice)
{
return cache_misses_[slice] + cache_hits_[slice];
};
virtual unsigned getSlice(size_t phys_addr)
{
int slice = 0;
if (slices_ >= 2)
{
slice = __builtin_popcountll(phys_addr & SLICE0) % 2;
if (slices_ >= 4)
{
slice |= (__builtin_popcountll(phys_addr & SLICE1) % 2) << 1;
if (slices_ >= 8)
{
slice |= (__builtin_popcountll(phys_addr & SLICE2) % 2) << 2;
}
}
}
return slice;
};
virtual unsigned getCacheSize()
{
return size_;
};
virtual uint64_t getLineSize()
{
return line_size_;
};
virtual unsigned getLines()
{
return lines_;
};
virtual unsigned getWays()
{
return ways_;
};
virtual string getName()
{
return name_;
};
virtual string getPolicy()
{
return policy_name_;
};
virtual unsigned getSlices()
{
return slices_;
};
virtual ~Cache() {};
CacheLine *memory_;
CacheMemory *c_memory_;
protected:
string name_;
string policy_name_;
uint64_t line_size_;
unsigned line_size_bits_;
uint32_t lines_;
unsigned line_bits_;
unsigned ways_;
unsigned way_bits_;
unsigned slices_;
uint32_t size_;
uint32_t occupied_lines_[MAX_SLICES];
uint64_t cache_misses_[MAX_SLICES];
uint64_t cache_hits_[MAX_SLICES];
};
#endif // CACHE_H