-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_sim.c
441 lines (367 loc) · 10.4 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/***************************************************************************
* * 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>
//REMOVE REMOVE REMOVE-------------
char* strsep(char** stringp, const char* delim)
{
char* start = *stringp;
char* p;
p = (start != NULL) ? strpbrk(start, delim) : NULL;
if (p == NULL)
{
*stringp = NULL;
}
else
{
*p = '\0';
*stringp = p + 1;
}
return start;
}
//REMOVE REMOVE REMOVE-------------
/* Do not add any more header files */
/*
* Various structures
*/
typedef enum
{
FIFO,
LRU,
Random
} replacement_p;
//type replacement_p with 3 attributes/policies
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 "";
}
//get_rep_policy(fifo, lru...)
typedef struct
{
uint32_t address;
} mem_access_t;
//mem_access_t type with attribute address
// These are statistics for the cache and should be maintained by you.
typedef struct
{
int cache_hits;
int cache_misses;
} result_t;
/////typdedef for cache block
typedef struct
{
int valid_bit; // 0 or 1
uint32_t tag; //whole tag
int tracker;
} cache_block;
/*
* 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;
int cache_block_size = 0;
/*
* Each of the variables below must be populated by you.
*/
uint32_t g_num_cache_tag_bits = 0;
uint32_t g_cache_offset_bits = 0;
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"); //extract token from string
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.
*
*/
///Variables related to CACHE
//uint32_t no_cache_blocks=0;
uint32_t no_index_bits = 0;
cache_block **cache;
//////declarations
uint32_t g_cache_offset_bits;
uint32_t number_of_sets;
uint32_t no_index_bits;
uint32_t g_num_cache_tag_bits;
//////cache_creator- adjustable according to set associativity
void cache_creator(number_of_cache_blocks, cache_block_size, associativity)
{
g_cache_offset_bits = log2(cache_block_size);
uint32_t number_of_sets = number_of_cache_blocks / associativity;
uint32_t no_index_bits = log2(number_of_sets);
uint32_t g_num_cache_tag_bits = 32 - no_index_bits - g_cache_offset_bits;
cache = (cache_block **)calloc(number_of_sets, sizeof(cache_block *));
//malloc vs calloc
//cache= malloc(number_of_cache_blocks *sizeof(cache_block));
//initialising valid bits of each block to 0
for (int i = 0; i < number_of_sets; i++)
{
cache[i] = calloc(associativity, sizeof(cache_block));
//malloc vs calloc
}
// for (int i=0; i<number_of_cache_blocks;i++)
// {
// cache[i]=cache_block;
// cache_block.valid_bit=0;
// }
}
void fifo(cache_block **cache, uint32_t tag, uint32_t index)
{
int hit = 0; //this is bool
for (int i = 0; i < associativity; i++)
{
if ((cache[index][i].tag == tag) && (cache[index][i].valid_bit == 1))
{
hit = 1;
}
}
if (hit == 1)
{
g_result.cache_hits++;
}
else
{
g_result.cache_misses++;
cache_block new; //variable new
new.tag = tag;
new.valid_bit = 1;
for (int i=associativity-1; i>=1;i--)
{
cache[index][i]=cache[index][i-1];
}
cache[index][0]=new;
}
}
void random(cache_block **cache, uint32_t tag, uint32_t index)
{
int hit = 0; //this is bool
for (int i = 0; i < associativity; i++)
{
if ((cache[index][i].tag == tag) && (cache[index][i].valid_bit == 1))
{
hit = 1;
}
}
if (hit == 1)
{
g_result.cache_hits++;
}
else
{
g_result.cache_misses++;
cache_block new; //variable new
new.tag = tag;
new.valid_bit = 1;
//g_result.cache_misses++;
uint32_t y=rand() % associativity;
// cache_block rand;
// cache[index][y]=rand;
cache[index][y]=new;
}
}
/*void lru(cache_block **cache, uint32_t tag, uint32_t index)
{
int hit = 0; //this is bool
for (int i = 0; i < associativity; i++)
{
cache[index][i].tracker=1;
}
for (int i = 0; i < associativity; i++)
{
if ((cache[index][i].tag == tag) && (cache[index][i].valid_bit == 1))
{
hit = 1;
cache[index][i].tracker=0;
for(int j=0;j<associativity ;j++)
{
if( j!=i)
{
cache[index][j].tracker++;
}
}
}
else
{
cache[index][i].tracker++;
}
}
if (hit == 1)
{
g_result.cache_hits++;
}
else
{
g_result.cache_misses++;
// cache_block new; //variable new
// new.tag = tag;
// new.valid_bit = 1;
// for (int i=1;i<=associativity;i++)
// {
// cache[index][i]=cache[index][i-1];
// }
// cache[index][0]=new;
}
}
*/
////helper function
uint32_t srl(uint32_t x, uint32_t n)
{
return (uint32_t)((uint32_t)x >> n);
}
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");
/* 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. */
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;
//cache_simulator(access);
/* Add your code here */
uint32_t tag = srl(access.address, no_index_bits + g_cache_offset_bits);
uint32_t filter = pow(2, no_index_bits) - 1;
uint32_t index = srl(access.address, g_cache_offset_bits) & filter;
if (replacement_policy == FIFO)
{
fifo(cache, tag, index);
}
// else if (replacement_policy == LRU)
// {
// lru(cache, tag, index);
// }
else
{
random(cache, tag, index);
}
}
/* Do not modify code below. */
/* Make sure that all the parameters are appropriately populated. */
free(cache);
print_statistics(g_num_cache_tag_bits, g_cache_offset_bits, &g_result);
/* Close the trace file. */
fclose(ptr_file);
return 0;
}