-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstats.cpp
More file actions
247 lines (208 loc) · 7.42 KB
/
stats.cpp
File metadata and controls
247 lines (208 loc) · 7.42 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
/* Statistic collection.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <villas/hist.hpp>
#include <villas/node.hpp>
#include <villas/stats.hpp>
#include <villas/timing.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
using namespace villas::utils;
std::unordered_map<Stats::Metric, Stats::MetricDescription> Stats::metrics = {
{Stats::Metric::SMPS_SKIPPED,
{"skipped", "samples", "Skipped samples and the distance between them"}},
{Stats::Metric::SMPS_REORDERED,
{"reordered", "samples",
"Reordered samples and the distance between them"}},
{Stats::Metric::GAP_SAMPLE,
{"gap_sent", "seconds", "Inter-message timestamps (as sent by remote)"}},
{Stats::Metric::GAP_RECEIVED,
{"gap_received", "seconds",
"Inter-message arrival time (as received by this instance)"}},
{Stats::Metric::OWD,
{"owd", "seconds", "One-way-delay (OWD) of received messages"}},
{Stats::Metric::AGE,
{"age", "seconds",
"Processing time of packets within the from receive to sent"}},
{Stats::Metric::SIGNAL_COUNT,
{"signal_cnt", "signals", "Number of signals per sample"}},
{Stats::Metric::RTP_LOSS_FRACTION,
{"rtp.loss_fraction", "percent", "Fraction lost since last RTP SR/RR."}},
{Stats::Metric::RTP_PKTS_LOST,
{"rtp.pkts_lost", "packets", "Cumulative number of packets lost"}},
{Stats::Metric::RTP_JITTER,
{"rtp.jitter", "seconds", "Interarrival jitter"}},
};
std::unordered_map<Stats::Type, Stats::TypeDescription> Stats::types = {
{Stats::Type::LAST, {"last", SignalType::FLOAT}},
{Stats::Type::HIGHEST, {"highest", SignalType::FLOAT}},
{Stats::Type::LOWEST, {"lowest", SignalType::FLOAT}},
{Stats::Type::MEAN, {"mean", SignalType::FLOAT}},
{Stats::Type::VAR, {"var", SignalType::FLOAT}},
{Stats::Type::STDDEV, {"stddev", SignalType::FLOAT}},
{Stats::Type::TOTAL, {"total", SignalType::INTEGER}}};
std::vector<TableColumn> Stats::columns = {
{10, -1, TableColumn::Alignment::LEFT, "Node", "s"},
{10, -1, TableColumn::Alignment::RIGHT, "Recv", "d", "pkts"},
{10, -1, TableColumn::Alignment::RIGHT, "Sent", "d", "pkts"},
{10, -1, TableColumn::Alignment::RIGHT, "Drop", "d", "pkts"},
{10, -1, TableColumn::Alignment::RIGHT, "Skip", "d", "pkts"},
{10, -1, TableColumn::Alignment::RIGHT, "OWD last", "f", "secs"},
{10, -1, TableColumn::Alignment::RIGHT, "OWD mean", "f", "secs"},
{10, -1, TableColumn::Alignment::RIGHT, "Rate last", "f", "pkt/sec"},
{10, -1, TableColumn::Alignment::RIGHT, "Rate mean", "f", "pkt/sec"},
{10, -1, TableColumn::Alignment::RIGHT, "Age mean", "f", "secs"},
{10, -1, TableColumn::Alignment::RIGHT, "Age Max", "f", "sec"},
{-7, -1, TableColumn::Alignment::RIGHT, "Signals", "d", "signals"}};
enum Stats::Format Stats::lookupFormat(const std::string &str) {
if (str == "human")
return Format::HUMAN;
else if (str == "json")
return Format::JSON;
else if (str == "matlab")
return Format::MATLAB;
throw std::invalid_argument("Invalid format");
}
enum Stats::Metric Stats::lookupMetric(const std::string &str) {
for (auto m : metrics) {
if (str == m.second.name)
return m.first;
}
throw std::invalid_argument("Invalid stats metric");
}
enum Stats::Type Stats::lookupType(const std::string &str) {
for (auto t : types) {
if (str == t.second.name)
return t.first;
}
throw std::invalid_argument("Invalid stats type");
}
Stats::Stats(int buckets, int warmup) : logger(Log::get("stats")) {
for (auto m : metrics) {
histograms.emplace(std::piecewise_construct, std::forward_as_tuple(m.first),
std::forward_as_tuple(buckets, warmup));
}
}
void Stats::update(enum Metric m, double val) { histograms[m].put(val); }
void Stats::reset() {
for (auto m : metrics)
histograms[m.first].reset();
}
json_t *Stats::toJson() const {
json_t *obj = json_object();
for (auto m : metrics) {
const Hist &h = histograms.at(m.first);
json_object_set_new(obj, m.second.name, h.toJson());
}
return obj;
}
void Stats::printHeader(enum Format fmt) {
switch (fmt) {
case Format::HUMAN:
setupTable();
table->header();
break;
default: {
}
}
}
void Stats::setupTable() {
if (!table) {
auto logger = Log::get("stats");
table = std::make_shared<Table>(logger, columns);
}
}
void Stats::printPeriodic(FILE *f, enum Format fmt, node::Node *n) const {
switch (fmt) {
case Format::HUMAN:
setupTable();
table->row(n->getNameShort(),
(uintmax_t)histograms.at(Metric::OWD).getTotal(),
(uintmax_t)histograms.at(Metric::AGE).getTotal(),
(uintmax_t)histograms.at(Metric::SMPS_REORDERED).getTotal(),
(uintmax_t)histograms.at(Metric::SMPS_SKIPPED).getTotal(),
(double)histograms.at(Metric::OWD).getLast(),
(double)histograms.at(Metric::OWD).getMean(),
(double)1.0 / histograms.at(Metric::GAP_RECEIVED).getLast(),
(double)1.0 / histograms.at(Metric::GAP_RECEIVED).getMean(),
(double)histograms.at(Metric::AGE).getMean(),
(double)histograms.at(Metric::AGE).getHighest(),
(uintmax_t)histograms.at(Metric::SIGNAL_COUNT).getLast());
break;
case Format::JSON: {
json_t *json_stats = json_pack(
"{ s: s, s: i, s: i, s: i, s: i, s: f, s: f, s: f, s: f, s: f, s: f, "
"s: i }",
"node", n->getNameShort().c_str(), "recv",
histograms.at(Metric::OWD).getTotal(), "sent",
histograms.at(Metric::AGE).getTotal(), "dropped",
histograms.at(Metric::SMPS_REORDERED).getTotal(), "skipped",
histograms.at(Metric::SMPS_SKIPPED).getTotal(), "owd_last",
1.0 / histograms.at(Metric::OWD).getLast(), "owd_mean",
1.0 / histograms.at(Metric::OWD).getMean(), "rate_last",
1.0 / histograms.at(Metric::GAP_SAMPLE).getLast(), "rate_mean",
1.0 / histograms.at(Metric::GAP_SAMPLE).getMean(), "age_mean",
histograms.at(Metric::AGE).getMean(), "age_max",
histograms.at(Metric::AGE).getHighest(), "signals",
histograms.at(Metric::SIGNAL_COUNT).getLast());
json_dumpf(json_stats, f, 0);
break;
}
default: {
}
}
}
void Stats::print(FILE *f, enum Format fmt, int verbose) const {
switch (fmt) {
case Format::HUMAN:
for (auto m : metrics) {
logger->info("{}: {}", m.second.name, m.second.desc);
histograms.at(m.first).print(logger, verbose, " ");
}
break;
case Format::JSON:
json_dumpf(toJson(), f, 0);
fflush(f);
break;
default: {
}
}
}
union SignalData Stats::getValue(enum Metric sm, enum Type st) const {
const Hist &h = histograms.at(sm);
union SignalData d;
switch (st) {
case Type::TOTAL:
d.i = h.getTotal();
break;
case Type::LAST:
d.f = h.getLast();
break;
case Type::HIGHEST:
d.f = h.getHighest();
break;
case Type::LOWEST:
d.f = h.getLowest();
break;
case Type::MEAN:
d.f = h.getMean();
break;
case Type::STDDEV:
d.f = h.getStddev();
break;
case Type::VAR:
d.f = h.getVar();
break;
default:
d.f = -1;
}
return d;
}
const Hist &Stats::getHistogram(enum Metric sm) const {
return histograms.at(sm);
}
std::shared_ptr<Table> Stats::table = std::shared_ptr<Table>();