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-06-08 22:38:21 +02:00
|
|
|
static void path_write(struct path *p, bool resend)
|
2015-05-06 13:22:22 +02:00
|
|
|
{
|
2015-10-09 12:50:35 +02:00
|
|
|
list_foreach(struct node *n, &p->destinations) {
|
2016-06-08 22:38:21 +02:00
|
|
|
int cnt = n->vectorize;
|
2016-09-22 21:20:21 -04:00
|
|
|
int sent, tosend, available, released;
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *smps[n->vectorize];
|
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
available = mpmc_queue_pull_many(&p->queue, (void **) smps, cnt);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (available < cnt)
|
|
|
|
warn("Queue underrun for path %s: available=%u expected=%u", path_name(p), available, cnt);
|
|
|
|
|
|
|
|
if (available == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tosend = hook_run(p, smps, available, HOOK_WRITE);
|
|
|
|
if (tosend == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sent = node_write(n, smps, tosend);
|
|
|
|
if (sent < 0)
|
|
|
|
error("Failed to sent %u samples to node %s", cnt, node_name(n));
|
|
|
|
else if (sent < tosend)
|
|
|
|
warn("Partial write to node %s", node_name(n));
|
|
|
|
|
|
|
|
debug(DBG_PATH | 15, "Sent %u messages to node %s", sent, node_name(n));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
released = pool_put_many(&p->pool, (void **) smps, sent);
|
|
|
|
if (sent != released)
|
|
|
|
warn("Failed to release %u samples to pool for path %s", sent - released, path_name(p));
|
2015-05-06 13:22:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:53:01 +01:00
|
|
|
/** Send messages asynchronously */
|
2015-05-06 13:22:22 +02:00
|
|
|
static void * path_run_async(void *arg)
|
2014-06-25 17:50:30 +00:00
|
|
|
{
|
2015-03-21 15:30:42 +01:00
|
|
|
struct path *p = arg;
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-05-06 13:20:02 +02:00
|
|
|
/* Block until 1/p->rate seconds elapsed */
|
2015-10-11 08:46:11 +02:00
|
|
|
for (;;) {
|
|
|
|
/* Check for overruns */
|
|
|
|
uint64_t expir = timerfd_wait(p->tfd);
|
2016-01-14 22:57:39 +01:00
|
|
|
if (expir == 0)
|
|
|
|
perror("Failed to wait for timer");
|
|
|
|
else if (expir > 1) {
|
2015-10-11 08:46:11 +02:00
|
|
|
p->overrun += expir;
|
2015-10-11 14:50:55 +02:00
|
|
|
warn("Overrun detected for path: overruns=%" PRIu64, expir);
|
2015-10-11 08:46:11 +02:00
|
|
|
}
|
2015-12-19 16:15:51 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
if (hook_run(p, NULL, 0, HOOK_ASYNC))
|
2015-09-22 15:33:01 +02:00
|
|
|
continue;
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
path_write(p, true);
|
2015-09-22 15:33:01 +02:00
|
|
|
}
|
2014-06-25 17:50:30 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Receive messages */
|
2014-06-05 09:34:29 +00:00
|
|
|
static void * path_run(void *arg)
|
|
|
|
{
|
2015-03-17 23:23:13 +01:00
|
|
|
struct path *p = arg;
|
2016-06-08 22:38:21 +02:00
|
|
|
unsigned cnt = p->in->vectorize;
|
|
|
|
int recv, enqueue, enqueued;
|
|
|
|
int ready = 0; /**< Number of blocks in smps[] which are allocated and ready to be used by node_read(). */
|
|
|
|
struct sample *smps[cnt];
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* Main thread loop */
|
2015-10-11 08:46:11 +02:00
|
|
|
for (;;) {
|
2016-06-08 22:38:21 +02:00
|
|
|
/* Fill smps[] free sample blocks from the pool */
|
2016-10-10 23:07:41 +02:00
|
|
|
ready += sample_get_many(&p->pool, smps, cnt - ready);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ready != cnt)
|
|
|
|
warn("Pool underrun for path %s", path_name(p));
|
|
|
|
|
|
|
|
/* Read ready samples and store them to blocks pointed by smps[] */
|
|
|
|
recv = p->in->_vt->read(p->in, smps, ready);
|
2015-10-09 15:49:26 +02:00
|
|
|
if (recv < 0)
|
2015-11-29 22:45:46 +01:00
|
|
|
error("Failed to receive message from node %s", node_name(p->in));
|
2016-06-08 22:38:21 +02:00
|
|
|
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-06-08 22:38:21 +02:00
|
|
|
debug(DBG_PATH | 15, "Received %u messages from node %s", recv, node_name(p->in));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:38:21 +02: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));
|
|
|
|
p->skipped += recv - enqueue;
|
2015-06-10 15:10:51 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
enqueued = mpmc_queue_push_many(&p->queue, (void **) smps, enqueue);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (enqueue != enqueued)
|
|
|
|
warn("Failed to enqueue %u samples for path %s", enqueue - enqueued, path_name(p));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
ready -= enqueued;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
debug(DBG_PATH | 3, "Enqueuing %u samples to queue of path %s", enqueue, path_name(p));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-01-14 22:56:16 +01:00
|
|
|
/* At fixed rate mode, messages are send by another (asynchronous) thread */
|
2016-06-08 22:38:21 +02:00
|
|
|
if (p->rate == 0)
|
|
|
|
path_write(p, false);
|
2015-03-21 18:04:52 +01:00
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
info("Starting path: %s (#hooks=%zu, rate=%.1f)",
|
|
|
|
path_name(p), list_length(&p->hooks), p->rate);
|
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
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* At fixed rate mode, we start another thread for sending */
|
2015-06-10 15:13:02 +02:00
|
|
|
if (p->rate) {
|
2016-01-14 22:57:39 +01:00
|
|
|
p->tfd = timerfd_create_rate(p->rate);
|
2015-06-10 15:13:02 +02:00
|
|
|
if (p->tfd < 0)
|
|
|
|
serror("Failed to create timer");
|
|
|
|
|
2015-05-06 13:22:22 +02:00
|
|
|
pthread_create(&p->sent_tid, NULL, &path_run_async, p);
|
2015-06-10 15:13:02 +02:00
|
|
|
}
|
2015-11-16 10:51:00 +01:00
|
|
|
|
|
|
|
p->state = PATH_RUNNING;
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-11-16 10:51:00 +01:00
|
|
|
return pthread_create(&p->recv_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
|
|
|
|
2014-09-09 09:03:01 +00:00
|
|
|
pthread_cancel(p->recv_tid);
|
|
|
|
pthread_join(p->recv_tid, NULL);
|
2014-06-25 17:50:30 +00:00
|
|
|
|
|
|
|
if (p->rate) {
|
2014-09-09 09:03:01 +00:00
|
|
|
pthread_cancel(p->sent_tid);
|
|
|
|
pthread_join(p->sent_tid, NULL);
|
2015-03-21 15:30:42 +01:00
|
|
|
|
2015-03-31 16:58:25 +02:00
|
|
|
close(p->tfd);
|
2014-06-25 17:50:30 +00:00
|
|
|
}
|
2015-11-16 10:51:00 +01:00
|
|
|
|
|
|
|
p->state = PATH_STOPPED;
|
2015-08-07 01:11:43 +02: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
|
|
|
|
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-06-08 22:38:21 +02:00
|
|
|
strcatf(&p->_name, "%s " MAG("=>"), node_name_short(p->in));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
list_foreach(struct node *n, &p->destinations)
|
|
|
|
strcatf(&p->_name, " %s", node_name_short(n));
|
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-06-08 22:38:21 +02:00
|
|
|
void path_init(struct path *p)
|
2015-03-21 15:23:57 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
list_init(&p->destinations);
|
|
|
|
list_init(&p->hooks);
|
|
|
|
|
|
|
|
/* Initialize hook system */
|
2015-10-09 13:30:41 +02:00
|
|
|
list_foreach(struct hook *h, &hooks) {
|
|
|
|
if (h->type & HOOK_INTERNAL)
|
|
|
|
list_push(&p->hooks, memdup(h, sizeof(*h)));
|
|
|
|
}
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2015-11-16 10:51:00 +01:00
|
|
|
p->state = PATH_CREATED;
|
2016-06-08 22:38:21 +02:00
|
|
|
}
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
int path_prepare(struct path *p)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* We sort the hooks according to their priority before starting the path */
|
|
|
|
list_sort(&p->hooks, hooks_sort_priority);
|
|
|
|
|
|
|
|
/* Allocate hook private memory */
|
|
|
|
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));
|
|
|
|
|
|
|
|
/* Initialize queue */
|
2016-09-22 21:20:21 -04:00
|
|
|
ret = pool_init(&p->pool, SAMPLE_LEN(p->samplelen), p->queuelen, &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret)
|
|
|
|
error("Failed to allocate memory pool for path");
|
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
ret = mpmc_queue_init(&p->queue, p->queuelen, &memtype_hugepage);
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize queue for path");
|
|
|
|
|
|
|
|
return 0;
|
2015-03-21 15:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void path_destroy(struct path *p)
|
2016-06-08 22:38:21 +02:00
|
|
|
{
|
|
|
|
hook_run(p, NULL, 0, HOOK_DEINIT); /* Release memory */
|
|
|
|
|
|
|
|
list_destroy(&p->destinations, NULL, false);
|
|
|
|
list_destroy(&p->hooks, NULL, true);
|
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
mpmc_queue_destroy(&p->queue);
|
2016-01-14 22:59:57 +01:00
|
|
|
pool_destroy(&p->pool);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
free(p->_name);
|
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) {
|
|
|
|
return (p->in == n) || list_contains(&p->destinations, n) ? 0 : 1;
|
|
|
|
}
|