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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <math.h> #include <unistd.h>
#define print(x) if (infoflag) printf(x)
#include "cachelab.h" #include "assert.h"
void getaddrfromline(char* buff, char* result); long getlownnumber(long addr, long n);
int main(int argc, char* argv[]) { long hit_count = 0; long eviction_count = 0; long miss_count = 0;
long tagbits = 0; long setsbits = 0; long linesperset = 0; long blockbits = 0;
char* filename = NULL;
bool infoflag = false;
FILE* fp = NULL;
long max = 1;
for (long i=1; i<argc; i++){ size_t length = strlen(argv[i]); assert(length == 2); switch (argv[i][1]) { case 'v': infoflag = true; break; case 's': setsbits = atoi(argv[++i]); break; case 'E': linesperset = atoi(argv[++i]); break; case 'b': blockbits = atoi(argv[++i]); break; case 't': filename = argv[++i]; break;
default: fprintf(stderr, "Parameter error, you should use -v -s number -E number -b number -t filename"); return -1; }
}
tagbits = sizeof(void*) * 8 - setsbits - blockbits;
assert(tagbits > 0);
long count = (1 << setsbits) * linesperset;
long* tagptr = calloc(count, sizeof(long)); long* usedptr = calloc(count, sizeof(long));
if (usedptr == NULL || tagptr == NULL){ fprintf(stderr, "calloc error."); return -1; }
fp = fopen(filename, "r");
char buff[1024]; char number[1024]; long addr;
long set; long tag;
while (fgets(buff, 1024, fp) != NULL) { memset(number, 0, 1024);
if(*(buff) == 'I')continue;
if(buff[strlen(buff)-1] == '\n')buff[strlen(buff)-1] = '\0';
if(infoflag && buff[0] == ' ') { printf("%s ", buff + 1); } else if (infoflag) { printf("%s ", buff); }
getaddrfromline(buff, number); sscanf(number, "%lx", &addr);
tag = getlownnumber(addr >> (blockbits + setsbits), tagbits); set = getlownnumber(addr >> blockbits, setsbits);
long* nowptr = usedptr + set * linesperset;
long* this = nowptr;
bool allone = true; for (int i=0; i < linesperset; i++) { if (*this != 0) { if (*(tagptr + set * linesperset + i) == tag) { if (*(buff+1) == 'M') { print("hit hit \n"); hit_count++; } else { print("hit \n"); } (*this) = max++; hit_count++; goto ff; } } else allone = false; this++; }
long index = 0;
if (allone == false) { this = nowptr; for (int i=0; i < linesperset; i++) { if (*this == 0) { *(tagptr + set * linesperset+i) = tag; break; } this++; } *this = max++; if (*(buff+1) == 'M') { print("miss hit \n"); hit_count++; *this = max++; } else { print("miss \n"); } miss_count++; }
else { long min = 0x7FFFFFFFFFFFFFFF; this = nowptr; for (int i=0; i < linesperset; i++) { if (*this != 0 && ((*this) < min)) { min = *this; index = i; } this++; } *(tagptr + set *linesperset + index) = tag; *(nowptr + index) = max++; if (*(buff+1) == 'M') { print("miss eviction hit \n"); hit_count++; } else { print("miss eviction \n"); } miss_count++; eviction_count++; } ff:; }
printf("hits:%ld ", hit_count); printf("misses:%ld ", miss_count); printf("evictions:%ld\n", eviction_count); free(tagptr); free(usedptr); fclose(fp); return 0; }
void getaddrfromline(char* buff, char* result) { long start = 0; long end = 0;
for (long i=0; ; i++) { if ((buff[i] == ' ') && (i != 0) &&(buff[i+1] != ' ')) { start = i + 1; } else if (buff[i] == ',') { end = i; break; } } strncpy(result, buff + start, end - start); }
long getlownnumber(long addr, long n) { long x = 1; for (long i=1; i<n; i++) x = x | (x << 1); return x & addr; }
|