From 86b979e7d6570cd2dd96af21eb7e36c6f567c8b0 Mon Sep 17 00:00:00 2001 From: Casper Andersson Date: Tue, 31 Dec 2024 02:48:19 +0100 Subject: [PATCH] stats: Rename functions and add file output and plotting File output not fully complete. Signed-off-by: Casper Andersson --- include/stats.h | 9 +++++---- plot.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ src/stats.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- src/tc.c | 9 +++++---- 4 files changed, 95 insertions(+), 12 deletions(-) create mode 100755 plot.sh diff --git a/include/stats.h b/include/stats.h index 29329b4..3ef9433 100644 --- a/include/stats.h +++ b/include/stats.h @@ -26,12 +26,13 @@ typedef struct { struct tsinfo *tsinfo; } Stats; -int init_stats(Stats *s, int size); -void free_stats(Stats *s); -int add_stats(Stats *s, int64_t tx_ts, int64_t rx_ts, int64_t correction, uint16_t seqid); +int stats_init(Stats *s, int size); +void stats_free(Stats *s); +int stats_add(Stats *s, int64_t tx_ts, int64_t rx_ts, int64_t correction, uint16_t seqid); Result stats_get_time_error(Stats *s); Result stats_get_latency(Stats *s); Result stats_get_pdv(Stats *s); -void show_stats(Stats *s, char *p1, char *p2, int count_left); +void stats_show(Stats *s, char *p1, char *p2, int count_left); +void stats_output_time_error(Stats *s, char *path); #endif /* __STATS_H__ */ diff --git a/plot.sh b/plot.sh new file mode 100755 index 0000000..c4cc8af --- /dev/null +++ b/plot.sh @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: 2024 Casper Andersson +#!/bin/bash + +metric=$1 +input=$2 +output=$3 + +if [ -z "$metric" ]; then + echo -e "Usage:\n plot.sh " + exit 1 +fi +if [ -z "$input" ]; then + echo -e "Usage:\n plot.sh " + exit 1 +fi +if [ -z "$output" ]; then + echo -e "Usage:\n plot.sh " + exit 1 +fi + +gnuplot <<-EOFMarker + set format y '%0.f' + # set yrange [0:*] + set xlabel 'Elapsed Time [s]' + set ylabel '$metric [ns]' + set grid ytics + set grid xtics + # set grid mxtics + set xtics nomirror + set ytics nomirror + set autoscale xfix + # unset key + # set key autotitle columnheader + #set style line 1 lc rgb '#E41A1C' pt 1 ps 1 lt 1 lw 2 # red + # set terminal pdfcairo enhanced color dashed font 'Arial, 14' rounded size 16 cm, 9.6 cm + set grid lt 1 lc rgb '#e6e6e6' #'grey' + set terminal pdfcairo rounded size 16 cm, 9.6 cm + set output '$output' + plot '$input' title '$metric' with lines ls 1 +EOFMarker + #set term svg + #set term png diff --git a/src/stats.c b/src/stats.c index a3176fd..0ae600a 100644 --- a/src/stats.c +++ b/src/stats.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only // SPDX-FileCopyrightText: 2024 Casper Andersson +#include "timestamping.h" #include #include #include @@ -15,7 +16,7 @@ loop and return a struct with all results. */ -int init_stats(Stats *s, int size) +int stats_init(Stats *s, int size) { if (size == 0) s->size = 100; @@ -30,13 +31,13 @@ int init_stats(Stats *s, int size) return 0; } -void free_stats(Stats *s) +void stats_free(Stats *s) { free(s->tsinfo); s->tsinfo = NULL; } -int add_stats(Stats *s, int64_t tx_ts, int64_t rx_ts, int64_t correction, uint16_t seqid) +int stats_add(Stats *s, int64_t tx_ts, int64_t rx_ts, int64_t correction, uint16_t seqid) { if (s->count == s->size) { s->size = s->size * 2; @@ -139,7 +140,7 @@ Result stats_get_pdv(Stats *s) return r; } -void show_stats(Stats *s, char *p1, char *p2, int count_left) +void stats_show(Stats *s, char *p1, char *p2, int count_left) { if (s->count == 0) { printf("No measurements\n"); @@ -172,3 +173,40 @@ void show_stats(Stats *s, char *p1, char *p2, int count_left) printf("Min : %" PRId64 " (lucky packet)\n", pdv.min); printf("===============\n"); } + +static int ts_sec(int64_t ts) +{ + return ts / NS_PER_SEC; +} + +static int ts_msec(int64_t ts) +{ + return (ts / 1000000) % 1000; +} + +static void write_val_to_file(Stats *s, FILE *fp, struct tsinfo *tsinfo, int64_t val) +{ + int64_t curr_time; + curr_time = tsinfo->tx_ts - s->tsinfo[0].tx_ts; + fprintf(fp, "%d.%03d %" PRId64 "\n", ts_sec(curr_time), ts_msec(curr_time), val); +} + +void stats_output_time_error(Stats *s, char *path) +{ + int64_t base_time, curr_time, time_error; + FILE *fp; + + base_time = s->tsinfo[0].tx_ts; + + fp = fopen(path, "w"); + if (!fp) { + ERR("failed opening file %s: %m", path); + return; + } + + for (int i = 0; i < s->count; i++) { + time_error = tsinfo_get_error(s->tsinfo[i]); + write_val_to_file(s, fp, &s->tsinfo[i], time_error); + } + fclose(fp); +} diff --git a/src/tc.c b/src/tc.c index 6b858e3..aad4f8a 100644 --- a/src/tc.c +++ b/src/tc.c @@ -148,7 +148,7 @@ static void run(struct pkt_cfg *cfg, char *p1, char *p2, int p1_sock, int p2_soc int err; int i; - err = init_stats(&s, cfg->count); + err = stats_init(&s, cfg->count); if (err) return; @@ -187,13 +187,14 @@ static void run(struct pkt_cfg *cfg, char *p1, char *p2, int p1_sock, int p2_soc } if (cfg->count > 0) cfg->count--; - add_stats(&s, tx_ts, rx_ts, correction, seqid); + stats_add(&s, tx_ts, rx_ts, correction, seqid); } if (!debugen && tc_running) printf("\n"); - show_stats(&s, p1, p2, cfg->count); - free_stats(&s); + stats_show(&s, p1, p2, cfg->count); + stats_output_time_error(&s, "timeerror.dat"); + stats_free(&s); } static int tc_parse_opt(int argc, char **argv, struct pkt_cfg *cfg, char **p1, char **p2,