2017-03-05 10:06:32 -04:00
|
|
|
/** Statistic hooks.
|
2016-06-08 22:38:50 +02:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-05 10:06:32 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-08 22:38:50 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
/** @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/common.h>
|
|
|
|
#include <villas/advio.h>
|
|
|
|
#include <villas/hook.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/stats.h>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/timing.h>
|
2016-06-08 22:38:50 +02:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
struct stats_collect {
|
2017-03-20 09:13:01 -03:00
|
|
|
struct stats stats;
|
|
|
|
|
|
|
|
enum stats_format format;
|
|
|
|
int verbose;
|
2017-05-05 22:26:34 +00:00
|
|
|
int warmup;
|
|
|
|
int buckets;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-06 23:48:19 +02:00
|
|
|
AFILE *output;
|
2017-08-01 11:56:47 +02:00
|
|
|
char *uri;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
|
|
|
struct sample *last;
|
2017-03-20 09:13:01 -03:00
|
|
|
};
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
static int stats_collect_init(struct hook *h)
|
2016-06-08 22:38:50 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
/* Register statistic object to path.
|
|
|
|
*
|
|
|
|
* This allows the path code to update statistics. */
|
2018-04-04 08:29:54 +02:00
|
|
|
if (h->node)
|
|
|
|
h->node->stats = &p->stats;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
/* Set default values */
|
|
|
|
p->format = STATS_FORMAT_HUMAN;
|
|
|
|
p->verbose = 0;
|
2017-05-05 22:26:34 +00:00
|
|
|
p->warmup = 500;
|
|
|
|
p->buckets = 20;
|
2017-03-27 12:26:11 +02:00
|
|
|
p->uri = NULL;
|
2017-07-06 23:48:19 +02:00
|
|
|
|
2017-07-12 00:57:33 +02:00
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stats_collect_destroy(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-01 11:56:47 +02:00
|
|
|
if (p->uri)
|
|
|
|
free(p->uri);
|
|
|
|
|
2017-07-09 14:37:02 +02:00
|
|
|
return stats_destroy(&p->stats);
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stats_collect_start(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
if (p->uri) {
|
2017-07-06 23:48:19 +02:00
|
|
|
p->output = afopen(p->uri, "w+");
|
2017-03-27 12:26:11 +02:00
|
|
|
if (!p->output)
|
|
|
|
error("Failed to open file %s for writing", p->uri);
|
|
|
|
}
|
2017-07-06 23:48:19 +02:00
|
|
|
|
2017-07-12 00:57:33 +02:00
|
|
|
return stats_init(&p->stats, p->buckets, p->warmup);
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int stats_collect_stop(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-06 23:48:19 +02:00
|
|
|
stats_print(&p->stats, p->uri ? p->output->file : stdout, p->format, p->verbose);
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
if (p->uri)
|
2017-07-06 23:48:19 +02:00
|
|
|
afclose(p->output);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stats_collect_restart(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
stats_reset(&p->stats);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stats_collect_periodic(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
stats_print_periodic(&p->stats, p->uri ? p->output->file : stdout, p->format, p->verbose, h->node);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
static int stats_collect_parse(struct hook *h, json_t *cfg)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret, fmt;
|
|
|
|
json_error_t err;
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
const char *format = NULL;
|
|
|
|
const char *uri = NULL;
|
|
|
|
|
2017-08-23 15:49:22 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: b, s?: i, s?: i, s?: s }",
|
2017-08-03 00:19:27 +02:00
|
|
|
"format", &format,
|
|
|
|
"verbose", &p->verbose,
|
|
|
|
"warmup", &p->warmup,
|
|
|
|
"buckets", &p->buckets,
|
|
|
|
"output", &uri
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
|
|
|
|
|
|
|
|
if (format) {
|
2017-07-28 18:11:52 +02:00
|
|
|
fmt = stats_lookup_format(format);
|
|
|
|
if (fmt < 0)
|
2017-08-03 00:19:27 +02:00
|
|
|
jerror(&err, "Invalid statistic output format: %s", format);
|
2017-07-28 18:11:52 +02:00
|
|
|
|
|
|
|
p->format = fmt;
|
2016-06-08 22:38:50 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
if (uri)
|
2017-08-01 11:56:47 +02:00
|
|
|
p->uri = strdup(uri);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-05 22:18:47 +02:00
|
|
|
static int stats_collect_read(struct hook *h, struct sample *smps[], unsigned *cnt)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
struct stats *s = &p->stats;
|
|
|
|
|
|
|
|
int dist;
|
|
|
|
struct sample *previous = p->last;
|
|
|
|
|
|
|
|
for (int i = 0; i < *cnt; i++) {
|
|
|
|
if (previous) {
|
|
|
|
stats_update(s, STATS_GAP_RECEIVED, time_delta(&previous->ts.received, &smps[i]->ts.received));
|
|
|
|
stats_update(s, STATS_GAP_SAMPLE, time_delta(&previous->ts.origin, &smps[i]->ts.origin));
|
|
|
|
stats_update(s, STATS_OWD, time_delta(&smps[i]->ts.origin, &smps[i]->ts.received));
|
|
|
|
|
|
|
|
dist = smps[i]->sequence - (int32_t) previous->sequence;
|
|
|
|
if (dist != 1)
|
|
|
|
stats_update(s, STATS_REORDERED, dist);
|
|
|
|
}
|
|
|
|
|
|
|
|
previous = smps[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->last)
|
|
|
|
sample_put(p->last);
|
|
|
|
|
|
|
|
if (previous)
|
|
|
|
sample_get(previous);
|
|
|
|
|
|
|
|
p->last = previous;
|
|
|
|
|
|
|
|
stats_commit(&p->stats);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stats_collect_write(struct hook *h, struct sample *smps[], unsigned *cnt)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct stats_collect *p = (struct stats_collect *) h->_vd;
|
2017-09-03 10:53:32 +02:00
|
|
|
|
|
|
|
struct timespec ts_sent = time_now();
|
|
|
|
|
|
|
|
for (int i = 0; i < *cnt; i++)
|
|
|
|
stats_update(&p->stats, STATS_TIME, time_delta(&smps[i]->ts.received, &ts_sent));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-03 10:53:32 +02:00
|
|
|
stats_commit(&p->stats);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-20 09:14:10 -03:00
|
|
|
return 0;
|
2016-06-08 22:38:50 +02:00
|
|
|
}
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
static struct plugin p = {
|
2017-03-05 10:06:32 -04:00
|
|
|
.name = "stats",
|
|
|
|
.description = "Collect statistics for the current path",
|
|
|
|
.type = PLUGIN_TYPE_HOOK,
|
|
|
|
.hook = {
|
2017-09-02 14:27:58 +02:00
|
|
|
.flags = HOOK_NODE,
|
2017-03-05 10:06:32 -04:00
|
|
|
.priority = 2,
|
2017-03-27 12:26:11 +02:00
|
|
|
.init = stats_collect_init,
|
|
|
|
.destroy= stats_collect_destroy,
|
|
|
|
.start = stats_collect_start,
|
|
|
|
.stop = stats_collect_stop,
|
|
|
|
.read = stats_collect_read,
|
2017-09-03 10:53:32 +02:00
|
|
|
.write = stats_collect_write,
|
2017-03-27 12:26:11 +02:00
|
|
|
.restart= stats_collect_restart,
|
|
|
|
.periodic= stats_collect_periodic,
|
|
|
|
.parse = stats_collect_parse,
|
|
|
|
.size = sizeof(struct stats_collect),
|
2017-03-05 10:06:32 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
REGISTER_PLUGIN(&p)
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2017-05-05 22:26:34 +00:00
|
|
|
/** @} */
|