-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache_sim.c
224 lines (198 loc) · 5.49 KB
/
cache_sim.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
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <assert.h>
// Global & types declaration {{{
#define WORD_WIDTH 0 // Bits
#define WORD_SIZE 4 // Bytes
enum cache_block_state {
INVALID,
VALID
};
typedef unsigned uint;
typedef struct cache_block{
enum cache_block_state status;
uint tag;
uint time;
}cache_block;
uint sets;
uint assoc;
uint block;
uint block_log2 = 0;
uint total_cache_accesses = 0;
uint total_cache_misses = 0;
uint sim_time = 0;
struct cache_block *cache;
//}}}
// Declarations {{{
uint index_of_addr(uint addr, uint index_width, uint block_width);
uint tag_of_addr(uint addr, uint index_width, uint block_width);
cache_block* get_block(uint offset, uint tag);
void insert_block(uint offset, uint tag);
cache_block* evict_block(uint offset);
bool is_full(uint offset);
cache_block* find_next_empty(uint offset);
void increment_all(uint offset, uint tag);
void cache_access(char inst_type, uint addr);
// }}}
// index_of_addr {{{
// It will compute the index of a given address
uint index_of_addr(uint addr, uint index_width, uint block_width) {
uint output = 0;
block_width += WORD_WIDTH;
uint index_pos = index_width + block_width;
uint mask_left = ((1 << (index_pos)) - 1); //! left = 4 then mask: 0000 1111 1111
output = addr & mask_left;
output >>= block_width;
return output;
}
// }}}
// tag_of_addr {{{
// It will compute the tag of a given address
uint tag_of_addr(uint addr, uint index_width, uint block_width) {
int shift = index_width + block_width + WORD_WIDTH;
addr >>= shift;
assert(addr < (1 << (WORD_SIZE - (index_width + block_width)))); // Invariant
return addr;
}
// }}}
// get_block {{{
// It will return the block inside the set given its tag
cache_block* get_block(uint offset, uint tag) {
int i;
for (i = 0; i < assoc; i++) {
cache_block* block = &cache[offset + i];
if (block->tag == tag)
return block;
}
return NULL;
}
// }}}
// insert_block {{{
// It will insert the block inside the cache
// If there is no space it will evict the LRU block
void insert_block(uint index, uint tag) {
uint offset = index * assoc;
cache_block* block = get_block(offset, tag);
if (block != NULL) { //! If we have a cache hit
block->time = 0;
} else { //! If we have a cache miss
total_cache_misses++;
cache_block *block = NULL;
if (is_full(offset)) { //! If our cache is full, evict
block = evict_block(offset); //! the LRU block
} else { //! If it is not full,
block = find_next_empty(offset); //! place it in the first empty slot
}
block->tag = tag;
block->status = VALID;
block->time = 0;
}
increment_all(offset, tag); //! Increment the time for each element but the one we just placed it
}
// }}}
// evict_block {{{
// It will return the LRU block of a given set
cache_block* evict_block(uint offset) {
int i = 0;
uint max = 0;
int max_index = 0;
for (; i < assoc; i++) {
cache_block* block = &cache[offset + i];
if (block->time > max) {
max = block->time;
max_index = i;
}
}
return &cache[offset + max_index];
}
// }}}
// is_full {{{
// returns whether the set is full
bool is_full(uint offset) {
int i;
for (i = 0; i < assoc; i++) {
cache_block* block = &cache[offset + i];
if (block->status == INVALID) return false;
}
return true;
}
// }}}
// find_next_empty {{{
// It will return the next empty slot
cache_block* find_next_empty(uint offset) {
int i;
for (i = 0; i < assoc; i++) {
cache_block* block = &cache[offset + i];
if (block->status == INVALID) return block;
}
return NULL;
}
// }}}
// increment_all {{{
// Increment the index of all the block in the give set
// but the one specified in the tag
void increment_all(uint offset, uint tag) {
int i;
for (i = 0; i < assoc; i++) {
cache_block* block = &cache[offset + i];
if (block->tag != tag) {
block->time = block->time + 1;
}
}
}
// }}}
// cache_access {{{
void cache_access(char inst_type, uint addr) {
uint index = index_of_addr(addr, log2(sets), block_log2);
uint tag = tag_of_addr(addr, log2(sets), block_log2);
assert(index < sets); // Invariant
assert((addr >> block_log2) == (index + (tag << (int)log2(sets)))); // Invariant: well formated tag and index
insert_block(index, tag);
}
// }}}
// main {{{
int main(int argc, char** argv)
{
if (argc != 5) {
printf("Usage: %s <number_of_sets> <associativity> <cache_block_size> <trace_file>\n", argv[0]);
return -1;
}
/*
input parameters
argv[1] = number of sets
argv[2] = associativity
argv[3] = cache block size
argv[4] = trace file name
*/
char inst_type = 0;
uint addr = 0;
sets = atoi(argv[1]);
assoc = atoi(argv[2]);
block = atoi(argv[3]);
while (block >>= 1) ++block_log2;
FILE *fp = fopen(argv[4], "r");
if (fp == NULL){
printf("trace file does not exist\n");
return -1;
}
cache=malloc(sizeof(struct cache_block)*sets*assoc);
int i=0;
for(i=0;i<sets*assoc;i++){
cache[i].time=0;
cache[i].tag=0;
cache[i].status=INVALID;
}
while (fscanf(fp, "%c 0x%x\n", &inst_type, &addr) != EOF) {
cache_access(inst_type, addr);
total_cache_accesses++;
sim_time++;
}
printf("Cache accesses = %u\n", total_cache_accesses);
printf("Cache misses = %u\n", total_cache_misses);
free(cache);
fclose(fp);
return 0;
}
// }}}