-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.cpp
More file actions
277 lines (235 loc) · 9.96 KB
/
cache.cpp
File metadata and controls
277 lines (235 loc) · 9.96 KB
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
/*
Ducksim: Cache
*/
#include <string.h>
#include <stdlib.h>
#include "cache.h"
#include "hash_functions.h"
/*Cache Constructor*/
//Cache::Cache(uint in_size, int in_associativity, int in_banks, int in_number_cache_lines,
// int in_number_data_blocks, ReplacementPolicy in_replacement_policy)
Cache::Cache(uint in_size, int in_associativity, int in_banks, int in_number_data_blocks, ReplacementPolicy in_replacement_policy)
: size(in_size)
, associativity(in_associativity)
, banks(in_banks)
, number_cache_lines(in_size/in_number_data_blocks)
, number_data_blocks(in_number_data_blocks)
, replacement_policy(in_replacement_policy)
{
//if ()
if (in_associativity==0) this->associativity = this->number_cache_lines; //Fully Associative
this->number_virtual_banks = this->associativity;
this->number_cache_sets = this->number_cache_lines / this->associativity;
for (int vbanks=0; vbanks<this->number_virtual_banks; ++vbanks) {
//vector <int> replacement_ordering_list;
CacheLines bank;
for (int i=0; i<this->number_cache_sets; ++i) {
/*Initialize Cache Line*/
CacheLine line = {
0, //tag
false, //valid
false, //dirty
in_number_data_blocks, //number_data_blocks
//0, //replacement_ordering
//0, //access_count
};
bank.push_back(line);
//replacement_ordering_list.push_back(0);
}
this->virtual_banks.push_back(bank); //push each bank in the virtual bank vector
//this->max_replacement_ordering.push_back(replacement_ordering_list);
}
/*Initialize Cache Stats*/
this->stats = (CacheStatistics){
0, //hits
0, //misses
0, //access
0, //replacements
0, //write_backs
//0, //bandwidth
};
/*Initilize Replacement Stats*/
this->replacement_stats = new CacheReplacementStats(this->number_virtual_banks, this->number_cache_sets);
/*Initilize upper and lower levels*/
this->upper_level = NULL;
this->lower_level = NULL;
}
Cache::~Cache() {
delete this->replacement_stats;
//this->replacement_stats = NULL;
}
/* Set all valid & dirty bits to 0 for cache reinitialization */
void Cache::reinit_cache(){
/*Reinitialize the Cache*/
for (int i=0; i<this->number_virtual_banks; ++i) {
for (int j=0; j<this->number_cache_sets; ++j) {
this->virtual_banks[i][j].valid = false;
this->virtual_banks[i][j].dirty = false;
}
}
/*Reinitialize the Replacement Stats*/
(this->replacement_stats)->reinit();
}
/* Check if an address is in the cache and update hit/miss counts accordingly */
bool Cache::access(uint address, bool write) {
int offset = address % this->number_data_blocks; //will need this when actually reading/writing data
address = address / this->number_data_blocks; //get rid of block offset
uint access_set_tag = address / this->number_cache_sets; //get rid of index for the tag
uint access_index = address % this->number_cache_sets; //get the actual index of the address
/*Start access stats*/
this->stats.access += 1;
bool hit = false;
CacheLines replacement_lines; //keep track of lines to be replaced so that replacement policy can select which one of them to replace later on
ReplacementLines replacement_indexes; //keep track of index of the replacement_lines in each virtual bank
/*Final values*/
int cache_bank_id = -1;
int cache_set_id = -1;
for (int vbank=0; vbank<this->number_virtual_banks; ++vbank) {
int access_set_index = hash_address(access_index, vbank, this->number_cache_sets, true); //get the set index where the index is mapped to in each bank : Rightnow just set associative
//if (vbank==0) cout <<"Tag, Index, Offset: "<<access_set_tag <<", " <<access_set_index <<", " <<offset<<endl;
if (this->virtual_banks[vbank][access_set_index].valid && (this->virtual_banks[vbank][access_set_index].tag == access_set_tag)) {//hit condition
hit = true;
cache_bank_id = vbank;
cache_set_id = access_set_index;
this->virtual_banks[vbank][access_set_index].dirty = write; //it's been written to now.. so set the dirty bit -- need to implement the write policy here.. later
//break; //can't break anymore -- need to build full replacement_indexes to be used for updating the replacement table
}
replacement_lines.push_back(this->virtual_banks[vbank][access_set_index]);
replacement_indexes.push_back(access_set_index);
}
/* Implement Read policies on miss? Read through? */
/*Update cache stats*/
if (hit) {
this->stats.hits += 1;
//cout << "<< HIT!! >>"<<endl;
}
else {
/* Address not in Cache => Implement Replacement Policy => Find out which cache line to replace */
int replacement_bank_id = get_replacement_line(replacement_lines, replacement_indexes); //replacement policy selects which virtual bank to replace cache line from
int replacement_set_index = replacement_indexes[replacement_bank_id];
this->virtual_banks[replacement_bank_id][replacement_set_index].tag = access_set_tag;
this->virtual_banks[replacement_bank_id][replacement_set_index].valid = true;
this->stats.misses += 1;
cache_bank_id = replacement_bank_id;
cache_set_id = replacement_set_index;
//cout << "<< MISS!! >>"<<endl;
}
//cout << "Bank, Set: "<<cache_bank_id<<" , "<<cache_set_id<<endl;
/*Update Replacement Stats*/
(this->replacement_stats)->update(replacement_indexes, this->number_virtual_banks, cache_bank_id, cache_set_id, hit);
/*printing replacement table.. get rid of it later*/
//std::ofstream f ("add.txt", std::ofstream::out);
//(this->replacement_stats)->dump_replacement_table(cout);
//f.close();
return hit;
}
/*Restore cache stat counts: HR/MR/AR*/
void Cache::restore_rates(void) {
this->stats.hits = 0;
this->stats.misses = 0;
this->stats.access = 0;
}
double Cache::get_miss_rate(void) {
return (double) this->stats.misses/this->stats.access;
}
/*Dump all cache stats*/
void Cache::dump_stats(ostream& stream) {
stream << "\n------------------- Cache Stats -------------------"<<endl;
stream << "Hit Rate: "<<(double)this->stats.hits/this->stats.access<<endl;
stream << "Miss Rate: "<<(double)this->stats.misses/this->stats.access<<endl;
stream << "Replacement Rate: "<<(double)this->stats.replacements/this->stats.access<<endl;
stream << "Write Back Rate: "<<(double)this->stats.write_backs/this->stats.access<<endl;
//stream << endl;
//stream << "Hits: "<<this->stats.hits<<endl;
//stream << "Misses: "<<this->stats.misses<<endl;
//stream << "Replacements: "<<this->stats.replacements<<endl;
//stream << "Total Accesses: "<<this->stats.access;
stream << "\n---------------------------------------------------"<<endl<<endl;
}
/*Dump all cache settings*/
void Cache::dump_settings(ostream& stream) {
stream << "Size: "<<this->size<<endl;
stream << "Associativity: "<<this->associativity<<endl;
stream << "number_cache_lines: "<<this->number_cache_lines<<endl;
stream << "number_cache_sets: "<<this->number_cache_sets<<endl;
stream << "number_data_blocks: "<<this->number_data_blocks<<endl;
}
/*Dump Current Cache State*/
void Cache::dump_cache(ostream& stream) { /*Will need more changes with skewed associative as we need to rehash to get back original index from the hashed index*/
stream << "\n------------------- Cache State -------------------"<<endl;
stream <<"Line\tBank\tSetIndex\tTag\tValid\tData"<<endl;
for (int i=0; i<this->number_cache_lines; ++i) {
int bank_id = i % this->number_virtual_banks;
int set_index = i / this->number_virtual_banks; /*Need to rehash this to get actual index back for skewed associative!!!*/
uint set_tag = this->virtual_banks[bank_id][set_index].tag;
uint block_start_address = (set_tag * this->number_cache_sets * this->number_data_blocks) //tag
+ (set_index*this->number_data_blocks) //index
+ 0; //offset
uint block_end_address = block_start_address //starting address
+ this->number_data_blocks; //offset
/*Output Cache Line*/
stream<<i<<"\t"<<bank_id<<"\t"<<set_index<<"\t\t"<<set_tag<<"\t"<<this->virtual_banks[bank_id][set_index].valid;
stream<<"\t"<<block_start_address<<" :: "<<block_end_address<<endl;
}
stream << "--------------------------------------------------"<<endl;
}
/*Run the address stream on current cache*/
bool Cache::run(istream& stream, int lines, char *fin) {
char hex_address[11];
FILE * out;
char fileout[200];
strcat(fileout, fin);
strcat(fileout,"_rates.out");
out = fopen64(fileout, "w+");
if (lines <= 0) {
while(stream.getline(hex_address, 11)) {
char newhex_address[9];
memcpy(newhex_address, hex_address+2, 9);
uint address = (uint) strtol(newhex_address, NULL, 16);
bool hit = access(address, false);
//printf("%08x, %s, %d\n", address, newhex_address,hit);
fprintf(out, "%c,%d\n",hex_address[0],hit);
}
}
else {
for (int i=0; i<lines; ++i){
stream.getline(hex_address, 11);
char newhex_address[9];
uint address = (uint) strtol(newhex_address, NULL, 16);
bool hit = access(address, false);
//printf("%08x, %s, %d\n", address, newhex_address,hit);
fprintf(out, "%c,%d\n",hex_address[0],hit);
}
}
fclose(out);
return true;
}
int Cache::get_replacement_line(CacheLines cache_replacement_lines, ReplacementLines replacement_lines ) {
int return_bank_id = -1;
if (this->associativity != 1) { /* Not DMC -- need to worry about replacement policies*/
/* Check for conflict i.e. check if all the replacement lines are valid */
bool conflict = true;
int vbank = 0;
while (vbank < this->number_virtual_banks) {
if (! cache_replacement_lines[vbank].valid) {
conflict = false;
return_bank_id = vbank;
vbank = this->number_virtual_banks; //found invalid line => STOP & use that for replacement
}
vbank += 1;
}
/*If conflict then need to implement replacement policy*/
if (conflict) {
return_bank_id = (this->replacement_stats)->implement_replacement_policy(replacement_lines, this->replacement_policy);
this->stats.replacements += 1; //something was replaced
//cout << "<< REPLACEDDDDDDDDDDDDDDD.... >>"<<endl;
}
else {
/* No Conflit => already found a replacement => nothing else to do.. */
}
}
else { //Just 1 virtual bank for DMC
return_bank_id = 0;
}
return return_bank_id;
}