2019-03-26 15:33:47 +01:00
|
|
|
/** Statistic hooks.
|
|
|
|
*
|
|
|
|
* @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
|
2019-03-26 15:33:47 +01: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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <villas/node/exceptions.hpp>
|
2019-06-23 13:35:42 +02:00
|
|
|
#include <villas/stats.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2019-04-06 13:55:08 +02:00
|
|
|
class StatsHook;
|
|
|
|
|
|
|
|
class StatsWriteHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
StatsHook *parent;
|
|
|
|
|
|
|
|
public:
|
2020-08-25 21:00:52 +02:00
|
|
|
StatsWriteHook(StatsHook *pa, struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
|
2019-10-29 20:19:01 +01:00
|
|
|
Hook(p, n, fl, prio, en),
|
|
|
|
parent(pa)
|
2019-04-06 13:55:08 +02:00
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::CHECKED;
|
2019-04-06 13:55:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 10:45:12 -04:00
|
|
|
virtual Hook::Reason process(struct sample *smp);
|
2019-04-06 13:55:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class StatsReadHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
sample *last;
|
|
|
|
|
2019-10-29 20:19:01 +01:00
|
|
|
StatsHook *parent;
|
|
|
|
|
2019-04-06 13:55:08 +02:00
|
|
|
public:
|
2020-08-25 21:00:52 +02:00
|
|
|
StatsReadHook(StatsHook *pa, struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
|
2019-10-29 20:19:01 +01:00
|
|
|
Hook(p, n, fl, prio, en),
|
2020-09-11 14:57:05 +02:00
|
|
|
last(nullptr),
|
2019-10-29 20:19:01 +01:00
|
|
|
parent(pa)
|
2019-04-06 13:55:08 +02:00
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::CHECKED;
|
2019-04-06 13:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void start()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::PREPARED);
|
2019-04-06 13:55:08 +02:00
|
|
|
|
|
|
|
last = nullptr;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STARTED;
|
2019-04-06 13:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void stop()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::STARTED);
|
2019-04-06 13:55:08 +02:00
|
|
|
|
|
|
|
if (last)
|
|
|
|
sample_decref(last);
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STOPPED;
|
2019-04-06 13:55:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 10:45:12 -04:00
|
|
|
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 {
|
|
|
|
|
2019-10-29 20:19:01 +01:00
|
|
|
friend StatsReadHook;
|
|
|
|
friend StatsWriteHook;
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
protected:
|
2020-09-13 08:42:30 +02:00
|
|
|
StatsReadHook *readHook;
|
|
|
|
StatsWriteHook *writeHook;
|
2019-04-06 13:55:08 +02:00
|
|
|
|
2019-06-23 13:35:42 +02:00
|
|
|
enum Stats::Format format;
|
2019-03-26 15:33:47 +01:00
|
|
|
int verbose;
|
|
|
|
int warmup;
|
|
|
|
int buckets;
|
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
std::shared_ptr<Stats> stats;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
FILE *output;
|
2019-06-26 20:18:02 +02:00
|
|
|
std::string uri;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
StatsHook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook(p, n, fl, prio, en),
|
2019-06-23 13:35:42 +02:00
|
|
|
format(Stats::Format::HUMAN),
|
2019-03-26 15:33:47 +01:00
|
|
|
verbose(0),
|
|
|
|
warmup(500),
|
|
|
|
buckets(20),
|
|
|
|
output(nullptr),
|
2019-06-26 20:18:02 +02:00
|
|
|
uri()
|
2019-03-26 15:33:47 +01:00
|
|
|
{
|
2020-09-13 08:42:30 +02:00
|
|
|
readHook = new StatsReadHook(this, p, n, fl, prio, en);
|
|
|
|
writeHook = new StatsWriteHook(this, p, n, fl, prio, en);
|
|
|
|
|
|
|
|
if (!readHook || !writeHook)
|
|
|
|
throw MemoryAllocationError();
|
|
|
|
|
2019-04-06 13:55:08 +02:00
|
|
|
/* Add child hooks */
|
2019-10-29 20:19:01 +01:00
|
|
|
if (node) {
|
2020-09-13 08:42:30 +02:00
|
|
|
vlist_push(&node->in.hooks, (void *) readHook);
|
|
|
|
vlist_push(&node->out.hooks, (void *) writeHook);
|
2019-10-29 20:19:01 +01:00
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
2020-09-13 08:42:30 +02:00
|
|
|
StatsHook & operator=(const StatsHook&) = delete;
|
|
|
|
StatsHook(const StatsHook&) = delete;
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
virtual void start()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::PREPARED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
if (!uri.empty()) {
|
2021-02-16 14:15:14 +01:00
|
|
|
output = fopen(uri.c_str(), "w+");
|
2019-03-26 15:33:47 +01:00
|
|
|
if (!output)
|
|
|
|
throw RuntimeError("Failed to open file '{}' for writing", uri);
|
|
|
|
}
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STARTED;
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void stop()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::STARTED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
stats->print(uri.empty() ? stdout : output, format, verbose);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
if (!uri.empty())
|
2021-02-16 14:15:14 +01:00
|
|
|
fclose(output);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STOPPED;
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void restart()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::STARTED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
stats->reset();
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-29 10:45:12 -04:00
|
|
|
virtual Hook::Reason process(struct sample *smp)
|
2019-10-29 20:19:01 +01:00
|
|
|
{
|
|
|
|
// Only call readHook if it hasnt been added to the node's hook list
|
|
|
|
if (!node)
|
2020-09-13 08:42:30 +02:00
|
|
|
return readHook->process(smp);
|
2019-10-29 20:19:01 +01:00
|
|
|
|
|
|
|
return Hook::Reason::OK;
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
virtual void periodic()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::STARTED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
stats->printPeriodic(uri.empty() ? stdout : output, format, node);
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
virtual void parse(json_t *json)
|
2019-03-26 15:33:47 +01:00
|
|
|
{
|
2019-06-23 13:35:42 +02:00
|
|
|
int ret;
|
2019-03-26 15:33:47 +01:00
|
|
|
json_error_t err;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state != State::STARTED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
Hook::parse(json);
|
2020-08-28 19:49:36 +02:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
const char *f = nullptr;
|
|
|
|
const char *u = nullptr;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: b, s?: i, s?: i, s?: s }",
|
2019-03-26 15:33:47 +01:00
|
|
|
"format", &f,
|
|
|
|
"verbose", &verbose,
|
|
|
|
"warmup", &warmup,
|
|
|
|
"buckets", &buckets,
|
|
|
|
"output", &u
|
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-hook-stats");
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
if (f) {
|
2019-06-23 13:35:42 +02:00
|
|
|
try {
|
|
|
|
format = Stats::lookupFormat(f);
|
2020-08-17 17:09:55 +02:00
|
|
|
} catch (const std::invalid_argument &e) {
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, "node-config-hook-stats", "Invalid statistic output format: {}", f);
|
2019-06-23 13:35:42 +02:00
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (u)
|
2019-06-26 20:18:02 +02:00
|
|
|
uri = u;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::PARSED;
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
2019-06-26 20:18:02 +02:00
|
|
|
|
|
|
|
virtual void prepare()
|
|
|
|
{
|
2019-08-28 16:56:22 +02:00
|
|
|
assert(state == State::CHECKED);
|
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
stats = std::make_shared<villas::Stats>(buckets, warmup);
|
|
|
|
|
2021-04-28 13:15:01 +00:00
|
|
|
/* Register statistic object to node.
|
2019-06-26 20:18:02 +02:00
|
|
|
*
|
2021-04-28 13:15:01 +00:00
|
|
|
* This allows the node code to update statistics. */
|
2019-10-29 20:19:01 +01:00
|
|
|
if (node)
|
|
|
|
node->stats = stats;
|
2019-08-28 16:56:22 +02:00
|
|
|
|
|
|
|
state = State::PREPARED;
|
2019-06-26 20:18:02 +02:00
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
};
|
|
|
|
|
2021-06-29 10:45:12 -04:00
|
|
|
Hook::Reason StatsWriteHook::process(struct sample *smp)
|
2019-10-29 20:19:01 +01:00
|
|
|
{
|
|
|
|
timespec now = time_now();
|
|
|
|
|
|
|
|
parent->stats->update(Stats::Metric::AGE, time_delta(&smp->ts.received, &now));
|
|
|
|
|
|
|
|
return Reason::OK;
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:45:12 -04:00
|
|
|
Hook::Reason StatsReadHook::process(struct sample *smp)
|
2019-10-29 20:19:01 +01:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_incref(smp);
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
sample_decref(last);
|
|
|
|
|
|
|
|
last = smp;
|
|
|
|
|
|
|
|
return Reason::OK;
|
|
|
|
}
|
|
|
|
|
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";
|
2021-06-18 21:00:10 -04:00
|
|
|
static HookPlugin<StatsHook, n, d, (int) Hook::Flags::READ_ONLY | (int) Hook::Flags::NODE_READ> p;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|