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/server/src/path.c

234 lines
4.4 KiB
C
Raw Normal View History

/** Message paths.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
*/
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sys/syscall.h>
#include "utils.h"
#include "path.h"
2015-03-21 15:29:00 +01:00
#ifndef sigev_notify_thread_id
#define sigev_notify_thread_id _sigev_un._tid
#endif
/** Linked list of paths. */
struct list paths;
2014-12-05 12:39:52 +01:00
/** Send messages asynchronously */
static void * path_send(void *arg)
{
2015-03-21 15:30:42 +01:00
struct path *p = arg;
int sig;
sigset_t set;
struct sigevent sev = {
.sigev_notify = SIGEV_THREAD_ID,
.sigev_signo = SIGALRM,
.sigev_notify_thread_id = syscall(SYS_gettid)
};
struct itimerspec its = {
.it_interval = timespec_rate(p->rate),
.it_value = { 1, 0 }
};
sigemptyset(&set);
sigaddset(&set, SIGALRM);
if(pthread_sigmask(SIG_BLOCK, &set, NULL))
serror("Set signal mask");
2015-03-21 15:30:42 +01:00
if (timer_create(CLOCK_REALTIME, &sev, &p->timer))
serror("Failed to create timer");
2015-03-21 15:30:42 +01:00
if (timer_settime(p->timer, 0, &its, NULL))
serror("Failed to start timer");
while (1) {
2014-12-05 12:39:52 +01:00
sigwait(&set, &sig); /* blocking wait for next timer tick */
FOREACH(&p->destinations, it)
node_write(it->node, p->current);
p->sent++;
}
return NULL;
}
/** Receive messages */
static void * path_run(void *arg)
{
2015-03-17 23:23:13 +01:00
struct path *p = arg;
p->previous = alloc(sizeof(struct msg));
p->current = alloc(sizeof(struct msg));
2014-12-05 12:39:52 +01:00
char buf[33];
2014-12-05 12:39:52 +01:00
/* Open deferred TCP connection */
node_start_defer(p->in);
// FIXME: node_start_defer(p->out);
/* Main thread loop */
while (1) {
node_read(p->in, p->current); /* Receive message */
2014-12-05 12:39:52 +01:00
p->received++;
/* Check header fields */
if (msg_verify(p->current)) {
p->invalid++;
continue; /* Drop message */
}
/* Update histogram and handle wrap-around */
int dist = (UINT16_MAX + p->current->sequence - p->previous->sequence) % UINT16_MAX;
if (dist > UINT16_MAX / 2)
dist -= UINT16_MAX;
hist_put(&p->histogram, dist);
/* Handle simulation restart */
if (p->current->sequence == 0 && abs(dist) >= 1) {
warn("Simulation for path %s restarted (prev->seq=%u, current->seq=%u, dist=%d)",
buf, p->previous->sequence, p->current->sequence, dist);
path_reset(p);
}
else if (dist <= 0 && p->received > 1) {
p->dropped++;
continue;
}
/* Call hook callbacks */
FOREACH(&p->hooks, it) {
if (it->hook(p->current, p)) {
p->skipped++;
continue;
}
}
/* At fixed rate mode, messages are send by another thread */
if (!p->rate) {
FOREACH(&p->destinations, it)
node_write(it->node, p->current);
p->sent++;
}
SWAP(p->previous, p->current);
}
return NULL;
}
int path_start(struct path *p)
2014-12-05 12:39:52 +01:00
{ INDENT
char buf[33];
path_print(p, buf, sizeof(buf));
info("Starting path: %s", buf);
2014-12-05 12:39:52 +01:00
/* At fixed rate mode, we start another thread for sending */
if (p->rate)
2015-03-21 15:31:42 +01:00
pthread_create(&p->sent_tid, NULL, &path_send, p);
2015-03-21 15:31:42 +01:00
return pthread_create(&p->recv_tid, NULL, &path_run, p);
}
int path_stop(struct path *p)
2014-12-05 12:39:52 +01:00
{ INDENT
char buf[33];
path_print(p, buf, sizeof(buf));
info("Stopping path: %s", buf);
2014-12-05 12:39:52 +01: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
timer_delete(p->timer);
}
if (p->received)
hist_print(&p->histogram);
return 0;
}
int path_reset(struct path *p)
{
p->sent = 0;
p->received = 1;
p->invalid = 0;
p->skipped = 0;
p->dropped = 0;
hist_reset(&p->histogram);
return 0;
}
2015-03-21 15:31:42 +01:00
void path_print_stats(struct path *p)
{
char buf[33];
path_print(p, buf, sizeof(buf));
2015-03-21 15:31:42 +01:00
info("%-32s : %-8u %-8u %-8u %-8u %-8u", buf,
p->sent, p->received, p->dropped, p->skipped, p->invalid);
}
int path_print(struct path *p, char *buf, int len)
{
*buf = 0;
2015-03-31 16:44:33 +02:00
strap(buf, len, "%s " MAG("=>"), p->in->name);
if (list_length(&p->destinations) > 1) {
strap(buf, len, " [");
FOREACH(&p->destinations, it)
strap(buf, len, " %s", it->node->name);
strap(buf, len, " ]");
}
else
2015-03-31 16:44:33 +02:00
strap(buf, len, " %s", p->out->name);
return 0;
}
struct path * path_create()
{
struct path *p = alloc(sizeof(struct path));
list_init(&p->destinations, NULL);
list_init(&p->hooks, NULL);
hist_create(&p->histogram, -HIST_SEQ, +HIST_SEQ, 1);
return p;
}
void path_destroy(struct path *p)
{
list_destroy(&p->destinations);
list_destroy(&p->hooks);
hist_destroy(&p->histogram);
free(p->current);
free(p->previous);
free(p);
}