Commit c4b422e1 authored by Dorel Coman's avatar Dorel Coman Committed by Oliver Horst
Browse files

memguard: removed unused ewma, we are using only ema() and was included in memguard.c

parent b69637a9
/*
* Created by Dorel Coman on 02.01.18.
*
* Exponentially weighted moving average (EWMA)
*
* This source code is licensed under the GNU General Public License,
* Version 2. See the file COPYING for more details.
*
* The original can be fond at the link:
* https://elixir.bootlin.com/linux/v4.2.8/source/lib/average.c
*
* The original code was discontinued from Linux v4.3
*
* For more documentation see ewma.c
*/
#ifndef PROJECT_EWMA_H
#define PROJECT_EWMA_H7
#include <stdint-gcc.h>
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
/**
*
*/
struct ewma {
unsigned long internal;
unsigned long factor;
unsigned long weight;
};
/**
* DOC: Exponentially Weighted Moving Average (EWMA)
*
* These are generic functions for calculating Exponentially Weighted Moving
* Averages (EWMA). We keep a structure with the EWMA parameters and a scaled
* up internal representation of the average value to prevent rounding errors.
* The factor for scaling up and the exponential weight (or decay rate) have to
* be specified thru the init fuction. The structure should not be accessed
* directly but only thru the helper functions.
*/
/**
* ewma_init() - Initialize EWMA parameters
* @avg: Average structure
* @factor: Factor to use for the scaled up internal value. The maximum value
* of averages can be ULONG_MAX/(factor*weight). For performance reasons
* factor has to be a power of 2.
* @weight: Exponential weight, or decay rate. This defines how fast the
* influence of older values decreases. For performance reasons weight has
* to be a power of 2.
*
* Initialize the EWMA parameters for a given struct ewma @avg.
*/
extern void ewma_init(struct ewma *avg, unsigned long factor,
unsigned long weight);
/**
*
* @param avg
* @param val
* @return
*/
extern struct ewma *ewma_add(struct ewma *avg, unsigned long val);
/**
* ewma_read() - Get average value
* @avg: Average structure
*
* Returns the average value held in @avg.
*/
static inline unsigned long ewma_read(const struct ewma *avg)
{
return avg->internal >> avg->factor;
}
/**
* This function computes the exponentially moving average
*
* @param new_sample to be used for updating the average
* @param old_average computed in the last step, or initial value of the sample
* @return the new average
*/
uint32_t ema(uint32_t new_sample, uint32_t old_average, float alpha);
#endif //PROJECT_EWMA_H
/*
* Created by Dorel Coman on 02.01.18.
*
* This source code is licensed under the GNU General Public License,
* Version 2. See the file COPYING for more details.
*
* The original can be fond at the link:
* https://elixir.bootlin.com/linux/v4.2.8/source/lib/average.c
*
* The original code was discontinued from Linux v4.3
*/
#include "memguard/ewma.h"
#include <stdint-gcc.h>
inline int is_power_of_2(uint64_t n)
{
return (n != 0 && ((n & (n - 1)) == 0));
}
void ewma_init(struct ewma *avg, uint64_t factor, uint64_t weight)
{
// avg->weight = (uint64_t) ilog2(weight);
// avg->factor = (uint64_t) ilog2(factor);
avg->internal = 0;
}
/**
* ewma_add() - Exponentially weighted moving average (EWMA)
* @avg: Average structure
* @val: Current value
*
* Add a sample to the average.
*/
struct ewma *ewma_add(struct ewma *avg, uint64_t val)
{
uint64_t internal = ACCESS_ONCE(avg->internal);
ACCESS_ONCE(avg->internal) = internal ?
(((internal << avg->weight) - internal) +
(val << avg->factor)) >> avg->weight :
(val << avg->factor);
return avg;
}
uint32_t ema(uint32_t new_sample, uint32_t old_average, float alpha)
{
uint32_t new_average = (uint32_t)(alpha * new_sample) + (uint32_t)((1-alpha) * old_average);
return new_average;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment