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

457 lines
10 KiB
C++
Raw Permalink Normal View History

2017-07-06 23:48:19 +02:00
/** Node type: Node-type for testing Round-trip Time.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2017-07-06 23:48:19 +02:00
* @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 <cstdio>
#include <cstring>
#include <sys/stat.h>
#include <linux/limits.h>
2017-07-06 23:48:19 +02:00
2017-12-09 02:19:28 +08:00
#include <villas/node.h>
#include <villas/sample.h>
#include <villas/timing.h>
#include <villas/plugin.h>
#include <villas/nodes/test_rtt.hpp>
2017-07-06 23:48:19 +02:00
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2019-04-08 08:59:51 +02:00
static struct plugin p;
2017-09-16 15:34:16 +02:00
static int test_rtt_case_start(struct test_rtt *t, int id)
2017-07-06 23:48:19 +02:00
{
int ret;
2019-01-07 10:28:55 +01:00
struct test_rtt_case *c = (struct test_rtt_case *) vlist_at(&t->cases, id);
2019-04-10 09:26:24 +02:00
info("Starting case #%d: filename=%s, rate=%f, values=%d, limit=%d", t->current, c->filename_formatted, c->rate, c->values, c->limit);
2017-07-06 23:48:19 +02:00
/* Open file */
ret = io_open(&t->io, c->filename_formatted);
2017-07-06 23:48:19 +02:00
if (ret)
return ret;
/* Start timer. */
2020-03-04 13:06:28 +01:00
t->task.setRate(c->rate);
2017-09-16 15:34:16 +02:00
t->counter = 0;
t->current = id;
2017-07-06 23:48:19 +02:00
return 0;
}
2017-09-16 15:34:16 +02:00
static int test_rtt_case_stop(struct test_rtt *t, int id)
2017-07-06 23:48:19 +02:00
{
2017-09-16 15:34:16 +02:00
int ret;
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
/* Stop timer */
2020-03-04 13:06:28 +01:00
t->task.stop();
2017-09-16 15:34:16 +02:00
/* Close file */
ret = io_close(&t->io);
if (ret)
return ret;
2017-07-06 23:48:19 +02:00
2019-04-10 09:26:24 +02:00
info("Stopping case #%d", id);
2017-07-06 23:48:19 +02:00
return 0;
}
static int test_rtt_case_destroy(struct test_rtt_case *c)
{
if (c->filename)
free(c->filename);
if (c->filename_formatted)
free(c->filename_formatted);
return 0;
}
2020-08-25 21:00:52 +02:00
int test_rtt_prepare(struct vnode *n)
{
struct test_rtt *t = (struct test_rtt *) n->_vd;
2019-04-08 08:59:51 +02:00
int ret;
unsigned max_values = 0;
/* Take current for time for test case prefix */
time_t ts = time(nullptr);
struct tm tm;
gmtime_r(&ts, &tm);
for (size_t i = 0; i < vlist_length(&t->cases); i++) {
struct test_rtt_case *c = (struct test_rtt_case *) vlist_at(&t->cases, i);
if (c->values > max_values)
max_values = c->values;
c->filename_formatted = new char[NAME_MAX];
if (!c->filename_formatted)
throw MemoryAllocationError();
2019-04-14 19:23:30 +02:00
strftime(c->filename_formatted, NAME_MAX, c->filename, &tm);
}
2019-06-23 16:13:23 +02:00
ret = signal_list_generate(&n->in.signals, max_values, SignalType::FLOAT);
if (ret)
return ret;
return 0;
}
2020-08-25 21:00:52 +02:00
int test_rtt_parse(struct vnode *n, json_t *cfg)
2017-07-06 23:48:19 +02:00
{
2017-09-16 15:34:16 +02:00
int ret;
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2017-07-06 23:48:19 +02:00
const char *format = "villas.binary";
const char *output = ".";
2017-09-16 15:34:16 +02:00
const char *prefix = node_name_short(n);
std::vector<int> rates;
std::vector<int> values;
2017-07-06 23:48:19 +02:00
2018-08-07 18:40:32 +02:00
size_t i;
2017-10-16 08:08:35 +02:00
json_t *json_cases, *json_case, *json_val;
2019-04-08 08:59:51 +02:00
json_t *json_rates = nullptr, *json_values = nullptr;
json_error_t err;
2017-07-06 23:48:19 +02:00
t->cooldown = 0;
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
/* Generate list of test cases */
ret = vlist_init(&t->cases);
if (ret)
return ret;
2017-09-16 15:34:16 +02:00
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: s, s?: s, s?: F, s: o }",
"prefix", &prefix,
"output", &output,
"format", &format,
"cooldown", &t->cooldown,
2017-10-16 08:08:35 +02:00
"cases", &json_cases
);
if (ret)
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
2017-07-06 23:48:19 +02:00
t->output = strdup(output);
2017-09-16 15:34:16 +02:00
t->prefix = strdup(prefix);
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
/* Initialize IO module */
2018-05-12 13:56:12 +02:00
t->format = format_type_lookup(format);
2017-09-16 15:34:16 +02:00
if (!t->format)
error("Invalid value for setting 'format' of node %s", node_name(n));
2017-07-06 23:48:19 +02:00
2019-01-07 10:28:55 +01:00
/* Construct vlist of test cases */
2017-10-16 08:08:35 +02:00
if (!json_is_array(json_cases))
2017-09-16 15:34:16 +02:00
error("The 'cases' setting of node %s must be an array.", node_name(n));
2018-08-07 18:40:32 +02:00
json_array_foreach(json_cases, i, json_case) {
2017-09-16 15:34:16 +02:00
int limit = -1;
double duration = -1; /* in secs */
2017-10-16 08:08:35 +02:00
ret = json_unpack_ex(json_case, &err, 0, "{ s: o, s: o, s?: i, s?: F }",
"rates", &json_rates,
"values", &json_values,
2017-09-16 15:34:16 +02:00
"limit", &limit,
"duration", &duration
);
if (limit > 0 && duration > 0)
error("The settings 'duration' and 'limit' of node %s must be used exclusively", node_name(n));
if (!json_is_array(json_rates) && !json_is_number(json_rates))
2017-09-16 15:34:16 +02:00
error("The 'rates' setting of node %s must be a real or an array of real numbers", node_name(n));
if (!json_is_array(json_values) && !json_is_integer(json_values))
2017-09-16 15:34:16 +02:00
error("The 'values' setting of node %s must be an integer or an array of integers", node_name(n));
values.clear();
rates.clear();
2017-09-16 15:34:16 +02:00
2017-10-16 08:08:35 +02:00
if (json_is_array(json_rates)) {
2018-08-07 18:40:32 +02:00
size_t j;
json_array_foreach(json_rates, j, json_val) {
2017-10-16 08:08:35 +02:00
if (!json_is_number(json_val))
2017-09-16 15:34:16 +02:00
error("The 'rates' setting of node %s must be an array of real numbers", node_name(n));
rates.push_back(json_integer_value(json_val));
2017-09-16 15:34:16 +02:00
}
}
else
rates.push_back(json_number_value(json_rates));
2017-09-16 15:34:16 +02:00
2017-10-16 08:08:35 +02:00
if (json_is_array(json_values)) {
2018-08-07 18:40:32 +02:00
size_t j;
json_array_foreach(json_values, j, json_val) {
2017-10-16 08:08:35 +02:00
if (!json_is_integer(json_val))
2017-09-16 15:34:16 +02:00
error("The 'values' setting of node %s must be an array of integers", node_name(n));
values.push_back(json_integer_value(json_val));
2017-09-16 15:34:16 +02:00
}
}
2019-04-10 09:26:24 +02:00
else
values.push_back(json_integer_value(json_values));
2017-07-06 23:48:19 +02:00
for (int rate : rates) {
for (int value : values) {
auto *c = new struct test_rtt_case;
if (!c)
throw MemoryAllocationError();
2017-07-06 23:48:19 +02:00
2020-09-10 17:35:35 +02:00
c->filename = nullptr;
c->filename_formatted = nullptr;
c->rate = rate;
c->values = value;
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
if (limit > 0)
c->limit = limit;
else if (duration > 0)
c->limit = duration * c->rate;
else
c->limit = 1000; /* default value */
2017-07-06 23:48:19 +02:00
c->filename = strf("%s/%s_values%d_rate%.0f.log", t->output, t->prefix, c->values, c->rate);
2017-09-16 15:34:16 +02:00
2019-01-07 10:28:55 +01:00
vlist_push(&t->cases, c);
2017-09-16 15:34:16 +02:00
}
2017-07-06 23:48:19 +02:00
}
}
2017-09-16 15:34:16 +02:00
return 0;
}
2017-07-06 23:48:19 +02:00
2020-08-25 21:00:52 +02:00
int test_rtt_init(struct vnode *n)
2020-03-04 13:06:28 +01:00
{
struct test_rtt *t = (struct test_rtt *) n->_vd;
new (&t->task) Task(CLOCK_MONOTONIC);
return 0;
}
2020-08-25 21:00:52 +02:00
int test_rtt_destroy(struct vnode *n)
2017-09-16 15:34:16 +02:00
{
int ret;
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2017-09-16 15:34:16 +02:00
ret = vlist_destroy(&t->cases, (dtor_cb_t) test_rtt_case_destroy, true);
2017-07-06 23:48:19 +02:00
if (ret)
return ret;
2020-03-04 13:06:28 +01:00
t->task.~Task();
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
if (t->output)
free(t->output);
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
if (t->prefix)
free(t->prefix);
2017-07-06 23:48:19 +02:00
return 0;
}
2020-08-25 21:00:52 +02:00
char * test_rtt_print(struct vnode *n)
2017-07-06 23:48:19 +02:00
{
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2017-07-06 23:48:19 +02:00
2019-01-07 10:28:55 +01:00
return strf("output=%s, prefix=%s, cooldown=%f, #cases=%zu", t->output, t->prefix, t->cooldown, vlist_length(&t->cases));
2017-07-06 23:48:19 +02:00
}
2020-08-25 21:00:52 +02:00
int test_rtt_start(struct vnode *n)
2017-07-06 23:48:19 +02:00
{
2017-09-16 15:34:16 +02:00
int ret;
struct stat st;
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2019-04-08 08:59:51 +02:00
struct test_rtt_case *c = (struct test_rtt_case *) vlist_first(&t->cases);
2017-09-16 15:34:16 +02:00
/* Create folder for results if not present */
ret = stat(t->output, &st);
if (ret || !S_ISDIR(st.st_mode)) {
ret = mkdir(t->output, 0777);
2019-04-08 08:59:51 +02:00
if (ret) {
2019-04-10 09:26:24 +02:00
warning("Failed to create output directory: %s", t->output);
return ret;
2019-04-08 08:59:51 +02:00
}
}
2019-06-23 16:13:23 +02:00
ret = io_init(&t->io, t->format, &n->in.signals, (int) SampleFlags::HAS_ALL & ~(int) SampleFlags::HAS_DATA);
if (ret)
return ret;
2020-03-04 13:06:28 +01:00
t->task.setRate(c->rate);
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
t->current = -1;
t->counter = -1;
2017-07-06 23:48:19 +02:00
return 0;
}
2020-08-25 21:00:52 +02:00
int test_rtt_stop(struct vnode *n)
2017-07-06 23:48:19 +02:00
{
int ret;
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2017-07-06 23:48:19 +02:00
if (t->counter >= 0) {
2017-09-16 15:34:16 +02:00
ret = test_rtt_case_stop(t, t->current);
if (ret)
return ret;
}
2017-07-06 23:48:19 +02:00
ret = io_destroy(&t->io);
if (ret)
return ret;
2017-07-06 23:48:19 +02:00
return 0;
}
2020-08-25 21:00:52 +02:00
int test_rtt_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
2017-07-06 23:48:19 +02:00
{
2019-04-08 08:59:51 +02:00
int ret;
2019-04-08 10:15:49 +02:00
unsigned i;
2017-07-06 23:48:19 +02:00
uint64_t steps;
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2017-07-06 23:48:19 +02:00
2019-04-08 10:15:49 +02:00
/* Handle start/stop of new cases */
2017-09-16 15:34:16 +02:00
if (t->counter == -1) {
if (t->current < 0) {
2017-09-16 15:34:16 +02:00
t->current = 0;
}
else {
ret = test_rtt_case_stop(t, t->current);
if (ret)
return ret;
2017-07-06 23:48:19 +02:00
2017-09-16 15:34:16 +02:00
t->current++;
}
2017-07-06 23:48:19 +02:00
2019-04-08 10:15:49 +02:00
if ((unsigned) t->current >= vlist_length(&t->cases)) {
info("This was the last case. Stopping node %s", node_name(n));
2019-06-23 16:13:23 +02:00
n->state = State::STOPPING;
return -1;
2017-07-06 23:48:19 +02:00
}
2017-09-16 15:34:16 +02:00
else {
ret = test_rtt_case_start(t, t->current);
if (ret)
return ret;
}
}
2019-01-07 10:28:55 +01:00
struct test_rtt_case *c = (struct test_rtt_case *) vlist_at(&t->cases, t->current);
2017-09-16 15:34:16 +02:00
/* Wait */
2020-03-04 13:06:28 +01:00
steps = t->task.wait();
2017-09-16 15:34:16 +02:00
if (steps > 1)
warning("Skipped %ld steps", (long) (steps - 1));
2017-09-16 15:34:16 +02:00
2019-04-08 08:59:51 +02:00
if ((unsigned) t->counter >= c->limit) {
2019-04-08 10:15:49 +02:00
info("Stopping case #%d", t->current);
2019-04-14 19:23:30 +02:00
t->counter = -1;
2017-09-16 15:34:16 +02:00
2019-04-08 10:15:49 +02:00
if (t->cooldown) {
info("Entering cooldown phase. Waiting %f seconds...", t->cooldown);
2020-03-04 13:06:28 +01:00
t->task.setTimeout(t->cooldown);
2019-04-08 10:15:49 +02:00
}
2019-04-10 09:26:24 +02:00
return 0;
2017-07-06 23:48:19 +02:00
}
2019-04-10 09:26:24 +02:00
else {
struct timespec now = time_now();
2017-07-06 23:48:19 +02:00
2019-04-10 09:26:24 +02:00
/* Prepare samples */
for (i = 0; i < cnt; i++) {
smps[i]->length = c->values;
smps[i]->sequence = t->counter;
smps[i]->ts.origin = now;
2019-06-23 16:13:23 +02:00
smps[i]->flags = (int) SampleFlags::HAS_DATA | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_TS_ORIGIN;
smps[i]->signals = &n->in.signals;
2019-04-10 09:26:24 +02:00
t->counter++;
}
return i;
}
2017-07-06 23:48:19 +02:00
}
2020-08-25 21:00:52 +02:00
int test_rtt_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
2017-07-06 23:48:19 +02:00
{
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
if (t->current < 0)
return 0;
2019-01-07 10:28:55 +01:00
struct test_rtt_case *c = (struct test_rtt_case *) vlist_at(&t->cases, t->current);
2017-07-06 23:48:19 +02:00
2019-04-08 08:59:51 +02:00
unsigned i;
for (i = 0; i < cnt; i++) {
2017-07-06 23:48:19 +02:00
if (smps[i]->length != c->values) {
warning("Discarding invalid sample due to mismatching length: expecting=%d, has=%d", c->values, smps[i]->length);
2017-07-06 23:48:19 +02:00
continue;
}
2017-09-16 15:34:16 +02:00
io_print(&t->io, &smps[i], 1);
2017-07-06 23:48:19 +02:00
}
return i;
}
2020-08-25 21:00:52 +02:00
int test_rtt_poll_fds(struct vnode *n, int fds[])
{
2017-10-18 15:39:53 +02:00
struct test_rtt *t = (struct test_rtt *) n->_vd;
2020-03-04 13:06:28 +01:00
fds[0] = t->task.getFD();
return 1;
}
2019-04-08 08:59:51 +02:00
__attribute__((constructor(110)))
static void register_plugin() {
p.name = "test_rtt";
p.description = "Test round-trip time with loopback";
2019-06-23 16:13:23 +02:00
p.type = PluginType::NODE;
p.node.instances.state = State::DESTROYED;
2019-04-08 08:59:51 +02:00
p.node.vectorize = 0;
2019-06-23 16:13:23 +02:00
p.node.flags = (int) NodeFlags::PROVIDES_SIGNALS;
2019-04-08 08:59:51 +02:00
p.node.size = sizeof(struct test_rtt);
p.node.parse = test_rtt_parse;
p.node.prepare = test_rtt_prepare;
2020-03-04 13:06:28 +01:00
p.node.init = test_rtt_init;
p.node.destroy = test_rtt_destroy;
2019-04-08 08:59:51 +02:00
p.node.print = test_rtt_print;
p.node.start = test_rtt_start;
p.node.stop = test_rtt_stop;
p.node.read = test_rtt_read;
p.node.write = test_rtt_write;
int ret = vlist_init(&p.node.instances);
2020-09-10 17:35:35 +02:00
if (!ret)
vlist_init_and_push(&plugins, &p);
2019-04-08 08:59:51 +02:00
}
2017-07-06 23:48:19 +02:00
2019-04-08 08:59:51 +02:00
__attribute__((destructor(110)))
static void deregister_plugin() {
2020-06-16 02:35:34 +02:00
vlist_remove_all(&plugins, &p);
2019-04-08 08:59:51 +02:00
}