-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_sim.c
376 lines (306 loc) · 16 KB
/
mem_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
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
/***************************************************************************
* * Inf2C-CS Coursework 2: Cache Simulation
* *
* * Instructor: Boris Grot
* *
* * TA: Siavash Katebzadeh
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <time.h>
/* Do not add any more header files */
/*
* Various structures
*/
typedef enum {FIFO, LRU, Random} replacement_p;
const char* get_replacement_policy(uint32_t p) {
switch(p) {
case FIFO: return "FIFO";
case LRU: return "LRU";
case Random: return "Random";
default: assert(0); return "";
};
return "";
}
typedef struct {
uint32_t address;
} mem_access_t;
// UPDATE
// These are statistics for the cache and should be maintained by you.
typedef struct {
uint32_t cache_hits;
uint32_t cache_misses;
} result_t;
// FIFO USE
/*
* Parameters for the cache that will be populated by the provided code skeleton.
*/
replacement_p replacement_policy = FIFO;
uint32_t associativity = 0;
uint32_t number_of_cache_blocks = 0;
uint32_t cache_block_size = 0;
// UPDATE
/*
* Each of the variables below must be populated by you.
*/
uint32_t g_num_cache_tag_bits = 0; // tag storage bits
uint32_t g_cache_offset_bits= 0; // offset storage bits
result_t g_result;
/* Reads a memory access from the trace file and returns
* 32-bit physical memory address
*/
mem_access_t read_transaction(FILE *ptr_file) {
char buf[1002];
char* token = NULL;
char* string = buf;
mem_access_t access;
if (fgets(buf, 1000, ptr_file)!= NULL) {
/* Get the address */
token = strsep(&string, " \n");
access.address = (uint32_t)strtoul(token, NULL, 16);
return access;
}
/* If there are no more entries in the file return an address 0 */
access.address = 0;
return access;
}
void print_statistics(uint32_t num_cache_tag_bits, uint32_t cache_offset_bits, result_t* r) {
/* Do Not Modify This Function */
uint32_t cache_total_hits = r->cache_hits;
uint32_t cache_total_misses = r->cache_misses;
printf("CacheTagBits:%u\n", num_cache_tag_bits);
printf("CacheOffsetBits:%u\n", cache_offset_bits);
printf("Cache:hits:%u\n", r->cache_hits);
printf("Cache:misses:%u\n", r->cache_misses);
printf("Cache:hit-rate:%2.1f%%\n", cache_total_hits / (float)(cache_total_hits + cache_total_misses) * 100.0);
}
/*
*
* Add any global variables and/or functions here as needed.
*
*/
// create block struct
typedef struct {
uint32_t tag; // tag of the address in the block
int blockOccupied; // if an address is stored there then it is 1, if block is empty then it is 0
int fifo_index; // keeps track of the first tag that was stored in the array
int access_times; // keeps a record of the number of times the block was accessed, to conclude which one was the least recently used
} block;
uint32_t g_cache_index_bits = 0; // index bits
uint32_t cache_sets = 0; // number of sets needed for the cache size depending on associativity
int main(int argc, char** argv) {
time_t t;
/* Intializes random number generator */
/* Important: *DO NOT* call this function anywhere else. */
srand((unsigned) time(&t));
/* ----------------------------------------------------- */
/* ----------------------------------------------------- */
/*
*
* Read command-line parameters and initialize configuration variables.
*
*/
int improper_args = 0;
char file[10000];
if (argc < 6) {
improper_args = 1;
printf("Usage: ./mem_sim [replacement_policy: FIFO LRU Random] [associativity: 1 2 4 8 ...] [number_of_cache_blocks: 16 64 256 1024] [cache_block_size: 32 64] mem_trace.txt\n");
} else {
/* argv[0] is program name, parameters start with argv[1] */
if (strcmp(argv[1], "FIFO") == 0) {
replacement_policy = FIFO;
} else if (strcmp(argv[1], "LRU") == 0) {
replacement_policy = LRU;
} else if (strcmp(argv[1], "Random") == 0) {
replacement_policy = Random;
} else {
improper_args = 1;
printf("Usage: ./mem_sim [replacement_policy: FIFO LRU Random] [associativity: 1 2 4 8 ...] [number_of_cache_blocks: 16 64 256 1024] [cache_block_size: 32 64] mem_trace.txt\n");
}
associativity = atoi(argv[2]);
number_of_cache_blocks = atoi(argv[3]);
cache_block_size = atoi(argv[4]);
strcpy(file, argv[5]);
}
if (improper_args) {
exit(-1);
}
assert(number_of_cache_blocks == 16 || number_of_cache_blocks == 64 || number_of_cache_blocks == 256 || number_of_cache_blocks == 1024);
assert(cache_block_size == 32 || cache_block_size == 64);
assert(number_of_cache_blocks >= associativity);
assert(associativity >= 1);
printf("input:trace_file: %s\n", file);
printf("input:replacement_policy: %s\n", get_replacement_policy(replacement_policy));
printf("input:associativity: %u\n", associativity);
printf("input:number_of_cache_blocks: %u\n", number_of_cache_blocks);
printf("input:cache_block_size: %u\n", cache_block_size);
printf("\n");
//printf("aaaa");
/* Open the file mem_trace.txt to read memory accesses */
FILE *ptr_file;
ptr_file = fopen(file,"r");
if (!ptr_file) {
printf("Unable to open the trace file: %s\n", file);
exit(-1);
}
/* result structure is initialized for you. */
memset(&g_result, 0, sizeof(result_t));
/* Do not delete any of the lines below.
* Use the following snippet and add your code to finish the task. */
/* You may want to setup your Cache structure here. */
//////////////////////////////////// CACHE STRUCTURE //////////////////////////////////////////
// Calculate the bits needed for the offset, index, tag
g_cache_offset_bits = log2(cache_block_size);
g_cache_index_bits = log2(number_of_cache_blocks / associativity);
g_num_cache_tag_bits = 32 - g_cache_offset_bits - g_cache_index_bits;
// Calculate number of sets in cache depending on associativity to create the cache
// The index is used to determine which cache set the address should go in
cache_sets = number_of_cache_blocks / associativity;
// Create 2D array for cache structure using pointers
// First create a one dimensional array of type blocks for the sets
block **myBlocks;
myBlocks = (block**)malloc(cache_sets*sizeof(block*));
// Then create the actual two dimensional array (sets x associativity) using the number of cache associativity
for (int i = 0; i < cache_sets; i++) {
myBlocks[i] = (block*)malloc(associativity*sizeof(block));
for (int j = 0; j < associativity; j++) {
// Initialise each block in the 2D array
myBlocks[i][j].tag = 0;
myBlocks[i][j].blockOccupied = 0;
myBlocks[i][j].fifo_index = 0;
myBlocks[i][j].access_times = 0;
}
}
// Read address
mem_access_t access;
/* Loop until the whole trace file has been read. */
while(1) {
access = read_transaction(ptr_file);
// If no transactions left, break out of loop.
if (access.address == 0)
break;
/* Add your code here */
// Get the bits for the offset, index and tag from the address
uint32_t address_tag = access.address >> (g_cache_index_bits + g_cache_offset_bits);
uint32_t address_index = access.address << g_num_cache_tag_bits >> (g_num_cache_tag_bits + g_cache_offset_bits);
uint32_t address_offset = access.address << (g_num_cache_tag_bits + g_cache_index_bits);
//printf("Tag: %u, Set index: %u, Offset: %u\n", address_tag, address_index, address_offset);
// Store the address bits in the cache
// 1. Set index determines the cache set
// 2. For each block in a cache set we compare the block with the tag of the address (hit or miss)
// 2.1. If block and tag match, look at valid bit, if it is 1 then it is a hit
// 2.2. Else it is a miss
// If set is full store depending on replacement policy: FIFO, LRU, Random
//printf("%u\n", address_index);
//if (strcmp(get_replacement_policy(replacement_policy), "FIFO") == 0) {
if (replacement_policy == FIFO) {
int address_stored = 0; // indicates whether the address has been stored in the 2D array or not
int evict_index = 0; // index of the block to evict and replace, minimum index
for (int j = 0; j < associativity; j++) {
if (myBlocks[address_index][j].blockOccupied == 0) {
myBlocks[address_index][j].tag = address_tag; // if the block is empty, save the address tag in the block tag
myBlocks[address_index][j].blockOccupied = 1; // set the block to occupied
address_stored = 1; // means that the address has been stored
g_result.cache_misses++;
break;
}
else if (myBlocks[address_index][j].blockOccupied == 1) { // check if the block is occupied
if (myBlocks[address_index][j].tag == address_tag) {
g_result.cache_hits++; // if address tag matches the block tag then it is a 'hit'
address_stored = 1;
break;
}
}
}
if (address_stored == 0) {
// Means that the blocks in the set are full and the address tag did not match any of block tags
// By the FIFO replacement policy, we evict the first block that was access and store our address tag there
int current_fifo_index = myBlocks[address_index][0].fifo_index;
for (int j = 0; j < associativity; j++) {
if (current_fifo_index > myBlocks[address_index][j].fifo_index) {
current_fifo_index = myBlocks[address_index][j].fifo_index;
evict_index = j;
}
}
myBlocks[address_index][evict_index].tag = address_tag;
g_result.cache_misses++;
myBlocks[address_index][evict_index].fifo_index += associativity;
}
}
if (replacement_policy == LRU) {
int address_stored = 0; // indicates whether the address has been stored in the 2D array or not
int evict_index = 0; // index of the block to evict and replace, least recently used block
for (int j = 0; j < associativity; j++) {
if (myBlocks[address_index][j].blockOccupied == 0) {
myBlocks[address_index][j].tag = address_tag; // if the block is empty, save the address tag in the block tag
myBlocks[address_index][j].blockOccupied = 1; // set the block to occupied
myBlocks[address_index][j].access_times++;
address_stored = 1; // means that the address has been stored
g_result.cache_misses++;
break;
}
else if (myBlocks[address_index][j].blockOccupied == 1) { // check if the block is occupied
if (myBlocks[address_index][j].tag == address_tag) {
myBlocks[address_index][j].access_times++;
g_result.cache_hits++; // if address tag matches the block tag then it is a 'hit'
address_stored = 1;
break;
}
}
}
if (address_stored == 0) {
// By LRU replacement policy, we evict the least recently used block and store our address in said block
int least_used_block = myBlocks[address_index][0].access_times;
for (int j = 0; j < associativity; j++) {
if (least_used_block > myBlocks[address_index][j].access_times) {
least_used_block = myBlocks[address_index][j].access_times;
evict_index = j;
}
}
myBlocks[address_index][evict_index].tag = address_tag;
g_result.cache_misses++;
}
}
if (replacement_policy == Random) {
int address_stored = 0; // indicates whether the address has been stored in the 2D array or not
int random_index = 0; // random index of block to replace when the blocks in the set are full
for (int j = 0; j < associativity; j++) {
if (myBlocks[address_index][j].blockOccupied == 0) {
myBlocks[address_index][j].tag = address_tag; // if the block is empty, save the address tag in the block tag
myBlocks[address_index][j].blockOccupied = 1; // set the block to occupied
address_stored = 1; // means that the address has been stored
g_result.cache_misses++;
break;
}
else if (myBlocks[address_index][j].blockOccupied == 1) { // check if the block is occupied
if (myBlocks[address_index][j].tag == address_tag) {
g_result.cache_hits++; // if address tag matches the block tag then it is a 'hit'
address_stored = 1;
break;
}
}
}
if (address_stored == 0) {
// Store the address in a random block
// The only number that changes every time we execute the program is the time of our computer
// The code at the start of the maing takes the seed for random index as the time, so the seed changes and thus the random number value changes
// srand((unsigned) time(&t));
random_index = rand() % (associativity); // Random block number is between 0 and (associativity - 1)
myBlocks[address_index][random_index].tag = address_tag;
g_result.cache_misses++;
}
}
}
// Free the 2D array because the allocated memory is not needed anymore
free(myBlocks);
/* Do not modify code below. */
/* Make sure that all the parameters are appropriately populated. */
print_statistics(g_num_cache_tag_bits, g_cache_offset_bits, &g_result);
/* Close the trace file. */
fclose(ptr_file);
return 0;
}