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
| #include <stdbool.h> #include <string.h> #include "cachelab.h" #include "getopt.h"
static int S; static int s; static int E; static int B; static int hits = 0; static int misses = 0; static int evictions = 0;
typedef struct _CacheLine { unsigned tag; struct _CacheLine *next; struct _CacheLine *prev; } CacheLine;
typedef struct _Cache { CacheLine *head; CacheLine *tail; int *size; } Cache;
static Cache *cache;
void parse_option(int argc, char **argv, char **fileName) { int option; while ((option = getopt(argc, argv, "s:E:b:t:")) != -1) { switch (option) { case 's': s = atoi(optarg); S = 1 << s; case 'E': E = atoi(optarg); case 'b': B = atoi(optarg); case 't': strcpy(*fileName, optarg); } } }
void initialize_cache() { cache = malloc(S * sizeof(*cache)); for (int i = 0; i < S; i++) { cache[i].head = malloc(sizeof(CacheLine)); cache[i].tail = malloc(sizeof(CacheLine));
cache[i].head->next = cache[i].tail; cache[i].tail->prev = cache[i].head; (cache[i].size) = (int *)malloc(sizeof(int)); *(cache[i].size) = 0; } }
void insert_first_line(CacheLine *node, Cache *curLru) { node->next = curLru->head->next; node->prev = curLru->head;
curLru->head->next->prev = node; curLru->head->next = node;
*(curLru->size) = *(curLru->size) + 1; }
void evict(CacheLine *nodeToDel, Cache *curLru) { nodeToDel->next->prev = nodeToDel->prev; nodeToDel->prev->next = nodeToDel->next; *(curLru->size) = *(curLru->size) - 1; }
void update(unsigned address) { unsigned int mask = 0xFFFFFFFF; unsigned int maskSet = mask >> (32 - s); unsigned int targetSet = ((maskSet) & (address >> B)); unsigned int targetTag = address >> (s + B);
Cache curLru = cache[targetSet];
CacheLine *cur = curLru.head->next; bool found = 0; while (cur != curLru.tail) { if (cur->tag == targetTag) { found = true; break; } cur = cur->next; }
if (found) { hits++; evict(cur, &curLru); insert_first_line(cur, &curLru); printf("> hit!, set: %d \n", targetSet); } else { CacheLine *newNode = malloc(sizeof(CacheLine)); newNode->tag = targetTag; if (*(curLru.size) == E) { evict(curLru.tail->prev, &curLru); insert_first_line(newNode, &curLru); evictions++; misses++; printf("> evic && miss set:%d\n", targetSet); } else { misses++; insert_first_line(newNode, &curLru); printf("> miss %d\n", targetSet); } } }
void cache_simulate(char *fileName) { initialize_cache();
FILE *file = fopen(fileName, "r"); char op; unsigned int address; int size;
while (fscanf(file, " %c %x,%d", &op, &address, &size) > 0) { printf("%c, %x %d\n", op, address, size); switch (op) { case 'L': update(address); break; case 'M': update(address); case 'S': update(address); break; } } }
int main(int argc, char **argv) { char *fileName = malloc(100 * sizeof(char));
parse_option(argc, argv, &fileName); cache_simulate(fileName); printSummary(hits, misses, evictions); return 0; }
|
评论