-
Notifications
You must be signed in to change notification settings - Fork 4
/
PLRU.cpp
153 lines (126 loc) · 2.8 KB
/
PLRU.cpp
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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define DELIM ","
#define CACHE_SIZE 4
#define PLRU_WAY 4
struct cacheLine{
int tag;
int valid;
};
struct cacheSet2
{
struct cacheLine c[2];
};
struct cacheSet4
{
struct cacheLine c[4];
};
struct cacheSet8
{
};
struct cacheSet16
{
struct cacheLine c[16];
};
struct cacheSet2 cache2[8191];
struct cacheSet4 cache4[4096];
struct cacheSet8 cache8[2048];
struct cacheSet16 cache16[1024];
int miss=0, hit=0;
int node2=0,node4=0,node8=0,node16=0;
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Missing filename.\n");
return EXIT_FAILURE;
}
FILE *fp = fopen(argv[1], "r");
if (!fp) {
printf("Invalid filename: %s\n", argv[1]);
return EXIT_FAILURE;
}
int cache[CACHE_SIZE] = {0};
int nodes = 0; // Stores each node as a bit in an int. Modify to allow caches of size N
int linesUsed = 0;
int hits = 0;
int iteration = 0;
int misses = 0;
char buffer[255];
char *token;
fscanf(fp, "%s", buffer);
token = strtok(buffer, DELIM);
while (token) {
iteration++;
int number = atoi(token);
// Find:
int found = -1;
int counter;
for (counter = 0; counter < CACHE_SIZE && found < 0; counter++) {
if (cache[counter] == number) {
// Read:
// nodes=00x -> line 0
// nodes=01x -> line 1
// nodes=1x0 -> line 2
// nodes=1x1 -> line 3
if (0 == counter)
nodes = (nodes & 1) | 6;
if (1 == counter)
nodes = (nodes & 1) | 4;
if (2 == counter)
nodes = (nodes & 2) | 1;
if (3 == counter)
nodes = (nodes & 2) | 0;
found = counter;
hits++;
}
}
// Write:
if (found == -1) {
int currLine = 0;
if (linesUsed < CACHE_SIZE) {
currLine = linesUsed;
linesUsed++;
}
else {
// nodes=00x -> line 0
// nodes=01x -> line 1
// nodes=1x0 -> line 2
// nodes=1x1 -> line 3
if (0 == (nodes & 6))
currLine = 0;
else if (2 == (nodes & 6))
currLine = 1;
else if (4 == (nodes & 5))
currLine = 2;
else if (5 == (nodes & 5))
currLine = 3;
misses++;
}
cache[currLine] = number;
// Update Nodes for last access (same as read, move to function):
// nodes=00x -> line 0
// nodes=01x -> line 1
// nodes=1x0 -> line 2
// nodes=1x1 -> line 3
if (0 == currLine)
nodes = (nodes & 1) | 6;
if (1 == currLine)
nodes = (nodes & 1) | 4;
if (2 == currLine)
nodes = (nodes & 2) | 1;
if (3 == currLine)
nodes = (nodes & 2) | 0;
}
// Print Cache Status
printf("Iteration: %3d: ", iteration);
for (counter = 0; counter < CACHE_SIZE; counter++) {
printf("%2d ", cache[counter]);
}
printf(" [Add: %d]\n", number);
// Get next token from file
token = strtok(NULL, DELIM);
}
fclose(fp);
printf("\nHITS: %2d\nMISSES: %2d\n\n", hits, misses);
return EXIT_SUCCESS;
}