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

208 lines
4.6 KiB
C
Raw Normal View History

/** Message paths.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. 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
*********************************************************************************/
#include <unistd.h>
#include <inttypes.h>
#include "utils.h"
#include "path.h"
#include "timing.h"
2015-06-02 22:04:03 +02:00
#include "config.h"
#include "pool.h"
static void path_write(struct path *p)
{
list_foreach(struct node *n, &p->destinations) {
int sent = node_write(
n, /* Destination node */
&p->pool, /* Pool of received messages */
n->vectorize /* Number of messages which should be sent */
);
debug(15, "Sent %u messages to node %s", sent, node_name(n));
p->sent += sent;
2015-08-07 01:11:43 +02:00
p->ts.sent = time_now(); /** @todo use hardware timestamps for socket node type */
}
}
/** 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);
}
if (p->received == 0)
continue;
if (hook_run(p, HOOK_ASYNC))
continue;
path_write(p);
}
return NULL;
}
/** Receive messages */
static void * path_run(void *arg)
{
2015-03-17 23:23:13 +01:00
struct path *p = arg;
2015-08-07 01:11:43 +02:00
/* Main thread loop */
for (;;) {
/* Receive message */
int recv = node_read(p->in, &p->pool, p->in->vectorize);
2015-10-09 15:49:26 +02:00
if (recv < 0)
error("Failed to receive message from node %s", node_name(p->in));
2015-10-09 15:49:26 +02:00
else if (recv == 0)
continue;
2015-08-07 01:11:43 +02:00
2016-01-14 22:56:16 +01:00
/** @todo Replace this timestamp by hardware timestamping for node type which support it. */
p->ts.last = p->ts.recv;
p->ts.recv = time_now();
2015-10-12 16:25:08 +02:00
debug(15, "Received %u messages from node %s", recv, node_name(p->in));
2015-08-07 01:11:43 +02:00
/* Run preprocessing hooks */
if (hook_run(p, HOOK_PRE)) {
p->skipped += recv;
continue;
}
2015-08-07 01:11:43 +02:00
/* For each received message... */
for (int i = 0; i < recv; i++) {
/* Update tail pointer of message pool by the amount of actually received messages. */
pool_push(&p->pool, 1);
p->received++;
2015-08-07 01:11:43 +02:00
/* Run hooks for filtering, stats collection and manipulation */
if (hook_run(p, HOOK_MSG)) {
p->skipped++;
continue;
}
}
2015-08-07 01:11:43 +02:00
/* Run post processing hooks */
if (hook_run(p, HOOK_POST)) {
p->skipped += recv;
continue;
}
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 */
if (!p->rate)
path_write(p);
}
return NULL;
}
int path_start(struct path *p)
2015-12-11 17:56:14 +01:00
{
info("Starting path: %s (poollen=%zu, msgsize=%zu, #hooks=%zu, rate=%.1f)",
path_name(p), pool_length(&p->pool), pool_stride(&p->pool), list_length(&p->hooks), p->rate);
/* We sort the hooks according to their priority before starting the path */
list_sort(&p->hooks, hooks_sort_priority);
2015-08-07 01:11:43 +02:00
if (hook_run(p, HOOK_PATH_START))
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
if (hook_run(p, HOOK_PATH_STOP))
return -1;
return 0;
}
const char * path_name(struct path *p)
{
if (!p->_name) {
strcatf(&p->_name, "%s " MAG("=>"), p->in->name);
2015-08-07 01:11:43 +02:00
list_foreach(struct node *n, &p->destinations) {
strcatf(&p->_name, " %s", n->name);
}
}
return p->_name;
}
struct path * path_create(size_t poolsize, size_t values)
{
struct path *p = alloc(sizeof(struct path));
list_init(&p->destinations, NULL);
list_init(&p->hooks, free);
pool_create(&p->pool, poolsize, 16 + values * sizeof(float)); /** @todo */
2015-08-07 01:11:43 +02:00
list_foreach(struct hook *h, &hooks) {
if (h->type & HOOK_INTERNAL)
list_push(&p->hooks, memdup(h, sizeof(*h)));
}
2015-11-16 10:51:00 +01:00
p->state = PATH_CREATED;
return p;
}
void path_destroy(struct path *p)
{
list_destroy(&p->destinations);
list_destroy(&p->hooks);
pool_destroy(&p->pool);
2015-08-07 01:11:43 +02:00
free(p->_name);
free(p);
}
int path_uses_node(struct path *p, struct node *n) {
return (p->in == n) || list_contains(&p->destinations, n) ? 0 : 1;
}