Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 2 | #ifndef _LINUX_AVERAGE_H |
| 3 | #define _LINUX_AVERAGE_H |
| 4 | |
Mark Rutland | ef4d9af | 2017-10-23 14:07:19 -0700 | [diff] [blame] | 5 | #include <linux/bug.h> |
| 6 | #include <linux/compiler.h> |
| 7 | #include <linux/log2.h> |
| 8 | |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 9 | /* |
| 10 | * Exponentially weighted moving average (EWMA) |
| 11 | * |
| 12 | * This implements a fixed-precision EWMA algorithm, with both the |
| 13 | * precision and fall-off coefficient determined at compile-time |
| 14 | * and built into the generated helper funtions. |
| 15 | * |
| 16 | * The first argument to the macro is the name that will be used |
| 17 | * for the struct and helper functions. |
| 18 | * |
| 19 | * The second argument, the precision, expresses how many bits are |
| 20 | * used for the fractional part of the fixed-precision values. |
| 21 | * |
| 22 | * The third argument, the weight reciprocal, determines how the |
| 23 | * new values will be weighed vs. the old state, new values will |
| 24 | * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note |
| 25 | * that this parameter must be a power of two for efficiency. |
| 26 | */ |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 27 | |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 28 | #define DECLARE_EWMA(name, _precision, _weight_rcp) \ |
Johannes Berg | 2377799 | 2015-07-13 12:17:25 +0200 | [diff] [blame] | 29 | struct ewma_##name { \ |
| 30 | unsigned long internal; \ |
| 31 | }; \ |
| 32 | static inline void ewma_##name##_init(struct ewma_##name *e) \ |
| 33 | { \ |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 34 | BUILD_BUG_ON(!__builtin_constant_p(_precision)); \ |
| 35 | BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \ |
| 36 | /* \ |
| 37 | * Even if you want to feed it just 0/1 you should have \ |
| 38 | * some bits for the non-fractional part... \ |
| 39 | */ \ |
| 40 | BUILD_BUG_ON((_precision) > 30); \ |
| 41 | BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \ |
Johannes Berg | 2377799 | 2015-07-13 12:17:25 +0200 | [diff] [blame] | 42 | e->internal = 0; \ |
| 43 | } \ |
| 44 | static inline unsigned long \ |
| 45 | ewma_##name##_read(struct ewma_##name *e) \ |
| 46 | { \ |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 47 | BUILD_BUG_ON(!__builtin_constant_p(_precision)); \ |
| 48 | BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \ |
| 49 | BUILD_BUG_ON((_precision) > 30); \ |
| 50 | BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \ |
| 51 | return e->internal >> (_precision); \ |
Johannes Berg | 2377799 | 2015-07-13 12:17:25 +0200 | [diff] [blame] | 52 | } \ |
| 53 | static inline void ewma_##name##_add(struct ewma_##name *e, \ |
| 54 | unsigned long val) \ |
| 55 | { \ |
Mark Rutland | ef4d9af | 2017-10-23 14:07:19 -0700 | [diff] [blame] | 56 | unsigned long internal = READ_ONCE(e->internal); \ |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 57 | unsigned long weight_rcp = ilog2(_weight_rcp); \ |
| 58 | unsigned long precision = _precision; \ |
Johannes Berg | 2377799 | 2015-07-13 12:17:25 +0200 | [diff] [blame] | 59 | \ |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 60 | BUILD_BUG_ON(!__builtin_constant_p(_precision)); \ |
| 61 | BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \ |
| 62 | BUILD_BUG_ON((_precision) > 30); \ |
| 63 | BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \ |
Johannes Berg | 2377799 | 2015-07-13 12:17:25 +0200 | [diff] [blame] | 64 | \ |
Mark Rutland | ef4d9af | 2017-10-23 14:07:19 -0700 | [diff] [blame] | 65 | WRITE_ONCE(e->internal, internal ? \ |
Johannes Berg | eb1e011 | 2017-02-15 09:49:26 +0100 | [diff] [blame] | 66 | (((internal << weight_rcp) - internal) + \ |
| 67 | (val << precision)) >> weight_rcp : \ |
Mark Rutland | ef4d9af | 2017-10-23 14:07:19 -0700 | [diff] [blame] | 68 | (val << precision)); \ |
Johannes Berg | 2377799 | 2015-07-13 12:17:25 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Bruno Randolf | c5485a7 | 2010-11-16 10:58:37 +0900 | [diff] [blame] | 71 | #endif /* _LINUX_AVERAGE_H */ |