-
Notifications
You must be signed in to change notification settings - Fork 1
/
llc-lru.c
126 lines (104 loc) · 3.38 KB
/
llc-lru.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
/*********** Author: Abhishek Kumar **************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
typedef struct {
unsigned long long tag;
unsigned long long lru;
} CacheTag;
#define SIZE 4194304
#define INVALID_TAG 0xfffffffffffffffULL
#define LLC_NUMSET 8192 /* 8 MB LLC: 8 X 1024 */
#define LLC_ASSOC 16
typedef unsigned long long ull;
CacheTag** create_cache (int numset, int assoc) {
/* Creates a cache with (number of sets = numset) and (associativity = assoc)
*/
int i, j;
CacheTag ** cache = (CacheTag**)malloc(numset*sizeof(CacheTag*));
assert(cache != NULL);
for (i=0; i<numset; i++) {
cache[i] = (CacheTag*)malloc(assoc*sizeof(CacheTag));
assert(cache[i] != NULL);
for ( j=0; j<assoc; j++) {
cache[i][j].tag = INVALID_TAG;
}
}
return cache;
}
int main (int argc, char **argv)
{
int i, LLCsetid, maxindex, tid;
unsigned long long block_addr, max, *uniqueId;
char output_name[256], input_name[256];
FILE *fp_in;
int llcway;
int hash_index;
CacheTag** LLCcache;
int block_type;
ull pc;
if (argc != 2) {
printf("Need two arguments: input file. Aborting...\n");
exit (1);
}
LLCcache = (CacheTag**)create_cache(LLC_NUMSET, LLC_ASSOC);
/* The following array is used to find the sequence number for an access to a set;
This sequence number acts as the timestamp for the access */
uniqueId = (unsigned long long*)malloc(LLC_NUMSET*sizeof(unsigned long long));
assert(uniqueId != NULL);
for (i=0; i<LLC_NUMSET; i++) {
uniqueId[i] = 0;
}
printf("Starting simulation...\n"); fflush(stdout);
// Simulate
unsigned long long num_misses=0;
sprintf(input_name, "%s", argv[1]);
fp_in = fopen(input_name, "r");
assert(fp_in != NULL);
while (!feof(fp_in)) {
fscanf(fp_in, "%llu %d %llu %d", &pc, &tid, &block_addr, &block_type);
hash_index = block_addr % SIZE;
LLCsetid = block_addr % LLC_NUMSET;
/* LLC cache lookup */
for (llcway=0; llcway<LLC_ASSOC; llcway++) {
if (LLCcache[LLCsetid][llcway].tag == block_addr) {
/* LLC cache hit; Update last use*/
LLCcache[LLCsetid][llcway].lru = uniqueId[LLCsetid];
break;
}
}
if (llcway==LLC_ASSOC) {
/* LLC cache miss */
num_misses++;
/* find victim block and replace it with current block */
/* check if there is invalid way */
for (llcway=0; llcway<LLC_ASSOC; llcway++) {
if (LLCcache[LLCsetid][llcway].tag == INVALID_TAG) break;
}
if (llcway==LLC_ASSOC) {
/* no invalid way; find MIN */
max = LLONG_MAX;
for (llcway=0; llcway<LLC_ASSOC; llcway++) {
if (LLCcache[LLCsetid][llcway].lru < max) {
max = LLCcache[LLCsetid][llcway].lru;
maxindex = llcway;
}
}
llcway = maxindex;
}
assert (llcway < LLC_ASSOC);
LLCcache[LLCsetid][llcway].tag = block_addr;
LLCcache[LLCsetid][llcway].lru = uniqueId[LLCsetid];
}
uniqueId[LLCsetid]++;
}
fclose(fp_in);
printf("Done reading file!\n");
unsigned long long tot=0;
for (i=0; i<LLC_NUMSET; i++) {
tot+=uniqueId[i];
}
printf("Miss rate %s: %lf\n", input_name,(num_misses*1.0)/tot);
return 0;
}