2017-09-03 10:53:32 +02:00
|
|
|
/** Sending statistics to another node.
|
|
|
|
*
|
|
|
|
* @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-09-03 10:53:32 +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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2017-11-02 13:08:12 +01:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
#include <villas/nodes/stats.hpp>
|
2019-06-23 16:13:23 +02:00
|
|
|
#include <villas/hook.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
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>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/node.h>
|
2017-09-03 10:53:32 +02:00
|
|
|
|
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
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
static struct vlist *nodes; /** The global list of nodes */
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
int stats_node_signal_destroy(struct stats_node_signal *s)
|
2018-08-20 18:28:27 +02:00
|
|
|
{
|
2019-02-15 09:40:38 +01:00
|
|
|
free(s->node_str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int stats_node_signal_parse(struct stats_node_signal *s, json_t *json)
|
2019-02-15 09:40:38 +01:00
|
|
|
{
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
const char *stats;
|
2019-02-18 01:09:33 +01:00
|
|
|
char *metric, *type, *node, *cpy, *lasts;
|
2019-02-15 09:40:38 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: s }",
|
2019-02-15 09:40:38 +01:00
|
|
|
"stats", &stats
|
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-stats");
|
2019-02-15 09:40:38 +01:00
|
|
|
|
|
|
|
cpy = strdup(stats);
|
|
|
|
|
2019-02-18 01:09:33 +01:00
|
|
|
node = strtok_r(cpy, ".", &lasts);
|
2019-02-15 09:40:38 +01:00
|
|
|
if (!node)
|
|
|
|
goto invalid_format;
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
metric = strtok_r(nullptr, ".", &lasts);
|
2019-02-15 09:40:38 +01:00
|
|
|
if (!metric)
|
|
|
|
goto invalid_format;
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
type = strtok_r(nullptr, ".", &lasts);
|
2019-02-15 09:40:38 +01:00
|
|
|
if (!type)
|
|
|
|
goto invalid_format;
|
|
|
|
|
2019-06-23 13:35:42 +02:00
|
|
|
s->metric = Stats::lookupMetric(metric);
|
|
|
|
s->type = Stats::lookupType(type);
|
2019-02-15 09:40:38 +01:00
|
|
|
|
|
|
|
s->node_str = strdup(node);
|
|
|
|
|
|
|
|
free(cpy);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
invalid_format:
|
|
|
|
free(cpy);
|
|
|
|
return -1;
|
2018-08-20 18:28:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 13:14:47 +02:00
|
|
|
int stats_node_type_start(villas::node::SuperNode *sn)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2019-04-23 13:14:47 +02:00
|
|
|
nodes = sn->getNodes();
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2018-12-02 02:56:52 +01:00
|
|
|
return 0;
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
int stats_node_prepare(struct vnode *n)
|
|
|
|
{
|
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
|
|
|
|
|
|
|
assert(vlist_length(&n->in.signals) == 0);
|
|
|
|
|
|
|
|
/* Generate signal list */
|
|
|
|
for (size_t i = 0; i < vlist_length(&s->signals); i++) {
|
|
|
|
struct stats_node_signal *stats_sig = (struct stats_node_signal *) vlist_at(&s->signals, i);
|
|
|
|
struct signal *sig;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
sig = signal_create(name.c_str(),
|
|
|
|
Stats::metrics[stats_sig->metric].unit,
|
|
|
|
Stats::types[stats_sig->type].signal_type);
|
|
|
|
|
|
|
|
vlist_push(&n->in.signals, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int stats_node_start(struct vnode *n)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
s->task.setRate(s->rate);
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(&s->signals); i++) {
|
|
|
|
struct stats_node_signal *stats_sig = (struct stats_node_signal *) vlist_at(&s->signals, i);
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
stats_sig->node = vlist_lookup_name<struct vnode>(nodes, stats_sig->node_str);
|
2019-02-15 09:40:38 +01:00
|
|
|
if (!stats_sig->node)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(n->config, "node-config-node-stats-node", "Invalid reference node {}", stats_sig->node_str);
|
2019-02-15 09:40:38 +01:00
|
|
|
}
|
2017-09-04 23:03:00 +02:00
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int stats_node_stop(struct vnode *n)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
s->task.stop();
|
2017-09-03 10:53:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * stats_node_print(struct vnode *n)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
return strf("rate=%f", s->rate);
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int stats_node_init(struct vnode *n)
|
2019-02-15 09:40:38 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
new (&s->task) Task(CLOCK_MONOTONIC);
|
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
ret = vlist_init(&s->signals);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int stats_node_destroy(struct vnode *n)
|
2019-02-15 09:40:38 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
s->task.~Task();
|
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
ret = vlist_destroy(&s->signals, (dtor_cb_t) stats_node_signal_destroy, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int stats_node_parse(struct vnode *n, json_t *json)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
|
|
|
int ret;
|
2019-02-15 09:40:38 +01:00
|
|
|
size_t i;
|
2017-09-03 10:53:32 +02:00
|
|
|
json_error_t err;
|
2019-02-15 09:40:38 +01:00
|
|
|
json_t *json_signals, *json_signal;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: F, s: { s: o } }",
|
2019-02-15 09:40:38 +01:00
|
|
|
"rate", &s->rate,
|
|
|
|
"in",
|
|
|
|
"signals", &json_signals
|
2017-09-03 10:53:32 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-stats");
|
2017-09-03 10:53:32 +02:00
|
|
|
|
|
|
|
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");
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
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
|
|
|
|
2019-02-15 09:40:38 +01: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;
|
2019-02-15 09:40:38 +01:00
|
|
|
if (!stats_sig)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
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");
|
2019-02-15 09:40:38 +01:00
|
|
|
|
|
|
|
vlist_push(&s->signals, stats_sig);
|
|
|
|
}
|
2017-09-05 01:07:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int stats_node_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2019-02-15 09:40:38 +01:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
if (!cnt)
|
2017-09-04 23:03:00 +02:00
|
|
|
return 0;
|
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
s->task.wait();
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
unsigned len = MIN(vlist_length(&s->signals), smps[0]->capacity);
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
struct stats_node_signal *sig = (struct stats_node_signal *) vlist_at(&s->signals, i);
|
2017-09-04 23:03:00 +02:00
|
|
|
|
2019-06-26 20:18:02 +02:00
|
|
|
auto st = sig->node->stats;
|
2019-02-15 09:40:38 +01:00
|
|
|
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);
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 09:40:38 +01:00
|
|
|
smps[0]->length = len;
|
2019-06-23 16:13:23 +02:00
|
|
|
smps[0]->flags = (int) SampleFlags::HAS_DATA;
|
2019-02-15 09:40:38 +01:00
|
|
|
smps[0]->signals = &n->in.signals;
|
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int stats_node_poll_fds(struct vnode *n, int fds[])
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2020-03-04 13:06:28 +01:00
|
|
|
fds[0] = s->task.getFD();
|
2019-01-21 15:47:34 +01:00
|
|
|
|
|
|
|
return 0;
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
static struct plugin p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
2019-04-23 00:36:06 +02:00
|
|
|
p.name = "stats";
|
|
|
|
p.description = "Send statistics to another node";
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::NODE;
|
|
|
|
p.node.instances.state = State::DESTROYED;
|
2019-04-07 15:13:40 +02:00
|
|
|
p.node.vectorize = 1;
|
2020-09-10 17:35:35 +02:00
|
|
|
p.node.flags = (int) NodeFlags::PROVIDES_SIGNALS;
|
2019-04-07 15:13:40 +02:00
|
|
|
p.node.size = sizeof(struct stats_node);
|
|
|
|
p.node.type.start = stats_node_type_start;
|
|
|
|
p.node.parse = stats_node_parse;
|
|
|
|
p.node.init = stats_node_init;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.destroy = stats_node_destroy;
|
2019-04-07 15:13:40 +02:00
|
|
|
p.node.print = stats_node_print;
|
2020-09-10 17:35:35 +02:00
|
|
|
p.node.prepare = stats_node_prepare;
|
2019-04-07 15:13:40 +02:00
|
|
|
p.node.start = stats_node_start;
|
|
|
|
p.node.stop = stats_node_stop;
|
|
|
|
p.node.read = stats_node_read;
|
2019-04-23 00:36:06 +02:00
|
|
|
p.node.poll_fds = stats_node_poll_fds;
|
2019-04-07 15:13:40 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret = vlist_init(&p.node.instances);
|
|
|
|
if (!ret)
|
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-07 15:13:40 +02:00
|
|
|
}
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
__attribute__((destructor(110)))
|
|
|
|
static void deregister_plugin() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-07 15:13:40 +02:00
|
|
|
}
|