All of lore.kernel.org
 help / color / mirror / Atom feed
* Calculate average latencies on the fly
@ 2016-11-14 10:00 Piotr Gregor
  2016-11-14 11:50 ` Piotr Gregor
  0 siblings, 1 reply; 10+ messages in thread
From: Piotr Gregor @ 2016-11-14 10:00 UTC (permalink / raw)
  To: Clark Williams, John Kacur; +Cc: linux-rt-users

[-- Attachment #1: Type: text/plain, Size: 312 bytes --]

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

[-- Attachment #2: calc_average_on_the_fly.patch --]
[-- Type: text/x-patch, Size: 1753 bytes --]

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


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-11-22 15:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-14 10:00 Calculate average latencies on the fly Piotr Gregor
2016-11-14 11:50 ` Piotr Gregor
2016-11-14 15:42   ` Mathieu Poirier
2016-11-14 16:11     ` Piotr Gregor
2016-11-14 16:58       ` Clark Williams
2016-11-16 14:47   ` John Kacur
2016-11-19 23:27     ` Piotr Gregor
2016-11-20  5:03       ` Tracy Smith
2016-11-21 18:32       ` John Kacur
     [not found]         ` <CAChUvXOj_D2N3o7BoEUjt1A1O0fXZnvGSz5ba=O55h12ns___A@mail.gmail.com>
2016-11-22 15:43           ` John Kacur

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.