c2f94677930abc9c81c03bd70dc20668ead0291d
[feed/routing.git] / batman-adv / patches / 0001-batman-adv-average-change-to-declare-precision-not-f.patch
1 From: Johannes Berg <johannes.berg@intel.com>
2 Date: Wed, 15 Feb 2017 09:49:26 +0100
3 Subject: [PATCH] batman-adv: average: change to declare precision, not factor
4
5 Declaring the factor is counter-intuitive, and people are prone
6 to using small(-ish) values even when that makes no sense.
7
8 Change the DECLARE_EWMA() macro to take the fractional precision,
9 in bits, rather than a factor, and update all users.
10
11 While at it, add some more documentation.
12
13 Acked-by: David S. Miller <davem@davemloft.net>
14 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
15 [sven@narfation.org: Added compatibility code]
16 Signed-off-by: Sven Eckelmann <sven@narfation.org>
17 ---
18 compat-include/linux/average.h | 67 +++++++++++++++++++++++++++---------------
19 net/batman-adv/types.h | 2 +-
20 2 files changed, 45 insertions(+), 24 deletions(-)
21
22 diff --git a/compat-include/linux/average.h b/compat-include/linux/average.h
23 index ec022cb6..a1e3c254 100644
24 --- a/compat-include/linux/average.h
25 +++ b/compat-include/linux/average.h
26 @@ -26,49 +26,70 @@
27
28 #include <linux/bug.h>
29
30 -#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
31 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
32 +#undef DECLARE_EWMA
33 +#endif /* < KERNEL_VERSION(4, 3, 0) */
34
35 -/* Exponentially weighted moving average (EWMA) */
36 +/*
37 + * Exponentially weighted moving average (EWMA)
38 + *
39 + * This implements a fixed-precision EWMA algorithm, with both the
40 + * precision and fall-off coefficient determined at compile-time
41 + * and built into the generated helper funtions.
42 + *
43 + * The first argument to the macro is the name that will be used
44 + * for the struct and helper functions.
45 + *
46 + * The second argument, the precision, expresses how many bits are
47 + * used for the fractional part of the fixed-precision values.
48 + *
49 + * The third argument, the weight reciprocal, determines how the
50 + * new values will be weighed vs. the old state, new values will
51 + * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
52 + * that this parameter must be a power of two for efficiency.
53 + */
54
55 -#define DECLARE_EWMA(name, _factor, _weight) \
56 +#define DECLARE_EWMA(name, _precision, _weight_rcp) \
57 struct ewma_##name { \
58 unsigned long internal; \
59 }; \
60 static inline void ewma_##name##_init(struct ewma_##name *e) \
61 { \
62 - BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
63 - BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
64 - BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
65 - BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
66 + BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
67 + BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
68 + /* \
69 + * Even if you want to feed it just 0/1 you should have \
70 + * some bits for the non-fractional part... \
71 + */ \
72 + BUILD_BUG_ON((_precision) > 30); \
73 + BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
74 e->internal = 0; \
75 } \
76 static inline unsigned long \
77 ewma_##name##_read(struct ewma_##name *e) \
78 { \
79 - BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
80 - BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
81 - BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
82 - BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
83 - return e->internal >> ilog2(_factor); \
84 + BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
85 + BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
86 + BUILD_BUG_ON((_precision) > 30); \
87 + BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
88 + return e->internal >> (_precision); \
89 } \
90 static inline void ewma_##name##_add(struct ewma_##name *e, \
91 unsigned long val) \
92 { \
93 unsigned long internal = ACCESS_ONCE(e->internal); \
94 - unsigned long weight = ilog2(_weight); \
95 - unsigned long factor = ilog2(_factor); \
96 + unsigned long weight_rcp = ilog2(_weight_rcp); \
97 + unsigned long precision = _precision; \
98 \
99 - BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
100 - BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
101 - BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
102 - BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
103 + BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
104 + BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
105 + BUILD_BUG_ON((_precision) > 30); \
106 + BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
107 \
108 ACCESS_ONCE(e->internal) = internal ? \
109 - (((internal << weight) - internal) + \
110 - (val << factor)) >> weight : \
111 - (val << factor); \
112 + (((internal << weight_rcp) - internal) + \
113 + (val << precision)) >> weight_rcp : \
114 + (val << precision); \
115 }
116
117 -#endif /* < KERNEL_VERSION(4, 3, 0) */
118 -
119 #endif /* _NET_BATMAN_ADV_COMPAT_LINUX_AVERAGE_H */
120 diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
121 index 8f64a5c0..66b25e41 100644
122 --- a/net/batman-adv/types.h
123 +++ b/net/batman-adv/types.h
124 @@ -402,7 +402,7 @@ struct batadv_gw_node {
125 struct rcu_head rcu;
126 };
127
128 -DECLARE_EWMA(throughput, 1024, 8)
129 +DECLARE_EWMA(throughput, 10, 8)
130
131 /**
132 * struct batadv_hardif_neigh_node_bat_v - B.A.T.M.A.N. V private neighbor