1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/stats.cpp

266 lines
7.6 KiB
C++
Raw Permalink Normal View History

/** Statistic collection.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <cstring>
2019-06-23 13:35:42 +02:00
#include <villas/stats.hpp>
2019-04-07 15:13:40 +02:00
#include <villas/hist.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/timing.h>
#include <villas/node.h>
#include <villas/utils.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/log.h>
#include <villas/node.h>
2019-04-07 15:13:40 +02:00
#include <villas/table.hpp>
2019-06-04 16:55:38 +02:00
using namespace villas;
using namespace villas::utils;
2019-06-23 13:35:42 +02:00
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::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 packtes lost" }},
{ Stats::Metric::RTP_JITTER, { "rtp.jitter", "seconds", "Interarrival jitter" }},
};
2019-06-23 13:35:42 +02:00
std::unordered_map<Stats::Type, Stats::TypeDescription> Stats::types = {
2019-06-23 16:13:23 +02:00
{ 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 }}
};
2019-06-23 13:35:42 +02:00
std::vector<TableColumn> Stats::columns = {
{ 10, TableColumn::Alignment::LEFT, "Node", "%s" },
{ 10, TableColumn::Alignment::RIGHT, "Recv", "%ju", "pkts" },
{ 10, TableColumn::Alignment::RIGHT, "Sent", "%ju", "pkts" },
{ 10, TableColumn::Alignment::RIGHT, "Drop", "%ju", "pkts" },
{ 10, TableColumn::Alignment::RIGHT, "Skip", "%ju", "pkts" },
{ 10, TableColumn::Alignment::RIGHT, "OWD last", "%lf", "secs" },
{ 10, TableColumn::Alignment::RIGHT, "OWD mean", "%lf", "secs" },
{ 10, TableColumn::Alignment::RIGHT, "Rate last", "%lf", "pkt/sec" },
{ 10, TableColumn::Alignment::RIGHT, "Rate mean", "%lf", "pkt/sec" },
{ 10, TableColumn::Alignment::RIGHT, "Age mean", "%lf", "secs" },
{ 10, TableColumn::Alignment::RIGHT, "Age Max", "%lf", "sec" }
};
enum Stats::Format Stats::lookupFormat(const std::string &str)
2017-07-28 18:11:52 +02:00
{
2019-06-23 13:35:42 +02:00
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");
2017-07-28 18:11:52 +02:00
}
2019-06-23 13:35:42 +02:00
enum Stats::Metric Stats::lookupMetric(const std::string &str)
{
2019-06-23 13:35:42 +02:00
for (auto m : metrics) {
if (str == m.second.name)
return m.first;
}
throw std::invalid_argument("Invalid stats metric");
}
2019-06-23 13:35:42 +02:00
enum Stats::Type Stats::lookupType(const std::string &str)
{
2019-06-23 13:35:42 +02:00
for (auto t : types) {
if (str == t.second.name)
return t.first;
}
throw std::invalid_argument("Invalid stats type");
}
2019-06-23 13:35:42 +02:00
Stats::Stats(int buckets, int warmup)
{
for (auto m : metrics) {
histograms.emplace(
std::piecewise_construct,
std::forward_as_tuple(m.first),
std::forward_as_tuple(buckets, warmup)
);
}
}
2019-06-23 13:35:42 +02:00
void Stats::update(enum Metric m, double val)
{
2019-06-23 13:35:42 +02:00
histograms[m].put(val);
}
2019-06-23 13:35:42 +02:00
void Stats::reset()
{
2019-06-23 13:35:42 +02:00
for (auto m : metrics)
histograms[m.first].reset();
}
2019-06-23 13:35:42 +02:00
json_t * Stats::toJson() const
{
json_t *obj = json_object();
2019-06-23 13:35:42 +02:00
for (auto m : metrics) {
const Hist &h = histograms.at(m.first);
2019-06-23 13:35:42 +02:00
json_object_set_new(obj, m.second.name, h.toJson());
}
return obj;
}
2019-06-23 13:35:42 +02:00
void Stats::printHeader(enum Format fmt)
{
switch (fmt) {
2019-06-23 13:35:42 +02:00
case Format::HUMAN:
setupTable();
table->header();
break;
default: { }
}
}
2019-06-23 13:35:42 +02:00
void Stats::setupTable()
{
2019-06-23 13:35:42 +02:00
if (!table)
table = std::make_shared<Table>(columns);
}
2019-03-26 15:31:27 +01:00
2020-08-25 21:00:52 +02:00
void Stats::printPeriodic(FILE *f, enum Format fmt, struct vnode *n) const
2019-06-23 13:35:42 +02:00
{
switch (fmt) {
2019-06-23 13:35:42 +02:00
case Format::HUMAN:
setupTable();
table->row(11,
node_name_short(n),
2019-06-23 13:35:42 +02:00
(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()
);
break;
2019-06-23 13:35:42 +02:00
case Format::JSON: {
2019-04-07 11:26:10 +02:00
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 }",
"node", node_name(n),
2019-06-23 13:35:42 +02:00
"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()
2019-04-07 11:26:10 +02:00
);
json_dumpf(json_stats, f, 0);
break;
}
default: { }
}
}
2019-06-23 13:35:42 +02:00
void Stats::print(FILE *f, enum Format fmt, int verbose) const
{
switch (fmt) {
2019-06-23 13:35:42 +02:00
case Format::HUMAN:
for (auto m : metrics) {
info("%s: %s", m.second.name, m.second.desc);
histograms.at(m.first).print(verbose);
}
break;
2019-06-23 13:35:42 +02:00
case Format::JSON:
json_dumpf(toJson(), f, 0);
fflush(f);
break;
default: { }
}
}
2019-06-23 13:35:42 +02:00
union signal_data Stats::getValue(enum Metric sm, enum Type st) const
2017-03-29 04:20:26 +02:00
{
2019-06-23 13:35:42 +02:00
const Hist &h = histograms.at(sm);
union signal_data d;
switch (st) {
2019-06-23 13:35:42 +02:00
case Type::TOTAL:
2019-06-04 16:55:38 +02:00
d.i = h.getTotal();
break;
2019-06-23 13:35:42 +02:00
case Type::LAST:
2019-06-04 16:55:38 +02:00
d.f = h.getLast();
break;
2019-06-23 13:35:42 +02:00
case Type::HIGHEST:
2019-06-04 16:55:38 +02:00
d.f = h.getHighest();
break;
2019-06-23 13:35:42 +02:00
case Type::LOWEST:
2019-06-04 16:55:38 +02:00
d.f = h.getLowest();
break;
2019-06-23 13:35:42 +02:00
case Type::MEAN:
2019-06-04 16:55:38 +02:00
d.f = h.getMean();
break;
2019-06-23 13:35:42 +02:00
case Type::STDDEV:
2019-06-04 16:55:38 +02:00
d.f = h.getStddev();
break;
2019-06-23 13:35:42 +02:00
case Type::VAR:
2019-06-04 16:55:38 +02:00
d.f = h.getVar();
break;
default:
d.f = -1;
2017-03-29 04:20:26 +02:00
}
return d;
}
2019-06-23 13:35:42 +02:00
const Hist & Stats::getHistogram(enum Metric sm) const
{
return histograms.at(sm);
}
std::shared_ptr<Table> Stats::table = std::shared_ptr<Table>();