Repository navigation
Expand file tree
/
Copy pathcache.c
More file actions
420 lines (368 loc) · 11.7 KB
/
Copy pathcache.c
File metadata and controls
420 lines (368 loc) · 11.7 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* Cache simulation using a functional system simulator.
*/
#include "cache.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <time.h>
#define AVDC_MALLOC(nelems, type) malloc(nelems * sizeof(type))
#define AVDC_FREE(p) free(p)
/**
* Cache block information.
*
* HINT: You will probably need to change this structure
*/
struct avdc_cache_line {
avdc_tag_t tag;
long count;
int valid;
};
/**
* Extract the cache line tag from a physical address.
*
* You probably don't want to change this function, instead you may
* want to change how the tag_shift field is calculated in
* avdc_resize().
*/
static inline avdc_pa_t
tag_from_pa(avdark_cache_t *self, avdc_pa_t pa)
{
return pa >> self->tag_shift;
}
/**
* Calculate the cache line index from a physical address.
*
* Feel free to experiment and change this function
*/
static inline int
index_from_pa(avdark_cache_t *self, avdc_pa_t pa)
{
return (pa >> self->block_size_log2) & (self->number_of_sets - 1);
}
/**
* Calculate a physical address from cache index and tag
*/
static inline avdc_pa_t
pa_from_tag_and_index(avdark_cache_t *self, avdc_pa_t tag, int index)
{
return (index << self->block_size_log2) | (tag << self->tag_shift);
}
/**
* Computes the log2 of a 32 bit integer value. Used in dc_init
*
* Do NOT modify!
*/
static int
log2_int32(uint32_t value)
{
int i;
for (i = 0; i < 32; i++) {
value >>= 1;
if (value == 0)
break;
}
return i;
}
/**
* Check if a number is a power of 2. Used for cache parameter sanity
* checks.
*
* Do NOT modify!
*/
static int
is_power_of_two(uint64_t val)
{
return ((((val)&(val-1)) == 0) && (val > 0));
}
void
avdc_dbg_log(avdark_cache_t *self, const char *msg, ...)
{
va_list ap;
if (self->dbg) {
const char *name = self->dbg_name ? self->dbg_name : "AVDC";
fprintf(stderr, "[%s] dbg: ", name);
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
}
}
int
avdc_access(avdark_cache_t *self, avdc_pa_t pa, avdc_access_type_t type)
{
self->count++;
/* HINT: You will need to update this function */
avdc_tag_t tag = tag_from_pa(self, pa);
int index = index_from_pa(self, pa);
int hit = 0;
if (strcmp(self->replacement,"LRU") == 0)
{
int i;
for (i = 0; i < self->assoc ; i++)
{
int index_temp = self->assoc * index + i;
if (self->lines[index_temp].valid && self->lines[index_temp].tag == tag){
hit = 1;
self->lines[index_temp].count = self->count;
break;
}
}
long min = self->count;
int lru_index = -1;
if (!hit) {
for (i = 0; i < self->assoc ; i++)
{
int index_temp = self->assoc * index + i;
if (self->lines[index_temp].count < min)
{
min = self->lines[index_temp].count;
lru_index = index_temp;
}
}
//report victim
self->last_victim = pa_from_tag_and_index(self, self->lines[lru_index].tag, lru_index);
self->last_valid = self->lines[lru_index].valid;
self->lines[lru_index].valid = 1;
self->lines[lru_index].tag = tag;
self->lines[lru_index].count = self->count;
}
}
if (strcmp(self->replacement,"RANDOM") == 0)
{
int i;
for (i = 0; i < self->assoc ; i++)
{
int index_temp = self->assoc * index + i;
if (self->lines[index_temp].valid && self->lines[index_temp].tag == tag){
hit = 1;
break;
}
}
if (!hit)
{
int random_index = self->assoc * index + rand() % self->assoc;
//report victim
self->last_victim = pa_from_tag_and_index(self, self->lines[random_index].tag, random_index);
self->last_valid = self->lines[random_index].valid;
self->lines[random_index].valid = 1;
self->lines[random_index].tag = tag;
}
}
if (strcmp(self->replacement,"FIFO") == 0)
{
int i;
for (i = 0; i < self->assoc ; i++)
{
int index_temp = self->assoc * index + i;
if (self->lines[index_temp].valid && self->lines[index_temp].tag == tag){
hit = 1;
break;
}
}
if (!hit){
int fifo_index = self->assoc * index + self->head[index];
//report victim
self->last_victim = pa_from_tag_and_index(self, self->lines[fifo_index].tag, fifo_index);
self->last_valid = self->lines[fifo_index].valid;
self->lines[fifo_index].valid = 1;
self->lines[fifo_index].tag = tag;
self->head[index] = (self->head[index]+1 == self->assoc)?
0 : self->head[index]+1;
}
}
/*
if (strcmp(self->replacement,"LRU2") == 0)
{
int i;
for (i = 0; i < self->assoc; i++)
{
int index_temp = self->assoc * index + i;
if (self->lines[index_temp].valid && self->lines[index_temp].tag == tag){
hit = 1;
break;
}
}
int fifo_index = self->assoc * index;
if (hit){
//move
avdc_tag_t hit_tag = self->lines[fifo_index + i].tag;
for (; i < self->assoc-1; i++)
{
self->lines[fifo_index + i].valid =
self->lines[fifo_index + i+1].valid;
self->lines[fifo_index + i].tag =
self->lines[fifo_index + i+1].tag;
}
self->lines[fifo_index + self->assoc-1].valid = 1;
self->lines[fifo_index + self->assoc-1].tag = hit_tag;
}else{ //!hit
//report victim
self->last_victim = pa_from_tag_and_index(self, self->lines[fifo_index].tag, fifo_index);
self->last_valid = self->lines[fifo_index].valid;
for (i = 0; i < self->assoc-1; i++)
{
self->lines[fifo_index + i].valid =
self->lines[fifo_index + i+1].valid;
self->lines[fifo_index + i].tag =
self->lines[fifo_index + i+1].tag;
}
self->lines[fifo_index + self->assoc-1].valid = 1;
self->lines[fifo_index + self->assoc-1].tag = tag;
}
}
*/
switch (type) {
case AVDC_READ: /* Read accesses */
avdc_dbg_log(self, "read: pa: 0x%.16lx, tag: 0x%.16lx, index: %d, hit: %d\n",
(unsigned long)pa, (unsigned long)tag, index, hit);
self->stat_data_read += 1;
if (!hit)
self->stat_data_read_miss += 1;
break;
case AVDC_WRITE: /* Write accesses */
avdc_dbg_log(self, "write: pa: 0x%.16lx, tag: 0x%.16lx, index: %d, hit: %d\n",
(unsigned long)pa, (unsigned long)tag, index, hit);
self->stat_data_write += 1;
if (!hit)
self->stat_data_write_miss += 1;
break;
}
return hit;
}
void
avdc_flush_cache(avdark_cache_t *self)
{
int i;
/* HINT: You will need to update this function */
for (i = 0; i < self->number_of_sets * self->assoc; i++)
{
self->lines[i].valid = 0;
self->lines[i].tag = 0;
self->lines[i].count = 0;
}
srand(time(NULL));
for(i = 0; i < self->number_of_sets; i++)
{
self->head[i] = 0;
//self->full[i] = false;
}
}
int
avdc_resize(avdark_cache_t *self,
avdc_size_t size, avdc_block_size_t block_size, avdc_assoc_t assoc)
{
/* HINT: This function precomputes some common values and
* allocates the self->lines array. You will need to update
* this to reflect any changes to how this array is supposed
* to be allocated.
*/
/* Verify that the parameters are sane */
if (!is_power_of_two(size) ||
!is_power_of_two(block_size) ||
!is_power_of_two(assoc)) {
fprintf(stderr, "size, block-size and assoc all have to be powers of two and > zero\n");
return 0;
}
/* Update the stored parameters */
self->size = size;
self->block_size = block_size;
self->assoc = assoc;
/* Cache some common values */
self->number_of_sets = (self->size / self->block_size) / self->assoc;
self->block_size_log2 = log2_int32(self->block_size);
self->tag_shift = self->block_size_log2 + log2_int32(self->number_of_sets);
//printf("%d %d %d %d\n",self->assoc,self->number_of_sets,self->block_size_log2,self->tag_shift);
/* (Re-)Allocate space for the tags array */
if (self->lines)
AVDC_FREE(self->lines);
if (self->head)
AVDC_FREE(self->head);
//if (self->full)
// AVDC_FREE(self->full);
/* HINT: If you change this, you may have to update
* avdc_delete() to reflect changes to how thie self->lines
* array is allocated. */
self->lines = AVDC_MALLOC(self->number_of_sets * self->assoc, avdc_cache_line_t);
self->head = AVDC_MALLOC(self->number_of_sets , int);
//self->lines = AVDC_MALLOC(self->number_of_sets , bool);
/* Flush the cache, this initializes the tag array to a known state */
avdc_flush_cache(self);
return 1;
}
void
avdc_print_info(avdark_cache_t *self)
{
fprintf(stderr, "Cache Info\n");
fprintf(stderr, "size: %d, assoc: %d, line-size: %d\n",
self->size, self->assoc, self->block_size);
}
void
avdc_print_internals(avdark_cache_t *self)
{
int i;
fprintf(stderr, "Cache Internals\n");
fprintf(stderr, "size: %d, assoc: %d, line-size: %d\n",
self->size, self->assoc, self->block_size);
for (i = 0; i < self->number_of_sets; i++)
fprintf(stderr, "tag: <0x%.16lx> valid: %d\n",
(long unsigned int)self->lines[i].tag,
self->lines[i].valid);
}
void
avdc_reset_statistics(avdark_cache_t *self)
{
self->stat_data_read = 0;
self->stat_data_read_miss = 0;
self->stat_data_write = 0;
self->stat_data_write_miss = 0;
}
avdark_cache_t *
avdc_new(avdc_size_t size, avdc_block_size_t block_size,
avdc_assoc_t assoc,avdc_replacement_t replacement)
{
avdark_cache_t *self;
self = AVDC_MALLOC(1, avdark_cache_t);
memset(self, 0, sizeof(*self));
// self->dbg = 1;
self->dbg = 0;
self->count = 0;
self->replacement = (char*)malloc(strlen(replacement)*sizeof(char));
strcpy(self->replacement,replacement);
if (!avdc_resize(self, size, block_size, assoc)) {
AVDC_FREE(self);
return NULL;
}
return self;
}
void
avdc_delete(avdark_cache_t *self)
{
if (self->lines)
AVDC_FREE(self->lines);
AVDC_FREE(self);
}
void
avdc_revoke(avdark_cache_t *self, avdc_pa_t pa)
{
avdc_tag_t tag = tag_from_pa(self, pa);
int index = index_from_pa(self, pa);
int i;
for (i = 0; i < self->assoc ; i++)
{
int index_temp = self->assoc * index + i;
if (self->lines[index_temp].valid && self->lines[index_temp].tag == tag)
self->lines[index_temp].valid = 0;
}
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 8
* indent-tabs-mode: nil
* c-file-style: "linux"
* compile-command: "make -k -C ../../"
* End:
*/