37/51 points\test 2,5 6 still fail for some reason
parent
e993711667
commit
0ef1eba093
|
@ -0,0 +1 @@
|
|||
167 71 67
|
160
src/csim.c
160
src/csim.c
|
@ -12,10 +12,10 @@
|
|||
#include <math.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 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;
|
||||
|
@ -27,7 +27,7 @@ int hit = 0, miss = 0, eviction = 0;
|
|||
struct cache_line {
|
||||
unsigned char v;
|
||||
unsigned int tag;
|
||||
unsigned int timestamp;
|
||||
unsigned int time;
|
||||
};
|
||||
|
||||
void print_help() {
|
||||
|
@ -40,23 +40,95 @@ void print_help() {
|
|||
printf(" -t <tracefile>: Name of the valgrind trace to replay\n");
|
||||
}
|
||||
|
||||
void accessMem (struct cache_line **cache, unsigned int address) {
|
||||
unsigned int set_bits = address >> (s + b);
|
||||
unsigned int tag_bits = address << (64-s-b) >> (64 - s);
|
||||
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);
|
||||
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_bits][i].v == 1 && cache[set_bits][i].tag == tag_bits) {
|
||||
cache[set_bits][i].timestamp = time;
|
||||
hit++;
|
||||
break;
|
||||
} else {
|
||||
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 opt (int argc, char **argv) {
|
||||
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) {
|
||||
|
@ -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();
|
||||
exit(0);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
S = pow(2, s);
|
||||
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++) {
|
||||
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].timestamp = 0;
|
||||
// printf("cache[%d][%d]: %c %x %ul\n", i, j, cache[i][j].v, cache[i][j].tag, cache[i][j].time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
clean();
|
||||
|
||||
printSummary(hit, miss, eviction);
|
||||
return 0;
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue