Split up code, made vars global

master
TuDatTr 2020-12-16 06:47:14 +01:00
parent a29e96ab6c
commit e993711667
1 changed files with 81 additions and 23 deletions

View File

@ -6,36 +6,58 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <getopt.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include "cachelab.h" #include "cachelab.h"
int v = -1; /* verbose */
int s = -1; /* set index */
int E = -1; /* Set of cache lines */
int b = -1; /* 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 { struct cache_line {
unsigned char v; unsigned char v;
unsigned int tag; unsigned int tag;
unsigned int timestamp; unsigned int timestamp;
}; };
typedef struct cache_line cache_line_t;
void print_help() { void print_help() {
printf("Usage: ./csim-ref [-hv] -s <s> -E <E> -b <b> -t <tracefile>"); printf("Usage: ./csim-ref [-hv] -s <s> -E <E> -b <b> -t <tracefile>\n");
printf(" -h: Optional help flag that prints usage info"); printf(" -h: Optional help flag that prints usage info\n");
printf(" -v: Optional verbose flag that displays trace info"); printf(" -v: Optional verbose flag that displays trace info\n");
printf(" -s <s>: Number of set index bits (S = 2 s is the number of sets)"); printf(" -s <s>: Number of set index bits (S = 2 s is the number of sets)\n");
printf(" -E <E>: Associativity (number of lines per set)"); printf(" -E <E>: Associativity (number of lines per set)\n");
printf(" -b <b>: Number of block bits (B = 2 b is the block size)"); printf(" -b <b>: Number of block bits (B = 2 b is the block size)\n");
printf(" -t <tracefile>: Name of the valgrind trace to replay"); printf(" -t <tracefile>: Name of the valgrind trace to replay\n");
} }
int main(int argc, char *argv[]) { void accessMem (struct cache_line **cache, unsigned int address) {
int c; unsigned int set_bits = address >> (s + b);
int v = -1, s = -1, E = -1, b = -1, S, B; unsigned int tag_bits = address << (64-s-b) >> (64 - s);
FILE *tracefile = NULL;
int hit = 0, miss = 0, eviction = 0;
for (int i = 0; i < E; i++) {
if (cache[set_bits][i].v == 1 && cache[set_bits][i].tag == tag_bits) {
cache[set_bits][i].timestamp = time;
hit++;
break;
} else {
}
}
time++;
}
void opt (int argc, char **argv) {
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) {
case 'h': case 'h':
@ -45,6 +67,9 @@ int main(int argc, char *argv[]) {
case 'v': case 'v':
v = 1; v = 1;
break; break;
case 's':
s = atoi(optarg);
break;
case 'E': case 'E':
E = atoi(optarg); E = atoi(optarg);
break; break;
@ -57,18 +82,20 @@ int main(int argc, char *argv[]) {
} }
} }
if (v == -1 || s == -1 || E == -1 || b == -1 || tracefile == NULL) { if (s == -1 || E == -1 || b == -1 || tracefile == NULL) {
print_help(); print_help();
exit(0); exit(0);
} }
S = pow(2, s); S = pow(2, s);
B = pow(2, b); B = pow(2, b);
}
cache_line_t **cache = malloc(S*sizeof(cache_line_t*)); void Init() {
cache = (struct cache_line **) malloc(S*sizeof(struct cache_line *));
for (int i = 0; i < (S * sizeof(cache_line_t*)); i++) { for (int i = 0; i < S; i++) {
cache_line_t *cache_set = malloc(E*sizeof(struct cache_line)); struct cache_line* cache_set = (struct cache_line *) malloc(E*sizeof(struct cache_line));
cache[i] = cache_set; cache[i] = cache_set;
for (int j = 0; j < E; j++) { for (int j = 0; j < E; j++) {
cache_set[j].v = '\0'; cache_set[j].v = '\0';
@ -76,17 +103,48 @@ int main(int argc, char *argv[]) {
cache_set[j].timestamp = 0; cache_set[j].timestamp = 0;
} }
} }
}
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 char op; // I = instruction, L = data load, S = data store, M = data modify
unsigned address; unsigned address;
int size; int size;
while (fscanf(tracefile, "%c %x %d", &op, &address, &size) > 0 ) { 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");
} }
}
fclose(tracefile); int main(int argc, char *argv[]) {
free(cache); opt(argc, argv);
printSummary(0, 0, 0);
Init();
traceCache();
CleanUp();
printSummary(hit, miss, eviction);
return 0; return 0;
} }