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-06-05 09:34:29 +00:00
|
|
|
|
2014-07-14 10:47:41 +00:00
|
|
|
#include "config.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-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:40 +00:00
|
|
|
int sd;
|
2014-09-09 12:07:57 +00:00
|
|
|
int running = 1;
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-09-08 14:07:44 +00:00
|
|
|
#define CLOCK_ID CLOCK_MONOTONIC_RAW
|
|
|
|
|
2014-09-09 12:07:57 +00:00
|
|
|
#define RTT_MIN 20
|
|
|
|
#define RTT_MAX 100
|
|
|
|
#define RTT_RESOLUTION 2
|
|
|
|
#define RTT_HIST (int) ((RTT_MAX - RTT_MIN) / RTT_RESOLUTION)
|
|
|
|
|
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-06-05 09:35:36 +00:00
|
|
|
if (argc != 4) {
|
|
|
|
printf("Usage: %s TEST LOCAL REMOTE\n", argv[0]);
|
2014-09-09 12:07:57 +00:00
|
|
|
printf(" TEST has to be 'rtt' for now\n");
|
2014-06-05 09:35:36 +00:00
|
|
|
printf(" LOCAL is a IP:PORT combination of the local host\n");
|
|
|
|
printf(" REMOTE is a IP:PORT combination of the remote host\n\n");
|
2014-09-07 16:29:01 +00:00
|
|
|
printf("Simulator2Simulator Server %s (built on %s %s)\n", BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
|
2014-06-05 09:34:29 +00:00
|
|
|
printf("Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-07-18 16:05:45 +00:00
|
|
|
struct node n = NODE_INIT("remote");
|
|
|
|
|
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-06-05 09:35:15 +00:00
|
|
|
/* Resolve addresses */
|
2014-06-25 01:53:35 +00:00
|
|
|
if (resolve_addr(argv[2], &n.local, 0))
|
|
|
|
error("Failed to resolve local address: %s", argv[2]);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-25 01:53:35 +00:00
|
|
|
if (resolve_addr(argv[3], &n.remote, 0))
|
|
|
|
error("Failed to resolve remote address: %s", argv[3]);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:35:18 +00:00
|
|
|
node_connect(&n);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-09-09 12:07:57 +00:00
|
|
|
if (!strcmp(argv[1], "rtt")) {
|
2014-07-18 16:05:45 +00:00
|
|
|
struct msg m = MSG_INIT(sizeof(struct timespec) / sizeof(float));
|
2014-06-25 01:53:39 +00:00
|
|
|
struct timespec *ts1 = (struct timespec *) &m.data;
|
|
|
|
struct timespec *ts2 = malloc(sizeof(struct timespec));
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-09-08 14:07:44 +00:00
|
|
|
double rtt;
|
|
|
|
double rtt_max = LLONG_MIN;
|
|
|
|
double rtt_min = LLONG_MAX;
|
|
|
|
double avg = 0;
|
2014-06-25 17:50:27 +00:00
|
|
|
int run = 0;
|
2014-09-09 12:07:57 +00:00
|
|
|
int bar;
|
|
|
|
unsigned hist[RTT_HIST];
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-09-09 12:07:57 +00:00
|
|
|
memset(hist, 0, RTT_HIST * sizeof(unsigned));
|
2014-06-25 17:50:31 +00:00
|
|
|
|
|
|
|
#if 1 /* Print header */
|
|
|
|
fprintf(stdout, "%17s", "timestamp");
|
|
|
|
#endif
|
2014-09-08 14:07:44 +00:00
|
|
|
fprintf(stdout, "%5s%10s%10s%10s%10s\n", "seq", "rtt", "min", "max", "avg");
|
2014-06-25 17:50:31 +00:00
|
|
|
|
2014-09-09 12:07:57 +00:00
|
|
|
while (running) {
|
2014-09-08 14:07:44 +00:00
|
|
|
m.sequence++;
|
2014-09-09 12:07:57 +00:00
|
|
|
run++;
|
2014-09-08 14:07:44 +00:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_ID, ts1);
|
2014-06-25 01:53:39 +00:00
|
|
|
msg_send(&m, &n);
|
|
|
|
msg_recv(&m, &n);
|
2014-09-08 14:07:44 +00:00
|
|
|
clock_gettime(CLOCK_ID, ts2);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-25 17:50:27 +00:00
|
|
|
rtt = timespec_delta(ts1, ts2);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-25 01:53:39 +00:00
|
|
|
if (rtt < 0) continue;
|
2014-06-05 09:34:51 +00:00
|
|
|
if (rtt > rtt_max) rtt_max = rtt;
|
|
|
|
if (rtt < rtt_min) rtt_min = rtt;
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-05 09:34:51 +00:00
|
|
|
avg += rtt;
|
2014-09-09 12:07:57 +00:00
|
|
|
|
|
|
|
/* Update histogram */
|
|
|
|
bar = (rtt * 1000 / RTT_RESOLUTION) - (RTT_MIN / RTT_RESOLUTION);
|
|
|
|
if (bar < RTT_HIST)
|
|
|
|
hist[bar]++;
|
2014-06-05 09:34:51 +00:00
|
|
|
|
2014-06-25 17:50:31 +00:00
|
|
|
#if 1
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
fprintf(stdout, "%17.6f", ts.tv_sec + ts.tv_nsec / 1e9);
|
|
|
|
#endif
|
|
|
|
|
2014-09-09 12:07:57 +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 / run);
|
2014-06-05 09:34:51 +00:00
|
|
|
}
|
2014-06-25 17:50:27 +00:00
|
|
|
|
|
|
|
free(ts2);
|
2014-09-09 12:07:57 +00:00
|
|
|
|
|
|
|
hist_plot(hist, RTT_HIST);
|
2014-09-09 12:08:04 +00:00
|
|
|
hist_dump(hist, RTT_HIST);
|
2014-06-05 09:34:51 +00:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-09-09 12:07:57 +00:00
|
|
|
close(sd);
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|