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

247 lines
6.1 KiB
C
Raw Normal View History

/** Message paths.
*
* @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
*********************************************************************************/
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>
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-06-08 22:38:21 +02:00
static void path_write(struct path *p, bool resend)
{
list_foreach(struct node *n, &p->destinations) {
2016-06-08 22:38:21 +02:00
int cnt = n->vectorize;
int sent, tosend, available, released;
2016-06-08 22:38:21 +02:00
struct sample *smps[n->vectorize];
2016-10-16 02:33:36 -04:00
available = 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));
2017-02-12 14:12:35 -03:00
debug(LOG_PATH | 15, "Sent %u messages to node %s", sent, node_name(n));
2015-08-07 01:11:43 +02: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));
}
}
/** Send messages asynchronously */
static void * path_run_async(void *arg)
{
2015-03-21 15:30:42 +01:00
struct path *p = arg;
/* Block until 1/p->rate seconds elapsed */
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) {
p->overrun += expir;
warn("Overrun detected for path: overruns=%" PRIu64, expir);
}
2016-06-08 22:38:21 +02:00
if (hook_run(p, NULL, 0, HOOK_ASYNC))
continue;
2016-06-08 22:38:21 +02:00
path_write(p, true);
}
return NULL;
}
/** Receive messages */
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
/* Main thread loop */
for (;;) {
2016-06-08 22:38:21 +02:00
/* Fill smps[] free sample blocks from the pool */
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)
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
2017-02-12 14:12:35 -03:00
debug(LOG_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-08-07 01:11:43 +02:00
2016-10-16 02:33:36 -04:00
enqueued = 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
2017-02-12 14:12:35 -03:00
debug(LOG_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);
}
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)
return -1;
2014-12-05 12:39:52 +01: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");
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;
2015-11-16 10:51:00 +01:00
return pthread_create(&p->recv_tid, NULL, &path_run, p);
}
int path_stop(struct path *p)
2015-12-11 17:56:14 +01:00
{
info("Stopping path: %s", path_name(p));
2015-08-07 01:11:43 +02:00
pthread_cancel(p->recv_tid);
pthread_join(p->recv_tid, NULL);
if (p->rate) {
pthread_cancel(p->sent_tid);
pthread_join(p->sent_tid, NULL);
2015-03-21 15:30:42 +01:00
close(p->tfd);
}
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))
return -1;
return 0;
}
const char * path_name(struct path *p)
{
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));
}
return p->_name;
}
2016-06-08 22:38:21 +02:00
void path_init(struct path *p)
{
2016-06-08 22:38:21 +02:00
list_init(&p->destinations);
list_init(&p->hooks);
/* Initialize hook system */
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
}
int path_destroy(struct path *p)
{
hook_run(p, NULL, 0, HOOK_DEINIT); /* Release memory */
list_destroy(&p->destinations, NULL, false);
list_destroy(&p->hooks, NULL, true);
queue_destroy(&p->queue);
pool_destroy(&p->pool);
free(p->_name);
return 0;
}
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 */
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-10-16 02:33:36 -04:00
ret = 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;
}
int path_uses_node(struct path *p, struct node *n) {
return (p->in == n) || list_contains(&p->destinations, n) ? 0 : 1;
}