From de6ec8056fc18d622f41a380316fb8709ab765fa Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 28 Aug 2018 22:55:51 +0100 Subject: [PATCH] fs/accounting: fix moving average speed for file stats Before this change the moving average for the individual file stats would start at 0 and only converge to the correct value over 15-30 seconds. This change starts the weighting period as 1 and moves it up once per sample which gets the average to a better value instantly. --- fs/accounting/accounting.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/accounting/accounting.go b/fs/accounting/accounting.go index a3746f5e9..398588d72 100644 --- a/fs/accounting/accounting.go +++ b/fs/accounting/accounting.go @@ -129,6 +129,7 @@ func (acc *Account) UpdateReader(in io.ReadCloser) { // averageLoop calculates averages for the stats in the background func (acc *Account) averageLoop() { tick := time.NewTicker(time.Second) + var period float64 defer tick.Stop() for { select { @@ -137,7 +138,11 @@ func (acc *Account) averageLoop() { // Add average of last second. elapsed := now.Sub(acc.lpTime).Seconds() avg := float64(acc.lpBytes) / elapsed - acc.avg = (avg + (averagePeriod-1)*acc.avg) / averagePeriod + // Soft start the moving average + if period < averagePeriod { + period++ + } + acc.avg = (avg + (period-1)*acc.avg) / period acc.lpBytes = 0 acc.lpTime = now // Unlock stats