1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020-2021 The FreeBSD Foundation 5 * 6 * This software was developed by Björn Zeeb under sponsorship from 7 * the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #ifndef _LINUXKPI_LINUX_AVERAGE_H 32 #define _LINUXKPI_LINUX_AVERAGE_H 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/types.h> 37 #include <linux/log2.h> 38 39 /* EWMA stands for Exponentially Weighted Moving Average. */ 40 /* 41 * Z_t = d X_t + (1 - d) * Z_(t-1); 0 < d <= 1, t >= 1; Roberts (1959). 42 * t : observation number in time. 43 * d : weight for current observation. 44 * Xt : observations over time. 45 * Zt : EWMA value after observation t. 46 * 47 * wmba_*_read seems to return up-to [u]long values; have to deal with 32/64bit. 48 * According to the ath5k.h change log this seems to be a fix-(_p)recision impl. 49 * assert 2/4 bits for frac. 50 * Also all (_d) values seem to be pow2 which simplifies maths (shift by 51 * d = ilog2(_d) instead of doing division (d = 1/_d)). Keep it this way until 52 * we hit the CTASSERT. 53 */ 54 55 #define DECLARE_EWMA(_name, _p, _d) \ 56 \ 57 CTASSERT((sizeof(unsigned long) <= 4) ? (_p < 30) : (_p < 60)); \ 58 CTASSERT(_d > 0 && powerof2(_d)); \ 59 \ 60 struct ewma_ ## _name { \ 61 unsigned long zt; \ 62 }; \ 63 \ 64 static __inline void \ 65 ewma_ ## _name ## _init(struct ewma_ ## _name *ewma) \ 66 { \ 67 /* No target (no historical data). */ \ 68 ewma->zt = 0; \ 69 } \ 70 \ 71 static __inline void \ 72 ewma_ ## _name ## _add(struct ewma_ ## _name *ewma, unsigned long x) \ 73 { \ 74 unsigned long ztm1 = ewma->zt; /* Z_(t-1). */ \ 75 int d = ilog2(_d); \ 76 \ 77 if (ewma->zt == 0) \ 78 ewma->zt = x << (_p); \ 79 else \ 80 ewma->zt = ((x << (_p)) >> d) + \ 81 (((ztm1 << d) - ztm1) >> d); \ 82 } \ 83 \ 84 static __inline unsigned long \ 85 ewma_ ## _name ## _read(struct ewma_ ## _name *ewma) \ 86 { \ 87 return (ewma->zt >> (_p)); \ 88 } \ 89 90 #endif /* _LINUXKPI_LINUX_AVERAGE_H */ 91