2014-07-14 11:49:44 +00:00
|
|
|
/** Message paths.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2016-02-09 05:33:19 +01:00
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
2016-06-08 23:21:42 +02:00
|
|
|
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
|
2015-08-07 01:11:43 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2014-06-25 17:50:30 +00:00
|
|
|
#include <unistd.h>
|
2016-06-08 22:38:21 +02:00
|
|
|
#include <string.h>
|
2015-10-11 14:50:55 +02:00
|
|
|
#include <inttypes.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
#include "config.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
2015-06-02 21:59:31 +02:00
|
|
|
#include "timing.h"
|
2016-01-14 22:59:57 +01:00
|
|
|
#include "pool.h"
|
2016-06-08 22:38:21 +02:00
|
|
|
#include "queue.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
static void path_read(struct path *p)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2016-11-07 22:17:45 -05:00
|
|
|
int recv;
|
|
|
|
int enqueue;
|
|
|
|
int enqueued;
|
2016-06-08 22:38:21 +02:00
|
|
|
int ready = 0; /**< Number of blocks in smps[] which are allocated and ready to be used by node_read(). */
|
2016-11-07 22:17:45 -05:00
|
|
|
|
|
|
|
struct path_source *ps = p->source;
|
|
|
|
|
|
|
|
int cnt = ps->node->vectorize;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
struct sample *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 */
|
|
|
|
ready += sample_alloc(&ps->pool, smps, cnt - ready);
|
|
|
|
if (ready != cnt)
|
|
|
|
warn("Pool underrun for path %s", path_name(p));
|
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[] */
|
|
|
|
recv = node_read(ps->node, smps, ready);
|
|
|
|
if (recv < 0)
|
|
|
|
error("Failed to receive message from node %s", node_name(ps->node));
|
|
|
|
else if (recv < ready)
|
|
|
|
warn("Partial read for path %s: read=%u expected=%u", path_name(p), recv, ready);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
debug(DBG_PATH | 15, "Received %u messages from node %s", recv, node_name(ps->node));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
/* Run preprocessing hooks for vector of samples */
|
|
|
|
enqueue = hook_run(p, smps, recv, HOOK_READ);
|
|
|
|
if (enqueue != recv) {
|
|
|
|
info("Hooks skipped %u out of %u samples for path %s", recv - enqueue, recv, path_name(p));
|
|
|
|
|
|
|
|
stats_update(p->stats, STATS_SKIPPED, recv - enqueue);
|
|
|
|
}
|
2016-10-20 18:05:56 -04:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
list_foreach(struct path_destination *pd, &p->destinations) {
|
|
|
|
enqueued = queue_push_many(&pd->queue, (void **) smps, enqueue);
|
|
|
|
if (enqueue != enqueued)
|
|
|
|
warn("Queue overrun for path %s", path_name(p));
|
2016-10-20 18:05:56 -04:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
for (int i = 0; i < enqueued; i++)
|
|
|
|
sample_get(smps[i]); /* increase reference count */
|
|
|
|
|
|
|
|
debug(DBG_PATH | 15, "Enqueued %u samples to queue of path %s", enqueued, path_name(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void path_write(struct path *p)
|
|
|
|
{
|
|
|
|
list_foreach(struct path_destination *pd, &p->destinations) {
|
|
|
|
int cnt = pd->node->vectorize;
|
|
|
|
int sent;
|
|
|
|
int tosend;
|
|
|
|
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)
|
|
|
|
warn("Queue underrun for path %s: available=%u expected=%u", path_name(p), available, cnt);
|
|
|
|
|
|
|
|
debug(DBG_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-10-20 18:05:56 -04:00
|
|
|
tosend = hook_run(p, smps, available, HOOK_WRITE);
|
|
|
|
if (tosend == 0)
|
|
|
|
continue;
|
2016-11-07 22:17:45 -05:00
|
|
|
|
|
|
|
sent = node_write(pd->node, smps, tosend);
|
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));
|
2016-10-20 18:05:56 -04:00
|
|
|
else if (sent < tosend)
|
2016-11-07 22:17:45 -05:00
|
|
|
warn("Partial write to node %s", node_name(pd->node));
|
2016-10-20 18:05:56 -04:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
debug(DBG_PATH | 15, "Sent %u messages to node %s", sent, node_name(pd->node));
|
2016-10-20 18:05:56 -04:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
released = 0;
|
|
|
|
for (int i = 0; i < sent; i++)
|
|
|
|
released += sample_put(smps[i]);
|
|
|
|
|
|
|
|
debug(DBG_PATH | 15, "Released %d samples back to memory pool", released);
|
2016-10-20 18:05:56 -04:00
|
|
|
}
|
2015-03-21 18:04:52 +01: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 (;;) {
|
|
|
|
path_read(p);
|
|
|
|
path_write(p);
|
|
|
|
}
|
2014-07-18 16:05:44 +00:00
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int path_start(struct path *p)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-10-20 18:01:06 -04:00
|
|
|
info("Starting path: %s (#hooks=%zu)",
|
|
|
|
path_name(p), list_length(&p->hooks));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
ret = hook_run(p, NULL, 0, HOOK_PATH_START);
|
|
|
|
if (ret)
|
2015-06-10 15:10:51 +02:00
|
|
|
return -1;
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-11-16 10:51:00 +01:00
|
|
|
p->state = PATH_RUNNING;
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2016-10-20 18:01:06 -04:00
|
|
|
return pthread_create(&p->tid, NULL, &path_run, p);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int path_stop(struct path *p)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-11-29 22:45:46 +01:00
|
|
|
info("Stopping path: %s", path_name(p));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-10-20 21:16:47 -04:00
|
|
|
pthread_cancel(p->tid);
|
|
|
|
pthread_join(p->tid, NULL);
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
if (hook_run(p, NULL, 0, HOOK_PATH_STOP))
|
2015-06-10 15:10:51 +02:00
|
|
|
return -1;
|
2015-04-01 13:25:03 +02:00
|
|
|
|
2016-10-20 18:04:18 -04:00
|
|
|
p->state = PATH_STOPPED;
|
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
return 0;
|
2014-07-04 09:47:28 +00:00
|
|
|
}
|
2015-03-18 15:45:06 +01:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
const char * path_name(struct path *p)
|
2015-03-18 15:45:06 +01:00
|
|
|
{
|
2015-11-29 22:45:46 +01:00
|
|
|
if (!p->_name) {
|
2016-11-07 22:17:45 -05:00
|
|
|
strcatf(&p->_name, "%s " MAG("=>"), node_name_short(p->source->node));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
list_foreach(struct path_destination *pd, &p->destinations)
|
|
|
|
strcatf(&p->_name, " %s", node_name_short(pd->node));
|
2015-10-17 19:05:15 +02:00
|
|
|
}
|
2015-10-13 15:40:34 +02:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
return p->_name;
|
2015-03-18 15:45:06 +01:00
|
|
|
}
|
2015-03-18 15:47:18 +01:00
|
|
|
|
2016-10-20 21:16:01 -04:00
|
|
|
int path_init(struct path *p)
|
2015-03-21 15:23:57 +01:00
|
|
|
{
|
2016-11-07 22:17:45 -05:00
|
|
|
int ret, max_queuelen = 0;
|
|
|
|
|
2016-10-20 21:16:01 -04:00
|
|
|
/* Add internal hooks if they are not already in the list*/
|
2015-10-09 13:30:41 +02:00
|
|
|
list_foreach(struct hook *h, &hooks) {
|
2016-11-07 22:17:45 -05:00
|
|
|
if (
|
2016-11-08 00:26:06 -05:00
|
|
|
(h->type & HOOK_AUTO) && /* should this hook be added implicitely? */
|
2016-11-07 22:17:45 -05:00
|
|
|
(list_lookup(&p->hooks, h->name) == NULL) /* is not already in list? */
|
|
|
|
)
|
2016-10-20 21:16:01 -04:00
|
|
|
list_push(&p->hooks, memdup(h, sizeof(struct hook)));
|
2015-10-09 13:30:41 +02:00
|
|
|
}
|
2016-11-07 22:17:45 -05:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/* We sort the hooks according to their priority before starting the path */
|
|
|
|
list_sort(&p->hooks, hooks_sort_priority);
|
|
|
|
|
|
|
|
ret = hook_run(p, NULL, 0, HOOK_INIT);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize hooks of path: %s", path_name(p));
|
|
|
|
|
|
|
|
/* Parse hook arguments */
|
|
|
|
ret = hook_run(p, NULL, 0, HOOK_PARSE);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to parse arguments for hooks of path: %s", path_name(p));
|
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
/* Initialize destinations */
|
|
|
|
list_foreach(struct path_destination *pd, &p->destinations) {
|
|
|
|
ret = queue_init(&pd->queue, pd->queuelen, &memtype_hugepage);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize queue for path");
|
|
|
|
|
|
|
|
if (pd->queuelen > max_queuelen)
|
|
|
|
max_queuelen = pd->queuelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize source */
|
|
|
|
ret = pool_init(&p->source->pool, max_queuelen, SAMPLE_LEN(p->source->samplelen), &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret)
|
2016-11-07 22:17:45 -05:00
|
|
|
error("Failed to allocate memory pool for path");
|
2016-10-20 18:04:18 -04:00
|
|
|
|
|
|
|
p->state = PATH_INITIALIZED;
|
2016-06-08 22:38:21 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-03-21 15:23:57 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
void path_source_destroy(struct path_source *ps)
|
|
|
|
{
|
|
|
|
pool_destroy(&ps->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void path_destination_destroy(struct path_destination *pd)
|
|
|
|
{
|
|
|
|
queue_destroy(&pd->queue);
|
|
|
|
}
|
|
|
|
|
2015-03-21 15:23:57 +01:00
|
|
|
void path_destroy(struct path *p)
|
2016-06-08 22:38:21 +02:00
|
|
|
{
|
2016-11-07 22:17:45 -05:00
|
|
|
list_destroy(&p->hooks, (dtor_cb_t) hook_destroy, true);
|
|
|
|
list_destroy(&p->destinations, (dtor_cb_t) path_destination_destroy, true);
|
2016-10-20 18:04:18 -04:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
path_source_destroy(p->source);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
free(p->_name);
|
2016-11-07 22:17:45 -05:00
|
|
|
free(p->source);
|
|
|
|
|
|
|
|
p->state = PATH_DESTROYED;
|
2015-03-18 15:47:18 +01:00
|
|
|
}
|
2016-01-14 22:52:08 +01:00
|
|
|
|
|
|
|
int path_uses_node(struct path *p, struct node *n) {
|
2016-11-07 22:17:45 -05:00
|
|
|
list_foreach(struct path_destination *pd, &p->destinations) {
|
|
|
|
if (pd->node == n)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p->source->node == n ? 0 : -1;
|
2016-01-14 22:52:08 +01:00
|
|
|
}
|
2016-11-07 22:17:45 -05:00
|
|
|
|
|
|
|
int path_reverse(struct path *p, struct path *r)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (list_length(&p->destinations) > 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct path_destination *first_pd = list_first(&p->destinations);
|
|
|
|
|
|
|
|
list_init(&r->destinations);
|
|
|
|
list_init(&r->hooks);
|
|
|
|
|
|
|
|
/* General */
|
|
|
|
r->enabled = p->enabled;
|
|
|
|
r->cfg = p->cfg;
|
|
|
|
|
|
|
|
struct path_destination *pd = alloc(sizeof(struct path_destination));
|
|
|
|
|
|
|
|
pd->node = p->source->node;
|
|
|
|
pd->queuelen = first_pd->queuelen;
|
|
|
|
|
|
|
|
list_push(&r->destinations, pd);
|
|
|
|
|
|
|
|
struct path_source *ps = alloc(sizeof(struct path_source));
|
|
|
|
|
|
|
|
ps->node = first_pd->node;
|
|
|
|
ps->samplelen = p->source->samplelen;
|
|
|
|
|
|
|
|
r->source = ps;
|
|
|
|
|
|
|
|
list_foreach(struct hook *h, &p->hooks) {
|
|
|
|
struct hook *hc = alloc(sizeof(struct hook));
|
|
|
|
|
|
|
|
ret = hook_copy(h, hc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
list_push(&r->hooks, hc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|