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
| #include "cachelab.h" #include <stdio.h> #include <stdlib.h> #include <getopt.h> #include <math.h>
typedef int bool;
#define true 1 #define false 0
unsigned long time = 0;
bool v_mode = false; int s = 0, E = 0, b = 0; char *file = "";
unsigned hit_count = 0, miss_count = 0, eviction_count = 0; void getOpt(bool *v_mode, int *s, int *E, int *b, char **file, int argc, char **argv); void access(unsigned long address); typedef struct { bool valid; unsigned long tag; unsigned long last_used_time;
} Line;
Line **cache = NULL;
int main(int argc, char **argv) {
getOpt(&v_mode, &s, &E, &b, &file, argc, argv);
FILE *trace_file = fopen(file, "r");
int S = 1<<s; cache = malloc(sizeof(Line *) * S); for (size_t i = 0; i < S; i++) { cache[i] = malloc(sizeof(Line) * E); }
for (size_t i = 0; i < S; i++) { for (size_t j = 0; j < E; j++) { cache[i][j].last_used_time = 0; cache[i][j].tag = 0; cache[i][j].valid = 0; } }
char buffer[64];
char type; unsigned long addr; int m_size; while (fgets(buffer, 64, trace_file)) { sscanf(buffer, " %c %lx,%d", &type, &addr, &m_size); switch (type) { case 'L': access(addr); break; case 'M': access(addr);
case 'S': access(addr); break;
default: break; } }
for (size_t i = 0; i < S; i++) { free(cache[i]); cache[i] = NULL; } free(cache); cache = NULL;
fclose(trace_file);
printSummary(hit_count, miss_count, eviction_count); return 0; }
void access(unsigned long address) { int group = (address >> b)&((1 << s) - 1) ; int tag = address >> (b + s);
for (size_t i = 0; i < E; i++) { if (cache[group][i].tag == tag && cache[group][i].valid == 1) { hit_count++; cache[group][i].last_used_time=++time; return; } }
for (size_t i = 0; i < E; i++) { if (cache[group][i].valid == 0) { cache[group][i].valid = 1; cache[group][i].last_used_time = ++time; cache[group][i].tag = tag; miss_count++; return;
}
}
eviction_count++; miss_count++; unsigned long lru_time = -1UL; unsigned line_index=0;
for (size_t i = 0; i < E; i++) { if (cache[group][i].last_used_time < lru_time) { lru_time=cache[group][i].last_used_time; line_index=i; } }
cache[group][line_index].last_used_time=++time; cache[group][line_index].tag=tag;
printf("test\n");
}
void getOpt(bool *v_mode, int *s, int *E, int *b, char **file, int argc, char **argv) { char opt; while ((opt = getopt(argc, argv, "v::s:E:b:t:ma")) != -1) { switch (opt) { case 'v': *v_mode = true; break; case 's': *s = atoi(optarg); break; case 'E': *E = atoi(optarg); break; case 'b': *b = atoi(optarg); break; case 't': *file = optarg; break; case '?':
fprintf(stderr, "Usage: %s [-hv] -s <num> -E <num> -b <num> -t <file>\n\ Options:\n\ -h Print this help message.\n\ -v Optional verbose flag.\n\ -s <num> Number of set index bits.\n\ -E <num> Number of lines per set.\n\ -b <num> Number of block offset bits.\n\ -t <file> Trace file.\n\ \n\ Examples:\n\ linux> %s -s 4 -E 1 -b 4 -t traces/yi.trace\n\ linux> %s -v -s 8 -E 2 -b 4 -t traces/yi.trace\n", argv[0], argv[0], argv[0]); exit(EXIT_FAILURE);
default: break; } } }
|