blob: 5576c28414968a1d384913885bfa8c3e5cd2b1c9 [file] [log] [blame]
Bruno Randolfc5485a72010-11-16 10:58:37 +09001/*
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
8#include <linux/module.h>
9#include <linux/average.h>
10#include <linux/bug.h>
Bruno Randolfaf556882010-12-02 19:50:37 +090011#include <linux/log2.h>
Bruno Randolfc5485a72010-11-16 10:58:37 +090012
13/**
14 * DOC: Exponentially Weighted Moving Average (EWMA)
15 *
16 * These are generic functions for calculating Exponentially Weighted Moving
17 * Averages (EWMA). We keep a structure with the EWMA parameters and a scaled
18 * up internal representation of the average value to prevent rounding errors.
19 * The factor for scaling up and the exponential weight (or decay rate) have to
20 * be specified thru the init fuction. The structure should not be accessed
21 * directly but only thru the helper functions.
22 */
23
24/**
25 * ewma_init() - Initialize EWMA parameters
26 * @avg: Average structure
27 * @factor: Factor to use for the scaled up internal value. The maximum value
Bruno Randolfaf556882010-12-02 19:50:37 +090028 * of averages can be ULONG_MAX/(factor*weight). For performance reasons
29 * factor has to be a power of 2.
Bruno Randolfc5485a72010-11-16 10:58:37 +090030 * @weight: Exponential weight, or decay rate. This defines how fast the
Bruno Randolfaf556882010-12-02 19:50:37 +090031 * influence of older values decreases. For performance reasons weight has
32 * to be a power of 2.
Bruno Randolfc5485a72010-11-16 10:58:37 +090033 *
34 * Initialize the EWMA parameters for a given struct ewma @avg.
35 */
36void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight)
37{
Bruno Randolfaf556882010-12-02 19:50:37 +090038 WARN_ON(!is_power_of_2(weight) || !is_power_of_2(factor));
39
40 avg->weight = ilog2(weight);
41 avg->factor = ilog2(factor);
Bruno Randolfc5485a72010-11-16 10:58:37 +090042 avg->internal = 0;
Bruno Randolfc5485a72010-11-16 10:58:37 +090043}
44EXPORT_SYMBOL(ewma_init);
45
46/**
47 * ewma_add() - Exponentially weighted moving average (EWMA)
48 * @avg: Average structure
49 * @val: Current value
50 *
51 * Add a sample to the average.
52 */
53struct ewma *ewma_add(struct ewma *avg, unsigned long val)
54{
55 avg->internal = avg->internal ?
Bruno Randolfaf556882010-12-02 19:50:37 +090056 (((avg->internal << avg->weight) - avg->internal) +
57 (val << avg->factor)) >> avg->weight :
58 (val << avg->factor);
Bruno Randolfc5485a72010-11-16 10:58:37 +090059 return avg;
60}
61EXPORT_SYMBOL(ewma_add);