-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache simulator.c
115 lines (100 loc) · 2.27 KB
/
cache simulator.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
#include <stdio.h>
#include <stdlib.h>
typedef struct blk *block_ptr;
typedef struct blk{
int valid;
int tag;
int period;
}blk;
block_ptr bp;
int cycle, miss;
int lru_time;
int find_index(int set, int b_count){
int i=0, t = 0;
int min = lru_time+1, index = 0;
while(i != b_count){
t = bp[set*b_count+i].period;
if(t < min){
min = t; index = i;
}
i++;
}
return index;
}
void read_data(int addr, int cache, int block, int b_count){
block_ptr temp;
int set_count = cache / (block*b_count);
int set = (addr/block) % set_count;
int size = 10, victim, i;
for(i=0;i<b_count;i++){
temp = &bp[set*b_count + i];
if(temp->valid == 1 && temp->tag == (addr/block)/set_count){
temp->period = lru_time;
return;
}else if(temp->valid == 0){
if(i < size)
size = i;
}
}
miss++;
if(size == 10){
victim = find_index(set, b_count);
temp = &bp[set*b_count + victim];
temp->valid = 1;
temp->period = lru_time;
temp->tag = (addr/block) / set_count;
}else{
temp = &bp[set*b_count+size];
temp->valid = 1;
temp->period = lru_time;
temp->tag = (addr/block)/set_count;
}
}
void check(int cache, int block, int b_count){
FILE *fp = NULL;
cycle=0; miss=0;
int empty, addr;
int num = cache / block;
double miss_rate = 0;
char mode;
bp = (block_ptr)calloc(num, sizeof(blk));
fp = fopen("gcc.trace", "r");
while(!feof(fp)){
fscanf(fp, "%c %x %d\n", &mode, &addr, &empty);
if(mode == 'l'){
read_data(addr, cache, block, b_count);
cycle++;
}
lru_time++;
}
miss_rate = (double)miss/(double)cycle;
printf("\ncache size : %d\n", cache);
printf("block size : %d\n", block);
printf("associative : %d\n", b_count);
printf("======================\n");
printf("read miss-rate : %.4lf\n\n", miss_rate);
free(bp);
fclose(fp);
}
int main(){
int cache, block, b_count;
int input;
while(input != 3){
printf("1.Input size\n2.simulate\n3.exit\n:");
scanf("%d", &input);
if(input == 1){
printf("cache size(1024,2048,4096,8192)\n: ");
scanf("%d", &cache);
printf("block size(16,64)\n: ");
scanf("%d", &block);
printf("associative(1,2,4,8)\n: ");
scanf("%d", &b_count);
printf("\nInput Success !\n\n");
}
else if(input == 2)
check(cache, block, b_count);
else
exit(0);
}
return 0;
}