DORMANTno replies

[PATCH] Fix cyclictest negative durations

From: Francis Giraldeau <hidden>
Date: 2016-07-04 16:43:50
Subsystem: the rest · Maintainer: Linus Torvalds

When the next interval is too far in the future, then now < next, and the
elapsed time calcdiff(now, next) is negative. If such a situation occurs, it is
obvious the calculation of the next interval was wrong, and we rewind it until
we find a positive value.

This bug can be reproduced by setting a very short interval, such as 1us:

Without the patch:
  ./cyclictest -i 1
  T: 0 ( 8429) P: 0 I:1 C:1099294 Min:      0 Act:  -34 Avg:-9223372036854775808 Max:      -1

With the patch:
  ./cyclictest -i 1
  T: 0 ( 8665) P: 0 I:1 C: 419151 Min:      0 Act:    0 Avg:    0 Max:      76

Signed-off-by: Francis Giraldeau <redacted>
---
 src/cyclictest/cyclictest.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 34053c5..151b0eb 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -914,6 +914,16 @@ void *timerthread(void *param)
 			goto out;
 		}
 
+		// the next interval was too far in the future, rewind it
+		while (calcdiff_ns(now, next) < 0) {
+			next.tv_sec -= interval.tv_sec;
+			next.tv_nsec -= interval.tv_nsec;
+			if (next.tv_nsec < 0) {
+				next.tv_sec--;
+				next.tv_nsec += NSEC_PER_SEC;
+			}
+		}
+
 		if (use_nsecs)
 			diff = calcdiff_ns(now, next);
 		else
-- 
2.7.4
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help