Skip to content

Commit eb1e011

Browse files
committed
average: change to declare precision, not factor
Declaring the factor is counter-intuitive, and people are prone to using small(-ish) values even when that makes no sense. Change the DECLARE_EWMA() macro to take the fractional precision, in bits, rather than a factor, and update all users. While at it, add some more documentation. Acked-by: David S. Miller <[email protected]> Signed-off-by: Johannes Berg <[email protected]>
1 parent 48cac18 commit eb1e011

File tree

7 files changed

+47
-26
lines changed

7 files changed

+47
-26
lines changed

drivers/net/virtio_net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module_param(gso, bool, 0444);
5151
* at once, the weight is chosen so that the EWMA will be insensitive to short-
5252
* term, transient changes in packet size.
5353
*/
54-
DECLARE_EWMA(pkt_len, 1, 64)
54+
DECLARE_EWMA(pkt_len, 0, 64)
5555

5656
/* With mergeable buffers we align buffer address and use the low bits to
5757
* encode its true size. Buffer size is up to 1 page so we need to align to

drivers/net/wireless/ath/ath5k/ath5k.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ struct ath5k_statistics {
12521252
#define ATH5K_TXQ_LEN_MAX (ATH_TXBUF / 4) /* bufs per queue */
12531253
#define ATH5K_TXQ_LEN_LOW (ATH5K_TXQ_LEN_MAX / 2) /* low mark */
12541254

1255-
DECLARE_EWMA(beacon_rssi, 1024, 8)
1255+
DECLARE_EWMA(beacon_rssi, 10, 8)
12561256

12571257
/* Driver state associated with an instance of a device */
12581258
struct ath5k_hw {

drivers/net/wireless/ralink/rt2x00/rt2x00.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ struct link_qual {
257257
int tx_failed;
258258
};
259259

260-
DECLARE_EWMA(rssi, 1024, 8)
260+
DECLARE_EWMA(rssi, 10, 8)
261261

262262
/*
263263
* Antenna settings about the currently active link.

include/linux/average.h

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,66 @@
11
#ifndef _LINUX_AVERAGE_H
22
#define _LINUX_AVERAGE_H
33

4-
/* Exponentially weighted moving average (EWMA) */
4+
/*
5+
* Exponentially weighted moving average (EWMA)
6+
*
7+
* This implements a fixed-precision EWMA algorithm, with both the
8+
* precision and fall-off coefficient determined at compile-time
9+
* and built into the generated helper funtions.
10+
*
11+
* The first argument to the macro is the name that will be used
12+
* for the struct and helper functions.
13+
*
14+
* The second argument, the precision, expresses how many bits are
15+
* used for the fractional part of the fixed-precision values.
16+
*
17+
* The third argument, the weight reciprocal, determines how the
18+
* new values will be weighed vs. the old state, new values will
19+
* get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
20+
* that this parameter must be a power of two for efficiency.
21+
*/
522

6-
#define DECLARE_EWMA(name, _factor, _weight) \
23+
#define DECLARE_EWMA(name, _precision, _weight_rcp) \
724
struct ewma_##name { \
825
unsigned long internal; \
926
}; \
1027
static inline void ewma_##name##_init(struct ewma_##name *e) \
1128
{ \
12-
BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
13-
BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
14-
BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
15-
BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
29+
BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
30+
BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
31+
/* \
32+
* Even if you want to feed it just 0/1 you should have \
33+
* some bits for the non-fractional part... \
34+
*/ \
35+
BUILD_BUG_ON((_precision) > 30); \
36+
BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
1637
e->internal = 0; \
1738
} \
1839
static inline unsigned long \
1940
ewma_##name##_read(struct ewma_##name *e) \
2041
{ \
21-
BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
22-
BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
23-
BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
24-
BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
25-
return e->internal >> ilog2(_factor); \
42+
BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
43+
BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
44+
BUILD_BUG_ON((_precision) > 30); \
45+
BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
46+
return e->internal >> (_precision); \
2647
} \
2748
static inline void ewma_##name##_add(struct ewma_##name *e, \
2849
unsigned long val) \
2950
{ \
3051
unsigned long internal = ACCESS_ONCE(e->internal); \
31-
unsigned long weight = ilog2(_weight); \
32-
unsigned long factor = ilog2(_factor); \
52+
unsigned long weight_rcp = ilog2(_weight_rcp); \
53+
unsigned long precision = _precision; \
3354
\
34-
BUILD_BUG_ON(!__builtin_constant_p(_factor)); \
35-
BUILD_BUG_ON(!__builtin_constant_p(_weight)); \
36-
BUILD_BUG_ON_NOT_POWER_OF_2(_factor); \
37-
BUILD_BUG_ON_NOT_POWER_OF_2(_weight); \
55+
BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
56+
BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
57+
BUILD_BUG_ON((_precision) > 30); \
58+
BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
3859
\
3960
ACCESS_ONCE(e->internal) = internal ? \
40-
(((internal << weight) - internal) + \
41-
(val << factor)) >> weight : \
42-
(val << factor); \
61+
(((internal << weight_rcp) - internal) + \
62+
(val << precision)) >> weight_rcp : \
63+
(val << precision); \
4364
}
4465

4566
#endif /* _LINUX_AVERAGE_H */

net/batman-adv/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ struct batadv_gw_node {
402402
struct rcu_head rcu;
403403
};
404404

405-
DECLARE_EWMA(throughput, 1024, 8)
405+
DECLARE_EWMA(throughput, 10, 8)
406406

407407
/**
408408
* struct batadv_hardif_neigh_node_bat_v - B.A.T.M.A.N. V private neighbor

net/mac80211/ieee80211_i.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ struct ieee80211_sta_tx_tspec {
428428
bool downgraded;
429429
};
430430

431-
DECLARE_EWMA(beacon_signal, 16, 4)
431+
DECLARE_EWMA(beacon_signal, 4, 4)
432432

433433
struct ieee80211_if_managed {
434434
struct timer_list timer;

net/mac80211/sta_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ struct mesh_sta {
372372
unsigned int fail_avg;
373373
};
374374

375-
DECLARE_EWMA(signal, 1024, 8)
375+
DECLARE_EWMA(signal, 10, 8)
376376

377377
struct ieee80211_sta_rx_stats {
378378
unsigned long packets;

0 commit comments

Comments
 (0)