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 209 210 211 212 213 214
| #include "cachelab.h" #include <unistd.h> #include <getopt.h> #include <stdio.h> #include <stdlib.h>
typedef struct{ int valid; int tag; int time_stamp; } cache_line;
int timestamp = 0;
int find_hit_cache(cache_line *cache_line,int E,int tag, int*hits) { int isHit = 0; for(int i = 0; i < E; i++) { if (cache_line[i].valid == 1 && cache_line[i].tag == tag ) { printf("hit \n"); *hits = *hits + 1; isHit = 1; cache_line[i].time_stamp = timestamp; return isHit; } } return isHit; }
int find_empty_cache(cache_line *cache_line,int E,int tag) { int have_empty_cache = 0; for (int i = 0; i< E; i++) { if (cache_line[i].valid == 0) { cache_line[i].valid = 1; cache_line[i].tag = tag; cache_line[i].time_stamp = timestamp; have_empty_cache = 1; return have_empty_cache; } } return have_empty_cache; }
int get_eviction_index(cache_line *cache_line, int E) { int max_time_stamp = timestamp; int eviction_index = -1; for (int i = 0; i< E; i++) { if (cache_line[i].time_stamp < max_time_stamp) { max_time_stamp = cache_line[i].time_stamp; eviction_index = i; } } return eviction_index; }
void LRU(cache_line *cache_line, int E,int tag) { int eviction_index = get_eviction_index(cache_line, E); cache_line[eviction_index].valid = 1; cache_line[eviction_index].tag = tag; cache_line[eviction_index].time_stamp = timestamp; }
int load_and_store(unsigned address,int b,int s,int u_max,int E,cache_line **cache, int *hits,int *misses,int *evications) { int set_index,tag; set_index = (address >> b) & u_max; tag = (address >> b) >> s;
int isHit = find_hit_cache(cache[set_index], E, tag, hits); if (isHit == 0) { printf("miss \n"); *misses = *misses + 1; int have_empty_cache = find_empty_cache(cache[set_index], E, tag); if (have_empty_cache == 0) { printf("evictions \n"); *evications = *evications + 1; LRU(cache[set_index], E, tag); } } timestamp++; return 0; }
int main(int argc, char** argv) { int opt,v,s,E,b,S,B; FILE * pFile; while(-1 != (opt = getopt(argc, argv, "h?v?s:E:b:t:"))){ switch(opt) { case 'h': printf("./csim: Missing required command line argument \n Usage: ./csim-ref [-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 ./csim -s 4 -E 1 -b 4 -t traces/yi.trace \n ./csim -v -s 8 -E 2 -b 4 -t traces/yi.trace \n"); break; case 'v': v = 1; printf("v:%d \n",v); break; case 's': s = atoi(optarg); S = 1 << s; printf("s:%d, S:%d \n",s,S); break; case 'E': E = atoi(optarg); printf("E:%d \n",E); break; case 'b': b = atoi(optarg); B = 1 << b; printf("b:%d, B:%d \n",b,B); break; case 't': pFile = fopen(optarg,"r"); printf("t:%s, file:%p \n",optarg,pFile); break; default: printf("非法参数 \n"); break; } } if(s == 0 || E == 0 || b == 0) { return 0; } cache_line **cache = (cache_line **)malloc(S * sizeof(cache_line *)); if (cache == NULL) { printf("内存分配失败 \n"); } for(int i = 0; i < S; i++) { cache[i] = (cache_line *)malloc(E * sizeof(cache_line)); if(cache[i] == NULL) { printf("内存分配失败,开始回滚 \n"); for (int j = 0; j < i; ++j) { free(cache[j]); } free(cache); } }
int u_max = 1; for(int i = 0; i < s - 1; i++) { u_max = (u_max << 1) | 1; }
char identifier; unsigned address; int size; int hits,misses,evictions;
while(fscanf(pFile," %c %x,%d",&identifier,&address,&size)>0) { if (identifier == 'L' || identifier == 'S'){ printf("identifier %c, addr:%x, size:%d \n",identifier,address,size); load_and_store(address,b,s,u_max,E,cache,&hits,&misses,&evictions); } else if (identifier == 'M') { printf("identifier %c, addr:%x, size:%d \n",identifier,address,size); load_and_store(address,b,s,u_max,E,cache,&hits,&misses,&evictions); load_and_store(address,b,s,u_max,E,cache,&hits,&misses,&evictions); } } fclose(pFile); printSummary(hits, misses, evictions);
for(int i = 0; i< S; i++) { free(cache[i]); } free(cache); return 0; }
|