-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.c
More file actions
561 lines (525 loc) · 18.7 KB
/
first.c
File metadata and controls
561 lines (525 loc) · 18.7 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include <limits.h>
typedef struct Cache{
unsigned int valid;
unsigned long int tag;
unsigned long int replacement;
long int replacement2;
}Cache;
int hit=0;
int miss=0;
int readnum=0;
int writenum=0;
long int push=0;
Cache** cache;
unsigned long int getTagIndex(unsigned long int, int);
unsigned long int getTagIndex(unsigned long int address, int move){
return address>>move;
}
unsigned long int getSetIndex(unsigned long int, int, int);
unsigned long int getSetIndex(unsigned long int address, int offsetbit,int setindexbit){
return (address>>offsetbit)&((1<<setindexbit)-1);
}
int getExtraCredit(char*);
int getExtraCredit(char* poli){
if(strcmp("fifo", poli)==0){
return 0;
}
return 1;
}
int getCachesize(char**);
int getCachesize(char** arg){
return atoi(arg[1]);
}
char* gettracefile(char**);
char* gettracefile(char** arg){
return arg[5];
}
int getBlocksize(char**);
int getBlocksize(char** arg){
return atoi(arg[4]);
}
int getSetnumber(char**);
int getSetnumber(char** arg){
char* associativity=arg[2];
int c=getCachesize(arg);//cache size
int b=getBlocksize(arg);//block size
if(strcmp("direct", associativity)==0){
return c/b;
}
if(strcmp("assoc", associativity)==0){
return 1;
}
char num[1000];
for(int i=6,j=0;i<strlen(associativity)&&j<1000;i++,j++){
num[j]=associativity[i];
}
return c/b/atoi(num);
}
int getassociativity(char**);
int getassociativity(char** arg){
char* associativity=arg[2];
int c=getCachesize(arg);//cache size
int b=getBlocksize(arg);//block size
if(strcmp("direct", associativity)==0){
return 1;
}
if(strcmp("assoc", associativity)==0){
return c/b;
}
char num[1000];
for(int i=6,j=0;i<strlen(associativity)&&j<1000;i++,j++){
num[j]=associativity[i];
}
return atoi(num);
// return 0;
}
Cache** initialize(char**);
Cache** initialize(char** arg){
int setnumber=getSetnumber(arg);
int ass=getassociativity(arg);
cache=(struct Cache **)malloc(sizeof(struct Cache)*setnumber);
for(int i=0;i<setnumber;i++){
cache[i]=(struct Cache *)malloc(sizeof(struct Cache)*ass);
for(int j=0;j<ass;j++){
cache[i][j].replacement=0;
cache[i][j].tag=0;
cache[i][j].valid=0;
cache[i][j].replacement2=0;
}
}
hit=0;
push=0;
miss=0;
readnum=0;
writenum=0;
return cache;
}
void print(){
// printf("reads: %d ",readnum);
// printf("writes: %d ",writenum);
// printf("hits: %d ",hit);
// printf("misses: %d\n",miss);
}
void prefet(int , unsigned long int ,unsigned long int );
void prefet(int ass, unsigned long int secondsetindex,unsigned long int secondtagindex){
for(int i=0;i<ass;i++){
//printf("%lx\t",cache[setindex][i].tag);
if(cache[secondsetindex][i].valid==1&&cache[secondsetindex][i].tag==secondtagindex){
//hit++;
// if(status=='W'){
// writenum++;
// }
break;
}
else if(cache[secondsetindex][i].valid==1&&ass==i+1){
//occupied
// miss++;
readnum++;
// if(status=='W'){
// writenum++;
// }
long int maxnumber=LONG_MAX;
for(int j=0;j<ass;j++){
if(maxnumber>=cache[secondsetindex][j].replacement){
maxnumber=cache[secondsetindex][j].replacement;
}
}
for(int j=0;j<ass;j++){
if(maxnumber==cache[secondsetindex][j].replacement){
cache[secondsetindex][j].tag=secondtagindex;
cache[secondsetindex][j].replacement=push;
push++;
break;
}
}
return;
}else if(cache[secondsetindex][i].valid==0){
//empty
// miss++;
readnum++;
// if(status=='W'){
// writenum++;
// }
cache[secondsetindex][i].valid=1;
cache[secondsetindex][i].tag=secondtagindex;
cache[secondsetindex][i].replacement=push;
push++;
break;
}
else
continue;
}
}
void prefetchlru(int,unsigned long int,unsigned long int);
void prefetchlru(int ass,unsigned long int secondsetindex,unsigned long int secondtagindex){
for(int i=0;i<ass;i++){
//printf("%lx\t",cache[setindex][i].tag);
if(cache[secondsetindex][i].valid==1&&cache[secondsetindex][i].tag==secondtagindex){
// hit++;
// if(status=='W'){
// writenum++;
// }
// cache[secondsetindex][i].replacement2=0;
// for(int j=0;j<ass;j++){
// if(i==j){
//
// }else{
// cache[secondsetindex][j].replacement2--;
// }
// }
break;
}
else if(cache[secondsetindex][i].valid==1&&ass==i+1){
//occupied
//miss++;
readnum++;
long int maxnumber=LONG_MAX;
for(int j=0;j<ass;j++){
if(maxnumber>=cache[secondsetindex][j].replacement2){
maxnumber=cache[secondsetindex][j].replacement2;
}
}
for(int j=0;j<ass;j++){
if(maxnumber==cache[secondsetindex][j].replacement2){
cache[secondsetindex][j].tag=secondtagindex;
cache[secondsetindex][j].replacement2=0;
for(int k=0;k<ass;k++){
if(j==k){
}else{
cache[secondsetindex][k].replacement2--;
}
}
break;
}
}
return;
}else if(cache[secondsetindex][i].valid==0){
//empty
// miss++;
readnum++;
// if(status=='W'){
// writenum++;
// }
cache[secondsetindex][i].valid=1;
cache[secondsetindex][i].tag=secondtagindex;
cache[secondsetindex][i].replacement2=0;
for(int j=0;j<ass;j++){
if(i==j){
}else{
cache[secondsetindex][j].replacement2--;
}
}
break;
}
else
continue;
}
}
void accesss(char,int ,int ,int , unsigned long int , unsigned long int ,unsigned long int ,unsigned long int );
void accesss(char status ,int prefetch,int policy,int ass, unsigned long int tagindex, unsigned long int setindex,unsigned long int secondtagindex,unsigned long int secondsetindex){
if(policy==0){
if(prefetch==0){
//no prefetch
for(int i=0;i<ass;i++){
//printf("%lx\t",cache[setindex][i].tag);
if(cache[setindex][i].valid==1&&cache[setindex][i].tag==tagindex){
hit++;
if(status=='W'){
writenum++;
}
break;
}
else if(cache[setindex][i].valid==1&&ass==i+1){
//occupied
miss++;
readnum++;
if(status=='W'){
writenum++;
}
long int maxnumber=LONG_MAX;
for(int j=0;j<ass;j++){
if(maxnumber>=cache[setindex][j].replacement){
maxnumber=cache[setindex][j].replacement;
}
}
for(int j=0;j<ass;j++){
if(maxnumber==cache[setindex][j].replacement){
cache[setindex][j].tag=tagindex;
cache[setindex][j].replacement=push;
push++;
break;
}
}
return;
}else if(cache[setindex][i].valid==0){
//empty
miss++;
readnum++;
if(status=='W'){
writenum++;
}
cache[setindex][i].valid=1;
cache[setindex][i].tag=tagindex;
cache[setindex][i].replacement=push;
push++;
break;
}
else
continue;
}
}else{
//prefetch
for(int i=0;i<ass;i++){
//printf("%lx\t",cache[setindex][i].tag);
if(cache[setindex][i].valid==1&&cache[setindex][i].tag==tagindex){
hit++;
if(status=='W'){
writenum++;
}
break;
}
else if(cache[setindex][i].valid==1&&ass==i+1){
//occupied
miss++;
readnum++;
if(status=='W'){
writenum++;
}
long int maxnumber=LONG_MAX;
for(int j=0;j<ass;j++){
if(maxnumber>=cache[setindex][j].replacement){
maxnumber=cache[setindex][j].replacement;
}
}
for(int j=0;j<ass;j++){
if(maxnumber==cache[setindex][j].replacement){
cache[setindex][j].tag=tagindex;
cache[setindex][j].replacement=push;
push++;
break;
}
}
prefet(ass,secondsetindex,secondtagindex);
return;
}else if(cache[setindex][i].valid==0){
//empty
miss++;
readnum++;
if(status=='W'){
writenum++;
}
cache[setindex][i].valid=1;
cache[setindex][i].tag=tagindex;
cache[setindex][i].replacement=push;
push++;
prefet(ass,secondsetindex,secondtagindex);
break;
}
else
continue;
}
}
}
else if(policy==1){
if(prefetch==0){
//no prefetch
for(int i=0;i<ass;i++){
//printf("%lx\t",cache[setindex][i].tag);
if(cache[setindex][i].valid==1&&cache[setindex][i].tag==tagindex){
hit++;
if(status=='W'){
writenum++;
}
cache[setindex][i].replacement2=0;
for(int j=0;j<ass;j++){
if(i==j){
}else{
cache[setindex][j].replacement2--;
}
}
break;
}
else if(cache[setindex][i].valid==1&&ass==i+1){
//occupied
miss++;
readnum++;
if(status=='W'){
writenum++;
}
long int maxnumber=LONG_MAX;
for(int j=0;j<ass;j++){
if(maxnumber>=cache[setindex][j].replacement2){
maxnumber=cache[setindex][j].replacement2;
}
}
for(int j=0;j<ass;j++){
if(maxnumber==cache[setindex][j].replacement2){
cache[setindex][j].tag=tagindex;
cache[setindex][j].replacement2=0;
for(int k=0;k<ass;k++){
if(j==k){
}else{
cache[setindex][k].replacement2--;
}
}
break;
}
}
return;
}else if(cache[setindex][i].valid==0){
//empty
miss++;
readnum++;
if(status=='W'){
writenum++;
}
cache[setindex][i].valid=1;
cache[setindex][i].tag=tagindex;
cache[setindex][i].replacement2=0;
for(int j=0;j<ass;j++){
if(i==j){
}else{
cache[setindex][j].replacement2--;
}
}
break;
}
else
continue;
}
}else{
//prefetch
for(int i=0;i<ass;i++){
//printf("%lx\t",cache[setindex][i].tag);
if(cache[setindex][i].valid==1&&cache[setindex][i].tag==tagindex){
hit++;
if(status=='W'){
writenum++;
}
cache[setindex][i].replacement2=0;
for(int j=0;j<ass;j++){
if(i==j){
}else{
cache[setindex][j].replacement2--;
}
}
break;
}
else if(cache[setindex][i].valid==1&&ass==i+1){
//occupied
miss++;
readnum++;
if(status=='W'){
writenum++;
}
long int maxnumber=LONG_MAX;
for(int j=0;j<ass;j++){
if(maxnumber>=cache[setindex][j].replacement2){
maxnumber=cache[setindex][j].replacement2;
}
}
for(int j=0;j<ass;j++){
if(maxnumber==cache[setindex][j].replacement2){
cache[setindex][j].tag=tagindex;
cache[setindex][j].replacement2=0;
for(int k=0;k<ass;k++){
if(j==k){
}else{
cache[setindex][k].replacement2--;
}
}
break;
}
}
prefetchlru(ass,secondsetindex,secondtagindex);
return;
}else if(cache[setindex][i].valid==0){
//empty
miss++;
readnum++;
if(status=='W'){
writenum++;
}
cache[setindex][i].valid=1;
cache[setindex][i].tag=tagindex;
cache[setindex][i].replacement2=0;
for(int j=0;j<ass;j++){
if(i==j){
}else{
cache[setindex][j].replacement2--;
}
}
prefetchlru(ass,secondsetindex,secondtagindex);
break;
}
else
continue;
}
}
}
}
int main(int argc, char** argv){
if(argc!=6)return 0;
//int cachesizeC=getCachesize(argv);// cache size
cache=initialize(argv);// initialize the new cache
// cache=create(getSetnumber(argv), getassociativity(argv));
int ass=getassociativity(argv);// associativity
int block=getBlocksize(argv);// block size
int set=getSetnumber(argv);//set number
char* poli=argv[3];
int ExtraCredit=getExtraCredit(poli);
char status;
unsigned long int address;
unsigned long int firstco;
FILE* fl=fopen(gettracefile(argv), "r");
int val=fscanf(fl, "%lx: %c %lx",&firstco,&status,&address);
int switcher=0;
//non-prefetch
while(val==3){
// if(readnum==2867&&writenum==2847&&hit==7068){
// readnum+=0;
// }
int offsetbit=log2(block);
int setindexbit=log2(set);
int sum=offsetbit+setindexbit;
unsigned long int tagindex=getTagIndex(address,sum);
unsigned long int setindex=getSetIndex(address, offsetbit, setindexbit);
unsigned long int secondaddress=address+block;
unsigned long int secondtagindex=getTagIndex(secondaddress,sum);
unsigned long int secondsetindex=getSetIndex(secondaddress, offsetbit, setindexbit);
accesss(status,switcher,ExtraCredit, ass, tagindex, setindex, secondtagindex, secondsetindex);
if(switcher==1)
print();
val = fscanf(fl, "%lx: %c %lx",&firstco,&status,&address);
if(val!=3){
switcher++;
if(switcher==1){
//non-prefetch
printf("no-prefetch\n");
printf("Memory reads: %d\n",readnum);
printf("Memory writes: %d\n",writenum);
printf("Cache hits: %d\n",hit);
printf("Cache misses: %d\n",miss);
}else if(switcher==2){
//prefetch
printf("with-prefetch\n");
printf("Memory reads: %d\n",readnum);
printf("Memory writes: %d\n",writenum);
printf("Cache hits: %d\n",hit);
printf("Cache misses: %d\n",miss);
break;
}else{
break;
}
fl=fopen(gettracefile(argv), "r");
val=fscanf(fl, "%lx: %c %lx",&firstco,&status,&address);
cache=initialize(argv);// initialize the new cache
//cache=create(getSetnumber(argv), getassociativity(argv));
}
}
return 0;
}