-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.c
97 lines (77 loc) · 2.33 KB
/
simulator.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "simulator.h"
#include "print_helpers.h"
simulator_t *make_simulator() {
simulator_t *sim = malloc(sizeof(simulator_t));
sim->trace = "route.1t.short.txt";
sim->verbose_f = false;
sim->limit_insn_f = false;
sim->insn_limit = 0;
sim->n_core = 1;
sim->protocol = NONE;
sim->lru_on_invalidate_f = false;
return sim;
}
/*
* Goes through the trace line by line (i.e., instruction by
* instruction) and simulates the program being executed on a
* multicore processor.
*/
void process_trace(simulator_t *sim) {
int i;
char *line = NULL;
// Program Stats
long total_insn = 0;
printf("Processing trace...\n");
char *path = malloc(strlen(sim->trace) + 7);
strncpy(path, "trace/", 7);
strcat(path, sim->trace);
FILE *trace = fopen(path, "r");
if (trace == NULL) {
printf("File \'%s\' not found\n", sim->trace);
exit(EXIT_FAILURE);
}
size_t len = 0;
size_t read;
while ((read = getline(&line, &len, trace)) != -1) {
if (sim->limit_insn_f && total_insn == sim->insn_limit) {
printf("Reached insn limit of %d. Ending Simulation...\n",
sim->insn_limit);
break;
}
int core = line[0]-'0'; // turn the '0' into a 0
if (core > (sim->n_core - 1)) {
printf("ERROR: this trace requires more cores!\n");
exit(EXIT_FAILURE);
}
enum action_t action = (line[2] == 'r') ? LOAD : STORE;
unsigned long address = strtol(&line[4], NULL, 16);
total_insn++;
// access the cache
bool hit_f = access_cache(sim->cache[core], address, action);
// prints the insn
if (sim->verbose_f) print_insn_info(sim, core, line[2], address, hit_f);
// misses go on the bus
// (LOAD --> LD_MISS, STORE --> ST_MISS)
if (!hit_f) {
for (i = 0; i < sim->n_core; i++){ // 1 core? does nothing
if (i != core) {
access_cache(sim->cache[i], address,
(action == LOAD) ? LD_MISS : ST_MISS);
}
}
}
}
fclose(trace);
if (line) free(line);
printf("Processed %ld lines.\n", total_insn);
// compute cache statistics
for (i = 0; i < sim->n_core; i++){
calculate_stat_rates(sim->cache[i]->stats, sim->cache[i]->block_size);
printf(" *** Results for Core %d ***\n", i);
print_stats(sim->cache[i]->stats, i);
}
}