Blame view

lib/average.c 1.95 KB
c5485a7e7   Bruno Randolf   lib: Add generic ...
1
2
3
4
5
6
  /*
   * lib/average.c
   *
   * This source code is licensed under the GNU General Public License,
   * Version 2.  See the file COPYING for more details.
   */
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
7
  #include <linux/export.h>
c5485a7e7   Bruno Randolf   lib: Add generic ...
8
  #include <linux/average.h>
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
9
  #include <linux/kernel.h>
c5485a7e7   Bruno Randolf   lib: Add generic ...
10
  #include <linux/bug.h>
af5568843   Bruno Randolf   lib: Improve EWMA...
11
  #include <linux/log2.h>
c5485a7e7   Bruno Randolf   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   Bruno Randolf   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   Bruno Randolf   lib: Add generic ...
30
   * @weight: Exponential weight, or decay rate. This defines how fast the
af5568843   Bruno Randolf   lib: Improve EWMA...
31
32
   *	influence of older values decreases. For performance reasons weight has
   *	to be a power of 2.
c5485a7e7   Bruno Randolf   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   Bruno Randolf   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   Bruno Randolf   lib: Add generic ...
42
  	avg->internal = 0;
c5485a7e7   Bruno Randolf   lib: Add generic ...
43
44
45
46
47
48
49
50
51
52
53
54
  }
  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)
  {
03144b586   Michael Dalton   lib: Ensure EWMA ...
55
56
57
58
  	unsigned long internal = ACCESS_ONCE(avg->internal);
  
  	ACCESS_ONCE(avg->internal) = internal ?
  		(((internal << avg->weight) - internal) +
af5568843   Bruno Randolf   lib: Improve EWMA...
59
60
  			(val << avg->factor)) >> avg->weight :
  		(val << avg->factor);
c5485a7e7   Bruno Randolf   lib: Add generic ...
61
62
63
  	return avg;
  }
  EXPORT_SYMBOL(ewma_add);