-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfifo.h
268 lines (265 loc) · 9.86 KB
/
fifo.h
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// compile ../../../pin -t obj-intel64/pinatrace.so -- executable file | gzip > trace.gz
// make obj-intel64/pinatrace.so
struct cacheLine
{
int valid;
long tag;
};
struct cacheSet2
{
struct cacheLine c[2];
int front, back;
};
struct cacheSet4
{
struct cacheLine c[4];
int front, back;
};
struct cacheSet8
{
struct cacheLine c[8];
int front, back;
};
struct cacheSet16
{
struct cacheLine c[16];
int front, back;
};
struct cacheSet2 cache2[512];
struct cacheSet4 cache4[256];
struct cacheSet8 cache8[128];
struct cacheSet16 cache16[64];
int miss, hit;
long long setNum, tag;
float calcforCacheset2(long long indices, long long tags, int size, int numOfWays)
{
int i, j, count;
for (i = 0; i < size; i++)
{
setNum = indices;
tag = tags;
// printf("Tag %d setNum %d\n",tag,setNum);
for (j = 0, count = 0; j < numOfWays; j++)
{
if (cache2[setNum].c[j].valid == 1 && cache2[setNum].c[j].tag == tag)
{
// printf("Cache hit at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
// hit++;
return 1;
break;
}
//Beginning with all misses
else if (cache2[setNum].c[j].valid == 0)
{
// printf("Cache miss with no data at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
cache2[setNum].c[j].valid = 1;
cache2[setNum].c[j].tag = tag;
cache2[setNum].front = (cache2[setNum].front + 1) % numOfWays;
// printf("Data added at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
// miss++;
return 0;
break;
}
else
{
// printf("Cache miss at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
count++;
}
// if (cache2[i].c[j].valid == 0 || cache2[i].c[j].tag!=tag)
// {
// count++;
// }
if (count == numOfWays)
{
// printf("All cache lines in the set checked and non match so fifo performed back%d\n", cache2[setNum].back);
miss++;
cache2[setNum].c[cache2[setNum].back].tag = tag;
cache2[setNum].c[cache2[setNum].back].valid = 1;
cache2[setNum].back = (cache2[setNum].back + 1) % numOfWays;
return 0;
}
}
}
return ((float)hit / size);
// printf("Hit %d Miss %d", hit, miss);
}
float calcforCacheset4(long long indices, long long tags, int size, int numOfWays)
{
int i, j, count;
for (i = 0; i < size; i++)
{
setNum = indices;
tag = tags;
// printf("Tag %d setNum %d\n",tag,setNum);
for (j = 0, count = 0; j < numOfWays; j++)
{
if (cache4[setNum].c[j].valid == 1 && cache4[setNum].c[j].tag == tag)
{
// printf("Cache hit at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache4[setNum].c[j].valid, cache4[setNum].c[j].tag);
hit++;
return 1;
break;
}
//Beginning with all misses
else if (cache4[setNum].c[j].valid == 0)
{
// printf("Cache miss with no data at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
cache4[setNum].c[j].valid = 1;
cache4[setNum].c[j].tag = tag;
cache4[setNum].front = (cache4[setNum].front + 1) % numOfWays;
// printf("Data added at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache4[setNum].c[j].valid, cache4[setNum].c[j].tag);
miss++;
return 0;
break;
}
else
{
// printf("Cache miss at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache4[setNum].c[j].valid, cache4[setNum].c[j].tag);
count++;
}
// if (cache2[i].c[j].valid == 0 || cache2[i].c[j].tag!=tag)
// {
// count++;
// }
if (count == numOfWays)
{
// printf("All cache lines in the set checked and non match so fifo performed back%d\n", cache2[setNum].back);
miss++;
cache4[setNum].c[cache4[setNum].back].tag = tag;
cache4[setNum].c[cache4[setNum].back].valid = 1;
cache4[setNum].back = (cache4[setNum].back + 1) % numOfWays;
return 0;
}
}
}
// return ((float)hit / size);
// printf("Hit %d Miss %d", hit, miss);
}
float calcforCacheset8(long long indices, long long tags, int size, int numOfWays)
{
int i, j, count;
for (i = 0; i < size; i++)
{
setNum = indices;
tag = tags;
// printf("Tag %d setNum %d\n",tag,setNum);
for (j = 0, count = 0; j < numOfWays; j++)
{
if (cache8[setNum].c[j].valid == 1 && cache8[setNum].c[j].tag == tag)
{
// printf("Cache hit at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache8[setNum].c[j].valid, cache8[setNum].c[j].tag);
hit++;
return 1;
break;
}
//Beginning with all misses
else if (cache8[setNum].c[j].valid == 0)
{
// printf("Cache miss with no data at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
cache8[setNum].c[j].valid = 1;
cache8[setNum].c[j].tag = tag;
cache8[setNum].front = (cache8[setNum].front + 1) % numOfWays;
// printf("Data added at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache8[setNum].c[j].valid, cache8[setNum].c[j].tag);
miss++;
return 0;
break;
}
else
{
// printf("Cache miss at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache8[setNum].c[j].valid, cache8[setNum].c[j].tag);
count++;
}
// if (cache2[i].c[j].valid == 0 || cache2[i].c[j].tag!=tag)
// {
// count++;
// }
if (count == numOfWays)
{
// printf("All cache lines in the set checked and non match so fifo performed back%d\n", cache2[setNum].back);
miss++;
cache8[setNum].c[cache8[setNum].back].tag = tag;
cache8[setNum].c[cache8[setNum].back].valid = 1;
cache8[setNum].back = (cache8[setNum].back + 1) % numOfWays;
return 0;
}
}
}
// return ((float)hit / size);
// printf("Hit %d Miss %d", hit, miss);
}
float calcforCacheset16(long long indices, long long tags, int size, int numOfWays)
{
int i, j, count;
for (i = 0; i < size; i++)
{
setNum = indices;
tag = tags;
// printf("Tag %d setNum %d\n",tag,setNum);
for (j = 0, count = 0; j < numOfWays; j++)
{
if (cache16[setNum].c[j].valid == 1 && cache16[setNum].c[j].tag == tag)
{
// printf("Cache hit at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache16[setNum].c[j].valid, cache16[setNum].c[j].tag);
hit++;
return 1;
break;
}
//Beginning with all misses
else if (cache16[setNum].c[j].valid == 0)
{
// printf("Cache miss with no data at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache2[setNum].c[j].valid, cache2[setNum].c[j].tag);
cache16[setNum].c[j].valid = 1;
cache16[setNum].c[j].tag = tag;
cache16[setNum].front = (cache16[setNum].front + 1) % numOfWays;
// printf("Data added at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache16[setNum].c[j].valid, cache16[setNum].c[j].tag);
miss++;
return 0;
break;
}
else
{
// printf("Cache miss at set %d and cache line %d valid:%d Tag:%d\n", setNum, j, cache16[setNum].c[j].valid, cache16[setNum].c[j].tag);
count++;
}
// if (cache2[i].c[j].valid == 0 || cache2[i].c[j].tag!=tag)
// {
// count++;
// }
if (count == numOfWays)
{
// printf("All cache lines in the set checked and non match so fifo performed back%d\n", cache16[setNum].back);
miss++;
cache16[setNum].c[cache16[setNum].back].tag = tag;
cache16[setNum].c[cache16[setNum].back].valid = 1;
cache16[setNum].back = (cache16[setNum].back + 1) % numOfWays;
return 0;
}
}
}
// printf("Hit %d Miss %d", hit, miss);
// return ((float)hit / size);
}
float fifo(long long indices, long long tags, int size, int numOfWays)
{
float percentage;
if (numOfWays == 2)
{
percentage = calcforCacheset2(indices, tags, size, numOfWays);
}
else if (numOfWays == 4)
{
percentage = calcforCacheset4(indices, tags, size, numOfWays);
}
else if (numOfWays == 8)
{
percentage = calcforCacheset8(indices, tags, size, numOfWays);
}
else
{
percentage = calcforCacheset16(indices, tags, size, numOfWays);
}
return percentage;
}