1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

Merge branch 'node-signal' into 'develop'

Add new node-type to generate test data within villas-node

See merge request !28
This commit is contained in:
Steffen Vogel 2017-07-07 00:11:50 +02:00
commit 053e4d9516
11 changed files with 433 additions and 144 deletions

View file

@ -189,6 +189,16 @@ nodes = {
"tcp://localhost:1235",
"tcp://localhost:12444"
],
},
signal_node = {
type = "signal",
signal = "sine", # One of "sine", "ramp", "triangle", "random", "mixed"
values = 4, # Number of values per sample
amplitude = 2.3, # Amplitude of generated signals
frequency = 10, # Frequency of generated signals
stddev = 2, # Standard deviation of random signals (normal distributed)
rate = 10.0, # Sample rate
}
};

View file

@ -0,0 +1,89 @@
/** Node-type for signal generation.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
/**
* @ingroup node
* @addtogroup signal Signal generation node-type-
* @{
*/
#pragma once
#include <libconfig.h>
#include "timing.h"
/* Forward declarations */
struct node;
struct sample;
enum signal_type {
SIGNAL_TYPE_RANDOM,
SIGNAL_TYPE_SINE,
SIGNAL_TYPE_SQUARE,
SIGNAL_TYPE_TRIANGLE,
SIGNAL_TYPE_RAMP,
SIGNAL_TYPE_MIXED
};
/** Node-type for signal generation.
* @see node_type
*/
struct signal {
int tfd; /**< timerfd file descriptor. */
int rt; /**< Real-time mode? */
enum signal_type type; /**< Signal type */
double rate; /**< Sampling rate. */
double frequency; /**< Frequency of the generated signals. */
double amplitude; /**< Amplitude of the generated signals. */
double stddev; /**< Standard deviation of random signals (normal distributed). */
int values; /**< The number of values which will be emitted by this node. */
int limit; /**< The number of values which should be generated by this node. <0 for infinitve. */
struct timespec started; /**< Point in time when this node was started. */
int counter; /**< The number of packets already emitted. */
};
/** @see node_type::print */
char * signal_print(struct node *n);
/** @see node_type::parse */
int signal_parse(struct node *n, config_setting_t *cfg);
/** @see node_type::open */
int signal_open(struct node *n);
/** @see node_type::close */
int signal_close(struct node *n);
/** @see node_type::read */
int signal_read(struct node *n, struct sample *smps[], unsigned cnt);
enum signal_type signal_lookup_type(const char *type);
void signal_get(struct signal *s, struct sample *t, struct timespec *now);
/** @} */

View file

@ -241,7 +241,10 @@ static inline int log2i(long long x) {
/** Sleep with rdtsc */
void rdtsc_sleep(uint64_t nanosecs, uint64_t start);
/** Register a exit callback for program termination (SIGINT / SIGKILL). */
void signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx));
/** Register a exit callback for program termination: SIGINT, SIGKILL & SIGALRM. */
int signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx));
/** Send signal \p sig to main thread. */
void killme(int sig);
pid_t spawn(const char* name, char *const argv[]);

View file

