[Tarantool-patches] [PATCH v3 2/3] fiber.top(): alter exponential moving average calculation

Serge Petrenko sergepetrenko at tarantool.org
Mon Nov 18 19:05:04 MSK 2019


When fiber EMA is 0 and first non-zero observation is added to it, we assumed
that EMA should be equal to this observation (i.e. average value should
be the same as the observed one). This breaks the following invariant:
sum of clock EMAs of all fibers equals clock EMA of the thread.
If one of the fibers is just spawned and has a big clock delta, it
will assign this delta to its EMA, while the thread will calculate the
new EMA as 15 * EMA / 16 + delta / 16, which may lead to a situation
when fiber EMA is greater than cord EMA.

This caused occasional test failures:
```
[001] Test failed! Result content mismatch:
[001] --- app/fiber.result	Mon Nov 18 17:00:48 2019
[001] +++ app/fiber.reject	Mon Nov 18 17:33:10 2019
[001] @@ -1511,7 +1511,7 @@
[001]  -- not exact due to accumulated integer division errors
[001]  sum_avg > 99 and sum_avg < 101 or sum_avg
[001]  ---
[001] -- true
[001] +- 187.59585601717
[001]  ...
[001]  tbl = nil
[001]  ---

```

Follow-up #2694
---
 src/lib/core/fiber.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c
index 1e08d0ec9..232c29911 100644
--- a/src/lib/core/fiber.c
+++ b/src/lib/core/fiber.c
@@ -1098,11 +1098,7 @@ loop_on_iteration_start(ev_loop *loop, ev_check *watcher, int revents)
 static inline uint64_t
 clock_diff_accumulate(uint64_t acc, uint64_t delta)
 {
-	if (acc > 0) {
-		return delta / 16 + 15 * acc / 16;
-	} else {
-		return delta;
-	}
+	return delta / 16 + 15 * acc / 16;
 }
 
 inline void
-- 
2.21.0 (Apple Git-122)



More information about the Tarantool-patches mailing list