Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 1 | /* |
| 2 | * lib/average.c |
| 3 | * |
| 4 | * This source code is licensed under the GNU General Public License, |
| 5 | * Version 2. See the file COPYING for more details. |
| 6 | */ |
| 7 | |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 8 | #include <linux/export.h> |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 9 | #include <linux/average.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 11 | #include <linux/bug.h> |
Bruno Randolf | af55688 | 2010-12-02 19:50:37 +0900 | [diff] [blame] | 12 | #include <linux/log2.h> |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * DOC: Exponentially Weighted Moving Average (EWMA) |
| 16 | * |
| 17 | * These are generic functions for calculating Exponentially Weighted Moving |
| 18 | * Averages (EWMA). We keep a structure with the EWMA parameters and a scaled |
| 19 | * up internal representation of the average value to prevent rounding errors. |
| 20 | * The factor for scaling up and the exponential weight (or decay rate) have to |
| 21 | * be specified thru the init fuction. The structure should not be accessed |
| 22 | * directly but only thru the helper functions. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * ewma_init() - Initialize EWMA parameters |
| 27 | * @avg: Average structure |
| 28 | * @factor: Factor to use for the scaled up internal value. The maximum value |
Bruno Randolf | af55688 | 2010-12-02 19:50:37 +0900 | [diff] [blame] | 29 | * of averages can be ULONG_MAX/(factor*weight). For performance reasons |
| 30 | * factor has to be a power of 2. |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 31 | * @weight: Exponential weight, or decay rate. This defines how fast the |
Bruno Randolf | af55688 | 2010-12-02 19:50:37 +0900 | [diff] [blame] | 32 | * influence of older values decreases. For performance reasons weight has |
| 33 | * to be a power of 2. |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 34 | * |
| 35 | * Initialize the EWMA parameters for a given struct ewma @avg. |
| 36 | */ |
| 37 | void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight) |
| 38 | { |
Bruno Randolf | af55688 | 2010-12-02 19:50:37 +0900 | [diff] [blame] | 39 | WARN_ON(!is_power_of_2(weight) || !is_power_of_2(factor)); |
| 40 | |
| 41 | avg->weight = ilog2(weight); |
| 42 | avg->factor = ilog2(factor); |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 43 | avg->internal = 0; |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 44 | } |
| 45 | EXPORT_SYMBOL(ewma_init); |
| 46 | |
| 47 | /** |
| 48 | * ewma_add() - Exponentially weighted moving average (EWMA) |
| 49 | * @avg: Average structure |
| 50 | * @val: Current value |
| 51 | * |
| 52 | * Add a sample to the average. |
| 53 | */ |
| 54 | struct ewma *ewma_add(struct ewma *avg, unsigned long val) |
| 55 | { |
Michael Dalton | 03144b5 | 2014-01-16 22:23:29 -0800 | [diff] [blame] | 56 | unsigned long internal = ACCESS_ONCE(avg->internal); |
| 57 | |
| 58 | ACCESS_ONCE(avg->internal) = internal ? |
| 59 | (((internal << avg->weight) - internal) + |
Bruno Randolf | af55688 | 2010-12-02 19:50:37 +0900 | [diff] [blame] | 60 | (val << avg->factor)) >> avg->weight : |
| 61 | (val << avg->factor); |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 62 | return avg; |
| 63 | } |
| 64 | EXPORT_SYMBOL(ewma_add); |