@ -25,7 +25,7 @@ LIB_ABI_VERSION = 1
LIB = $(BUILDDIR)/$(LIB_NAME).so.$(LIB_ABI_VERSION)
# Object files for libvillas
LIB_SRCS += $(addprefix lib/nodes/, file.c cbuilder.c shmem.c) \
LIB_SRCS += $(addprefix lib/nodes/, file.c cbuilder.c shmem.c signal.c) \
$(addprefix lib/kernel/, kernel.c rt.c) \
$(addprefix lib/, sample.c path.c node.c hook.c \
log.c log_config.c utils.c super_node.c hist.c timing.c pool.c \

View file

@ -335,8 +335,8 @@ retry: values = sample_io_villas_fscan(f->read.handle->file, s, &flags); /* Get
goto retry;
case FILE_EOF_EXIT:
info("Reached end-of-file");
exit(EXIT_SUCCESS);
info("Reached end-of-file of node %s", node_name(n));
killme(SIGTERM);
}
}

209
lib/nodes/signal.c Normal file
View file

@ -0,0 +1,209 @@
/** Node-type for signal generation.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <math.h>
#include "node.h"
#include "plugin.h"
#include "nodes/signal.h"
enum signal_type signal_lookup_type(const char *type)
{
if (!strcmp(type, "random"))
return SIGNAL_TYPE_RANDOM;
else if (!strcmp(type, "sine"))
return SIGNAL_TYPE_SINE;
else if (!strcmp(type, "square"))
return SIGNAL_TYPE_SQUARE;
else if (!strcmp(type, "triangle"))
return SIGNAL_TYPE_TRIANGLE;
else if (!strcmp(type, "ramp"))
return SIGNAL_TYPE_RAMP;
else if (!strcmp(type, "mixed"))
return SIGNAL_TYPE_MIXED;
else
return -1;
}
int signal_parse(struct node *n, config_setting_t *cfg)
{
struct signal *s = n->_vd;
const char *type;
if (!config_setting_lookup_string(cfg, "signal", &type))
s->type = SIGNAL_TYPE_MIXED;
else {
s->type = signal_lookup_type(type);
if (s->type == -1)
cerror(cfg, "Unknown signal type '%s'", type);
}
if (!config_setting_lookup_bool(cfg, "realtime", &s->rt))
s->rt = 1;
if (!config_setting_lookup_int(cfg, "limit", &s->limit))
s->limit = -1;
if (!config_setting_lookup_int(cfg, "values", &s->values))
s->values = 1;
if (!config_setting_lookup_float(cfg, "rate", &s->rate))
s->rate = 10;
if (!config_setting_lookup_float(cfg, "frequency", &s->frequency))
s->frequency = 1;
if (!config_setting_lookup_float(cfg, "amplitude", &s->amplitude))
s->amplitude = 1;
if (!config_setting_lookup_float(cfg, "stddev", &s->stddev))
s->stddev = 0.02;
return 0;
}
int signal_open(struct node *n)
{
struct signal *s = n->_vd;
s->counter = 0;
s->started = time_now();
/* Setup timer */
if (s->rt) {
s->tfd = timerfd_create_rate(s->rate);
if (s->tfd < 0)
return -1;
}
else
s->tfd = -1;
return 0;
}
int signal_close(struct node *n)
{
struct signal* s = n->_vd;
close(s->tfd);
return 0;
}
int signal_read(struct node *n, struct sample *smps[], unsigned cnt)
{
struct signal *s = n->_vd;
struct sample *t = smps[0];
struct timespec now;
int steps;
assert(cnt == 1);
/* Throttle output if desired */
if (s->rt) {
/* Block until 1/p->rate seconds elapsed */
steps = timerfd_wait(s->tfd);
if (steps > 1)
warn("Missed steps: %u", steps);
now = time_now();
}
else {
struct timespec offset = time_from_double(s->counter * 1.0 / s->rate);
now = time_add(&s->started, &offset);
steps = 1;
}
double running = time_delta(&s->started, &now);
t->ts.origin =
t->ts.received = now;
t->sequence = s->counter;
t->length = s->values;
for (int i = 0; i < MIN(s->values, t->capacity); i++) {
int rtype = (s->type != SIGNAL_TYPE_MIXED) ? s->type : i % 4;
switch (rtype) {
case SIGNAL_TYPE_RANDOM: t->data[i].f += box_muller(0, s->stddev); break;
case SIGNAL_TYPE_SINE: t->data[i].f = s->amplitude * sin(running * s->frequency * 2 * M_PI); break;
case SIGNAL_TYPE_TRIANGLE: t->data[i].f = s->amplitude * (fabs(fmod(running * s->frequency, 1) - .5) - 0.25) * 4; break;
case SIGNAL_TYPE_SQUARE: t->data[i].f = s->amplitude * ( (fmod(running * s->frequency, 1) < .5) ? -1 : 1); break;
case SIGNAL_TYPE_RAMP: t->data[i].f = fmod(s->counter, s->rate / s->frequency); /** @todo send as integer? */ break;
}
}
if (s->limit > 0 && s->counter >= s->limit) {
info("Reached limit");
killme(SIGTERM);
}
s->counter += steps;
return 1;
}
char * signal_print(struct node *n)
{
struct signal *s = n->_vd;
char *type, *buf = NULL;
switch (s->type) {
case SIGNAL_TYPE_MIXED: type = "mixed"; break;
case SIGNAL_TYPE_RAMP: type = "ramp"; break;
case SIGNAL_TYPE_TRIANGLE: type = "triangle"; break;
case SIGNAL_TYPE_SQUARE: type = "square"; break;
case SIGNAL_TYPE_SINE: type = "sine"; break;
case SIGNAL_TYPE_RANDOM: type = "random"; break;
default: return NULL;
}
strcatf(&buf, "signal=%s, rate=%.2f, values=%d, frequency=%.2f, amplitude=%.2f, stddev=%.2f",
type, s->rate, s->values, s->frequency, s->amplitude, s->stddev);
if (s->limit > 0)
strcatf(&buf, ", limit=%d", s->limit);
return buf;
};
static struct plugin p = {
.name = "signal",
.description = "Signal generation",
.type = PLUGIN_TYPE_NODE,
.node = {
.vectorize = 1,
.size = sizeof(struct signal),
.parse = signal_parse,
.print = signal_print,
.start = signal_open,
.stop = signal_close,
.read = signal_read,
}
};
REGISTER_PLUGIN(&p)
LIST_INIT_STATIC(&p.node.instances)

