Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int hit_count, miss_count, eviction_count;
- long long usage;
- void csim(unsigned long long address, long long** cache, int s, int E, int b, int v)
- {
- int i;
- unsigned long long eff_address, cache_address, mask;
- long long lru;
- int ev_index; // eviction index
- lru = 0xFFFFFFFF;
- mask = (1 << s) - 1;
- eff_address = (address >> b) & mask; // внутри блока
- cache_address = address >> (b + s);
- for (i = 0; i < E; i++)
- {
- // проверка актуальности + адреса
- if (cache[eff_address][3 * i + 1] == cache_address && cache[eff_address][3 * i] != 0)
- {
- hit_count++;
- if (v == 1)
- printf("hit ");
- cache[eff_address][3 * i + 2] = usage;
- usage++;
- return;
- }
- }
- miss_count++;
- if (v == 1)
- printf("miss ");
- for (i = 0; i < E; i++)
- {
- if (cache[eff_address][3 * i + 2] < lru)
- {
- ev_index = i;
- lru = cache[eff_address][3 * i + 2];
- }
- }
- if (cache[eff_address][3 * ev_index] != 0)
- {
- eviction_count++;
- if (v == 1)
- printf("eviction ");
- }
- cache[eff_address][3 * ev_index] = 1; // актуальность
- cache[eff_address][3 * ev_index + 1] = cache_address; // метка
- cache[eff_address][3 * ev_index + 2] = usage; // значение
- usage++;
- return;
- }
- int main(int argc, char** argv)
- {
- int s, b, E, v;
- int i;
- char c;
- char buffer[BUFSIZE];
- char* trace_file;
- long long** cache;
- unsigned long long address;
- unsigned length;
- v = 0; // подробный вывод
- hit_count = eviction_count = miss_count = 0;
- s = b = E = 0;
- usage = 0;
- trace_file = NULL;
- while((c = getopt(argc, argv, "s:E:b:t:vh")) != -1)
- {
- if (c == 's')
- s = atoi(optarg);
- else if (c == 'E')
- E = atoi(optarg);
- else if (c == 'b')
- b = atoi(optarg);
- else if (c == 't')
- trace_file = optarg;
- else if (c == 'v')
- v = 1;
- else
- fprintf(stderr, "No such option.\n");
- }
- if (s == 0 || b == 0 || E == 0 || trace_file == NULL)
- {
- fprintf(stderr, "Not enough arguments.\n");
- return 0;
- }
- // создание пустого кэша
- int s2 = 1 << s;
- cache = malloc(s2 * sizeof(*cache));
- for (i = 0; i < s2; i++)
- cache[i] = calloc(3 * E, sizeof(*cache[i]));
- FILE* fp = fopen(trace_file, "r");
- if (fp == NULL)
- {
- fprintf(stderr, "No such file.\n");
- return 0;
- }
- // первый символ - Г - игнорируется
- while (fgets(buffer, BUFSIZE, fp))
- {
- if (buffer[1] == 'S' || buffer[1] == 'M' || buffer[1] == 'L')
- {
- sscanf(buffer+2, "%llx,%u", &address, &length);
- if (v == 1)
- printf("%c %llx,%u ", buffer[1], address, length);
- csim(address, cache, s, E, b, v);
- }
- if (buffer[1] == 'M')
- csim(address, cache, s, E, b, v);
- }
- fclose(fp);
- for (i = 0; i < s2; i++)
- free(cache[i]);
- free(cache);
- printSummary(hit_count, miss_count, eviction_count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement