-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCPU.cpp
More file actions
203 lines (162 loc) · 6.03 KB
/
SimpleCPU.cpp
File metadata and controls
203 lines (162 loc) · 6.03 KB
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
#include <iostream>
#include <vector>
#include "SimpleCPU.h"
SimpleCPU::SimpleCPU(const std::string& trace_file_0, const std::string& trace_file_1, const CacheConfig& L1_config, const CacheConfig& L2_config, unsigned int instruction_latency, unsigned int L1_latency, unsigned int L2_latency, unsigned int dram_latency)
: replayer_0(trace_file_0), replayer_1(trace_file_1), instruction_latency(instruction_latency),
cache_controller(L1_config, L2_config, L1_latency, L2_latency, dram_latency)
{
instruction_count[0] = 0;
instruction_count[1] = 0;
mem_accesses[0] = 0;
mem_accesses[1] = 0;
cycles[0] = 0;
cycles[1] = 0;
}
void SimpleCPU::run(void)
{
int64_t delay = 0;
record_type record_0;
record_type record_1;
std::vector<uint64_t>::iterator it_0, it_end_0, it_1, it_end_1;
if (replayer_0)
{
record_0 = replayer_0.next();
delay += record_0.first * instruction_latency;
cycles[0] += record_0.first * instruction_latency;
instruction_count[0] += record_0.first;
it_0 = record_0.second.begin();
it_end_0 = record_0.second.end();
}
if (replayer_1)
{
record_1 = replayer_1.next();
delay -= record_1.first * instruction_latency;
cycles[1] += record_1.first * instruction_latency;
instruction_count[1] += record_1.first;
it_1 = record_1.second.begin();
it_end_1 = record_1.second.end();
}
bool core_0_completed = false;
bool core_1_completed = false;
while (!core_0_completed && !core_1_completed)
{
//WARNING: For now, in case of tie, core 0 always goes first
while (it_0 != it_end_0 && delay <= 0)
{
const unsigned latency = cache_controller.access(*it_0, 0);
delay += latency;
cycles[0] += latency;
++it_0;
++mem_accesses[0];
}
//Check if we exited because we consumed all the mem accesses in the current record. Reload a new one,
//or exit the infinite loop if there is no more record for this core
if (it_0 == it_end_0)
{
if (replayer_0)
{
record_0 = replayer_0.next();
delay += record_0.first * instruction_latency;
cycles[0] += record_0.first * instruction_latency;
instruction_count[0] += record_0.first;
it_0 = record_0.second.begin();
it_end_0 = record_0.second.end();
}
else
core_0_completed = true;
}
while (it_1 != it_end_1 && delay > 0)
{
const unsigned latency = cache_controller.access(*it_1, 1);
delay -= latency;
cycles[1] += latency;
++it_1;
++mem_accesses[1];
}
if (it_1 == it_end_1)
{
if (replayer_1)
{
record_1 = replayer_1.next();
delay -= record_1.first * instruction_latency;
cycles[1] += record_1.first * instruction_latency;
instruction_count[1] += record_1.first;
it_1 = record_1.second.begin();
it_end_1 = record_1.second.end();
}
else
core_1_completed = true;
}
}
//Finish processing the remaining instructions, if any
if (!core_0_completed)
{
//Finish processing the current memory instructions, if any, before treating the remaining records
while (it_0 != it_end_0)
{
const unsigned latency = cache_controller.access(*it_0, 0);
cycles[0] += latency;
++it_0;
++mem_accesses[0];
}
while (replayer_0)
{
record_0 = replayer_0.next();
cycles[0] += record_0.first * instruction_latency;
instruction_count[0] += record_0.first;
for (it_0 = record_0.second.begin(); it_0 != record_0.second.end(); ++it_0)
{
const unsigned latency = cache_controller.access(*it_0, 0);
cycles[0] += latency;
++mem_accesses[0];
}
}
}
if (!core_1_completed)
{
//Finish processing the current memory instructions, if any, before treating the remaining records
while (it_1 != it_end_1)
{
const unsigned latency = cache_controller.access(*it_1, 1);
cycles[1] += latency;
++it_1;
++mem_accesses[1];
}
while (replayer_1)
{
record_1 = replayer_1.next();
cycles[1] += record_1.first * instruction_latency;
instruction_count[1] += record_1.first;
for (it_1 = record_1.second.begin(); it_1 != record_1.second.end(); ++it_1)
{
const unsigned latency = cache_controller.access(*it_1, 1);
cycles[1] += latency;
++mem_accesses[1];
}
}
}
print_statistics();
}
void SimpleCPU::print_statistics(std::ostream& os)
{
auto L1_stats = cache_controller.get_L1_stats();
auto L2_stats = cache_controller.get_L2_stats();
os << "==Core 0==" << std::endl;
os << "Instructions: " << instruction_count[0] << std::endl;
os << "Memory accesses: " << mem_accesses[0] << std::endl;
os << "Cycles: " << cycles[0] << " (CPI: " << cycles[0] / (float) instruction_count[0] << ')' << std::endl;
os << "L1 stats:" << std::endl;
print_cache_stats(L1_stats[0], os);
os << "L2 stats:" << std::endl;
print_cache_stats(L2_stats[0], os);
os << std::endl;
os << "==Core 1==" << std::endl;
os << "Instructions: " << instruction_count[1] << std::endl;
os << "Memory accesses: " << mem_accesses[1] << std::endl;
os << "Cycles: " << cycles[1] << " (CPI: " << cycles[1] / (float) instruction_count[1] << ')' << std::endl;
os << "L1 stats:" << std::endl;
print_cache_stats(L1_stats[1], os);
os << "L2 stats:" << std::endl;
print_cache_stats(L2_stats[1], os);
os << std::endl;
}