View file

@ -34,6 +34,8 @@
#include "config.h"
#include "utils.h"
pthread_t main_thread;
void print_copyright()
{
printf("VILLASnode %s (built on %s %s)\n",
@ -297,26 +299,48 @@ void rdtsc_sleep(uint64_t nanosecs, uint64_t start)
}
/* Setup exit handler */
void signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx))
int signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx))
{
int ret;
info("Initialize signals");
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = cb
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGINT, &sa_quit, NULL);
sigaction(SIGTERM, &sa_quit, NULL);
sigaction(SIGALRM, &sa_quit, NULL);
struct sigaction sa_chld = {
.sa_flags = 0,
.sa_handler = SIG_IGN
};
main_thread = pthread_self();
sigaction(SIGCHLD, &sa_chld, NULL);
sigemptyset(&sa_quit.sa_mask);
ret = sigaction(SIGINT, &sa_quit, NULL);
if (ret)
return ret;
ret = sigaction(SIGTERM, &sa_quit, NULL);
if (ret)
return ret;
ret = sigaction(SIGALRM, &sa_quit, NULL);
if (ret)
return ret;
ret = sigaction(SIGCHLD, &sa_chld, NULL);
if (ret)
return ret;
return 0;
}
void killme(int sig)
{
pthread_kill(main_thread, sig);
}
pid_t spawn(const char* name, char *const argv[])

View file

@ -22,7 +22,7 @@
TAROPTS = --exclude-ignore-recursive=.distignore --transform='s|^\.|villas-node-$(VERSION_NUM)|' --show-transformed-names
TAR_VILLAS = $(BUILDDIR)/packaging/villas-node-$(VERSION_NUM)-1.$(GIT_BRANCH)_$(subst -,_,$(VARIANT)).$(shell date +%Y%m%d)git$(GIT_REV).tar.gz
TAR_VILLAS = $(BUILDDIR)/packaging/villas-node-$(VERSION_NUM)-1.$(subst -,_,$(GIT_BRANCH))_$(subst -,_,$(VARIANT)).$(shell date +%Y%m%d)git$(GIT_REV).tar.gz
DEPLOY_USER ?= acs
DEPLOY_HOST ?= villas.fein-aachen.org

View file

