2019-03-26 15:33:47 +01:00
|
|
|
/* Statistic hooks.
|
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2019-03-26 15:33:47 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
#include <memory>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/common.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
#include <villas/hook.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/node.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
#include <villas/node/exceptions.hpp>
|
2019-06-23 13:35:42 +02:00
|
|
|
#include <villas/stats.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/timing.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2019-04-06 13:55:08 +02:00
|
|
|
class StatsHook;
|
|
|
|
|
|
|
|
class StatsWriteHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
StatsHook *parent;
|
2019-04-06 13:55:08 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
StatsWriteHook(StatsHook *pa, Path *p, Node *n, int fl, int prio,
|
|
|
|
bool en = true)
|
|
|
|
: Hook(p, n, fl, prio, en), parent(pa) {
|
|
|
|
// This hook has no config. We never call parse() for it
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Hook::Reason process(struct Sample *smp);
|
2019-04-06 13:55:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class StatsReadHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
struct Sample *last;
|
2019-04-06 13:55:08 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
StatsHook *parent;
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2019-04-06 13:55:08 +02:00
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
StatsReadHook(StatsHook *pa, Path *p, Node *n, int fl, int prio,
|
|
|
|
bool en = true)
|
|
|
|
: Hook(p, n, fl, prio, en), last(nullptr), parent(pa) {
|
|
|
|
// This hook has no config. We never call parse() for it
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void start() {
|
|
|
|
assert(state == State::PREPARED);
|
|
|
|
|
|
|
|
last = nullptr;
|
|
|
|
|
|
|
|
state = State::STARTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void stop() {
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
sample_decref(last);
|
|
|
|
|
|
|
|
state = State::STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Hook::Reason process(struct Sample *smp);
|
2019-04-06 13:55:08 +02:00
|
|
|
};
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
class StatsHook : public Hook {
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
friend StatsReadHook;
|
|
|
|
friend StatsWriteHook;
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
std::shared_ptr<StatsReadHook> readHook;
|
|
|
|
std::shared_ptr<StatsWriteHook> writeHook;
|
2019-04-06 13:55:08 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
enum Stats::Format format;
|
|
|
|
int verbose;
|
|
|
|
int warmup;
|
|
|
|
int buckets;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::shared_ptr<Stats> stats;
|
2019-06-26 20:18:02 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
FILE *output;
|
|
|
|
std::string uri;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
StatsHook(Path *p, Node *n, int fl, int prio, bool en = true)
|
|
|
|
: Hook(p, n, fl, prio, en), format(Stats::Format::HUMAN), verbose(0),
|
|
|
|
warmup(500), buckets(20), output(nullptr), uri() {
|
|
|
|
readHook = std::make_shared<StatsReadHook>(this, p, n, fl, prio, en);
|
|
|
|
writeHook = std::make_shared<StatsWriteHook>(this, p, n, fl, prio, en);
|
|
|
|
|
|
|
|
if (!readHook || !writeHook)
|
|
|
|
throw MemoryAllocationError();
|
|
|
|
|
|
|
|
// Add child hooks
|
|
|
|
if (node) {
|
|
|
|
node->in.hooks.push_back(readHook);
|
|
|
|
node->out.hooks.push_back(writeHook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StatsHook &operator=(const StatsHook &) = delete;
|
|
|
|
StatsHook(const StatsHook &) = delete;
|
|
|
|
|
|
|
|
virtual void start() {
|
|
|
|
assert(state == State::PREPARED);
|
|
|
|
|
|
|
|
if (!uri.empty()) {
|
|
|
|
output = fopen(uri.c_str(), "w+");
|
|
|
|
if (!output)
|
|
|
|
throw RuntimeError("Failed to open file '{}' for writing", uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
state = State::STARTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void stop() {
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
stats->print(uri.empty() ? stdout : output, format, verbose);
|
|
|
|
|
|
|
|
if (!uri.empty())
|
|
|
|
fclose(output);
|
|
|
|
|
|
|
|
state = State::STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void restart() {
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
stats->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Hook::Reason process(struct Sample *smp) {
|
|
|
|
// Only call readHook if it hasnt been added to the node's hook list
|
|
|
|
if (!node)
|
|
|
|
return readHook->process(smp);
|
|
|
|
|
|
|
|
return Hook::Reason::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void periodic() {
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
stats->printPeriodic(uri.empty() ? stdout : output, format, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void parse(json_t *json) {
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
assert(state != State::STARTED);
|
|
|
|
|
|
|
|
Hook::parse(json);
|
|
|
|
|
|
|
|
const char *f = nullptr;
|
|
|
|
const char *u = nullptr;
|
|
|
|
|
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: b, s?: i, s?: i, s?: s }",
|
|
|
|
"format", &f, "verbose", &verbose, "warmup", &warmup,
|
|
|
|
"buckets", &buckets, "output", &u);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-hook-stats");
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
try {
|
|
|
|
format = Stats::lookupFormat(f);
|
|
|
|
} catch (const std::invalid_argument &e) {
|
|
|
|
throw ConfigError(json, "node-config-hook-stats",
|
|
|
|
"Invalid statistic output format: {}", f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u)
|
|
|
|
uri = u;
|
|
|
|
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare() {
|
|
|
|
assert(state == State::CHECKED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
stats = std::make_shared<villas::Stats>(buckets, warmup);
|
2019-06-26 20:18:02 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (node)
|
|
|
|
node->setStats(stats);
|
2019-08-28 16:56:22 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
state = State::PREPARED;
|
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
};
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Hook::Reason StatsWriteHook::process(struct Sample *smp) {
|
|
|
|
timespec now = time_now();
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
parent->stats->update(Stats::Metric::AGE,
|
|
|
|
time_delta(&smp->ts.received, &now));
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return Reason::OK;
|
2019-10-29 20:19:01 +01:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Hook::Reason StatsReadHook::process(struct Sample *smp) {
|
|
|
|
if (last) {
|
|
|
|
if (smp->flags & last->flags & (int)SampleFlags::HAS_TS_RECEIVED)
|
|
|
|
parent->stats->update(Stats::Metric::GAP_RECEIVED,
|
|
|
|
time_delta(&last->ts.received, &smp->ts.received));
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (smp->flags & last->flags & (int)SampleFlags::HAS_TS_ORIGIN)
|
|
|
|
parent->stats->update(Stats::Metric::GAP_SAMPLE,
|
|
|
|
time_delta(&last->ts.origin, &smp->ts.origin));
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if ((smp->flags & (int)SampleFlags::HAS_TS_ORIGIN) &&
|
|
|
|
(smp->flags & (int)SampleFlags::HAS_TS_RECEIVED))
|
|
|
|
parent->stats->update(Stats::Metric::OWD,
|
|
|
|
time_delta(&smp->ts.origin, &smp->ts.received));
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (smp->flags & last->flags & (int)SampleFlags::HAS_SEQUENCE) {
|
|
|
|
int dist = smp->sequence - (int32_t)last->sequence;
|
|
|
|
if (dist != 1)
|
|
|
|
parent->stats->update(Stats::Metric::SMPS_REORDERED, dist);
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
parent->stats->update(Stats::Metric::SIGNAL_COUNT, smp->length);
|
2021-11-10 05:30:49 -05:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
sample_incref(smp);
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (last)
|
|
|
|
sample_decref(last);
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
last = smp;
|
2019-10-29 20:19:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return Reason::OK;
|
2019-10-29 20:19:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
// Register hook
|
2020-06-14 15:00:02 +02:00
|
|
|
static char n[] = "stats";
|
2021-04-28 13:15:01 +00:00
|
|
|
static char d[] = "Collect statistics for the current node";
|
2023-09-07 11:46:39 +02:00
|
|
|
static HookPlugin<StatsHook, n, d, (int)Hook::Flags::NODE_READ> p;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|