]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/average.h
1 #ifndef _LINUX_AVERAGE_H
2 #define _LINUX_AVERAGE_H
4 /* Exponentially weighted moving average (EWMA) */
6 /* For more documentation see lib/average.c */
9 unsigned long internal
;
14 extern void ewma_init(struct ewma
*avg
, unsigned long factor
,
15 unsigned long weight
);
17 extern struct ewma
*ewma_add(struct ewma
*avg
, unsigned long val
);
20 * ewma_read() - Get average value
21 * @avg: Average structure
23 * Returns the average value held in @avg.
25 static inline unsigned long ewma_read(const struct ewma
*avg
)
27 return avg
->internal
>> avg
->factor
;
30 #define DECLARE_EWMA(name, _factor, _weight) \
31 struct ewma_##name { \
32 unsigned long internal; \
34 static inline void ewma_##name##_init(struct ewma_##name *e) \
36 BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
37 BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
38 BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
39 BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
42 static inline unsigned long \
43 ewma_##name##_read(struct ewma_##name *e) \
45 BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
46 BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
47 BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
48 BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
49 return e->internal >> ilog2(_factor); \
51 static inline void ewma_##name##_add(struct ewma_##name *e, \
54 unsigned long internal = ACCESS_ONCE(e->internal); \
55 unsigned long weight = ilog2(_weight); \
56 unsigned long factor = ilog2(_factor); \
58 BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
59 BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
60 BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
61 BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
63 ACCESS_ONCE(e->internal) = internal ? \
64 (((internal << weight) - internal) + \
65 (val << factor)) >> weight : \
69 #endif /* _LINUX_AVERAGE_H */