-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
218 lines (169 loc) · 5.55 KB
/
cache.c
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
#include "cache.h"
struct cache_info cache = {
.create = cache_create,
.destroy = cache_destroy,
.read = cache_read,
.write = cache_write
};
static int num_of_bits(int num) {
int ret = 0;
while ((num >>= 1)) {
++ret;
}
return ret;
}
int cache_create(struct cache_info *ci, int _capacity, int _n_way, int _block_sz) {
// Initialize configuration variables
ci->capacity = _capacity * 1024;
ci->n_way = _n_way;
ci->block_sz = _block_sz;
ci->n_entry = ci->capacity / ci->block_sz;
ci->n_set = ci->n_entry / ci->n_way;
ci->b_offset = num_of_bits(ci->block_sz);
ci->b_index = num_of_bits(ci->capacity / ci->n_way / ci->block_sz);
ci->b_tag = 64 - ci->b_offset - ci->b_index;
ci->offset_mask = UINT64_MAX >> ( 64 - ci->b_offset);
ci->tag_mask = UINT64_MAX << ( 64 - ci->b_tag);
ci->index_mask = ( UINT64_MAX ^ ci->offset_mask ) & (UINT64_MAX ^ ci->tag_mask);
// Allocate cache
ci->way = (struct cache_way *)malloc(sizeof(struct cache_way) * ci->n_way);
ci->set = (struct cache_set *)malloc(sizeof(struct cache_set) * ci->n_set);
for (int i = 0; i < ci->n_way; i++) {
ci->way[i].tbl =
(struct cache_entry *)malloc(sizeof(struct cache_entry) * ci->n_set);
}
for (int i = 0; i < ci->n_set; i++) {
ci->set[i].ptr =
(struct cache_entry **)malloc(sizeof(struct cache_entry *) * ci->n_way);
ci->set[i].lru = lru_init(ci->n_way);
// Initialize cache entries
for (int j = 0; j < ci->n_way; j++) {
ci->set[i].ptr[j] = &ci->way[j].tbl[i];
ci->set[i].ptr[j]->is_valid = false;
ci->set[i].ptr[j]->lru_ptr =
lru_push(ci->set[i].lru, (void *)ci->set[i].ptr[j]);
}
}
return CA_EXIT_SUCCESS;
}
int cache_destroy(struct cache_info *ci) {
int i, j;
// Calculate some info
ci->total_cnt = ci->read_cnt + ci->write_cnt;
for (i = 0; i < ci->n_way; i++) {
for (j = 0; j < ci->n_set; j++) {
struct cache_entry *entry = ci->set[j].ptr[i];
if (entry->is_valid) {
ci->checksum ^= ( ( entry->tag ^ j ) << 1 ) | entry->is_dirty;
}
}
}
// Print info
puts("-- General Stats --");
printf("Capacity: %d\n", ci->capacity / 1024);
printf("Way: %d\n", ci->n_way);
printf("Block size: %d\n", ci->block_sz);
printf("Total accesses: %lu\n", ci->total_cnt);
printf("Read accesses: %lu\n", ci->read_cnt);
printf("Write accesses: %lu\n", ci->write_cnt);
printf("Read misses: %lu\n", ci->read_miss);
printf("Write misses: %lu\n", ci->write_miss);
printf("Read miss rate: %.2f%%\n", (float)ci->read_miss / ci->read_cnt * 100);
printf("Write miss rate: %.2f%%\n", (float)ci->write_miss / ci->write_cnt * 100);
printf("Clean evictions: %lu\n", ci->clean_evict);
printf("Dirty evictions: %lu\n", ci->dirty_evict);
printf("Checksum: 0x%lx\n", ci->checksum);
// Clear
for (int i = 0; i < ci->n_way; i++) {
free(ci->way[i].tbl);
}
for (int i = 0; i < ci->n_set; i++) {
lru_free(ci->set[i].lru);
}
free(ci->way);
free(ci->set);
return CA_EXIT_SUCCESS;
}
static inline uint64_t get_index(uint64_t addr) {
return ( ( addr & cache.index_mask ) >> cache.b_offset );
}
static inline uint64_t get_tag(uint64_t addr) {
return ( ( addr & cache.tag_mask ) >> ( 64 - cache.b_tag ) );
}
static void check_cache(struct cache_entry **target, struct cache_set *set, uint64_t tag, bool *is_hit, bool *is_full) {
struct cache_entry *iter;
// Check cache hit or miss
for (int i = cache.n_way-1; i >= 0; i--) {
iter = set->ptr[i];
if (!iter->is_valid) {
*is_full = false;
*target = iter;
continue;
}
if (tag == iter->tag) {
*is_hit = true;
*target = iter;
break;
}
}
}
static void evict_victim(struct cache_entry **target, struct cache_set *set) {
*target = lru_get_victim(set->lru);
if ((*target)->is_dirty) {
++cache.dirty_evict;
// Write-back
// target->data => memory
} else {
++cache.clean_evict;
}
}
static void read_data(struct cache_entry **target, uint64_t tag) {
(*target)->tag = tag;
(*target)->is_valid = true;
(*target)->is_dirty = false;
// (*target)->data <= memory
}
int cache_read(uint64_t addr) {
struct cache_set *set;
uint64_t tag;
struct cache_entry *target;
bool is_hit = false;
bool is_full = true;
set = &cache.set[get_index(addr)];
tag = get_tag(addr);
++cache.read_cnt;
check_cache(&target, set, tag, &is_hit, &is_full);
if (!is_hit) {
++cache.read_miss;
if (is_full) {
evict_victim(&target, set);
}
read_data(&target, tag);
}
// Update
lru_update(set->lru, target->lru_ptr);
return CA_EXIT_SUCCESS;
}
int cache_write(uint64_t addr) {
struct cache_set *set;
uint64_t tag;
struct cache_entry *target;
bool is_hit = false;
bool is_full = true;
set = &cache.set[get_index(addr)];
tag = get_tag(addr);
++cache.write_cnt;
check_cache(&target, set, tag, &is_hit, &is_full);
if (!is_hit) {
++cache.write_miss;
if (is_full) {
evict_victim(&target, set);
}
read_data(&target, tag);
}
// Update
lru_update(set->lru, target->lru_ptr);
// Mark as dirty
target->is_dirty = true;
return CA_EXIT_SUCCESS;
}