Skip to content

Commit

Permalink
stats: Rename functions and add file output and plotting
Browse files Browse the repository at this point in the history
File output not fully complete.

Signed-off-by: Casper Andersson <[email protected]>
  • Loading branch information
cappe987 committed Dec 31, 2024
1 parent 70aaf42 commit 86b979e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
9 changes: 5 additions & 4 deletions include/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
43 changes: 43 additions & 0 deletions plot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2024 Casper Andersson <[email protected]>
#!/bin/bash

metric=$1
input=$2
output=$3

if [ -z "$metric" ]; then
echo -e "Usage:\n plot.sh <metric> <input-file> <output-file>"
exit 1
fi
if [ -z "$input" ]; then
echo -e "Usage:\n plot.sh <metric> <input-file> <output-file>"
exit 1
fi
if [ -z "$output" ]; then
echo -e "Usage:\n plot.sh <metric> <input-file> <output-file>"
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
46 changes: 42 additions & 4 deletions src/stats.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: 2024 Casper Andersson <[email protected]>

#include "timestamping.h"
#include <inttypes.h>
#include <errno.h>
#include <stdlib.h>
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
9 changes: 5 additions & 4 deletions src/tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 86b979e

Please sign in to comment.