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