-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.c
213 lines (194 loc) · 8.95 KB
/
gen.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
#include <stdio.h>
#include <stdlib.h>
#include "L1.h"
#include "L2.h"
#include "gen.h"
#include "configlib.h"
// #define DEBUG
unsigned long long log2(unsigned long long num){
unsigned long long count = 0;
while(num >>= 1)
count++;
return count;
}
int cacheFlush(){
int i;
for(i =0; i < (L2_cache_size/L1_block_size); i++){
}
return 0;
};
int access(char op, unsigned long long int address){
unsigned long long int cycles = 0;
if(op == 'R'){ // read
if(inL1d(address)){
cycles+=L1_hit_time;
l1dHitCount++;
}
else{
cycles+=L1_miss_time;
l1dMissCount++;
if(inL2(address)){ // not in L1 cache but in L2
cycles+=L2_hit_time+L1_hit_time+(L2_transfer_time*( ((float) L1_block_size) / ((float ) L2_bus_width) ));
cycles+=loadL1d(address); // put it in the L1 cache after accessing it from teh L2 cache.
l2HitCount++;
}
else{
cycles+=L2_miss_time+mem_sendaddr+mem_ready + (((float)(mem_chunktime*L2_block_size))/((float) mem_chunksize))+L2_hit_time+L1_hit_time+(L2_transfer_time*( ((float) L1_block_size) / ((float ) L2_bus_width) ));
cycles+=loadL2(address);
cycles+=loadL1d(address);
l2MissCount++;
}
}
readCycle += cycles;
}
else if(op == 'I'){ // instruction
#ifdef DEBUG
printf("inL1i returns %d\n", inL1i(address));
#endif /* DEBUG */
if(inL1i(address)){
cycles+=L1_hit_time;
l1iHitCount++;
}
else{
cycles+=L1_miss_time;
l1iMissCount++;
if(inL2(address)){ // not in L1 cache but in L2
cycles+=L2_hit_time+L1_hit_time+(L2_transfer_time*( ((float) L1_block_size) / ((float ) L2_bus_width) ));
loadL1i(address); // put it in the L1 cache after accessing it from the L2 cache. Note: don't care about writebacks since instructions won't.
l2HitCount++;
}
else{
cycles+=L2_miss_time+mem_sendaddr+mem_ready+ (((float)(mem_chunktime*L2_block_size))/((float) mem_chunksize))+L2_hit_time+L1_hit_time+(L2_transfer_time*( ((float) L1_block_size) / ((float ) L2_bus_width) ));
cycles+=loadL2(address);
loadL1i(address);
l2MissCount++;
}
}
instCycle += cycles;
}
else if(op == 'W'){ // write
if(inL1d(address)){
cycles+=L1_hit_time;
l1dHitCount++;
}
else{
cycles+=L1_miss_time;
l1dMissCount++;
if(inL2(address)){ // not in L1 cache but in L2
cycles+=L2_hit_time+L1_hit_time+(L2_transfer_time*( ((float) L1_block_size) / ((float ) L2_bus_width) ));
cycles+=loadL1d(address); // put it in the L1 cache after accessing it from teh L2 cache.
l2HitCount++;
}
else{
cycles+=L2_miss_time+mem_sendaddr+mem_ready+ (((float)(mem_chunktime*L2_block_size))/((float) mem_chunksize))+L2_hit_time+L1_hit_time+(L2_transfer_time*( ((float) L1_block_size) / ((float ) L2_bus_width) ));
cycles+=loadL2(address);
cycles+=loadL1d(address);
l2MissCount++;
}
}
writeCycle += cycles;
dirtyL1d(1, address); // we mark the L1 block dirty no matter what. L2 is left clean as per the instructions.
}
else{
return -1;
}
return cycles;
}
int printOutput(){
dataCount = readCount + writeCount;
refCount = instCount + dataCount;
l1iRefCount = l1iMissCount+l1iHitCount;
l1dRefCount = l1dMissCount+l1dHitCount;
l2RefCount = l2MissCount+l2HitCount;
printf("Memory system:\n");
printf("Dcache size = %llu : ways = %llu : block size = %llu\n", L1_cache_size, L1_assoc, L1_block_size);
printf("Icache size = %llu : ways = %llu : block size = %llu\n", L1_cache_size, L1_assoc, L1_block_size);
printf("L2-cache size = %llu : ways = %llu : block size = %llu\n", L2_cache_size, L2_assoc, L2_block_size);
printf("Memory ready time = %d : chunksize = %d : chunktime = %d\n", mem_ready, mem_chunksize, mem_chunktime);
printf("\n");
printf("Execute time = %llu; Total refs = %llu\n", totalCycle, refCount);
printf("Flush time = %llu\n", flushTime);
printf("Inst refs = %llu; Data refs = %llu\n", instCount, dataCount);
printf("\n");
printf("Number of reference types: [Percentage]\n");
printf(" Reads = %llu [ %.1f%%]\n", readCount, (float)readCount / (float)refCount *100.0);
printf(" Writes = %llu [ %.1f%%]\n", writeCount, (float)writeCount / (float)refCount *100.0);
printf(" Inst. = %llu [ %.1f%%]\n", instCount, (float)instCount / (float)refCount *100.0);
printf(" Total = %llu\n", refCount);
printf("\n");
printf("Total cycles for activities: [Percentage]\n");
printf(" Reads = %llu [%.1f%%]\n", readCycle, (float)readCycle / (float)totalCycle *100.0);
printf(" Writes = %llu [%.1f%%]\n", writeCycle, (float)writeCycle / (float)totalCycle *100.0);
printf(" Inst. = %llu [%.1f%%]\n", instCycle, (float)instCycle / (float)totalCycle *100.0);
printf(" Total = %llu\n", totalCycle);
printf("\n");
printf("Average goes here\n");
printf("\n");
printf("Memory Level: L1i\n");
printf(" Hit Count = %llu Miss Count = %llu\n", l1iHitCount, l1iMissCount);
printf(" Total Requests = %llu\n", l1iRefCount);
printf(" Hit Rate = %.1f%% Miss Rate = %.1f%%\n", (float)l1iHitCount/(float)l1iRefCount * 100, (float)l1iMissCount/(float)l1iRefCount * 100);
printf(" Kickouts = %llu; Dirty kickouts = %llu; Transfers = %llu\n", l1iKickouts, l1iDirtyKickouts, l1iTransfers);
printf(" Flush Kickouts = %llu\n", l1iFlushKickouts);
printf("\n");
printf("Memory Level: L1d\n");
printf(" Hit Count = %llu Miss Count = %llu\n", l1dHitCount, l1dMissCount);
printf(" Total Requests = %llu\n", l1dRefCount);
printf(" Hit Rate = %.1f%% Miss Rate = %.1f%%\n", (float)l1dHitCount/(float)l1dRefCount * 100, (float)l1dMissCount/(float)l1dRefCount * 100);
printf(" Kickouts = %llu; Dirty kickouts = %llu; Transfers = %llu\n", l1dKickouts, l1dDirtyKickouts, l1dTransfers);
printf(" Flush Kickouts = %llu\n", l1dFlushKickouts);
printf("\n");
printf("Memory Level: L2\n");
printf(" Hit Count = %llu Miss Count = %llu\n", l2HitCount, l2MissCount);
printf(" Total Requests = %llu\n", l2RefCount);
printf(" Hit Rate = %.1f%% Miss Rate = %.1f%%\n", (float)l2HitCount/(float)l2RefCount * 100, (float)l2MissCount/(float)l2RefCount * 100);
printf(" Kickouts = %llu; Dirty kickouts = %llu; Transfers = %llu\n", l2Kickouts, l2DirtyKickouts, l2Transfers);
printf(" Flush Kickouts = %llu\n", l2FlushKickouts);
}
int printValid(){
int i;
printf("\n");
printf("Cache final contents - Index and Tag values are in HEX\n");
printf("\n");
printf("Memory Level: L1i\n");
for(i = 0; i < ((L1_cache_size)/L1_block_size); i++){
if(dmL1i[i].addr != 0)
printf("Index: %x | V:%d D:%d Tag: %llx |\n", i, dmL1i[i].valid, dmL1i[i].dirty, dmL1i[i].addr >> numBitsL1);
}
printf("\n");
printf("Memory Level: L1d\n");
for(i = 0; i < ((L1_cache_size)/L1_block_size); i++){
if(dmL1d[i].addr != 0)
printf("Index: %x | V:%d D:%d Tag: %llx |\n", i, dmL1d[i].valid, dmL1d[i].dirty, dmL1d[i].addr >> numBitsL1);
}
printf("\n");
printf("Memory Level: L2\n");
for(i = 0; i < (L2_cache_size/L2_block_size); i++){
if(dmL2[i].addr != 0)
printf("Index: %x | V:%d D:%d Tag: %llx |\n", i, dmL2[i].valid, dmL2[i].dirty, dmL2[i].addr >> numBitsL2);
}
}
int printL1(){
int i;
printf("\n");
printf("All Blocks in L1 Instruction Cache\n");
printf("\n");
for(i = 0; i < ((L1_cache_size)/L1_block_size); i++){
printf("Block: %llx Tag: 0x%llx Address: 0x%llx Dirty: %d Valid: %d\n", dmL1i[i].addr % blockNumL1, dmL1i[i].tag, dmL1i[i].addr, dmL1i[i].dirty, dmL1i[i].valid);
}
printf("\n");
printf("All Blocks in L1 Data Cache\n");
printf("\n");
for(i = 0; i < ((L1_cache_size)/L1_block_size); i++){
printf("Block: %llx Tag: 0x%llx Address: 0x%llx Dirty: %d Valid: %d\n", dmL1d[i].addr % blockNumL1, dmL1d[i].tag, dmL1d[i].addr, dmL1d[i].dirty, dmL1d[i].valid);
}
}
int printL2(){
int i;
printf("\n");
printf("All Blocks in L2 Cache\n");
printf("\n");
for(i = 0; i < (L2_cache_size/L2_block_size); i++){
printf("Block: %llx Tag: 0x%llx Address: 0x%llx Dirty: %d Valid: %d\n", dmL2[i].addr % blockNumL2, dmL2[i].tag, dmL2[i].addr, dmL2[i].dirty, dmL2[i].valid);
}
}