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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cachelab.h"
#define BUFF_SIZE 25
#define BEGIN_STATE 0
#define EMPTY_LINE 1
#define REPLACE_LINE 2
#define HIT_LINE 3
void printUsage()
{
printf("Usage: ./csim-ref [-hv] -s <num> -E <num> -b <num> -t <file>\n");
}
void handleFormetError()
{
fprintf(stderr, "./csim-ref: Missing required command line argument\n");
fprintf(stderr, "Usage: ./csim-ref [-hv] -s <num> -E <num> -b <num> -t <file>\n");
}
// 缓存行
typedef struct cacheLine
{
unsigned int valid; // 有效位
unsigned int tag; // 标记位
unsigned int log; // 记录最近访问情况
/*每次有更新时将每个有效的行的 log 的值加 1,
在每次访问后将对应行的 log 的值设置为 1,
需要替换时选择替换 log 的值最大的缓存块
*/
} CacheLine;
// 缓存组
typedef struct cacheSet
{
CacheLine *lines;
} CacheSet;
// 缓存模拟
typedef struct cache
{
CacheSet *sets;
int s, e, b;
} Cache;
typedef struct testLog
{
int miss, hit, eviction;
} TestLog;
// 初始化模拟缓存
Cache *initCache(int s, int e, int b)
{
int setNum = 1 << s;
Cache *newCache = malloc(sizeof(Cache));
newCache->s = s;
newCache->e = e;
newCache->b = b;
newCache->sets = malloc(sizeof(CacheSet) * setNum);
for (int i = 0; i < setNum; ++i)
{
newCache->sets[i].lines = malloc(sizeof(CacheLine) * e);
for (int j = 0; j < e; ++j)
{
newCache->sets[i].lines[j].log = 0;
newCache->sets[i].lines[j].tag = 0;
newCache->sets[i].lines[j].valid = 0;
}
}
return newCache;
}
// 进行一次更新(读或写操作)
void update(Cache *cache, unsigned int address, unsigned int length, TestLog *log, int version)
{
unsigned int setNum, tag, blockOffset;
tag = address >> (cache->b + cache->s);
blockOffset = (address << (64 - cache->b)) >> (64 - cache->b);
setNum = (address << (64 - cache->s - cache->b)) >> (64 - cache->s);
CacheLine *lines = cache->sets[setNum].lines;
int lineNum = 0;
int state = BEGIN_STATE;
for (int i = 0; i < cache->e; ++i)
{
if(lines[i].valid)
{
lines[i].log++;
if(lines[i].tag == tag)
{
lineNum = i;
state = HIT_LINE;
}
}
}
// 如果命中
if (state == HIT_LINE)
{
log->hit++;
if(version)
printf("hit ");
lines[lineNum].log = 0;
}
// 如果没有命中
else
{
log->miss++;
if(version)
printf("miss ");
// 确定要替换的行(或空行)
for (int i = 0; i < cache->e; ++i)
{
if(lines[i].valid)
{
if(state == BEGIN_STATE)
{
lineNum = i;
state = REPLACE_LINE;
}
else if(state == REPLACE_LINE)
{
if(lines[lineNum].log < lines[i].log)
lineNum = i;
}
}
else
{
if(state != EMPTY_LINE)
{
lineNum = i;
state = EMPTY_LINE;
}
}
}
// 进行替换
if (state == REPLACE_LINE)
{
lines[lineNum].log = 0;
lines[lineNum].tag = tag;
log->eviction++;
if(version)
printf("eviction ");
}
// 填入空行
else if(state == EMPTY_LINE)
{
lines[lineNum].log = 0;
lines[lineNum].tag = tag;
lines[lineNum].valid = 1;
}
}
// 判断要更新的内存长度是否超出一个缓存块
unsigned int maxOffset = 1;
for (int i = 0; i < cache->b; ++i)
maxOffset = (maxOffset << 1) + 1;
unsigned int temp = maxOffset - blockOffset + 1;
if (maxOffset < blockOffset + length - 1)
update(cache, address + temp, length - temp, log, version);
}
void testCache(FILE *fp, TestLog *log, int s, int e, int b, int version)
{
Cache *cache = initCache(s, e, b);
char command[BUFF_SIZE] = {0};
char operation;
unsigned int address, length;
while (fgets(command, BUFF_SIZE, fp))
{
if (command[0] != ' ')
continue;
sscanf(command, " %c%x,%x\n", &operation, &address, &length);
if(version)
printf("%c %x,%x ", operation, address, length);
switch (operation)
{
case 'L':
update(cache, address, length, log, version);
break;
case 'S':
update(cache, address, length, log, version);
break;
case 'M':
update(cache, address, length, log, version);
update(cache, address, length, log, version);
break;
default:
break;
}
if(version)
printf("\n");
memset(command, 0, BUFF_SIZE);
}
}
int main(int argc, char const *argv[])
{
int s; // 序号所占的位宽
int e; // 每组的缓存块个数
int b; // 偏移量所占的位宽
int t; // argv[t]为跟踪文件
int version = 0; // 指示 -v 参数
int se = 0, ee = 0, be = 0, te = 0; // 判断 s,e,b,t 参数是否给出
FILE *fp; // 跟踪文件
if (argc == 2)
{
if (strcmp(argv[1], "-h"))
{
handleFormetError();
return EXIT_FAILURE;
}
printUsage();
return EXIT_SUCCESS;
}
else if (argc == 9 || argc == 10)
{
for (int i = 1; i < argc; ++i)
{
if (!strcmp(argv[i], "-s"))
{
s = atoi(argv[++i]);
se = 1;
}
else if (!strcmp(argv[i], "-E"))
{
e = atoi(argv[++i]);
ee = 1;
}
else if (!strcmp(argv[i], "-b"))
{
b = atoi(argv[++i]);
be = 1;
}
else if (!strcmp(argv[i], "-t"))
{
t = ++i;
te = 1;
}
else if (!strcmp(argv[i], "-v"))
version = 1;
}
if (!(se && ee && be && te))
{
handleFormetError();
return EXIT_FAILURE;
}
if (argc == 10 && !version)
{
handleFormetError();
return EXIT_FAILURE;
}
}
else
{
handleFormetError();
return EXIT_FAILURE;
}
// 打开跟踪文件
fp = fopen(argv[t], "r");
if (fp == NULL)
{
fprintf(stderr, "%s: No such file or directory\n", argv[t]);
return EXIT_FAILURE;
}
// 初始化测试记录
TestLog *log = malloc(sizeof(TestLog));
log->eviction = 0;
log->hit = 0;
log->miss = 0;
// 进行模拟测试
testCache(fp, log, s, e, b, version);
printSummary(log->hit, log->miss, log->eviction);
return 0;
}
|