1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/path.c

604 lines
14 KiB
C
Raw Normal View History

/** Message paths.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @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-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-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
*********************************************************************************/
2016-06-08 22:38:21 +02:00
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
2016-06-08 22:38:21 +02:00
#include <string.h>
#include <inttypes.h>
2017-08-30 23:53:35 +02:00
#include <poll.h>
2016-06-08 22:38:21 +02:00
#include "config.h"
#include "utils.h"
#include "path.h"
#include "timing.h"
#include "pool.h"
2016-06-08 22:38:21 +02:00
#include "queue.h"
2016-11-20 13:11:37 -05:00
#include "hook.h"
#include "plugin.h"
#include "memory.h"
#include "stats.h"
#include "node.h"
static int path_source_init(struct path_source *ps)
{
int ret;
ret = pool_init(&ps->pool, MAX(DEFAULT_QUEUELEN, ps->node->vectorize), SAMPLE_LEN(ps->node->samplelen), &memtype_hugepage);
if (ret)
return ret;
return 0;
}
static int path_source_destroy(struct path_source *ps)
{
int ret;
ret = pool_destroy(&ps->pool);
if (ret)
return ret;
return 0;
}
static void path_source_read(struct path *p, struct path_source *ps)
{
2017-08-30 23:53:35 +02:00
int ready, recv, mux, enqueue, enqueued;
2016-11-07 22:17:45 -05:00
int cnt = ps->node->vectorize;
2015-08-07 01:11:43 +02:00
2017-08-30 23:53:35 +02:00
struct sample *read_smps[cnt];
struct sample *muxed_smps[cnt];
2015-08-07 01:11:43 +02:00
2016-11-07 22:17:45 -05:00
/* Fill smps[] free sample blocks from the pool */
2017-08-30 23:53:35 +02:00
ready = sample_alloc(&ps->pool, read_smps, cnt);
2016-11-07 22:17:45 -05:00
if (ready != cnt)
2017-08-30 23:53:35 +02:00
warn("Pool underrun for path source %s", node_name(ps->node));
2015-08-07 01:11:43 +02:00
2016-11-07 22:17:45 -05:00
/* Read ready samples and store them to blocks pointed by smps[] */
2017-08-30 23:53:35 +02:00
recv = node_read(ps->node, read_smps, ready);
if (recv == 0)
goto out2;
else if (recv < 0)
2016-11-07 22:17:45 -05:00
error("Failed to receive message from node %s", node_name(ps->node));
else if (recv < ready)
2017-07-06 23:48:19 +02:00
warn("Partial read for path %s: read=%u, expected=%u", path_name(p), recv, ready);
2017-08-30 23:53:35 +02:00
/* Mux samples */
mux = sample_alloc(&p->pool, muxed_smps, recv);
if (mux != recv)
warn("Pool underrun for path %s", path_name(p));
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
for (int i = 0; i < mux; i++) {
mapping_remap(&ps->mappings, p->last_sample, read_smps[i], NULL);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
p->last_sample->sequence = p->sequence++;
sample_copy(muxed_smps[i], p->last_sample);
}
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
/* Run processing hooks */
enqueue = hook_process_list(&p->hooks, muxed_smps, mux);
if (enqueue == 0)
goto out1;
2017-08-30 23:53:35 +02:00
2017-04-05 12:40:21 +02:00
/* Keep track of the lowest index that wasn't enqueued;
* all following samples must be freed here */
for (size_t i = 0; i < list_length(&p->destinations); i++) {
struct path_destination *pd = list_at(&p->destinations, i);
2017-08-30 23:53:35 +02:00
enqueued = queue_push_many(&pd->queue, (void **) muxed_smps, enqueue);
2016-11-07 22:17:45 -05:00
if (enqueue != enqueued)
warn("Queue overrun for path %s", path_name(p));
2017-04-17 23:54:44 +02:00
/* Increase reference counter of these samples as they are now also owned by the queue. */
2017-08-30 23:53:35 +02:00
sample_get_many(muxed_smps, enqueued);
2016-11-07 22:17:45 -05:00
debug(LOG_PATH | 15, "Enqueued %u samples from %s to queue of %s", enqueued, node_name(ps->node), node_name(pd->node));
2016-11-07 22:17:45 -05:00
}
2017-04-17 23:54:44 +02:00
out1: sample_put_many(muxed_smps, recv);
out2: sample_put_many(read_smps, ready);
}
static int path_destination_init(struct path_destination *pd, int queuelen)
{
int ret;
ret = queue_init(&pd->queue, queuelen, &memtype_hugepage);
if (ret)
return ret;
return 0;
}
static int path_destination_destroy(struct path_destination *pd)
{
int ret;
ret = queue_destroy(&pd->queue);
if (ret)
return ret;
return 0;
2017-08-30 23:53:35 +02:00
}
static void path_poll(struct path *p)
{
int ret;
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
ret = poll(p->reader.pfds, p->reader.nfds, -1);
if (ret < 0)
serror("Failed to poll");
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
int updates = 0;
for (int i = 0; i < p->reader.nfds; i++) {
struct path_source *ps = list_at(&p->sources, i);
if (p->reader.pfds[i].revents & POLLIN) {
path_source_read(p, ps);
2017-08-30 23:53:35 +02:00
updates++;
}
}
2016-11-07 22:17:45 -05:00
}
static void path_write(struct path *p)
{
for (size_t i = 0; i < list_length(&p->destinations); i++) {
struct path_destination *pd = list_at(&p->destinations, i);
2016-11-07 22:17:45 -05:00
int cnt = pd->node->vectorize;
int sent;
int available;
int released;
struct sample *smps[cnt];
/* As long as there are still samples in the queue */
while (1) {
available = queue_pull_many(&pd->queue, (void **) smps, cnt);
2016-10-20 18:05:56 -04:00
if (available == 0)
2016-11-07 22:17:45 -05:00
break;
else if (available < cnt)
debug(LOG_PATH | 5, "Queue underrun for path %s: available=%u expected=%u", path_name(p), available, cnt);
debug(LOG_PATH | 15, "Dequeued %u samples from queue of node %s which is part of path %s", available, node_name(pd->node), path_name(p));
2016-11-07 22:17:45 -05:00
sent = node_write(pd->node, smps, available);
2016-10-20 18:05:56 -04:00
if (sent < 0)
2016-11-07 22:17:45 -05:00
error("Failed to sent %u samples to node %s", cnt, node_name(pd->node));
else if (sent < available)
warn("Partial write to node %s: written=%d, expected=%d", node_name(pd->node), sent, available);
2016-10-20 18:05:56 -04:00
released = sample_put_many(smps, sent);
debug(LOG_PATH | 15, "Released %d samples back to memory pool", released);
2016-10-20 18:05:56 -04:00
}
}
2016-11-07 22:17:45 -05:00
}
/** Main thread function per path: receive -> sent messages */
static void * path_run(void *arg)
{
struct path *p = arg;
for (;;) {
/* We only need to poll in case there is more than one source */
2017-09-03 10:52:46 +02:00
path_poll(p);
2016-11-07 22:17:45 -05:00
path_write(p);
}
return NULL;
}
int path_init(struct path *p)
2015-12-11 17:56:14 +01:00
{
int ret;
2015-08-07 01:11:43 +02:00
assert(p->state == STATE_DESTROYED);
2014-12-05 12:39:52 +01:00
list_init(&p->hooks);
list_init(&p->destinations);
2017-08-28 14:38:30 +02:00
list_init(&p->sources);
2017-09-02 14:20:38 +02:00
2017-08-27 18:01:12 +02:00
p->_name = NULL;
/* Default values */
p->reverse = 0;
p->enabled = 1;
p->queuelen = DEFAULT_QUEUELEN;
/* Add internal hooks if they are not already in the list */
for (size_t i = 0; i < list_length(&plugins); i++) {
struct plugin *q = list_at(&plugins, i);
if (q->type != PLUGIN_TYPE_HOOK)
continue;
struct hook_type *vt = &q->hook;
if ((vt->flags & HOOK_PATH) && (vt->flags & HOOK_BUILTIN)) {
struct hook *h = alloc(sizeof(struct hook));
ret = hook_init(h, vt, p, NULL);
if (ret)
return ret;
list_push(&p->hooks, h);
}
}
p->state = STATE_INITIALIZED;
2016-06-08 22:38:21 +02:00
return 0;
}
2017-08-30 23:53:35 +02:00
int path_init2(struct path *p)
{
int ret;
2017-08-30 23:53:35 +02:00
assert(p->state == STATE_CHECKED);
/* We sort the hooks according to their priority before starting the path */
list_sort(&p->hooks, hook_cmp_priority);
/* Initialize destinations */
for (size_t i = 0; i < list_length(&p->destinations); i++) {
struct path_destination *pd = list_at(&p->destinations, i);
ret = path_destination_init(pd, p->queuelen);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
}
/* Initialize sources */
for (size_t i = 0; i < list_length(&p->sources); i++) {
struct path_source *ps = list_at(&p->sources, i);
ret = path_source_init(ps);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
}
/* Calc sample length of path */
p->samplelen = 0;
for (size_t i = 0; i < list_length(&p->sources); i++) {
struct path_source *ps = list_at(&p->sources, i);
for (size_t i = 0; i < list_length(&ps->mappings); i++) {
struct mapping_entry *me = list_at(&ps->mappings, i);
if (me->offset + me->length > p->samplelen)
p->samplelen = me->offset + me->length;
}
2017-08-30 23:53:35 +02:00
}
ret = pool_init(&p->pool, MAX(1, list_length(&p->destinations)) * p->queuelen, SAMPLE_LEN(p->samplelen), &memtype_hugepage);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
sample_alloc(&p->pool, &p->last_sample, 1);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
/* Prepare poll() */
p->reader.nfds = list_length(&p->sources);
p->reader.pfds = alloc(sizeof(struct pollfd) * p->reader.nfds);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
for (int i = 0; i < p->reader.nfds; i++) {
struct path_source *ps = list_at(&p->sources, i);
p->reader.pfds[i].fd = node_fd(ps->node);
p->reader.pfds[i].events = POLLIN;
}
return 0;
}
int path_parse(struct path *p, json_t *cfg, struct list *nodes)
2015-12-11 17:56:14 +01:00
{
int ret;
json_error_t err;
2017-08-28 14:38:30 +02:00
json_t *json_in;
json_t *json_out = NULL;
json_t *json_hooks = NULL;
2015-08-07 01:11:43 +02:00
2017-08-28 14:38:30 +02:00
struct list sources = { .state = STATE_DESTROYED };
struct list destinations = { .state = STATE_DESTROYED };
2017-08-28 14:38:30 +02:00
list_init(&sources);
list_init(&destinations);
2017-08-28 14:38:30 +02:00
ret = json_unpack_ex(cfg, &err, 0, "{ s: o, s?: o, s?: o, s?: b, s?: b, s?: i, s?: i }",
"in", &json_in,
"out", &json_out,
"hooks", &json_hooks,
"reverse", &p->reverse,
"enabled", &p->enabled,
"queuelen", &p->queuelen
);
if (ret)
jerror(&err, "Failed to parse path configuration");
2017-08-28 14:38:30 +02:00
/* Input node(s) */
ret = mapping_parse_list(&sources, json_in, nodes);
2017-08-28 14:38:30 +02:00
if (ret)
error("Failed to parse input mapping of path %s", path_name(p));
/* Optional settings */
/* Output node(s) */
2017-08-28 14:38:30 +02:00
if (json_out) {
ret = node_parse_list(&destinations, json_out, nodes);
if (ret)
jerror(&err, "Failed to parse output nodes");
}
2017-08-28 14:38:30 +02:00
for (size_t i = 0; i < list_length(&sources); i++) {
struct mapping_entry *me = list_at(&sources, i);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
struct path_source *ps = NULL;
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
/* Check if there is already a path_source for this source */
for (size_t j = 0; j < list_length(&p->sources); j++) {
struct path_source *pt = list_at(&p->sources, j);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
if (pt->node == me->node) {
ps = pt;
break;
}
}
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
if (!ps) {
ps = alloc(sizeof(struct path_source));
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
ps->node = me->node;
ps->mappings.state = STATE_DESTROYED;
2017-08-30 23:53:35 +02:00
list_init(&ps->mappings);
2017-08-30 23:53:35 +02:00
list_push(&p->sources, ps);
}
list_push(&ps->mappings, me);
}
for (size_t i = 0; i < list_length(&destinations); i++) {
struct node *n = list_at(&destinations, i);
struct path_destination *pd = alloc(sizeof(struct path_destination));
2017-07-06 23:48:19 +02:00
pd->node = n;
list_push(&p->destinations, pd);
}
2017-08-30 23:53:35 +02:00
if (json_hooks) {
ret = hook_parse_list(&p->hooks, json_hooks, p, NULL);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
}
2017-08-28 14:38:30 +02:00
list_destroy(&sources, NULL, false);
list_destroy(&destinations, NULL, false);
p->cfg = cfg;
p->state = STATE_PARSED;
return 0;
}
int path_check(struct path *p)
{
2017-03-13 23:51:38 -03:00
assert(p->state != STATE_DESTROYED);
2017-08-28 14:38:30 +02:00
for (size_t i = 0; i < list_length(&p->sources); i++) {
struct path_source *ps = list_at(&p->sources, i);
if (!ps->node->_vt->read)
error("Source node '%s' is not supported as a source for path '%s'", node_name(ps->node), path_name(p));
}
for (size_t i = 0; i < list_length(&p->destinations); i++) {
struct path_destination *pd = list_at(&p->destinations, i);
if (!pd->node->_vt->write)
error("Destiation node '%s' is not supported as a sink for path '%s'", node_name(pd->node), path_name(p));
}
2017-08-28 14:38:30 +02:00
if (!IS_POW2(p->queuelen)) {
p->queuelen = LOG2_CEIL(p->queuelen);
warn("Queue length should always be a power of 2. Adjusting to %d", p->queuelen);
}
p->state = STATE_CHECKED;
return 0;
}
int path_start(struct path *p)
2016-11-07 22:17:45 -05:00
{
int ret;
assert(p->state == STATE_CHECKED);
2017-08-31 11:31:43 +02:00
info("Starting path %s: enabled=%s, reversed=%s, queuelen=%d, samplelen=%d, #hooks=%zu, #sources=%zu, #destinations=%zu",
path_name(p),
p->enabled ? "yes": "no",
p->reverse ? "yes": "no",
2017-09-03 10:52:46 +02:00
p->queuelen, p->samplelen,
2017-08-31 11:31:43 +02:00
list_length(&p->hooks),
list_length(&p->sources),
list_length(&p->destinations)
);
for (size_t i = 0; i < list_length(&p->hooks); i++) {
struct hook *h = list_at(&p->hooks, i);
ret = hook_start(h);
2017-03-17 01:08:48 -03:00
if (ret)
return ret;
}
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
p->sequence = 0;
2017-03-13 23:51:38 -03:00
/* Start one thread per path for sending to destinations */
2017-09-03 10:52:46 +02:00
ret = pthread_create(&p->tid, NULL, &path_run, p);
if (ret)
return ret;
p->state = STATE_STARTED;
return 0;
2016-11-07 22:17:45 -05:00
}
int path_stop(struct path *p)
2016-11-07 22:17:45 -05:00
{
int ret;
if (p->state != STATE_STARTED)
return 0;
info("Stopping path: %s", path_name(p));
2017-09-03 10:52:46 +02:00
ret = pthread_cancel(p->tid);
if (ret)
return ret;
ret = pthread_join(p->tid, NULL);
if (ret)
return ret;
for (size_t i = 0; i < list_length(&p->hooks); i++) {
struct hook *h = list_at(&p->hooks, i);
ret = hook_stop(h);
2017-03-17 01:08:48 -03:00
if (ret)
return ret;
}
p->state = STATE_STOPPED;
return 0;
2016-11-07 22:17:45 -05:00
}
int path_destroy(struct path *p)
2016-06-08 22:38:21 +02:00
{
if (p->state == STATE_DESTROYED)
return 0;
2016-11-07 22:17:45 -05:00
list_destroy(&p->hooks, (dtor_cb_t) hook_destroy, true);
2017-08-28 14:38:30 +02:00
list_destroy(&p->sources, (dtor_cb_t) path_source_destroy, true);
2016-11-07 22:17:45 -05:00
list_destroy(&p->destinations, (dtor_cb_t) path_destination_destroy, true);
if (p->_name)
free(p->_name);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
pool_destroy(&p->pool);
p->state = STATE_DESTROYED;
return 0;
}
const char * path_name(struct path *p)
{
if (!p->_name) {
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, "[");
2017-08-28 14:38:30 +02:00
for (size_t i = 0; i < list_length(&p->sources); i++) {
struct path_source *ps = list_at(&p->sources, i);
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " %s", node_name_short(ps->node));
}
2017-09-02 14:20:38 +02:00
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " ] " CLR_MAG("=>") " [");
2017-08-28 14:38:30 +02:00
for (size_t i = 0; i < list_length(&p->destinations); i++) {
struct path_destination *pd = list_at(&p->destinations, i);
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " %s", node_name_short(pd->node));
}
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " ]");
}
return p->_name;
}
2017-08-28 14:38:30 +02:00
int path_uses_node(struct path *p, struct node *n)
{
for (size_t i = 0; i < list_length(&p->destinations); i++) {
struct path_destination *pd = list_at(&p->destinations, i);
2016-11-07 22:17:45 -05:00
if (pd->node == n)
return 0;
}
2017-09-02 14:20:38 +02:00
2017-08-28 14:38:30 +02:00
for (size_t i = 0; i < list_length(&p->sources); i++) {
struct path_source *ps = list_at(&p->sources, i);
if (ps->node == n)
return 0;
}
2016-11-07 22:17:45 -05:00
2017-08-28 14:38:30 +02:00
return -1;
}
2016-11-07 22:17:45 -05:00
int path_reverse(struct path *p, struct path *r)
{
int ret;
2017-08-28 14:38:30 +02:00
if (list_length(&p->destinations) != 1 || list_length(&p->sources) != 1)
2016-11-07 22:17:45 -05:00
return -1;
2017-09-02 14:20:38 +02:00
2016-11-07 22:17:45 -05:00
/* General */
r->enabled = p->enabled;
2017-09-02 14:20:38 +02:00
2017-08-28 14:38:30 +02:00
/* Source / Destinations */
struct path_destination *orig_pd = list_first(&p->destinations);
struct path_source *orig_ps = list_first(&p->sources);
2017-08-28 14:38:30 +02:00
struct path_destination *new_pd = alloc(sizeof(struct path_destination));
struct path_source *new_ps = alloc(sizeof(struct path_source));
2016-11-07 22:17:45 -05:00
2017-08-28 14:38:30 +02:00
new_pd->node = orig_ps->node;
2017-09-02 14:20:38 +02:00
2017-08-28 14:38:30 +02:00
new_ps->node = orig_pd->node;
2017-08-28 14:38:30 +02:00
list_push(&r->destinations, new_pd);
list_push(&r->sources, new_ps);
2016-11-07 22:17:45 -05:00
for (size_t i = 0; i < list_length(&p->hooks); i++) {
struct hook *h = list_at(&p->hooks, i);
struct hook *g = alloc(sizeof(struct hook));
ret = hook_init(g, h->_vt, r, NULL);
2016-11-07 22:17:45 -05:00
if (ret)
return ret;
list_push(&r->hooks, g);
}
2016-11-07 22:17:45 -05:00
return 0;
2017-04-05 12:40:21 +02:00
}