diff mbox

Calculate average latencies on the fly

Message ID CAK+oA4V8yFDnieAa4tsCQKTapNe6gA_VLJvvEh6_u_jgWpyahQ@mail.gmail.com
State Superseded
Headers show

Commit Message

Piotr Gregor Nov. 14, 2016, 10 a.m. UTC
Hi,

There is a possibility for average to overflow as it is first accumulated
and only divided when printing statistics. Attached is the patch
to calculate average on the go.

Patch is diffed between stable/v1.0 branch
git format-patch origin/stable/v1.0 --stdout > calc_average_on_the_fly.patch

cheers,
Piotr
diff mbox

Patch

From cb9a85222dff1a299fde25a96243139cde6a4947 Mon Sep 17 00:00:00 2001
From: Piotr Gregor <piotrgregor@rsyncme.org>
Date: Fri, 11 Nov 2016 11:28:09 +0000
Subject: [PATCH] rt-tests: cyclictest: Calculate average latencies on the fly

Prevents stat->avg variable from overflow.
---
 src/cyclictest/cyclictest.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 3f1bef1..2c4e450 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -1178,7 +1178,11 @@  static void *timerthread(void *param)
 			if (refresh_on_max)
 				pthread_cond_signal(&refresh_on_max_cond);
 		}
-		stat->avg += (double) diff;
+		if (stat->cycles == 0) {
+			stat->avg = (double) diff;
+		} else {
+			stat->avg = (stat->avg + (double) diff) * ((double) stat->cycles / (double) (stat->cycles + 1));
+		}
 
 		if (trigger && (diff > trigger)) {
 			trigger_update(par, diff, calctime(now));
@@ -2005,7 +2009,7 @@  static void print_hist(struct thread_param *par[], int nthreads)
 	fprintf(fd, "# Avg Latencies:");
 	for (j = 0; j < nthreads; j++)
 		fprintf(fd, " %05lu", par[j]->stats->cycles ?
-		       (long)(par[j]->stats->avg/par[j]->stats->cycles) : 0);
+		       (long)(par[j]->stats->avg) : 0);
 	fprintf(fd, "\n");
 	fprintf(fd, "# Max Latencies:");
 	maxmax = 0;
@@ -2059,7 +2063,7 @@  static void print_stat(FILE *fp, struct thread_param *par, int index, int verbos
 			fprintf(fp, fmt, index, stat->tid, par->prio,
 				par->interval, stat->cycles, stat->min,
 				stat->act, stat->cycles ?
-				(long)(stat->avg/stat->cycles) : 0, stat->max);
+				(long)(stat->avg) : 0, stat->max);
 
 			if (smi)
 				fprintf(fp," SMI:%8ld", stat->smi_count);
-- 
2.7.4