37/51 points\test 2,5 6 still fail for some reason

master
TuDatTr 2020-12-17 13:07:26 +01:00
parent e993711667
commit 0ef1eba093
7 changed files with 97 additions and 70 deletions

1
src/.csim_results Normal file
View File

@ -0,0 +1 @@
167 71 67

BIN
src/csim Executable file

Binary file not shown.

View File

@ -12,10 +12,10 @@
#include <math.h> #include <math.h>
#include "cachelab.h" #include "cachelab.h"
int v = -1; /* verbose */ int v = 0; /* verbose */
int s = -1; /* set index */ int s = 0; /* set index */
int E = -1; /* Set of cache lines */ int E = 0; /* Set of cache lines */
int b = -1; /* Block offset */ int b = 0; /* Block offset */
int S; /* Set of Set of cache lines*/ int S; /* Set of Set of cache lines*/
int B; /* Block size */ int B; /* Block size */
long long time = 0; long long time = 0;
@ -27,7 +27,7 @@ int hit = 0, miss = 0, eviction = 0;
struct cache_line { struct cache_line {
unsigned char v; unsigned char v;
unsigned int tag; unsigned int tag;
unsigned int timestamp; unsigned int time;
}; };
void print_help() { void print_help() {
@ -40,23 +40,95 @@ void print_help() {
printf(" -t <tracefile>: Name of the valgrind trace to replay\n"); printf(" -t <tracefile>: Name of the valgrind trace to replay\n");
} }
void accessMem (struct cache_line **cache, unsigned int address) { void init() {
unsigned int set_bits = address >> (s + b); cache = (struct cache_line **) malloc(S*sizeof(struct cache_line *));
unsigned int tag_bits = address << (64-s-b) >> (64 - s); for (int i = 0; i < S; i++) {
struct cache_line* cache_set = (struct cache_line *) malloc(E*sizeof(struct cache_line));
for (int i = 0; i < E; i++) { cache[i] = cache_set;
if (cache[set_bits][i].v == 1 && cache[set_bits][i].tag == tag_bits) { for (int j = 0; j < E; j++) {
cache[set_bits][i].timestamp = time; cache_set[j].v = 0;
hit++; cache_set[j].tag = 0;
break; cache_set[j].time = 0;
} else {
} }
} }
time++;
} }
void opt (int argc, char **argv) { void clean() {
for (int i = 0; i < S; i++) {
free(cache[i]);
}
free(cache);
fclose(tracefile);
}
void accessMem (int addr) {
unsigned int set = ((addr >> b) & ((1LL << s) - 1));
unsigned int tag = addr >> (b + s);
printf("addr %x, set %x, tag %x ", addr, set, tag);
struct cache_line *cache_set = cache[set];
long evic_time = 0xffffffffffffffff;
int evic_line = 0;
for (int i = 0; i < E; i++) {
if (cache_set[i].tag == tag && cache_set[i].v != 0){
if (v) { printf("hit "); }
hit++;
cache_set[i].time = time;
time++;
return;
}
}
if (v) { printf("miss "); }
miss++;
for (int i = 0; i < E; i++) {
if (cache_set[i].time < evic_time) {
evic_line = i;
evic_time = cache_set[i].time;
}
}
if (cache_set[evic_line].v == 1) {
eviction++;
if (v) { printf("eviction "); }
}
cache_set[evic_line].v = 1;
cache_set[evic_line].tag = tag;
cache_set[evic_line].time = time;
time++;
return;
}
void trace() {
char op;
unsigned address;
int size;
while (fscanf(tracefile, " %c %x,%d", &op, &address, &size) > 0 ) {
switch (op) {
case 'M':
if (v) { printf("%c %x,%d ", op, address, size); }
accessMem(address);
accessMem(address);
printf("\n");
break;
case 'L':
if (v) { printf("%c %x,%d ", op, address, size); }
accessMem(address);
printf("\n");
break;
case 'S':
if (v) { printf("%c %x,%d ", op, address, size); }
accessMem(address);
printf("\n");
break;
}
}
}
int main(int argc, char *argv[]) {
char c; char c;
while ((c = getopt(argc, argv, "s:E:b:t:vh")) != -1) { while ((c = getopt(argc, argv, "s:E:b:t:vh")) != -1) {
switch (c) { switch (c) {
@ -82,68 +154,22 @@ void opt (int argc, char **argv) {
} }
} }
if (s == -1 || E == -1 || b == -1 || tracefile == NULL) { if (s == 0 || E == 0 || b == 0 || tracefile == NULL) {
print_help(); print_help();
exit(0); exit(1);
} }
S = pow(2, s); S = pow(2, s);
B = pow(2, b); B = pow(2, b);
}
void Init() {
cache = (struct cache_line **) malloc(S*sizeof(struct cache_line *));
init();
trace();
for (int i = 0; i < S; i++) { for (int i = 0; i < S; i++) {
struct cache_line* cache_set = (struct cache_line *) malloc(E*sizeof(struct cache_line));
cache[i] = cache_set;
for (int j = 0; j < E; j++) { for (int j = 0; j < E; j++) {
cache_set[j].v = '\0'; // printf("cache[%d][%d]: %c %x %ul\n", i, j, cache[i][j].v, cache[i][j].tag, cache[i][j].time);
cache_set[j].tag = 0;
cache_set[j].timestamp = 0;
} }
} }
} clean();
void CleanUp() {
for (int i = 0; i < S; i++) {
free(cache[i]);
}
free(cache);
fclose(tracefile);
}
void traceCache() {
char op; // I = instruction, L = data load, S = data store, M = data modify
unsigned address;
int size;
while (fscanf(tracefile, " %c %x,%d", &op, &address, &size) > 0 ) {
if (v == 1) {
printf("%c %x,%d", op, address, size);
}
switch (op) {
case 'M':
accessMem(cache, address);
accessMem(cache, address);
break;
case 'L':
accessMem(cache, address);
break;
case 'S':
accessMem(cache, address);
break;
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
opt(argc, argv);
Init();
traceCache();
CleanUp();
printSummary(hit, miss, eviction); printSummary(hit, miss, eviction);
return 0; return 0;

BIN
src/test-trans Executable file

Binary file not shown.

BIN
src/tracegen Executable file

Binary file not shown.

BIN
src/trans.o Normal file

Binary file not shown.

BIN
src/tuan-handin.tar Normal file

Binary file not shown.