2014-07-14 11:49:44 +00:00
|
|
|
/** Some basic tests.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2015-06-03 10:13:35 +02:00
|
|
|
#include <string.h>
|
2014-06-05 09:34:40 +00:00
|
|
|
#include <signal.h>
|
2014-06-05 09:34:51 +00:00
|
|
|
#include <unistd.h>
|
2015-01-20 13:52:32 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/stat.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-07-14 10:47:41 +00:00
|
|
|
#include "config.h"
|
2014-12-05 12:41:06 +01:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "msg.h"
|
2014-07-07 07:53:42 +00:00
|
|
|
#include "node.h"
|
2014-06-05 09:34:51 +00:00
|
|
|
#include "utils.h"
|
2014-12-09 19:46:25 +00:00
|
|
|
#include "hist.h"
|
2015-06-02 22:06:15 +02:00
|
|
|
#include "timing.h"
|
2016-01-14 22:59:57 +01:00
|
|
|
#include "pool.h"
|
2015-06-02 22:06:15 +02:00
|
|
|
|
2015-12-13 00:46:22 +01:00
|
|
|
struct settings settings; /** <The global configuration */
|
2016-01-15 15:34:28 +01:00
|
|
|
struct list nodes;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
static struct node *node;
|
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
/* Test options */
|
2016-02-04 17:13:01 +01:00
|
|
|
static int running = 1; /**< Initiate shutdown if zero */
|
|
|
|
static int count = -1; /**< Amount of messages which should be sent (default: -1 for unlimited) */
|
2015-01-20 13:52:29 +00:00
|
|
|
|
|
|
|
/** File descriptor for Matlab results.
|
|
|
|
* This allows you to write Matlab results in a seperate log file:
|
|
|
|
*
|
|
|
|
* ./test etc/example.conf rtt -f 3 3>> measurement_results.m
|
|
|
|
*/
|
2015-06-02 22:04:03 +02:00
|
|
|
static int fd = STDOUT_FILENO;
|
2015-01-20 13:52:29 +00:00
|
|
|
|
2015-12-13 00:46:22 +01:00
|
|
|
/* Histogram */
|
|
|
|
static double low = 0; /**< Lowest value in histogram. */
|
|
|
|
static double high = 2e-4; /**< Highest value in histogram. */
|
|
|
|
static double res = 1e-5; /**< Histogram resolution. */
|
2015-01-20 13:52:29 +00:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
#define CLOCK_ID CLOCK_MONOTONIC
|
2014-09-08 14:07:44 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
/* Prototypes */
|
|
|
|
void test_rtt();
|
|
|
|
|
2015-06-05 12:15:31 +02:00
|
|
|
void quit()
|
2014-06-05 09:34:40 +00:00
|
|
|
{
|
2014-09-09 12:07:57 +00:00
|
|
|
running = 0;
|
2014-06-05 09:34:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
void usage()
|
2016-02-07 03:17:45 +01:00
|
|
|
{
|
2016-10-22 20:37:02 -04:00
|
|
|
printf("Usage: villas-test CONFIG TEST NODE [ARGS]\n");
|
2016-02-07 03:17:45 +01:00
|
|
|
printf(" CONFIG path to a configuration file\n");
|
|
|
|
printf(" TEST the name of the test to execute: 'rtt'\n");
|
|
|
|
printf(" NODE name of the node which shoud be used\n\n");
|
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-12-05 12:41:06 +01:00
|
|
|
config_t config;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
if (argc < 4) {
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2014-06-05 09:34:29 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:51 +00:00
|
|
|
/* Setup signals */
|
|
|
|
struct sigaction sa_quit = {
|
|
|
|
.sa_flags = SA_SIGINFO,
|
|
|
|
.sa_sigaction = quit
|
|
|
|
};
|
|
|
|
|
|
|
|
sigemptyset(&sa_quit.sa_mask);
|
2014-06-05 09:35:10 +00:00
|
|
|
sigaction(SIGTERM, &sa_quit, NULL);
|
2014-06-05 09:34:51 +00:00
|
|
|
sigaction(SIGINT, &sa_quit, NULL);
|
2016-04-16 19:54:26 +02:00
|
|
|
|
|
|
|
list_init(&nodes);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-10-11 13:25:06 +02:00
|
|
|
log_init();
|
2016-07-14 09:47:00 +02:00
|
|
|
cfg_parse(argv[1], &config, &settings, &nodes, NULL);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-11-20 02:45:16 -05:00
|
|
|
info("Initialize memory system");
|
|
|
|
memory_init();
|
|
|
|
|
2015-09-19 15:26:30 +02:00
|
|
|
node = list_lookup(&nodes, argv[3]);
|
2014-12-05 12:41:06 +01:00
|
|
|
if (!node)
|
2014-12-09 19:46:26 +00:00
|
|
|
error("There's no node with the name '%s'", argv[3]);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-12-13 00:45:20 +01:00
|
|
|
node_init(node->_vt, argc-3, argv+3, config_root_setting(&config));
|
2014-12-05 12:41:06 +01:00
|
|
|
node_start(node);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
/* Parse Arguments */
|
2015-05-06 11:49:13 +02:00
|
|
|
char c, *endptr;
|
2015-01-20 13:52:29 +00:00
|
|
|
while ((c = getopt (argc-3, argv+3, "l:h:r:f:c:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
count = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
case 'f':
|
|
|
|
fd = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
case 'l':
|
|
|
|
low = strtod(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case 'h':
|
|
|
|
high = strtod(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case 'r':
|
|
|
|
res = strtod(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
case '?':
|
2016-10-30 22:05:29 -04:00
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2015-01-20 13:52:29 +00:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
continue;
|
2016-02-07 03:17:45 +01:00
|
|
|
|
|
|
|
check: if (optarg == endptr)
|
2015-01-21 13:25:32 +00:00
|
|
|
error("Failed to parse parse option argument '-%c %s'", c, optarg);
|
2015-01-20 13:52:29 +00:00
|
|
|
}
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
if (!strcmp(argv[2], "rtt"))
|
|
|
|
test_rtt();
|
|
|
|
else
|
|
|
|
error("Unknown test: '%s'", argv[2]);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
node_stop(node);
|
2015-12-13 00:45:20 +01:00
|
|
|
node_deinit(node->_vt);
|
2016-01-15 15:34:28 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
list_destroy(&nodes, (dtor_cb_t) node_destroy, false);
|
2016-07-14 09:47:00 +02:00
|
|
|
cfg_destroy(&config);
|
2015-01-20 13:52:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_rtt() {
|
2015-06-02 22:43:57 +02:00
|
|
|
struct hist hist;
|
2016-06-08 22:38:21 +02:00
|
|
|
|
|
|
|
struct timespec send, recv;
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
struct sample *smp_send = alloc(SAMPLE_LEN(2));
|
|
|
|
struct sample *smp_recv = alloc(SAMPLE_LEN(2));
|
|
|
|
|
2015-06-02 22:43:57 +02:00
|
|
|
hist_create(&hist, low, high, res);
|
2014-06-25 17:50:31 +00:00
|
|
|
|
2015-06-02 22:43:57 +02:00
|
|
|
/* Print header */
|
|
|
|
fprintf(stdout, "%17s%5s%10s%10s%10s%10s%10s\n", "timestamp", "seq", "rtt", "min", "max", "mean", "stddev");
|
2014-06-25 17:50:31 +00:00
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
while (running && (count < 0 || count--)) {
|
2016-06-08 22:38:21 +02:00
|
|
|
clock_gettime(CLOCK_ID, &send);
|
2015-09-30 11:12:49 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
node_write(node, &smp_send, 1); /* Ping */
|
|
|
|
node_read(node, &smp_recv, 1); /* Pong */
|
2015-09-30 11:12:49 +02:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_ID, &recv);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
double rtt = time_delta(&recv, &send);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2015-06-02 22:43:57 +02:00
|
|
|
if (rtt < 0)
|
|
|
|
warn("Negative RTT: %f", rtt);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-02 22:43:57 +02:00
|
|
|
hist_put(&hist, rtt);
|
2014-06-05 09:34:51 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
smp_send->sequence++;
|
2014-09-09 12:07:57 +00:00
|
|
|
|
2015-09-30 11:12:49 +02:00
|
|
|
fprintf(stdout, "%10lu.%06lu%5u%10.3f%10.3f%10.3f%10.3f%10.3f\n",
|
2016-06-08 22:38:21 +02:00
|
|
|
recv.tv_sec, recv.tv_nsec / 1000, smp_send->sequence,
|
2015-06-02 22:43:57 +02:00
|
|
|
1e3 * rtt, 1e3 * hist.lowest, 1e3 * hist.highest,
|
|
|
|
1e3 * hist_mean(&hist), 1e3 * hist_stddev(&hist));
|
2014-06-05 09:34:51 +00:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-01-20 13:52:32 +00:00
|
|
|
struct stat st;
|
|
|
|
if (!fstat(fd, &st)) {
|
|
|
|
FILE *f = fdopen(fd, "w");
|
2016-10-22 20:37:02 -04:00
|
|
|
hist_dump_matlab(&hist, f);
|
2015-01-20 13:52:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
error("Invalid file descriptor: %u", fd);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-10-30 22:55:56 -04:00
|
|
|
hist_print(&hist, 1);
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2015-06-02 22:43:57 +02:00
|
|
|
hist_destroy(&hist);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|