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>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-06-05 09:34:40 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
2014-06-05 09:34:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2014-06-05 09:35:36 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-09-10 12:22:21 +00:00
|
|
|
#include <netdb.h>
|
2015-01-20 13:52:32 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.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"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
static struct settings set;
|
|
|
|
static struct node *node;
|
2015-03-21 15:19:41 +01:00
|
|
|
extern struct list nodes;
|
2014-12-05 12:41:06 +01:00
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
/* Test options */
|
2014-09-09 12:07:57 +00:00
|
|
|
int running = 1;
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
/** Amount of messages which should be sent (default: -1 for unlimited) */
|
|
|
|
int count = -1;
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
*/
|
|
|
|
int fd = STDOUT_FILENO;
|
|
|
|
|
|
|
|
/** Lowest value in histogram. */
|
|
|
|
double low = 0;
|
|
|
|
/** Highest value in histogram. */
|
|
|
|
double high = 2e-4;
|
|
|
|
/** Histogram resolution. */
|
|
|
|
double res = 1e-5;
|
|
|
|
|
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();
|
|
|
|
|
2014-06-05 09:34:51 +00:00
|
|
|
void quit(int sig, siginfo_t *si, void *ptr)
|
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
|
|
|
}
|
|
|
|
|
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-01-20 13:52:29 +00:00
|
|
|
if (argc < 4) {
|
|
|
|
printf("Usage: %s CONFIG TEST NODE [ARGS]\n", argv[0]);
|
2014-12-05 12:41:06 +01:00
|
|
|
printf(" CONFIG path to a configuration file\n");
|
2014-12-09 19:46:26 +00:00
|
|
|
printf(" TEST the name of the test to execute: 'rtt'\n");
|
2014-12-05 12:41:06 +01:00
|
|
|
printf(" NODE name of the node which shoud be used\n\n");
|
2015-03-21 18:16:28 +01:00
|
|
|
|
2014-09-10 12:22:18 +00:00
|
|
|
printf("Simulator2Simulator Server %s (built on %s %s)\n",
|
|
|
|
BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
|
2015-03-21 18:16:28 +01:00
|
|
|
printf(" Copyright 2015, Institute for Automation of Complex Power Systems, EONERC\n");
|
|
|
|
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
|
|
|
|
|
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);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
config_init(&config);
|
|
|
|
config_parse(argv[1], &config, &set, &nodes, NULL);
|
|
|
|
|
2015-03-21 15:19:41 +01:00
|
|
|
node = node_lookup_name(argv[3], &nodes);
|
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-01-20 13:52:33 +00:00
|
|
|
node->refcnt++;
|
2014-12-05 12:41:06 +01:00
|
|
|
node_start(node);
|
|
|
|
node_start_defer(node);
|
2015-01-20 13:52:29 +00:00
|
|
|
|
|
|
|
/* Parse Arguments */
|
|
|
|
char c;
|
|
|
|
char *endptr;
|
|
|
|
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 '?':
|
|
|
|
if (optopt == 'c')
|
|
|
|
error("Option -%c requires an argument.", optopt);
|
2015-01-21 13:25:32 +00:00
|
|
|
else if (isprint(optopt))
|
|
|
|
error("Unknown option '-%c'.", optopt);
|
2015-01-20 13:52:29 +00:00
|
|
|
else
|
2015-01-21 13:25:32 +00:00
|
|
|
error("Unknown option character '\\x%x'.", optopt);
|
2015-03-21 18:03:55 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2015-01-20 13:52:29 +00:00
|
|
|
default:
|
2015-03-21 18:03:55 +01:00
|
|
|
abort();
|
2015-01-20 13:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
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);
|
|
|
|
config_destroy(&config);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_rtt() {
|
|
|
|
struct msg m = MSG_INIT(sizeof(struct timespec) / sizeof(float));
|
|
|
|
struct timespec *ts1 = (struct timespec *) &m.data;
|
2015-03-17 23:21:31 +01:00
|
|
|
struct timespec *ts2 = alloc(sizeof(struct timespec));
|
2015-01-20 13:52:31 +00:00
|
|
|
|
|
|
|
double rtt;
|
|
|
|
double rtt_max = LLONG_MIN;
|
|
|
|
double rtt_min = LLONG_MAX;
|
|
|
|
double avg = 0;
|
|
|
|
|
|
|
|
struct hist histogram;
|
2015-03-21 15:26:52 +01:00
|
|
|
hist_create(&histogram, low, high, res);
|
2014-06-25 17:50:31 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
#if 1 /* Print header */
|
|
|
|
fprintf(stdout, "%17s", "timestamp");
|
2014-06-25 17:50:31 +00:00
|
|
|
#endif
|
2015-01-20 13:52:31 +00:00
|
|
|
fprintf(stdout, "%5s%10s%10s%10s%10s\n", "seq", "rtt", "min", "max", "avg");
|
2014-06-25 17:50:31 +00:00
|
|
|
|
2015-01-20 13:52:29 +00:00
|
|
|
while (running && (count < 0 || count--)) {
|
2015-01-20 13:52:31 +00:00
|
|
|
clock_gettime(CLOCK_ID, ts1);
|
|
|
|
node_write(node, &m);
|
|
|
|
node_read(node, &m);
|
|
|
|
clock_gettime(CLOCK_ID, ts2);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
rtt = timespec_delta(ts1, ts2);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
if (rtt < 0) continue;
|
|
|
|
if (rtt > rtt_max) rtt_max = rtt;
|
|
|
|
if (rtt < rtt_min) rtt_min = rtt;
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
avg += rtt;
|
|
|
|
|
|
|
|
hist_put(&histogram, rtt);
|
2014-06-05 09:34:51 +00:00
|
|
|
|
2014-06-25 17:50:31 +00:00
|
|
|
#if 1
|
2015-01-20 13:52:31 +00:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
fprintf(stdout, "%17.6f", ts.tv_sec + ts.tv_nsec / 1e9);
|
2014-06-25 17:50:31 +00:00
|
|
|
#endif
|
2015-01-20 13:52:31 +00:00
|
|
|
m.sequence++;
|
2014-09-09 12:07:57 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
fprintf(stdout, "%5u%10.3f%10.3f%10.3f%10.3f\n", m.sequence,
|
|
|
|
1e3 * rtt, 1e3 * rtt_min, 1e3 * rtt_max, 1e3 * avg / m.sequence);
|
2014-06-05 09:34:51 +00:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
free(ts2);
|
2014-09-09 12:07:57 +00:00
|
|
|
|
2015-01-20 13:52:31 +00:00
|
|
|
hist_print(&histogram);
|
|
|
|
|
2015-01-20 13:52:32 +00:00
|
|
|
struct stat st;
|
|
|
|
if (!fstat(fd, &st)) {
|
|
|
|
FILE *f = fdopen(fd, "w");
|
|
|
|
hist_matlab(&histogram, f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error("Invalid file descriptor: %u", fd);
|
|
|
|
|
2015-03-21 15:26:52 +01:00
|
|
|
hist_destroy(&histogram);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|