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