From 634603c173a652098fcabe50f84c54b18e7eb8bb Mon Sep 17 00:00:00 2001
From: Jonas <j.koeppeler@tu-berlin.de>
Date: Mon, 28 Oct 2024 14:19:02 +0000
Subject: [PATCH] misc: Align tc_iterate behavior
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The execution of tc_iterate.sh can cause different behaviors depending
on whether the tc_iterate binary is installed. The tc_iterate prog runs
collects tc statistics "count"-times where as the bash loop runs for
"length" seconds. This leads to different measuring durations.

Signed-off-by: Jonas Köppeler <j.koeppeler@tu-berlin.de>
---
 flent/runners.py            |  3 +--
 flent/scripts/tc_iterate.sh |  6 ++----
 misc/tc_iterate.c           | 15 ++++++++-------
 3 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/flent/runners.py b/flent/runners.py
index dd300fd3..455e934e 100644
--- a/flent/runners.py
+++ b/flent/runners.py
@@ -2311,12 +2311,11 @@ def find_binary(self, interface, interval, length, host='localhost'):
             interface = 'eth0'
 
         return "{bash} {script} -i {interface} -I {interval:.2f} " \
-            "-c {count:.0f} -l {length} -H {host}".format(
+            "-l {length} -H {host}".format(
                 bash=bash,
                 script=script,
                 interface=interface,
                 interval=interval,
-                count=length // interval + 1,
                 length=length,
                 host=host)
 
diff --git a/flent/scripts/tc_iterate.sh b/flent/scripts/tc_iterate.sh
index a500a7b1..e5fc30bc 100755
--- a/flent/scripts/tc_iterate.sh
+++ b/flent/scripts/tc_iterate.sh
@@ -1,16 +1,14 @@
 #!/bin/bash
 
 interface=eth0
-count=10
 length=20
 interval=0.1
 command=qdisc
 host=localhost
 
-while getopts "i:c:l:I:C:H:" opt; do
+while getopts "i:l:I:C:H:" opt; do
     case $opt in
         i) interface=$OPTARG ;;
-        c) count=$OPTARG ;;
         l) length=$OPTARG ;;
         I) interval=$OPTARG ;;
         C) command=$OPTARG ;;
@@ -23,7 +21,7 @@ buffer=""
 
 
 command_string=$(cat <<EOF
-which tc_iterate >/dev/null && exec tc_iterate $buffer -i $interface -c $count -I $interval -C $command;
+which tc_iterate >/dev/null && exec tc_iterate $buffer -i $interface -l $length -I $interval -C $command;
 endtime=\$(date -d "$length sec" +%s%N);
 while (( \$(date +%s%N) <= \$endtime )); do
     tc -s $command show dev $interface;
diff --git a/misc/tc_iterate.c b/misc/tc_iterate.c
index 76df2ff0..9d2e5bc1 100644
--- a/misc/tc_iterate.c
+++ b/misc/tc_iterate.c
@@ -54,7 +54,7 @@
 #define NSEC_PER_SEC (1000000000.0)
 
 struct arg {
-	int count;
+	int length;
 	struct timespec interval;
 	double finterval;
 	char *interface;
@@ -66,7 +66,7 @@ typedef struct arg args;
 
 static const struct option long_options[] = {
 	{ "interface", required_argument	, NULL , 'i' } ,
-	{ "count"    , required_argument	, NULL , 'c' } ,
+	{ "length"    , required_argument	, NULL , 'l' } ,
 	{ "interval" , required_argument	, NULL , 'I' } ,
 	{ "command"  , required_argument	, NULL , 'C' } ,
 	{ "help"     , no_argument		, NULL , 'h' } ,
@@ -80,7 +80,7 @@ void usage (char *err) {
 		"\t-h --help \n"
 		"\t-b --buffer \n"
 		"\t-i --interface [eth0*,wlan0,etc]\n"
-		"\t-c --count     [number of iterations]\n"
+		"\t-l --length    [duration in seconds]\n"
 		"\t-I --interval  [fractional number of seconds]\n"
 		"\t-C --command   [qdisc]\n");
 	exit(-1);
@@ -90,13 +90,13 @@ static void defaults(args *a) {
 	a->interface = "eth0";
 	a->command = "qdisc";
 	a->finterval=.2;
-	a->count=10;
+	a->length=20;
 	a->interval.tv_nsec = 0;
 	a->interval.tv_sec = 0;
 	a->buffer = 0;
 }
 
-#define QSTRING "i:c:I:C:hb"
+#define QSTRING "i:l:I:C:hb"
 
 int process_options(int argc, char **argv, args *o)
 {
@@ -113,7 +113,7 @@ int process_options(int argc, char **argv, args *o)
 
 		switch (opt)
 		{
-		case 'c': o->count = strtoul(optarg,NULL,10);  break;
+		case 'l': o->length = strtoul(optarg,NULL,10);  break;
 		case 'I': o->finterval = strtod(optarg,NULL); break;
 		case 'C': o->command = optarg; break;
 		case 'i': o->interface = optarg; break;
@@ -193,6 +193,7 @@ int forkit(args *a)
 	sprintf(cmd,"%s show dev %s\n",a->command,a->interface);
 	int csize = strlen(cmd);
 	int ctr = 0;
+	int length_count = a->length/a->finterval;
 	timerfd_settime(timer,0,&new_value,NULL); // relative timer
 
 	do {
@@ -206,7 +207,7 @@ int forkit(args *a)
 			result(out,0,BUFFERSIZE,buffer);
 			perror("reading cmd output");
 		}
-	} while (ctr < a->count);
+	} while (ctr < length_count);
 	close(tool);
 	close(in);
 	if(a->buffer) {