@ -56,7 +56,7 @@ rpm-libwebsockets: | $(RPMDIR)/RPMS/x86_64/ $(BUILDDIR)/thirdparty/libwebsockets
# We patch version number and release fields of the spec file based on the current Git commit
$(SPEC_VILLAS): $(SRCDIR)/packaging/rpm/villas-node.spec | $$(dir $$@)
sed -e "s/§VERSION§/$(VERSION_NUM)/g" \
-e "s/§RELEASE§/1.$(GIT_BRANCH)_$(subst -,_,$(VARIANT)).$(shell date +%Y%m%d)git$(GIT_REV)/g" < $^ > $@
-e "s/§RELEASE§/1.$(subst -,_,$(GIT_BRANCH))_$(subst -,_,$(VARIANT)).$(shell date +%Y%m%d)git$(GIT_REV)/g" < $^ > $@
sign-rpm:
rpmsign $(RPMDIR)/RPMS/*/.rpm

View file

@ -54,8 +54,6 @@ struct dir {
struct node *node;
pthread_t ptid; /**< Parent thread id */
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
if (signal == SIGALRM)
@ -140,14 +138,14 @@ retry: reason = sample_io_villas_fscan(stdin, s, NULL);
}
leave2: info("Reached send limit. Terminating...");
pthread_kill(ptid, SIGINT);
killme(SIGTERM);
return NULL;
/* We reached EOF on stdin here. Lets kill the process */
leave: if (recvv.limit < 0) {
info("Reached end-of-file. Terminating...");
pthread_kill(ptid, SIGINT);
killme(SIGTERM);
}
return NULL;
@ -193,7 +191,7 @@ static void * recv_loop(void *ctx)
}
leave: info("Reached receive limit. Terminating...");
pthread_kill(ptid, SIGINT);
killme(SIGTERM);
return NULL;
return NULL;
@ -208,8 +206,6 @@ int main(int argc, char *argv[])
.enabled = true,
.limit = -1
};
ptid = pthread_self();
char c, *endptr;
while ((c = getopt(argc, argv, "hxrsd:l:L:t:")) != -1) {

View file

@ -32,18 +32,27 @@
#include <villas/sample.h>
#include <villas/sample_io.h>
#include <villas/timing.h>
#include <villas/node.h>
#include <villas/nodes/signal.h>
#define CLOCKID CLOCK_REALTIME
enum SIGNAL_TYPE {
TYPE_RANDOM,
TYPE_SINE,
TYPE_SQUARE,
TYPE_TRIANGLE,
TYPE_RAMP,
TYPE_MIXED
/* Some default values */
struct signal s = {
.rate = 10,
.frequency = 1,
.amplitude = 1,
.stddev = 0.02,
.type = SIGNAL_TYPE_MIXED,
.rt = 1,
.values = 1,
.limit = -1
};
struct node n = {
._vd = &s
};
struct log l;
void usage()
{
printf("Usage: villas-signal [OPTIONS] SIGNAL\n");
@ -68,55 +77,46 @@ void usage()
print_copyright();
}
int main(int argc, char *argv[])
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
struct log log;
struct timespec start, now;
signal_close(&n);
enum {
MODE_RT,
MODE_NON_RT
} mode = MODE_RT;
info(GRN("Goodbye!"));
exit(EXIT_SUCCESS);
}
/* Some default values */
double rate = 10;
double freq = 1;
double ampl = 1;
double stddev = 0.02;
double running;
int type = TYPE_MIXED;
int values = 1;
int limit = -1;
int counter, tfd, steps, level = V;
int signal_parse_cli(struct signal *s, int argc, char *argv[])
{
char *type;
/* Parse optional command line arguments */
char c, *endptr;
while ((c = getopt(argc, argv, "hv:r:f:l:a:D:d:n")) != -1) {
switch (c) {
case 'd':
level = strtoul(optarg, &endptr, 10);
goto check;
case 'l':
limit = strtoul(optarg, &endptr, 10);
goto check;
case 'v':
values = strtoul(optarg, &endptr, 10);
goto check;
case 'r':
rate = strtof(optarg, &endptr);
goto check;
case 'f':
freq = strtof(optarg, &endptr);
goto check;
case 'a':
ampl = strtof(optarg, &endptr);
goto check;
case 'D':
stddev = strtof(optarg, &endptr);
l.level = strtoul(optarg, &endptr, 10);
goto check;
case 'n':
mode = MODE_NON_RT;
s->rt = 0;
break;
case 'l':
s->limit = strtoul(optarg, &endptr, 10);
goto check;
case 'v':
s->values = strtoul(optarg, &endptr, 10);
goto check;
case 'r':
s->rate = strtof(optarg, &endptr);
goto check;
case 'f':
s->frequency = strtof(optarg, &endptr);
goto check;
case 'a':
s->amplitude = strtof(optarg, &endptr);
goto check;
case 'D':
s->stddev = strtof(optarg, &endptr);
goto check;
case 'h':
case '?':
usage();
@ -133,97 +133,55 @@ check: if (optarg == endptr)
usage();
exit(EXIT_FAILURE);
}
type = argv[optind];
char *typestr = argv[optind];
s->type = signal_lookup_type(type);
if (s->type == -1)
error("Invalid signal type: %s", type);
/* Parse signal type */
if (!strcmp(typestr, "random"))
type = TYPE_RANDOM;
else if (!strcmp(typestr, "sine"))
type = TYPE_SINE;
else if (!strcmp(typestr, "square"))
type = TYPE_SQUARE;
else if (!strcmp(typestr, "triangle"))
type = TYPE_TRIANGLE;
else if (!strcmp(typestr, "ramp"))
type = TYPE_RAMP;
else if (!strcmp(typestr, "mixed"))
type = TYPE_MIXED;
else
error("Invalid signal type: %s", typestr);
return 0;
}
log_init(&log, level, LOG_ALL);
int main(int argc, char *argv[])
{
int ret;
ret = signal_parse_cli(&s, argc, argv);
if (ret)
error("Failed to parse command line options");
ret = log_init(&l, l.level, LOG_ALL);
if (ret)
error("Failed to initialize log");
ret = signals_init(quit);
if (ret)
error("Failed to intialize signals");
info("Starting signal generation: %s", signal_print(&n));
/* Allocate memory for message buffer */
struct sample *s = alloc(SAMPLE_LEN(values));
struct sample *t = alloc(SAMPLE_LEN(s.values));
t->capacity = s.values;
/* Print header */
printf("# VILLASnode signal params: type=%s, values=%u, rate=%f, limit=%d, amplitude=%f, freq=%f\n",
typestr, values, rate, limit, ampl, freq);
argv[optind], s.values, s.rate, s.limit, s.amplitude, s.frequency);
printf("# %-20s\t\t%s\n", "sec.nsec(seq)", "data[]");
/* Setup timer */
if (mode == MODE_RT) {
tfd = timerfd_create_rate(rate);
if (tfd < 0)
serror("Failed to create timer");
}
else
tfd = -1;
ret = signal_open(&n);
if (ret)
serror("Failed to start node");
start = time_now();
for (;;) {
signal_read(&n, &t, 1);
counter = 0;
while (limit < 0 || counter < limit) {
if (mode == MODE_RT) {
now = time_now();
running = time_delta(&start, &now);
}
else {
struct timespec offset;
running = counter * 1.0 / rate;
offset = time_from_double(running);
now = time_add(&start, &offset);
}
s->ts.origin = now;
s->sequence = counter;
s->length = values;
for (int i = 0; i < values; i++) {
int rtype = (type != TYPE_MIXED) ? type : i % 4;
switch (rtype) {
case TYPE_RANDOM: s->data[i].f += box_muller(0, stddev); break;
case TYPE_SINE: s->data[i].f = ampl * sin(running * freq * 2 * M_PI); break;
case TYPE_TRIANGLE: s->data[i].f = ampl * (fabs(fmod(running * freq, 1) - .5) - 0.25) * 4; break;
case TYPE_SQUARE: s->data[i].f = ampl * ( (fmod(running * freq, 1) < .5) ? -1 : 1); break;
case TYPE_RAMP: s->data[i].f = fmod(counter, rate / freq); /** @todo send as integer? */ break;
}
}
sample_io_villas_fprint(stdout, s, SAMPLE_IO_ALL & ~SAMPLE_IO_OFFSET);
sample_io_villas_fprint(stdout, t, SAMPLE_IO_ALL & ~SAMPLE_IO_OFFSET);
fflush(stdout);
/* Throttle output if desired */
if (mode == MODE_RT) {
/* Block until 1/p->rate seconds elapsed */
steps = timerfd_wait(tfd);
if (steps > 1)
warn("Missed steps: %u", steps);
counter += steps;
}
else
counter += 1;
}
if (mode == MODE_RT)
close(tfd);
free(s);
return 0;
}