2017-09-03 10:53:32 +02:00
|
|
|
/** Sending statistics to another node.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2018-08-20 18:39:04 +02:00
|
|
|
* @copyright 2017-2018, 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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-11-02 13:08:12 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/nodes/stats.h>
|
|
|
|
#include <villas/hook.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/stats.h>
|
2018-08-23 17:31:01 +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
|
|
|
|
2017-09-05 14:51:16 +02:00
|
|
|
#define STATS_METRICS 6
|
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
static struct list *nodes; /** The global list of nodes */
|
|
|
|
|
2018-08-20 18:28:27 +02:00
|
|
|
static void stats_init_signals(struct node *n)
|
|
|
|
{
|
|
|
|
struct stats_desc *desc;
|
|
|
|
struct signal *sig;
|
|
|
|
|
|
|
|
for (int i = 0; i < STATS_COUNT; i++) {
|
|
|
|
desc = &stats_metrics[i];
|
|
|
|
|
|
|
|
/* Total */
|
|
|
|
sig = alloc(sizeof(struct signal));
|
|
|
|
sig->name = strf("%s.%s", desc->name, "total");
|
|
|
|
sig->type = SIGNAL_TYPE_INTEGER;
|
|
|
|
list_push(&n->signals, sig);
|
|
|
|
|
|
|
|
/* Last */
|
|
|
|
sig = alloc(sizeof(struct signal));
|
|
|
|
sig->name = strf("%s.%s", desc->name, "last");
|
|
|
|
sig->unit = strdup(desc->unit);
|
|
|
|
sig->type = SIGNAL_TYPE_FLOAT;
|
|
|
|
list_push(&n->signals, sig);
|
|
|
|
|
|
|
|
/* Highest */
|
|
|
|
sig = alloc(sizeof(struct signal));
|
|
|
|
sig->name = strf("%s.%s", desc->name, "highest");
|
|
|
|
sig->unit = strdup(desc->unit);
|
|
|
|
sig->type = SIGNAL_TYPE_FLOAT;
|
|
|
|
list_push(&n->signals, sig);
|
|
|
|
|
|
|
|
/* Lowest */
|
|
|
|
sig = alloc(sizeof(struct signal));
|
|
|
|
sig->name = strf("%s.%s", desc->name, "lowest");
|
|
|
|
sig->unit = strdup(desc->unit);
|
|
|
|
sig->type = SIGNAL_TYPE_FLOAT;
|
|
|
|
list_push(&n->signals, sig);
|
|
|
|
|
|
|
|
/* Mean */
|
|
|
|
sig = alloc(sizeof(struct signal));
|
|
|
|
sig->name = strf("%s.%s", desc->name, "mean");
|
|
|
|
sig->unit = strdup(desc->unit);
|
|
|
|
sig->type = SIGNAL_TYPE_FLOAT;
|
|
|
|
list_push(&n->signals, sig);
|
|
|
|
|
|
|
|
/* Variance */
|
|
|
|
sig = alloc(sizeof(struct signal));
|
|
|
|
sig->name = strf("%s.%s", desc->name, "var");
|
|
|
|
sig->unit = strf("%s^2", desc->unit); // variance has squared unit of variable
|
|
|
|
sig->type = SIGNAL_TYPE_FLOAT;
|
|
|
|
list_push(&n->signals, sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
int stats_node_type_start() /// @todo: Port to C++
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2018-07-03 21:51:48 +02:00
|
|
|
nodes = NULL;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
return -1;
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int stats_node_start(struct node *n)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
ret = task_init(&s->task, s->rate, CLOCK_MONOTONIC);
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to create task");
|
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
s->node = list_lookup(nodes, s->node_str);
|
|
|
|
if (!s->node)
|
|
|
|
error("Invalid reference node %s for setting 'node' of node %s", s->node_str, node_name(n));
|
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
int stats_node_stop(struct node *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
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = task_destroy(&s->task);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * stats_node_print(struct node *n)
|
|
|
|
{
|
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
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
return strf("node=%s, rate=%f", s->node_str, s->rate);
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int stats_node_parse(struct node *n, json_t *cfg)
|
|
|
|
{
|
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;
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
const char *node;
|
|
|
|
|
2017-09-04 23:16:58 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s: s, s: F }",
|
2017-09-03 10:53:32 +02:00
|
|
|
"node", &node,
|
|
|
|
"rate", &s->rate
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
|
|
|
|
|
|
|
|
if (s->rate <= 0)
|
|
|
|
error("Setting 'rate' of node %s must be positive", node_name(n));
|
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
s->node_str = strdup(node);
|
|
|
|
|
2018-08-20 18:28:27 +02:00
|
|
|
stats_init_signals(n);
|
2017-09-04 23:03:00 +02:00
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-05 01:07:11 +02:00
|
|
|
int stats_node_destroy(struct node *n)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *s = (struct stats_node *) n->_vd;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
|
|
|
if (s->node_str)
|
2017-09-05 10:50:25 +02:00
|
|
|
free(s->node_str);
|
2017-09-05 01:07:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int stats_node_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-09-03 10:53:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_node *sn = (struct stats_node *) n->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
struct stats *s = sn->node->stats;
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
if (!cnt)
|
2017-09-04 23:03:00 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!sn->node->stats)
|
2017-09-03 10:53:32 +02:00
|
|
|
return 0;
|
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
task_wait(&sn->task);
|
2017-09-03 10:53:32 +02:00
|
|
|
|
2017-09-05 14:51:16 +02:00
|
|
|
smps[0]->length = MIN(STATS_COUNT * 6, smps[0]->capacity);
|
2018-08-20 18:28:27 +02:00
|
|
|
smps[0]->flags = SAMPLE_HAS_DATA;
|
2017-09-04 23:03:00 +02:00
|
|
|
|
2017-09-05 14:51:16 +02:00
|
|
|
for (int i = 0; i < 6 && (i+1)*STATS_METRICS <= smps[0]->length; i++) {
|
2017-09-04 23:03:00 +02:00
|
|
|
int tot = hist_total(&s->histograms[i]);
|
|
|
|
|
2017-09-05 14:51:16 +02:00
|
|
|
smps[0]->data[i*STATS_METRICS+0].f = tot ? hist_total(&s->histograms[i]) : 0;
|
|
|
|
smps[0]->data[i*STATS_METRICS+1].f = tot ? hist_last(&s->histograms[i]) : 0;
|
|
|
|
smps[0]->data[i*STATS_METRICS+2].f = tot ? hist_highest(&s->histograms[i]) : 0;
|
|
|
|
smps[0]->data[i*STATS_METRICS+3].f = tot ? hist_lowest(&s->histograms[i]) : 0;
|
|
|
|
smps[0]->data[i*STATS_METRICS+4].f = tot ? hist_mean(&s->histograms[i]) : 0;
|
|
|
|
smps[0]->data[i*STATS_METRICS+5].f = tot ? hist_var(&s->histograms[i]) : 0;
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stats_node_fd(struct node *n)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
return task_fd(&s->task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct plugin p = {
|
|
|
|
.name = "stats",
|
|
|
|
.description = "Send statistics to another node",
|
|
|
|
.type = PLUGIN_TYPE_NODE,
|
2017-09-04 23:03:00 +02:00
|
|
|
.node = {
|
2018-07-16 08:08:17 +02:00
|
|
|
.vectorize = 1,
|
2018-08-20 18:28:27 +02:00
|
|
|
.flags = NODE_TYPE_PROVIDES_SIGNALS,
|
2018-07-16 08:08:17 +02:00
|
|
|
.size = sizeof(struct stats_node),
|
|
|
|
.type.start = stats_node_type_start,
|
|
|
|
.parse = stats_node_parse,
|
|
|
|
.destroy = stats_node_destroy,
|
|
|
|
.print = stats_node_print,
|
|
|
|
.start = stats_node_start,
|
|
|
|
.stop = stats_node_stop,
|
|
|
|
.read = stats_node_read,
|
|
|
|
.fd = stats_node_fd
|
2017-09-03 10:53:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p)
|
2017-09-04 23:03:00 +02:00
|
|
|
LIST_INIT_STATIC(&p.node.instances)
|
2017-09-03 10:53:32 +02:00
|
|
|
|
|
|
|
/** @} */
|