2014-07-14 11:49:44 +00:00
|
|
|
/** Nodes.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -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/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2018-02-06 21:26:12 +01:00
|
|
|
#include <villas/config.h>
|
2018-05-13 13:52:02 +02:00
|
|
|
#include <villas/hook.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/config_helper.h>
|
|
|
|
#include <villas/mapping.h>
|
|
|
|
#include <villas/timing.h>
|
2018-03-21 16:59:19 +01:00
|
|
|
#include <villas/signal.h>
|
2018-06-30 18:20:30 +02:00
|
|
|
#include <villas/memory.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
static int node_direction_init(struct node_direction *nd, struct node *n)
|
2015-11-23 16:42:43 +01:00
|
|
|
{
|
2018-06-15 14:33:22 +02:00
|
|
|
nd->enabled = 0;
|
2018-05-24 09:04:41 +02:00
|
|
|
nd->vectorize = 1;
|
|
|
|
nd->builtin = 1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
list_init(&nd->signals);
|
2018-03-26 14:33:20 +02:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-09-02 14:27:58 +02:00
|
|
|
/* Add internal hooks if they are not already in the list */
|
2018-05-24 09:04:41 +02:00
|
|
|
list_init(&nd->hooks);
|
|
|
|
|
|
|
|
if (nd->builtin) {
|
2018-02-17 11:18:26 +01:00
|
|
|
int ret;
|
2017-12-20 20:50:24 +01:00
|
|
|
for (size_t i = 0; i < list_length(&plugins); i++) {
|
|
|
|
struct plugin *q = (struct plugin *) list_at(&plugins, i);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-12-20 20:50:24 +01:00
|
|
|
if (q->type != PLUGIN_TYPE_HOOK)
|
|
|
|
continue;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-12-20 20:50:24 +01:00
|
|
|
struct hook_type *vt = &q->hook;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-02-17 09:39:45 +01:00
|
|
|
if (!(vt->flags & HOOK_NODE) || !(vt->flags & HOOK_BUILTIN))
|
|
|
|
continue;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) alloc(sizeof(struct hook));
|
2017-09-02 14:27:58 +02:00
|
|
|
|
|
|
|
ret = hook_init(h, vt, NULL, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
list_push(&nd->hooks, h);
|
2017-09-02 14:27:58 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int node_direction_destroy(struct node_direction *nd, struct node *n)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = list_destroy(&nd->signals, (dtor_cb_t) signal_destroy, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
#ifdef WITH_HOOKS
|
|
|
|
ret = list_destroy(&nd->hooks, (dtor_cb_t) hook_destroy, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int node_direction_parse(struct node_direction *nd, struct node *n, json_t *cfg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-05-26 01:10:33 +02:00
|
|
|
json_error_t err;
|
2018-05-24 09:04:41 +02:00
|
|
|
json_t *json_hooks = NULL;
|
|
|
|
json_t *json_signals = NULL;
|
|
|
|
|
2018-05-26 01:10:33 +02:00
|
|
|
nd->cfg = cfg;
|
2018-06-15 14:33:22 +02:00
|
|
|
nd->enabled = 1;
|
2018-05-26 01:10:33 +02:00
|
|
|
|
2018-06-15 14:33:22 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: o, s?: o, s?: i, s?: b, s?: b }",
|
2018-05-24 09:04:41 +02:00
|
|
|
"hooks", &json_hooks,
|
|
|
|
"signals", &json_signals,
|
|
|
|
"vectorize", &nd->vectorize,
|
2018-06-15 14:33:22 +02:00
|
|
|
"builtin", &nd->builtin,
|
|
|
|
"enabled", &nd->enabled
|
2018-05-24 09:04:41 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse node '%s'", node_name(n));
|
|
|
|
|
|
|
|
#ifdef WITH_HOOKS
|
|
|
|
if (json_hooks) {
|
|
|
|
ret = hook_parse_list(&nd->hooks, json_hooks, NULL, n);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* WITH_HOOKS */
|
|
|
|
|
|
|
|
if (json_signals) {
|
|
|
|
ret = signal_parse_list(&nd->signals, json_signals);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to parse signal definition of node '%s'", node_name(n));
|
|
|
|
}
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
2016-01-14 22:59:57 +01:00
|
|
|
}
|
2015-11-23 16:42:43 +01:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
static int node_direction_check(struct node_direction *nd, struct node *n)
|
|
|
|
{
|
|
|
|
if (nd->vectorize <= 0)
|
|
|
|
error("Invalid `vectorize` value %d for node %s. Must be natural number!", nd->vectorize, node_name(n));
|
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
if (node_type(n)->vectorize && node_type(n)->vectorize < nd->vectorize)
|
2018-05-24 09:04:41 +02:00
|
|
|
error("Invalid value for `vectorize`. Node type requires a number smaller than %d!",
|
2018-07-16 08:30:41 +02:00
|
|
|
node_type(n)->vectorize);
|
2018-05-24 09:04:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int node_direction_start(struct node_direction *nd, struct node *n)
|
2017-09-02 14:27:58 +02:00
|
|
|
{
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2018-05-24 09:04:41 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
/* We sort the hooks according to their priority before starting the path */
|
2018-05-24 09:04:41 +02:00
|
|
|
list_sort(&nd->hooks, hook_cmp_priority);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < list_length(&nd->hooks); i++) {
|
|
|
|
struct hook *h = (struct hook *) list_at(&nd->hooks, i);
|
|
|
|
|
|
|
|
ret = hook_start(h);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-06 21:26:12 +01:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-09-02 14:27:58 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
static int node_direction_stop(struct node_direction *nd, struct node *n)
|
|
|
|
{
|
|
|
|
#ifdef WITH_HOOKS
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < list_length(&nd->hooks); i++) {
|
|
|
|
struct hook *h = (struct hook *) list_at(&nd->hooks, i);
|
|
|
|
|
|
|
|
ret = hook_stop(h);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* WITH_HOOKS */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int node_init(struct node *n, struct node_type *vt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
assert(n->state == STATE_DESTROYED);
|
|
|
|
|
|
|
|
n->_vt = vt;
|
|
|
|
n->_vd = alloc(vt->size);
|
|
|
|
|
|
|
|
n->stats = NULL;
|
|
|
|
n->name = NULL;
|
|
|
|
n->_name = NULL;
|
|
|
|
n->_name_long = NULL;
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
n->samplelen = DEFAULT_SAMPLELEN;
|
|
|
|
|
|
|
|
ret = node_direction_init(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = node_direction_init(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
n->state = STATE_INITIALIZED;
|
|
|
|
|
|
|
|
list_push(&vt->instances, n);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
int node_parse(struct node *n, json_t *json, const char *name)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2018-05-13 13:52:02 +02:00
|
|
|
struct node_type *nt;
|
2016-06-08 22:38:21 +02:00
|
|
|
int ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
const char *type;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
n->name = strdup(name);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: s, s?: i }",
|
2017-08-03 00:19:27 +02:00
|
|
|
"type", &type,
|
2018-06-12 20:45:03 +02:00
|
|
|
"samplelen", &n->samplelen
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse node '%s'", node_name(n));
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2018-05-13 13:52:02 +02:00
|
|
|
nt = node_type_lookup(type);
|
2018-07-16 08:30:41 +02:00
|
|
|
assert(nt == node_type(n));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
struct {
|
|
|
|
const char *str;
|
|
|
|
struct node_direction *dir;
|
|
|
|
} dirs[] = {
|
|
|
|
{ "in", &n->in },
|
|
|
|
{ "out", &n->out }
|
|
|
|
};
|
2018-05-26 01:10:33 +02:00
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
const char *fields[] = { "builtin", "vectorize", "signals", "hooks" };
|
|
|
|
|
|
|
|
for (int j = 0; j < ARRAY_LEN(dirs); j++) {
|
|
|
|
json_t *json_dir = json_object_get(json, dirs [j].str);
|
|
|
|
|
2018-06-15 14:36:00 +02:00
|
|
|
// Skip if direction is unused
|
|
|
|
if (!json_dir)
|
|
|
|
json_dir = json_pack("{ s: b }", "enabled", 0);
|
2018-06-12 20:45:03 +02:00
|
|
|
|
|
|
|
// Copy missing fields from main node config to direction config
|
|
|
|
for (int i = 0; i < ARRAY_LEN(fields); i++) {
|
|
|
|
json_t *json_field_dir = json_object_get(json_dir, fields[i]);
|
|
|
|
json_t *json_field_node = json_object_get(json, fields[i]);
|
|
|
|
|
|
|
|
if (json_field_node && !json_field_dir)
|
|
|
|
json_object_set(json_dir, fields[i], json_field_node);
|
|
|
|
}
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
ret = node_direction_parse(dirs[j].dir, n, json_dir);
|
2018-03-21 16:59:19 +01:00
|
|
|
if (ret)
|
2018-06-12 20:45:03 +02:00
|
|
|
error("Failed to parse %s direction of node '%s'", dirs[j].str, node_name(n));
|
2018-03-21 16:59:19 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
ret = node_type(n)->parse ? node_type(n)->parse(n, json) : 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
if (ret)
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Failed to parse node '%s'", node_name(n));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
n->cfg = json;
|
2017-03-11 23:50:30 -03:00
|
|
|
n->state = STATE_PARSED;
|
2016-06-08 22:38:21 +02:00
|
|
|
|
|
|
|
return ret;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 01:55:06 +02:00
|
|
|
int node_parse_cli(struct node *n, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
assert(node_type(n));
|
2017-07-13 01:55:06 +02:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
if (node_type(n)->parse_cli) {
|
2017-08-03 00:19:27 +02:00
|
|
|
n->name = strdup("cli");
|
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
ret = node_type(n)->parse_cli(n, argc, argv);
|
2017-07-13 22:13:40 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-07-13 01:55:06 +02:00
|
|
|
|
2017-07-13 22:13:40 +02:00
|
|
|
n->state = STATE_PARSED;
|
|
|
|
}
|
|
|
|
else {
|
2017-08-03 00:19:27 +02:00
|
|
|
n->cfg = json_load_cli(argc, argv);
|
|
|
|
if (!n->cfg)
|
|
|
|
return -1;
|
2017-07-13 22:13:40 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
ret = node_parse(n, n->cfg, "cli");
|
2017-07-13 22:13:40 +02:00
|
|
|
}
|
2017-07-13 01:55:06 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int node_check(struct node *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
int ret;
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(n->state != STATE_DESTROYED);
|
2015-12-13 00:45:20 +01:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_check(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-10-09 13:04:52 +02:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_check(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
ret = node_type(n)->check ? node_type(n)->check(n) : 0;
|
2018-07-02 10:59:45 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
n->state = STATE_CHECKED;
|
|
|
|
|
|
|
|
return 0;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2014-12-05 12:39:52 +01:00
|
|
|
int node_start(struct node *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-11-16 10:51:00 +01:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(n->state == STATE_CHECKED);
|
2018-07-16 08:30:41 +02:00
|
|
|
assert(node_type(n)->state == STATE_STARTED);
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
info("Starting node %s", node_name_long(n));
|
2018-07-16 14:42:11 +02:00
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_start(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_start(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
ret = node_type(n)->start ? node_type(n)->start(n) : 0;
|
2017-03-13 23:51:38 -03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-06-05 09:34:56 +00:00
|
|
|
}
|
2017-03-13 23:51:38 -03:00
|
|
|
|
|
|
|
n->state = STATE_STARTED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-19 01:56:00 -04:00
|
|
|
n->sequence = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-16 10:51:00 +01:00
|
|
|
return ret;
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:39:52 +01:00
|
|
|
int node_stop(struct node *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-11-16 10:51:00 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-07-07 15:24:48 +02:00
|
|
|
if (n->state != STATE_STARTED && n->state != STATE_CONNECTED)
|
2017-03-29 04:25:30 +02:00
|
|
|
return 0;
|
2015-11-29 22:45:46 +01:00
|
|
|
|
|
|
|
info("Stopping node %s", node_name(n));
|
2018-07-16 14:42:11 +02:00
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_stop(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_stop(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
ret = node_type(n)->stop ? node_type(n)->stop(n) : 0;
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-16 10:51:00 +01:00
|
|
|
if (ret == 0)
|
2017-03-11 23:50:30 -03:00
|
|
|
n->state = STATE_STOPPED;
|
2015-11-16 10:51:00 +01:00
|
|
|
|
|
|
|
return ret;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2015-03-18 16:18:10 +01:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int node_destroy(struct node *n)
|
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
int ret;
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(n->state != STATE_DESTROYED && n->state != STATE_STARTED);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_destroy(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = node_direction_destroy(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
if (node_type(n)->destroy){
|
|
|
|
ret = (int) node_type(n)->destroy(n);
|
2018-07-09 12:55:36 +02:00
|
|
|
if(ret){
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
list_remove(&node_type(n)->instances, n);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
if (n->_vd)
|
|
|
|
free(n->_vd);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
if (n->_name)
|
|
|
|
free(n->_name);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-24 18:59:45 +02:00
|
|
|
if (n->_name_long)
|
|
|
|
free(n->_name_long);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-01 11:56:47 +02:00
|
|
|
if (n->name)
|
|
|
|
free(n->name);
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
n->state = STATE_DESTROYED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int node_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2017-12-09 02:23:29 +08:00
|
|
|
int readd, nread = 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-07-16 08:30:59 +02:00
|
|
|
assert(n->state == STATE_STARTED);
|
|
|
|
assert(node_type(n)->read);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
/* Send in parts if vector not supported */
|
2018-07-16 08:30:41 +02:00
|
|
|
if (node_type(n)->vectorize > 0 && node_type(n)->vectorize < cnt) {
|
2017-03-11 23:50:30 -03:00
|
|
|
while (cnt - nread > 0) {
|
2018-07-16 08:30:41 +02:00
|
|
|
readd = node_type(n)->read(n, &smps[nread], MIN(cnt - nread, node_type(n)->vectorize), release);
|
2017-08-22 12:31:12 +02:00
|
|
|
if (readd < 0)
|
|
|
|
return readd;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-07-02 18:58:58 +02:00
|
|
|
nread += readd;
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-07-16 08:30:41 +02:00
|
|
|
nread = node_type(n)->read(n, smps, cnt, release);
|
2017-08-22 12:31:12 +02:00
|
|
|
if (nread < 0)
|
|
|
|
return nread;
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 14:28:55 +02:00
|
|
|
/* Add missing fields */
|
|
|
|
for (int i = 0; i < nread; i++) {
|
2017-03-11 23:50:30 -03:00
|
|
|
smps[i]->source = n;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (!(smps[i]->flags & SAMPLE_HAS_SEQUENCE))
|
2017-09-04 14:28:55 +02:00
|
|
|
smps[i]->sequence = n->sequence++;
|
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (!(smps[i]->flags & SAMPLE_HAS_ORIGIN) ||
|
|
|
|
!(smps[i]->flags & SAMPLE_HAS_RECEIVED)) {
|
2017-09-04 14:28:55 +02:00
|
|
|
|
|
|
|
struct timespec now = time_now();
|
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (!(smps[i]->flags & SAMPLE_HAS_RECEIVED))
|
2017-09-04 14:28:55 +02:00
|
|
|
smps[i]->ts.received = now;
|
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (!(smps[i]->flags & SAMPLE_HAS_ORIGIN))
|
2017-09-04 14:28:55 +02:00
|
|
|
smps[i]->ts.origin = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-09-04 14:28:55 +02:00
|
|
|
/* Run read hooks */
|
2018-05-24 09:04:41 +02:00
|
|
|
int rread = hook_read_list(&n->in.hooks, smps, nread);
|
2018-05-23 02:25:27 +02:00
|
|
|
int skipped = nread - rread;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-05-23 02:25:27 +02:00
|
|
|
if (skipped > 0 && n->stats != NULL) {
|
|
|
|
stats_update(n->stats, STATS_SKIPPED, skipped);
|
2017-09-02 14:27:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 02:25:27 +02:00
|
|
|
debug(LOG_NODES | 5, "Received %u samples from node %s of which %d have been skipped", nread, node_name(n), skipped);
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
return rread;
|
2017-12-09 02:23:29 +08:00
|
|
|
#else
|
2018-05-23 02:25:27 +02:00
|
|
|
debug(LOG_NODES | 5, "Received %u samples from node %s", nread, node_name(n));
|
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
return nread;
|
|
|
|
#endif /* WITH_HOOKS */
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
int node_write(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2017-07-02 18:58:58 +02:00
|
|
|
int sent, nsent = 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-07-16 08:30:59 +02:00
|
|
|
assert(n->state == STATE_STARTED);
|
|
|
|
assert(node_type(n)->write);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-09-16 15:04:59 +02:00
|
|
|
/* Run write hooks */
|
2018-05-24 09:04:41 +02:00
|
|
|
cnt = hook_write_list(&n->out.hooks, smps, cnt);
|
2017-09-02 14:27:58 +02:00
|
|
|
if (cnt <= 0)
|
|
|
|
return cnt;
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
/* Send in parts if vector not supported */
|
2018-07-16 08:30:41 +02:00
|
|
|
if (node_type(n)->vectorize > 0 && node_type(n)->vectorize < cnt) {
|
2017-07-02 18:58:58 +02:00
|
|
|
while (cnt - nsent > 0) {
|
2018-07-16 08:30:41 +02:00
|
|
|
sent = node_type(n)->write(n, &smps[nsent], MIN(cnt - nsent, node_type(n)->vectorize), release);
|
2017-08-22 12:31:12 +02:00
|
|
|
if (sent < 0)
|
|
|
|
return sent;
|
|
|
|
|
2017-07-02 18:58:58 +02:00
|
|
|
nsent += sent;
|
|
|
|
debug(LOG_NODES | 5, "Sent %u samples to node %s", sent, node_name(n));
|
|
|
|
}
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
else {
|
2018-07-16 08:30:41 +02:00
|
|
|
nsent = node_type(n)->write(n, smps, cnt, release);
|
2017-08-22 12:31:12 +02:00
|
|
|
if (nsent < 0)
|
|
|
|
return nsent;
|
|
|
|
|
2017-07-02 18:58:58 +02:00
|
|
|
debug(LOG_NODES | 5, "Sent %u samples to node %s", nsent, node_name(n));
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return nsent;
|
|
|
|
}
|
|
|
|
|
2015-12-12 12:40:55 +01:00
|
|
|
char * node_name(struct node *n)
|
2015-10-17 19:05:15 +02:00
|
|
|
{
|
2015-11-29 22:45:46 +01:00
|
|
|
if (!n->_name)
|
2018-07-16 08:30:41 +02:00
|
|
|
strcatf(&n->_name, CLR_RED("%s") "(" CLR_YEL("%s") ")", n->name, plugin_name(node_type(n)));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
return n->_name;
|
|
|
|
}
|
2015-10-17 19:05:15 +02:00
|
|
|
|
2015-12-12 12:40:55 +01:00
|
|
|
char * node_name_long(struct node *n)
|
2015-11-29 22:45:46 +01:00
|
|
|
{
|
2015-12-11 18:19:35 +01:00
|
|
|
if (!n->_name_long) {
|
2018-07-16 08:30:41 +02:00
|
|
|
if (node_type(n)->print) {
|
|
|
|
struct node_type *vt = node_type(n);
|
2017-04-06 12:12:56 +02:00
|
|
|
char *name_long = vt->print(n);
|
2018-05-24 09:04:41 +02:00
|
|
|
strcatf(&n->_name_long, "%s: #in.hooks=%zu, in.vectorize=%d, #out.hooks=%zu, out.vectorize=%d, samplelen=%d, %s",
|
|
|
|
node_name(n),
|
|
|
|
list_length(&n->in.hooks), n->in.vectorize,
|
|
|
|
list_length(&n->out.hooks), n->out.vectorize,
|
|
|
|
n->samplelen, name_long);
|
|
|
|
|
2015-12-11 18:19:35 +01:00
|
|
|
free(name_long);
|
|
|
|
}
|
|
|
|
else
|
2017-05-05 19:24:16 +00:00
|
|
|
n->_name_long = node_name(n);
|
2015-12-11 18:19:35 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
return n->_name_long;
|
2015-10-17 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 02:02:29 +01:00
|
|
|
const char * node_name_short(struct node *n)
|
|
|
|
{
|
|
|
|
return n->name;
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int node_reverse(struct node *n)
|
2015-03-18 16:18:10 +01:00
|
|
|
{
|
2018-07-16 08:30:41 +02:00
|
|
|
return node_type(n)->reverse ? node_type(n)->reverse(n) : -1;
|
2015-03-21 15:29:00 +01:00
|
|
|
}
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2017-08-30 00:22:58 +02:00
|
|
|
int node_fd(struct node *n)
|
|
|
|
{
|
2018-07-16 08:30:41 +02:00
|
|
|
return node_type(n)->fd ? node_type(n)->fd(n) : -1;
|
2017-08-30 00:22:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-16 14:42:23 +02:00
|
|
|
struct node_type * node_type(struct node *n)
|
|
|
|
{
|
2018-07-16 20:26:23 +02:00
|
|
|
assert(n->state != STATE_DESTROYED);
|
2018-07-16 14:42:23 +02:00
|
|
|
|
|
|
|
return n->_vt;
|
|
|
|
}
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
struct memory_type * node_memory_type(struct node *n, struct memory_type *parent)
|
2018-06-29 17:32:07 +02:00
|
|
|
{
|
2018-07-16 08:30:41 +02:00
|
|
|
return node_type(n)->memory_type ? node_type(n)->memory_type(n, parent) : &memory_hugepage;
|
2018-06-29 17:32:07 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int node_parse_list(struct list *list, json_t *cfg, struct list *all)
|
2015-03-21 15:23:57 +01:00
|
|
|
{
|
2016-11-20 12:59:37 -05:00
|
|
|
struct node *node;
|
2017-08-03 00:19:27 +02:00
|
|
|
const char *str;
|
|
|
|
char *allstr = NULL;
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
size_t index;
|
|
|
|
json_t *elm;
|
|
|
|
|
|
|
|
switch (json_typeof(cfg)) {
|
|
|
|
case JSON_STRING:
|
|
|
|
str = json_string_value(cfg);
|
|
|
|
node = list_lookup(all, str);
|
|
|
|
if (!node)
|
|
|
|
goto invalid2;
|
|
|
|
|
|
|
|
list_push(list, node);
|
2016-11-20 12:59:37 -05:00
|
|
|
break;
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
case JSON_ARRAY:
|
|
|
|
json_array_foreach(cfg, index, elm) {
|
|
|
|
if (!json_is_string(elm))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
node = list_lookup(all, json_string_value(elm));
|
|
|
|
if (!node)
|
2017-09-18 22:56:40 +02:00
|
|
|
goto invalid;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
list_push(list, node);
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-03 00:19:27 +02:00
|
|
|
goto invalid;
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
error("The node list must be an a single or an array of strings referring to the keys of the 'nodes' section");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
invalid2:
|
|
|
|
for (size_t i = 0; i < list_length(all); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct node *n = (struct node *) list_at(all, i);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
strcatf(&allstr, " %s", node_name_short(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
error("Unknown node '%s'. Choose of one of: %s", str, allstr);
|
|
|
|
|
2017-08-28 14:35:50 +02:00
|
|
|
return 0;
|
2017-09-02 14:20:38 +02:00
|
|
|
}
|