-
Notifications
You must be signed in to change notification settings - Fork 1
/
m1.c
299 lines (245 loc) · 9.12 KB
/
m1.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <regex.h>
#include "ParseInput.h"
#include "ParseHex.h"
int fromBinary(const char *s) {
return (int) strtol(s, NULL, 2);
}
signed int binaryToHex(char* value) {
char *a = value;
int num = 0;
do {
int b = *a=='1'?1:0;
num = (num<<1)|b;
a++;
} while (*a);
return num;
}
void m1(ParsedArgs *myIns) {
// SET VARIABLES BELOW
double pricePerKB = 0.09; // 9 cents per KB as per instruction file
// END SET VARIABLES
int cSize = myIns->cacheSize;
int bSize = myIns->blockSize;
int asso = myIns->associativity;
char *rp;
if(myIns->replacementVal == 0){
rp = "Random";
}
if(myIns->replacementVal == 1){
rp = "Round-Robin";
}
if(myIns->replacementVal == 2){
rp = "Last Recently Used";
}
printf("\n");
printf("Trace File: %s\n", myIns->traceFileName);
printf("\n");
printf("***** Cache Input Parameters ***** \n");
printf("Cache Size: %dKB\n", cSize);
printf("Block Size: %d bytes\n", bSize);
printf("Associativity: %d \n", asso);
printf("Replacement Policy: %s \n", rp);
printf("\n");
FILE *file;
char line[256];
file = fopen(myIns->traceFileName, "r");
if (file == NULL){
printf("file does not exist?!\n");
exit(1);
}
// BLOCK TOTAL
int bit_value = cSize * 1024;
int bit_block_value = bSize * 8;
int block_total = bit_value / bit_block_value;
block_total = block_total * 8;
//INDEX
double bit_block = log( bSize) / log( 2 );
double bit_asso = log( asso ) / log( 2 );
double bit_size = log( bit_value ) / log( 2 );
int index = ( int) bit_size - ( (int) bit_block + (int) bit_asso);
//TAG SIZE
int tag = 32 - (int) bit_block - index;
//NUMBER OF ROWS
int rows = block_total / asso;
//OVERHEAD
int overhead = ((tag + 1) * block_total) / 8;
//IMPLEMATION SIZE
int overhead_convert = overhead / 1024;
int act_size = cSize + overhead_convert;
int act_size_bytes = act_size * 1024;
//COST
double cost = act_size * pricePerKB;
printf("***** Cache Calculated Values *****\n\n");
printf("Total # Blocks: %d\n", block_total);
printf("Tag Size: %d bits\n", tag);
printf("Index Size: %d bits\n", index);
printf("Total # Rows: %d\n", rows);
printf("Overhead Size: %d bytes\n", overhead);
printf("Implementation Memory Size: %d.00 KB (%d bytes)\n", act_size, act_size_bytes);
printf("Cost: $%.2lf\n", cost);
printf("\n");
// M2 STARTS HERE ???
int count = 0;
int line_count = 1;
char real_size[3];
struct cache newCache[rows][asso];
int i, j;
int missCount;
int hitCount;
int readCount;
// start used for CPI
int cycleTotal = 0;
int instructionCount = 0;
// end used for CPI
for(i = 0; i < rows; i++) {
for(j = 0; j < asso; j++) {
newCache[i][j].tag = (char *)malloc(sizeof(char)*(tag+1));
newCache[i][j].valid = 0;
}
}
// Parse trace file
while (fgets(line, sizeof(line), file)) {
signed int address;
signed int address_two;
signed int address_three;
char reg[20], temp[20], str[20];
char size[5];
char binaryNum[100];
char a[3][100];
int addressArr[3];
int cnt;
//find addresses in file
if(line_count == 1) {
sscanf(line, "%s %s %x", reg, size, &address);
real_size[0] = size[1];
real_size[1] = size[2];
// printf("0x%x: 00%s\n", address, real_size);
sprintf(str, "%x", address);
strcpy(binaryNum, "");
HexToBin(str, binaryNum);
strcpy(a[0], binaryNum);
addressArr[0] = address;
count++;
} else if(line_count == 2) {
addressArr[0] = -1;
sscanf(line, "%s %x %s %s %x %s", temp, &address_two, temp, temp, &address_three, temp);
if(address_two > 0) {
count++;
strcpy(binaryNum, "");
HexToBin(str, binaryNum);
strcpy(a[1], binaryNum);
addressArr[1] = address_two;
instructionCount += 1; // USED FOR CPI
} else {
strcpy(a[1], "");
addressArr[1] = -1;
}
if(address_three > 0) {
count++;
strcpy(binaryNum, "");
HexToBin(str, binaryNum);
strcpy(a[2], binaryNum);
addressArr[2] = address_three;
} else {
strcpy(a[2], "");
addressArr[2] = -1;
}
} else {
line_count = 0;
}
//loop through address array and add to cache
for(cnt = 0; cnt < line_count; ++cnt) {
if(strcmp(a[cnt], "") != 0 && addressArr[cnt] != -1) {
int j = 0, k = 0, z = 28, l = 0;
int oSize = log( bSize ) / log( 2 );
char tagValue[tag];
char indexValue[index];
char offsetValue[oSize];
int indexS = 32 - oSize - index;
int tagS = 32 - oSize - index - tag;
int iIndex;
while (z <= 32) {
offsetValue[l] = a[cnt][z];
z++;
l++;
}
while(indexS < 32 - oSize) {
indexValue[k] = a[cnt][indexS];
k++;
indexS++;
}
while(tagS < 32 - oSize - index) {
tagValue[j] = a[cnt][tagS];
// printf("%d", tagValue[j]);
j++;
tagS++;
}
iIndex = fromBinary(indexValue);
for(i = 0; i < asso; i++) {
if(newCache[iIndex][i].valid == 0) {
newCache[iIndex][i].valid = 1;
newCache[iIndex][i].tag = tagValue;
cycleTotal += (4*ceil(bSize/4)); // used for CPI
missCount += 1;
break;
} else if(newCache[iIndex][i].valid == 1) {
if(strcmp(newCache[iIndex][i].tag, tagValue) == 0) {
hitCount += 1;
cycleTotal += 1; // used for CPI
}
} else {
// random replacement
int num = (rand() % (asso - 0 + 1)) + 0;
newCache[iIndex][num].tag = tagValue;
}
}
printf("INST: %x\n", atoi(real_size));
// tagValue[j++] = '\0';
indexValue[k++] = '\0';
offsetValue[z++] = '\0';
// printf("BLOCK: %d\n", bSize);
// printf("SEE: %x\n", addressArr[cnt]);
// printf("SEE2: %x\n", addressArr[cnt] + 4);
// signed int off = binaryToHex(indexValue);
// printf("%s\n", indexValue);
// printf("%x\n", off);
}
}
if(count == 20) {
break;
}
line_count++;
}
printf("MISS: %d\n", missCount);
// BEGIN MODULE 2 PRINTOUTS
// BEGIN CALCULATION "Cache Simulation Results"
int totalCacheAccesses = 0;
int cacheHits = 0;
int cacheMisses = 0;
int compulsoryMisses = 0;
int conflictMisses = 0;
// END CALCULATION "Cache Simulation Results"
// BEGIN CALCULATION "CACHE HIT & MISS RATE"
double hitRate = (double) (cacheHits / totalCacheAccesses) * 100; // done
double missRate = (double) (cacheMisses / totalCacheAccesses) * 100; // done
double cpi = (double) (cycleTotal / instructionCount); // done
double unusedCacheSpaceKB = (double) ( (block_total-compulsoryMisses) * (bSize+overhead) ) / 1024; // done
double totalCacheSpaceKB = (double) act_size; // done
double cacheSpacePercentUsage = (unusedCacheSpaceKB / totalCacheSpaceKB) * 100; // done
double cacheSpaceWasteDollars = unusedCacheSpaceKB * pricePerKB; // done
int unusedCacheBlocks = 0;
int totalCacheBlocks = block_total; // done
printf("***** ***** CACHE HIT & MISS RATE: ***** *****\n\n");
printf("Hit Rate: %.4lf%\n", hitRate);
printf("Miss Rate: %.4lf%\n", missRate);
printf("CPI: %.2lf Cycles/Instruction\n", cpi);
printf("Unused Cache Space: %.2lf KB / %.2lf KB = %.2lf% Waste: $%.2lf\n", unusedCacheSpaceKB, totalCacheSpaceKB, cacheSpacePercentUsage, cacheSpaceWasteDollars);
printf("Unused Cache Blocks: %d / %d\n", unusedCacheBlocks, totalCacheBlocks);
// END CALCULATION "CACHE HIT & MISS RATE"
// END MODULE 2 PRINTOUTS
fclose(file);
}