-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.c
411 lines (366 loc) · 10.8 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <math.h>
int associativity = 2; // Associativity of cache
int blocksize_bytes = 32; // Cache Block size in bytes
int cachesize_kb = 64; // Cache size in KB
int miss_penalty = 30;
long tagSize = 1;
void
print_usage ()
{
printf ("Usage: gunzip2 -c <tracefile> | ./cache -a assoc -l blksz -s size -mp mispen\n");
printf (" tracefile : The memory trace file\n");
printf (" -a assoc : The associativity of the cache\n");
printf (" -l blksz : The blocksize (in bytes) of the cache\n");
printf (" -s size : The size (in KB) of the cache\n");
printf (" -mp mispen: The miss penalty (in cycles) of a miss\n");
exit (0);
}
typedef struct cache
{
int valid;
long tag[32];
int lru[32];
int dirty[32];
} cache;
void hexToBin(char input[], char output[])
{
int i;
int size;
size = strlen(input);
for (i = 0; i < size; i++)
{
if (input[i] =='0')
{
output[i*4] = '0'; output[i*4+1] = '0'; output[i*4+2] = '0'; output[i*4+3] = '0';
}
else if (input[i] =='1')
{
output[i*4] = '0'; output[i*4+1] = '0'; output[i*4+2] = '0'; output[i*4+3] = '1';
}
else if (input[i] =='2')
{
output[i*4] = '0'; output[i*4+1] = '0'; output[i*4+2] = '1'; output[i*4+3] = '0';
}
else if (input[i] =='3')
{
output[i*4] = '0'; output[i*4+1] = '0'; output[i*4+2] = '1'; output[i*4+3] = '1';
}
else if (input[i] =='4')
{
output[i*4] = '0'; output[i*4+1] = '1'; output[i*4+2] = '0'; output[i*4+3] = '0';
}
else if (input[i] =='5')
{
output[i*4] = '0'; output[i*4+1] = '1'; output[i*4+2] = '0'; output[i*4+3] = '1';
}
else if (input[i] =='6')
{
output[i*4] = '0'; output[i*4+1] = '1'; output[i*4+2] = '1'; output[i*4+3] = '0';
}
else if (input[i] =='7')
{
output[i*4] = '0'; output[i*4+1] = '1'; output[i*4+2] = '1'; output[i*4+3] = '1';
}
else if (input[i] =='8')
{
output[i*4] = '1'; output[i*4+1] = '0'; output[i*4+2] = '0'; output[i*4+3] = '0';
}
else if (input[i] =='9')
{
output[i*4] = '1'; output[i*4+1] = '0'; output[i*4+2] = '0'; output[i*4+3] = '1';
}
else if (input[i] =='a')
{
output[i*4] = '1'; output[i*4+1] = '0'; output[i*4+2] = '1'; output[i*4+3] = '0';
}
else if (input[i] =='b')
{
output[i*4] = '1'; output[i*4+1] = '0'; output[i*4+2] = '1'; output[i*4+3] = '1';
}
else if (input[i] =='c')
{
output[i*4] = '1'; output[i*4+1] = '1'; output[i*4+2] = '0'; output[i*4+3] = '0';
}
else if (input[i] =='d')
{
output[i*4] = '1'; output[i*4+1] = '1'; output[i*4+2] = '0'; output[i*4+3] = '1';
}
else if (input[i] =='e')
{
output[i*4] = '1'; output[i*4+1] = '1'; output[i*4+2] = '1'; output[i*4+3] = '0';
}
else if (input[i] =='f')
{
output[i*4] = '1'; output[i*4+1] = '1'; output[i*4+2] = '1'; output[i*4+3] = '1';
}
}
output[size*4] = '\0';
}
int main(int argc, char * argv []) {
long address;
int loadstore, icount;
char marker;
long IC = 0;
long programIC = 0;
int i = 0;
int j = 1;
int dirty_eviction = 0;
int load_hits = 0;
int load_misses = 0;
int store_hits = 0;
int store_misses = 0;
int load = 0;
int store = 0;
long et = 0;
// Process the command line arguments
while (j < argc) {
if (strcmp ("-a", argv [j]) == 0) {
j++;
if (j >= argc)
print_usage ();
associativity = atoi (argv [j]);
j++;
} else if (strcmp ("-l", argv [j]) == 0) {
j++;
if (j >= argc)
print_usage ();
blocksize_bytes = atoi (argv [j]);
j++;
} else if (strcmp ("-s", argv [j]) == 0) {
j++;
if (j >= argc)
print_usage ();
cachesize_kb = atoi (argv [j]);
j++;
} else if (strcmp ("-mp", argv [j]) == 0) {
j++;
if (j >= argc)
print_usage ();
miss_penalty = atoi (argv [j]);
j++;
} else {
print_usage ();
}
}
// print out cache configuration
printf("Cache parameters:\n");
printf ("Cache Size (KB)\t\t\t%d\n", cachesize_kb);
printf ("Cache Associativity\t\t%d\n", associativity);
printf ("Cache Block Size (bytes)\t%d\n", blocksize_bytes);
printf ("Miss penalty (cyc)\t\t%d\n", miss_penalty);
printf ("\n");
//Calculate the tag, index, block offset bits
int blockOffsetBits = (int)(log(blocksize_bytes)/log(2));
int set = (int)((cachesize_kb*1024)/(blocksize_bytes*associativity));
int indexBits = (int)(log(set)/log(2));
int tagBits = 32 - indexBits - blockOffsetBits;
char input[32];
char output[32];
char tagBin[32];
char index[32];
char blockOffset[32];
cache newCache[set];
//Initializing cache = null
for (i=0; i < set; i++)
{
for (j=0; j<associativity; j++)
{
newCache[i].tag[j] = -1;
newCache[i].lru[j] = -1;
newCache[i].dirty[j] = 0;
}
newCache[i].valid = 0;
}
while (scanf("%c %d %lx %d\n",&marker,&loadstore,&address,&icount) != EOF) {
//here is where you will want to process your memory accesses
//convert address to binary
sprintf(input, "%lx", address);
hexToBin(input, output);
//tag = output[0 - (tagBits-1)];
//index = output[tagBits -> (tagBits+index-1)];
for (i = 0; i<tagBits; i++)
{
tagBin[i] = output[i];
}
tagBin[i] = '\0';
for (i = 0; i<indexBits; i++)
{
index[i] = output[tagBits+i];
}
index[i] = '\0';
//Convert index to dec (indexDec)
int indexDec = 0;
int indexi = 0;
int d = 1;
for (i= strlen(index) - 1; i>=0; i--)
{
indexi = index[i] - '0';
indexDec = indexDec + (d*indexi);
d = 2 * d;
}
//Convert tagBin to dec (tagDec)
int tagDec = 0;
int tagi = 0;
d = 1;
for (i= strlen(tagBin) - 1; i>=0; i--)
{
tagi = tagBin[i] - '0';
tagDec = tagDec + (d*tagi);
d = 2 * d;
}
//Compare tag
if (newCache[indexDec].valid == 0)
{
newCache[indexDec].valid = 1;
newCache[indexDec].tag[0] = tagDec;
newCache[indexDec].lru[0] = 0;
et = et + miss_penalty;
if (loadstore == 0)
{
//load
load++;
load_misses++;
} else {
//store
store++;
store_misses++;
newCache[indexDec].dirty[0] = 1;
}
}
else if (newCache[indexDec].valid == 1)
{
int hit = 0;
for (i = 0; i<associativity; i++)
{
if (newCache[indexDec].tag[i] == tagDec)
{
// HIT!!!
hit = 1;
//update lru
for (j = 0; j < associativity; j++)
{
if ((newCache[indexDec].lru[j] != -1) && (newCache[indexDec].lru[j] < newCache[indexDec].lru[i]))
newCache[indexDec].lru[j] = newCache[indexDec].lru[j] + 1;
}
newCache[indexDec].lru[i] = 0;
if (loadstore == 0)
{
//load
load++;
load_hits++;
} else {
//store
store++;
store_hits++;
newCache[indexDec].dirty[i] = 1;
}
break;
}
}
if (hit == 0)
{
// MISS!!!!
et = et + miss_penalty;
int found = 0;
//find the lru block in cache
for (i = 0; i<associativity; i++)
{
if (newCache[indexDec].lru[i] == -1)
{
found = 1;
newCache[indexDec].tag[i] = tagDec;
//update lru
for (j = 0; j < i; j++)
{
newCache[indexDec].lru[j] = newCache[indexDec].lru[j] + 1;
}
newCache[indexDec].lru[i] = 0;
if (loadstore == 0) {
//load
load++;
load_misses++;
} else {
//store
store++;
store_misses++;
newCache[indexDec].dirty[i] = 1;
}
break;
}
}
if (found == 0)
{
// all blocks are in use.
// replace the highest lru
int lru1 = 0;
for (j = 0; j<associativity; j++)
{
if (newCache[indexDec].lru[j] == (associativity - 1))
{
lru1 = j;
break;
}
}
newCache[indexDec].tag[lru1] = tagDec;
for (j = 0; j < associativity; j++)
{
newCache[indexDec].lru[j] = newCache[indexDec].lru[j] + 1;
}
newCache[indexDec].lru[lru1] = 0;
if (loadstore == 0) {
//load
load++;
load_misses++;
if (newCache[indexDec].dirty[lru1] == 1)
{
dirty_eviction++;
et = et+ 2;
newCache[indexDec].dirty[lru1] = 0;
}
} else {
//store
store++;
store_misses++;
if (newCache[indexDec].dirty[lru1] == 1)
{
dirty_eviction++;
et = et + 2;
} else {
newCache[indexDec].dirty[lru1] = 1;
}
}
}
}
}
IC++;
programIC = programIC + icount;
}
// Here is where you want to print out stats
float overall_miss_rate = ((float)load_misses+store_misses)/IC;
float read_miss_rate = ((float)load_misses)/load;
float write_miss_rate = ((float)store_misses/store);
float memory_CPI = ((miss_penalty)*(load_misses+store_misses))/ ((float)programIC);
float total_CPI = 1 + memory_CPI;
float AMAT = overall_miss_rate*miss_penalty;
printf("Simulation results:\n");
// Use your simulator to output the following statistics. The
// print statements are provided, just replace the question marks with
// your calcuations.
printf("\texecution time %ld cycles\n", et+programIC);
printf("\tinstructions %ld\n", programIC);
printf("\tmemory accesses %ld\n", IC);
printf("\toverall miss rate %.2f\n", overall_miss_rate);
printf("\tread miss rate %.2f\n", read_miss_rate);
printf("\tmemory CPI %.2f\n", memory_CPI);
printf("\ttotal CPI %.2f\n", total_CPI);
printf("\taverage memory access time %.2f cycles\n", AMAT);
printf("dirty evictions %d\n", dirty_eviction);
printf("load_misses %d\n", load_misses);
printf("store_misses %d\n", store_misses);
printf("load_hits %d\n", load_hits);
printf("store_hits %d\n", store_hits);
}