-
Notifications
You must be signed in to change notification settings - Fork 0
/
csim.c
170 lines (145 loc) · 4.54 KB
/
csim.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
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NUMBITS 64 // number of address bits
typedef struct cache_line {
long long tag;
int accessTime;
bool valid;
} cache_line;
// E-way associative cache with S sets
static int S, E;
// s index bits, b bits block offset in 64-bit address
static int s, b;
static bool vFlag = 0; // verbose flag
// Keep track the performance of the cache
static int hits, misses, evictions;
void printUsageInfo(char *argv[]) {
printf("Usage: %s [-hv] -s <num> -E <num> -b <num> -t <file>\n", argv[0]);
printf("Options:\n");
printf(" -h\t Print this help message.\n");
printf(" -v\t Optional verbose flag.\n");
printf(" -s <num> Number of set index bits.\n");
printf(" -E <num> Number of lines per set.\n");
printf(" -b <num> Number of block offset bits.\n");
printf(" -t <file> Trace file.\n");
printf("\n");
printf("Examples:\n");
printf(" linux> %s -s 4 -E 1 -b 4 -t traces/yi.trace\n", argv[0]);
printf(" linux> %s -v -s 8 -E 2 -b 4 -t traces/yi.trace\n", argv[0]);
}
void printSummary(int hits, int misses, int evictions) {
printf("hits:%d misses:%d evictions:%d\n", hits, misses, evictions);
}
cache_line *initCache() {
// Allocate 1-D array to be used as 2-D array
cache_line *cache = (cache_line *) malloc(S * E * sizeof(cache_line));
for (int i = 0; i < S; i++) {
for (int j = 0; j < E; j++) {
cache_line line;
line.valid = 0;
cache[i * E + j] = line;
}
}
return cache;
}
void accessCache(cache_line *cache, long long address, int *time) {
// Extract value of index bits
int index = (address >> b) & ((1 << s) - 1);
int setIndex = index * E;
// Extract value of tag bits;
long long tag = address >> (b + s);
int loadTime = *time;
int lru = loadTime; // Time of least recently used cache line
int emptySlot = -1;
int lruPos = -1;
for (int i = 0; i < E; i++) {
if (cache[setIndex + i].valid && cache[setIndex + i].tag == tag) {
cache[setIndex + i].accessTime = loadTime;
hits++;
if (vFlag) printf("hit ");
return;
} else if (!cache[setIndex + i].valid) {
emptySlot = i;
} else if (cache[setIndex + i].accessTime < lru) {
lru = cache[setIndex + i].accessTime;
lruPos = i;
}
}
// Cold miss
if (emptySlot != -1) {
cache[setIndex + emptySlot].valid = 1;
cache[setIndex + emptySlot].tag = tag;
cache[setIndex + emptySlot].accessTime = loadTime;
misses++;
if (vFlag) printf("miss ");
return;
} else {
// Miss and eviction
cache[setIndex + lruPos].tag = tag;
cache[setIndex + lruPos].accessTime = loadTime;
misses++;
evictions++;
if (vFlag) printf("miss eviction ");
return;
}
}
int main(int argc, char *argv[]) {
int opt;
char *traceFile;
while ((opt = getopt(argc, argv, "hvs:E:b:t:")) != -1) {
switch (opt) {
case 'h':
printUsageInfo(argv);
exit(0);
case 'v':
vFlag = 1;
break;
case 's':
s = atoi(optarg);
S = 1 << s;
break;
case 'E':
E = atoi(optarg);
break;
case 'b':
b = atoi(optarg);
break;
case 't':
traceFile = optarg;
break;
default:
printUsageInfo(argv);
exit(1);
}
}
hits = misses = evictions = 0;
cache_line *cache = initCache();
FILE *filePtr;
filePtr = fopen(traceFile, "r");
char identifier;
long long address;
int size, time;
time = 0;
while (fscanf(filePtr, " %c %llx,%d", &identifier, &address, &size) > 0) {
if (identifier == 'I') continue;
printf(" %c %llx,%d ", identifier, address, size);
if (identifier == 'L') {
accessCache(cache, address, &time);
} else if (identifier == 'S') {
accessCache(cache, address, &time);
} else if (identifier == 'M') {
accessCache(cache, address, &time);
accessCache(cache, address, &time);
}
time++;
printf("\n");
}
free(cache);
fclose(filePtr);
printSummary(hits, misses, evictions);
return 0;
}