From cee998af6b0d88371846806dd6d2c7ceaca867d9 Mon Sep 17 00:00:00 2001 From: TuDatTr Date: Sun, 20 Dec 2020 18:38:38 +0100 Subject: [PATCH] Fixed eviction time, which caused each value to be evicted --- csim.c | 174 ------------------------------------------------ src/csim.c | 17 +++-- src/trace.tmp | 0 tuan-handin.tar | Bin 10240 -> 20480 bytes 4 files changed, 8 insertions(+), 183 deletions(-) delete mode 100644 csim.c create mode 100644 src/trace.tmp diff --git a/csim.c b/csim.c deleted file mode 100644 index b26e098..0000000 --- a/csim.c +++ /dev/null @@ -1,174 +0,0 @@ -/** - * Groupmembers: - * 1. Anna Schlittenhardt - * 2. Tuan-Dat Tran - */ - -#include -#include -#include -#include -#include -#include -#include "cachelab.h" - -int v = 0; /* verbose */ -int s = 0; /* set index */ -int E = 0; /* Set of cache lines */ -int b = 0; /* Block offset */ -int S; /* Set of Set of cache lines*/ -int B; /* Block size */ -long long time = 0; -FILE *tracefile; /* File to trace from */ -struct cache_line **cache; /* cache */ - -int hit = 0, miss = 0, eviction = 0; - -struct cache_line { - unsigned char v; - unsigned int tag; - unsigned int time; -}; - -void print_help() { - printf("Usage: ./csim-ref [-hv] -s -E -b -t \n"); - printf(" -h: Optional help flag that prints usage info\n"); - printf(" -v: Optional verbose flag that displays trace info\n"); - printf(" -s : Number of set index bits (S = 2 s is the number of sets)\n"); - printf(" -E : Associativity (number of lines per set)\n"); - printf(" -b : Number of block bits (B = 2 b is the block size)\n"); - printf(" -t : Name of the valgrind trace to replay\n"); -} - -void init() { - cache = (struct cache_line **) malloc(S*sizeof(struct cache_line *)); - 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++) { - cache_set[j].v = 0; - cache_set[j].tag = 0; - cache_set[j].time = 0; - } - } -} - -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); - struct cache_line *cache_set = cache[set]; - long evic_time = 0; - 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) { - if (v) { printf("eviction "); } - 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; - while ((c = getopt(argc, argv, "s:E:b:t:vh")) != -1) { - switch (c) { - case 'h': - print_help(); - exit(0); - break; - case 'v': - v = 1; - break; - case 's': - s = atoi(optarg); - break; - case 'E': - E = atoi(optarg); - break; - case 'b': - b = atoi(optarg); - break; - case 't': - tracefile = fopen(optarg, "r"); - break; - } - } - - if (s == 0 || E == 0 || b == 0 || tracefile == NULL) { - print_help(); - exit(1); - } - - S = pow(2, s); - B = pow(2, b); - - init(); - trace(); - for (int i = 0; i < S; i++) { - for (int j = 0; j < E; j++) { - // printf("cache[%d][%d]: %c %x %ul\n", i, j, cache[i][j].v, cache[i][j].tag, cache[i][j].time); - } - } - clean(); - - printSummary(hit, miss, eviction); - return 0; -} diff --git a/src/csim.c b/src/csim.c index e7ed320..1086bfb 100644 --- a/src/csim.c +++ b/src/csim.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "cachelab.h" int v = 0; /* verbose */ @@ -41,9 +42,9 @@ void print_help() { } void init() { - cache = (struct cache_line **) malloc(S*sizeof(struct cache_line *)); + cache = (struct cache_line **) malloc(S * sizeof(struct cache_line *)); for (int i = 0; i < S; i++) { - struct cache_line* cache_set = (struct cache_line *) malloc(E*sizeof(struct cache_line)); + 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++) { cache_set[j].v = 0; @@ -51,6 +52,7 @@ void init() { cache_set[j].time = 0; } } + printf("cache: %ld, cache_set: %ld, cache_line: %ld\n", sizeof(cache), sizeof(cache[0]), sizeof(cache[0][0])); } void clean() { @@ -64,11 +66,13 @@ void clean() { void accessMem (int addr) { unsigned int set = ((addr >> b) & ((1LL << s) - 1)); unsigned int tag = addr >> (b + s); + struct cache_line *cache_set = cache[set]; - unsigned long evic_time = 0; + unsigned long evic_time = ULONG_MAX; unsigned int evic_line = 0; + for (int i = 0; i < E; i++) { - if (cache_set[i].tag == tag && cache_set[i].v != 0){ + if (cache_set[i].tag == tag && cache_set[i].v == 1){ if (v) { printf("hit "); } hit++; cache_set[i].time = time; @@ -164,11 +168,6 @@ int main(int argc, char *argv[]) { init(); trace(); - for (int i = 0; i < S; i++) { - for (int j = 0; j < E; j++) { - // printf("cache[%d][%d]: %c %x %ul\n", i, j, cache[i][j].v, cache[i][j].tag, cache[i][j].time); - } - } clean(); printSummary(hit, miss, eviction); diff --git a/src/trace.tmp b/src/trace.tmp new file mode 100644 index 0000000..e69de29 diff --git a/tuan-handin.tar b/tuan-handin.tar index 167007218da906cb77431301e58cf2e1e8650e58..6728326eedf727fd27c965dda68d9073a730637d 100644 GIT binary patch literal 20480 zcmeHNZBygO5#F!yujnJStj&wTz%D7kR6!kkl?rgFz*drS2sM_+_HdTul4P^o9{)X0 z_h?2EHhbhYn{2A2DlBO<{W4GY%yjne^sq==Cd?Y3DK);rl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