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/src/villas-test-rtt.cpp

240 lines
6.3 KiB
C++
Raw Normal View History

/** Measure round-trip time.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2018-08-20 18:39:04 +02:00
* @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +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.
*
2017-04-27 12:56:43 +02:00
* 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.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
2015-06-03 10:13:35 +02:00
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#include <inttypes.h>
2018-07-03 21:04:28 +02:00
#include <iostream>
2018-10-20 14:20:51 +02:00
#include <atomic>
#include <villas/node/config.h>
2018-10-20 14:20:51 +02:00
#include <villas/super_node.hpp>
#include <villas/exceptions.hpp>
#include <villas/log.hpp>
2016-11-20 02:45:39 -05:00
#include <villas/node.h>
2018-10-20 14:20:51 +02:00
#include <villas/utils.hpp>
2016-11-20 02:45:39 -05:00
#include <villas/hist.h>
#include <villas/timing.h>
#include <villas/pool.h>
2018-10-20 14:20:51 +02:00
#include <villas/kernel/rt.hpp>
2016-11-20 02:45:39 -05:00
2018-10-20 14:20:51 +02:00
#define CLOCK_ID CLOCK_MONOTONIC
2018-10-20 14:20:51 +02:00
using namespace villas;
using namespace villas::node;
2018-10-20 14:20:51 +02:00
static std::atomic<bool> stop(false);
2017-03-06 19:09:44 -04:00
void quit(int signal, siginfo_t *sinfo, void *ctx)
{
2018-10-20 14:20:51 +02:00
stop = true;
}
2016-10-22 20:37:02 -04:00
void usage()
{
2018-08-27 11:25:56 +02:00
std::cout << "Usage: villas-test-rtt [OPTIONS] CONFIG NODE" << std::endl
<< " CONFIG path to a configuration file" << std::endl
<< " NODE name of the node which shoud be used" << std::endl
<< " OPTIONS is one or more of the following options:" << std::endl
<< " -c CNT send CNT messages" << std::endl
<< " -f FD use file descriptor FD for result output instead of stdout" << std::endl
<< " -b BKTS number of buckets for histogram" << std::endl
<< " -w WMUP duration of histogram warmup phase" << std::endl
<< " -h show this usage information" << std::endl
<< " -V show the version of the tool" << std::endl << std::endl;
2018-10-20 14:20:51 +02:00
utils::print_copyright();
}
int main(int argc, char *argv[])
{
2018-08-17 12:41:10 +02:00
int ret;
2018-10-20 14:20:51 +02:00
struct hist hist;
struct timespec send, recv;
struct sample *smp_send = (struct sample *) new char[SAMPLE_LENGTH(2)];
struct sample *smp_recv = (struct sample *) new char[SAMPLE_LENGTH(2)];
struct node *node;
SuperNode sn;
Logger logger = logging.get("test-rtt");
2018-10-20 14:20:51 +02:00
/* Test options */
int count = -1; /**< Amount of messages which should be sent (default: -1 for unlimited) */
hist_cnt_t hist_warmup = 100;
int hist_buckets = 20;
/** 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;
/* Parse Arguments */
2018-11-30 19:57:22 +01:00
int c;
char *endptr;
2018-10-20 14:20:51 +02:00
while ((c = getopt (argc, argv, "w:h:r:f:c:b:Vd:")) != -1) {
switch (c) {
case 'c':
count = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
case 'f':
fd = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
case 'w':
hist_warmup = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
case 'b':
hist_buckets = strtoul(optarg, &endptr, 10);
goto check;
2018-08-27 11:23:21 +02:00
2018-05-08 11:43:16 +02:00
case 'V':
2018-10-20 14:20:51 +02:00
utils::print_version();
2018-05-08 11:43:16 +02:00
exit(EXIT_SUCCESS);
2018-10-20 14:20:51 +02:00
case 'd':
logging.setLevel(optarg);
break;
case 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
2015-08-07 01:11:43 +02:00
continue;
check: if (optarg == endptr)
throw RuntimeError("Failed to parse parse option argument '-{} {}'", c, optarg);
}
if (argc != optind + 2) {
usage();
exit(EXIT_FAILURE);
}
2018-10-20 14:20:51 +02:00
char *uri = argv[optind];
char *nodestr = argv[optind + 1];
2018-10-20 14:20:51 +02:00
ret = utils::signals_init(quit);
2018-08-17 12:41:10 +02:00
if (ret)
throw RuntimeError("Failed to initialize signals subsystem");
2018-10-20 14:20:51 +02:00
if (uri) {
ret = sn.parseUri(uri);
if (ret)
throw RuntimeError("Failed to parse configuration");
2018-10-20 14:20:51 +02:00
}
else
logger->warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
2018-07-03 21:51:48 +02:00
node = sn.getNode(nodestr);
if (!node)
throw RuntimeError("There's no node with the name '{}'", nodestr);
2018-12-02 02:56:52 +01:00
ret = node_type_start(node->_vt, reinterpret_cast<super_node *>(&sn));
2018-08-17 12:40:03 +02:00
if (ret)
throw RuntimeError("Failed to start node-type {}: reason={}", node_type_name(node->_vt), ret);
2018-08-17 12:40:03 +02:00
ret = node_init2(node);
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2018-08-17 12:40:03 +02:00
ret = node_start(node);
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node_name(node), ret);
2018-10-20 14:20:51 +02:00
ret = hist_init(&hist, hist_buckets, hist_warmup);
2018-08-17 12:41:10 +02:00
if (ret)
throw RuntimeError("Failed to initialize histogram");
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");
2018-10-20 14:20:51 +02:00
while (!stop && (count < 0 || count--)) {
2016-06-08 22:38:21 +02:00
clock_gettime(CLOCK_ID, &send);
unsigned release;
release = 1; // release = allocated
node_write(node, &smp_send, 1, &release); /* Ping */
release = 1; // release = allocated
node_read(node, &smp_recv, 1, &release); /* Pong */
clock_gettime(CLOCK_ID, &recv);
2016-06-08 22:38:21 +02:00
double rtt = time_delta(&recv, &send);
2015-06-02 22:43:57 +02:00
if (rtt < 0)
2018-10-20 14:20:51 +02:00
logger->warn("Negative RTT: {}", rtt);
2015-08-07 01:11:43 +02:00
2015-06-02 22:43:57 +02:00
hist_put(&hist, rtt);
2016-06-08 22:38:21 +02:00
smp_send->sequence++;
fprintf(stdout, "%10lu.%06lu%5" PRIu64 "%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));
}
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);
2018-10-20 14:20:51 +02:00
fclose(f);
}
else
throw RuntimeError("Invalid file descriptor: {}", fd);
2015-08-07 01:11:43 +02:00
hist_print(&hist, 1);
2018-10-20 14:20:51 +02:00
ret = hist_destroy(&hist);
if (ret)
throw RuntimeError("Failed to destroy histogram");
2018-10-20 14:20:51 +02:00
ret = node_stop(node);
if (ret)
throw RuntimeError("Failed to stop node {}: reason={}", node_name(node), ret);
2018-10-20 14:20:51 +02:00
ret = node_type_stop(node->_vt);
if (ret)
throw RuntimeError("Failed to stop node-type {}: reason={}", node_type_name(node->_vt), ret);
2018-10-20 14:20:51 +02:00
delete smp_send;
delete smp_recv;
return 0;
}