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/nodes/stats.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

257 lines
6.3 KiB
C++
Raw Permalink Normal View History

/* Sending statistics to another node.
*
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
*/
#include <cstring>
2017-11-02 13:08:12 +01:00
#include <villas/exceptions.hpp>
2019-06-23 16:13:23 +02:00
#include <villas/hook.hpp>
#include <villas/node_compat.hpp>
2019-04-07 15:13:40 +02:00
#include <villas/nodes/stats.hpp>
#include <villas/sample.hpp>
2019-06-23 13:35:42 +02:00
#include <villas/stats.hpp>
2019-04-23 13:14:47 +02:00
#include <villas/super_node.hpp>
2021-06-21 16:11:42 -04:00
#include <villas/utils.hpp>
2019-06-23 13:35:42 +02:00
using namespace villas;
2019-04-23 13:14:47 +02:00
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2019-04-23 13:14:47 +02:00
2021-06-21 16:11:42 -04:00
static NodeList nodes; // The global list of nodes
int villas::node::stats_node_signal_destroy(struct stats_node_signal *s) {
free(s->node_str);
return 0;
}
int villas::node::stats_node_signal_parse(struct stats_node_signal *s,
json_t *json) {
json_error_t err;
int ret;
const char *stats;
char *metric, *type, *node, *cpy, *lasts;
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: s }", "stats", &stats);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-stats");
cpy = strdup(stats);
node = strtok_r(cpy, ".", &lasts);
if (!node)
goto invalid_format;
2019-04-07 15:13:40 +02:00
metric = strtok_r(nullptr, ".", &lasts);
if (!metric)
goto invalid_format;
2019-04-07 15:13:40 +02:00
type = strtok_r(nullptr, ".", &lasts);
if (!type)
goto invalid_format;
2019-06-23 13:35:42 +02:00
s->metric = Stats::lookupMetric(metric);
s->type = Stats::lookupType(type);
s->node_str = strdup(node);
free(cpy);
return 0;
invalid_format:
free(cpy);
return -1;
}
int villas::node::stats_node_type_start(villas::node::SuperNode *sn) {
if (sn == nullptr)
throw RuntimeError("Stats node-type requires super-node");
2019-04-23 13:14:47 +02:00
nodes = sn->getNodes();
2018-12-02 02:56:52 +01:00
return 0;
}
int villas::node::stats_node_prepare(NodeCompat *n) {
auto *s = n->getData<struct stats_node>();
2020-09-10 17:35:35 +02:00
assert(n->getInputSignals(false)->size() == 0);
2020-09-10 17:35:35 +02:00
// Generate signal list
for (size_t i = 0; i < list_length(&s->signals); i++) {
struct stats_node_signal *stats_sig =
(struct stats_node_signal *)list_at(&s->signals, i);
2020-09-10 17:35:35 +02:00
const char *metric = Stats::metrics[stats_sig->metric].name;
const char *type = Stats::types[stats_sig->type].name;
auto name = fmt::format("{}.{}.{}", stats_sig->node_str, metric, type);
auto sig = std::make_shared<Signal>(
2020-09-10 17:35:35 +02:00
name.c_str(), Stats::metrics[stats_sig->metric].unit,
Stats::types[stats_sig->type].signal_type);
n->in.signals->push_back(sig);
2020-09-10 17:35:35 +02:00
}
return 0;
}
int villas::node::stats_node_start(NodeCompat *n) {
auto *s = n->getData<struct stats_node>();
2020-03-04 13:06:28 +01:00
s->task.setRate(s->rate);
for (size_t i = 0; i < list_length(&s->signals); i++) {
struct stats_node_signal *stats_sig =
(struct stats_node_signal *)list_at(&s->signals, i);
2021-06-21 16:11:42 -04:00
stats_sig->node = nodes.lookup(stats_sig->node_str);
if (!stats_sig->node)
throw ConfigError(n->getConfig(), "node-config-node-stats-node",
"Invalid reference node {}", stats_sig->node_str);
}
2017-09-04 23:03:00 +02:00
return 0;
}
int villas::node::stats_node_stop(NodeCompat *n) {
auto *s = n->getData<struct stats_node>();
2020-03-04 13:06:28 +01:00
s->task.stop();
return 0;
}
char *villas::node::stats_node_print(NodeCompat *n) {
auto *s = n->getData<struct stats_node>();
return strf("rate=%f", s->rate);
}
int villas::node::stats_node_init(NodeCompat *n) {
int ret;
auto *s = n->getData<struct stats_node>();
2020-03-04 13:06:28 +01:00
new (&s->task) Task(CLOCK_MONOTONIC);
ret = list_init(&s->signals);
if (ret)
return ret;
return 0;
}
int villas::node::stats_node_destroy(NodeCompat *n) {
int ret;
auto *s = n->getData<struct stats_node>();
2020-03-04 13:06:28 +01:00
s->task.~Task();
ret = list_destroy(&s->signals, (dtor_cb_t)stats_node_signal_destroy, true);
if (ret)
return ret;
return 0;
}
int villas::node::stats_node_parse(NodeCompat *n, json_t *json) {
auto *s = n->getData<struct stats_node>();
int ret;
size_t i;
json_error_t err;
json_t *json_signals, *json_signal;
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: F, s: { s: o } }", "rate", &s->rate,
"in", "signals", &json_signals);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-stats");
if (s->rate <= 0)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, "node-config-node-stats-rate",
"Setting 'rate' must be positive");
if (!json_is_array(json_signals))
2021-02-16 14:15:14 +01:00
throw ConfigError(json, "node-config-node-stats-in-signals",
"Setting 'in.signals' must be an array");
2017-09-04 23:03:00 +02:00
json_array_foreach(json_signals, i, json_signal) {
2020-09-10 17:35:35 +02:00
auto *stats_sig = new struct stats_node_signal;
if (!stats_sig)
throw MemoryAllocationError();
ret = stats_node_signal_parse(stats_sig, json_signal);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json_signal, "node-config-node-stats-signals",
"Failed to parse statistics signal definition");
list_push(&s->signals, stats_sig);
}
2017-09-05 01:07:11 +02:00
return 0;
}
int villas::node::stats_node_read(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
auto *s = n->getData<struct stats_node>();
if (!cnt)
2017-09-04 23:03:00 +02:00
return 0;
2020-03-04 13:06:28 +01:00
s->task.wait();
unsigned len = MIN(list_length(&s->signals), smps[0]->capacity);
for (size_t i = 0; i < len; i++) {
struct stats_node_signal *sig =
(struct stats_node_signal *)list_at(&s->signals, i);
2017-09-04 23:03:00 +02:00
auto st = sig->node->getStats();
if (!st)
return -1;
2017-09-04 23:03:00 +02:00
2019-06-23 13:35:42 +02:00
smps[0]->data[i] = st->getValue(sig->metric, sig->type);
}
smps[0]->length = len;
2019-06-23 16:13:23 +02:00
smps[0]->flags = (int)SampleFlags::HAS_DATA;
smps[0]->signals = n->getInputSignals(false);
return 1;
}
int villas::node::stats_node_poll_fds(NodeCompat *n, int fds[]) {
auto *s = n->getData<struct stats_node>();
2020-03-04 13:06:28 +01:00
fds[0] = s->task.getFD();
return 0;
}
static NodeCompatType p;
2019-04-07 15:13:40 +02:00
__attribute__((constructor(110))) static void register_plugin() {
2021-06-21 16:11:42 -04:00
p.name = "stats";
p.description = "Send statistics to another node";
p.vectorize = 1;
p.flags = (int)NodeFactory::Flags::PROVIDES_SIGNALS;
2021-06-21 16:11:42 -04:00
p.size = sizeof(struct stats_node);
p.type.start = stats_node_type_start;
p.parse = stats_node_parse;
p.init = stats_node_init;
p.destroy = stats_node_destroy;
p.print = stats_node_print;
p.prepare = stats_node_prepare;
p.start = stats_node_start;
p.stop = stats_node_stop;
p.read = stats_node_read;
p.poll_fds = stats_node_poll_fds;
static NodeCompatFactory ncp(&p);
2019-04-07 15:13:40 +02:00
}