-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.cpp
More file actions
299 lines (232 loc) · 7.29 KB
/
stats.cpp
File metadata and controls
299 lines (232 loc) · 7.29 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <functional>
#include <iomanip>
#include <sstream>
#include <boost/io/ios_state.hpp>
#include <fmt/format.h>
#include "log.hpp"
#include "stats.hpp"
#include "throw-with-trace.hpp"
#define WIN_SIZE 7
namespace acc = boost::accumulators;
using fmt::literals::operator""_format;
Stats::Stats(const std::vector<std::string> &counters)
{
init(counters);
}
void Stats::init_derived_metrics_total(const std::vector<std::string> &counters)
{
bool instructions = std::find(counters.begin(), counters.end(), "instructions") != counters.end();
bool cycles = std::find(counters.begin(), counters.end(), "cycles") != counters.end();
bool ref_cycles = std::find(counters.begin(), counters.end(), "ref-cycles") != counters.end();
if (instructions && cycles)
{
derived_metrics_total.push_back(std::make_pair("ipc", [this]()
{
double inst = acc::sum(this->events.at("instructions"));
double cycl = acc::sum(this->events.at("cycles"));
return inst / cycl;
}));
}
if (instructions && ref_cycles)
{
derived_metrics_total.push_back(std::make_pair("ref-ipc", [this]()
{
double inst = acc::sum(this->events.at("instructions"));
double ref_cycl = acc::sum(this->events.at("ref-cycles"));
return inst / ref_cycl;
}));
}
}
void Stats::init_derived_metrics_int(const std::vector<std::string> &counters)
{
bool instructions = std::find(counters.begin(), counters.end(), "instructions") != counters.end();
bool cycles = std::find(counters.begin(), counters.end(), "cycles") != counters.end();
bool ref_cycles = std::find(counters.begin(), counters.end(), "ref-cycles") != counters.end();
if (instructions && cycles)
{
derived_metrics_int.push_back(std::make_pair("ipc", [this]()
{
double inst = this->get_interval("instructions");
double cycl = this->get_interval("cycles");
return inst / cycl;
}));
}
if (instructions && ref_cycles)
{
derived_metrics_int.push_back(std::make_pair("ref-ipc", [this]()
{
double inst = this->get_interval("instructions");
double ref_cycl = this->get_interval("ref-cycles");
return inst / ref_cycl;
}));
}
}
void Stats::init(const std::vector<std::string> &counters)
{
assert(!initialized);
for (const auto &c : counters)
events.insert(std::make_pair(c, accum_t(acc::tag::rolling_window::window_size = WIN_SIZE)));
init_derived_metrics_int(counters);
init_derived_metrics_total(counters);
// Test der metrics
if (derived_metrics_int.size() != derived_metrics_total.size())
throw_with_trace(std::runtime_error("Different number of derived metrics for int ({}) and total ({})"_format(
derived_metrics_int.size(), derived_metrics_total.size())));
for(const auto &der : derived_metrics_int)
{
auto it = std::find_if(derived_metrics_total.begin(), derived_metrics_total.end(),
[&der](const auto& tuple) {return tuple.first == der.first;});
if (it == derived_metrics_total.end())
throw_with_trace(std::runtime_error("Different derived metrics for int and total results"));
}
for (const auto &der : derived_metrics_int)
events.insert(std::make_pair(der.first, accum_t(acc::tag::rolling_window::window_size = WIN_SIZE)));
// Store the names of the counters
names = counters;
initialized = true;
}
Stats& Stats::accum(const counters_t &counters)
{
assert(initialized);
last = curr;
curr = counters;
const auto &curr_id_idx = curr.get<by_id>();
const auto &last_id_idx = last.get<by_id>();
assert(!curr.empty());
// App has just started, no last data
if (last.empty())
{
auto it = curr_id_idx.cbegin();
while (it != curr_id_idx.cend())
{
events.at(it->name)(it->value);
it++;
}
}
// We have data from the last interval
else
{
assert(curr.size() == last.size());
auto curr_it = curr_id_idx.cbegin();
auto last_it = last_id_idx.cbegin();
while (curr_it != curr_id_idx.cend() && last_it != last_id_idx.cend())
{
const Counter &c = *curr_it;
const Counter &l = *last_it;
assert(c.id == l.id);
assert(c.name == l.name);
double value = c.snapshot ?
c.value :
c.value - l.value;
if (value < 0)
LOGERR("Negative interval value ({}) for the counter '{}'"_format(value, c.name));
events.at(c.name)(value);
curr_it++;
last_it++;
}
}
// Compute and add derived metrics
for (const auto &der : derived_metrics_int)
events.at(der.first)(der.second());
counter++;
return *this;
}
std::string Stats::header_to_string(const std::string &sep) const
{
if (!names.size()) return "";
std::stringstream ss;
auto it = names.begin();
ss << *it;
it++;
for (; it != names.end(); it++)
ss << sep << *it;
for (const auto &der : derived_metrics_int) // Int, snapshot and total have the same derived metrics
ss << sep << der.first;
return ss.str();
}
// Some of the counters are collected as snapshots of the state of the system (i.e. the cache space occupation).
// This is taken into account to compute the value of the metric for the interval. If force_snapshot is true, then
// the function prints the value of the counter. If not, it prints the difference with the previous interval, unless
// the metric is collected as an snapshot.
std::string Stats::data_to_string_total(const std::string &sep) const
{
std::stringstream ss;
assert(curr.size() > 0);
const auto &name_index = curr.get<by_name>();
for (size_t i = 0; i < names.size(); i++)
{
const auto &name = names[i];
const accum_t &event = events.at(name);
const auto &c = name_index.find(name);
double value = c->snapshot ?
acc::mean(event) :
acc::sum(event);
ss << value;
if (i < names.size() - 1)
ss << sep;
}
// Derived metrics
for (auto it = derived_metrics_total.cbegin(); it != derived_metrics_total.cend(); it++)
{
double value = it->second();
ss << sep << value;
}
return ss.str();
}
std::string Stats::data_to_string_int(const std::string &sep) const
{
std::stringstream ss;
assert(curr.size() > 0);
const auto &curr_id_idx = curr.get<by_id>();
auto curr_it = curr_id_idx.cbegin();
while (curr_it != curr_id_idx.cend())
{
ss << acc::last(events.at(curr_it->name));
curr_it++;
if (curr_it != curr.cend())
ss << sep;
}
// Derived metrics
for (auto it = derived_metrics_int.cbegin(); it != derived_metrics_int.cend(); it++)
{
double value = it->second();
ss << sep << value;
}
return ss.str();
}
double Stats::get_interval(const std::string &name) const
{
const auto &curr_index = curr.get<by_name>();
const auto &last_index = last.get<by_name>();
const auto c = curr_index.find(name);
const auto l = last_index.find(name);
if (last.size() != curr.size() && last.size() != 0)
throw_with_trace(std::runtime_error("Inconsistency between current data and last interval data"));
if (c == curr_index.end())
throw_with_trace(std::runtime_error("Missing current data"));
double value = c->snapshot || last.size() == 0 ?
c->value :
c->value - l->value;
return value;
}
double Stats::get_current(const std::string &name) const
{
const auto &curr_index = curr.get<by_name>();
auto it = curr_index.find(name);
if (it == curr_index.end())
throw_with_trace(std::runtime_error("Event not monitorized '{}'"_format(name)));
return it->value;
}
double Stats::sum(const std::string &name) const
{
return acc::sum(events.at(name));
}
const counters_t& Stats::get_current_counters() const
{
return curr;
}
void Stats::reset_counters()
{
last = counters_t();
curr = counters_t();
}