3#include <click/glue.hh>
7template <
unsigned alpha,
unsigned shift>
12 void update(
unsigned obs) {
13 val = (obs * alpha + ((1 << shift) - alpha) * obs) >> shift;
17 unsigned unscaled_average()
const {
21 unsigned average()
const {
95 typedef typename P::value_type value_type;
104 : _avg(scaled_value) {
116 return (_avg + (P::scaled_one() >> 1)) >> P::scale();
125 inline void assign(value_type scaled_value) {
131 inline void update(value_type x);
138 void update_n(value_type x,
unsigned n);
148 inline void update_with(value_type x) CLICK_DEPRECATED;
160 value_type x_scaled = (x << P::scale()) + P::compensation();
161 unsigned stability = P::stability_shift();
162#if HAVE_ARITHMETIC_RIGHT_SHIFT
163 _avg +=
static_cast<typename P::signed_value_type
>(x_scaled - _avg) >> stability;
166 _avg -= (_avg - x_scaled) >> stability;
168 _avg += (x_scaled - _avg) >> stability;
177 value_type x_scaled = x << P::scale();
181 x_scaled += P::compensation();
182 unsigned stability = P::stability_shift();
183#if HAVE_ARITHMETIC_RIGHT_SHIFT
185 _avg +=
static_cast<typename P::signed_value_type
>(x_scaled - _avg) >> stability;
189 _avg -= (_avg - x_scaled) >> stability;
192 _avg += (x_scaled - _avg) >> stability;
201 return cp_unparse_real2(scaled_average(), P::scale());
231template <
unsigned STABILITY,
unsigned SCALE,
typename T =
unsigned,
typename U =
int>
234 typedef T value_type;
235 typedef U signed_value_type;
246 static_assert(SCALE <
sizeof(T) * 8,
"SCALE too big for EWMA type.");
252 return (value_type) 1 << SCALE;
258 return 1 << (STABILITY - 1);
289template <
unsigned SCALE,
typename T =
unsigned,
typename U =
int>
292 typedef T value_type;
293 typedef U signed_value_type;
321 return (value_type) 1 << SCALE;
394 typedef typename P::value_type value_type;
395 typedef typename P::signed_value_type signed_value_type;
399 _current_epoch = P::epoch();
400 for (
unsigned i = 0; i < P::rate_count; i++)
413 return _avg[ratenum].scaled_average();
418 unsigned scale(
unsigned ratenum = 0)
const {
419 return _avg[ratenum].scale();
429 inline int rate(
unsigned ratenum = 0)
const;
439 inline void update(signed_value_type delta,
unsigned ratenum = 0);
453 unsigned _current_epoch;
454 value_type _current[P::rate_count];
457 inline void update_time(
unsigned now);
474template <
unsigned STABILITY,
unsigned SCALE,
typename T =
unsigned,
typename U =
int>
483 return click_jiffies();
502 unsigned jj = _current_epoch;
504 for (
unsigned i = 0; i < P::rate_count; i++) {
506 _avg[i].
update(_current[i]);
510 _avg[i].update_n(0, now - jj - 1);
513 _current_epoch = now;
521 update_time(P::epoch());
522 _current[ratenum] += delta;
529 return (scaled_average(ratenum) * P::epoch_frequency()) >> _avg[ratenum].scale();
536 return cp_unparse_real2(scaled_average(ratenum) * P::epoch_frequency(), _avg[ratenum].scale());
An exponentially weighted moving average.
Definition ewma.hh:93
String unparse() const
Unparse the current average into a String.
Definition ewma.hh:199
void assign(value_type scaled_value)
Assign the EWMA to scaled average scaled_value.
Definition ewma.hh:125
void clear()
Reset the EWMA to value 0.
Definition ewma.hh:120
void update(value_type x)
Update the moving average with a new observation.
Definition ewma.hh:158
value_type scaled_average() const
Return the current scaled moving average.
Definition ewma.hh:109
void update_n(value_type x, unsigned n)
Update the moving average with n identical observations.
Definition ewma.hh:174
void update_with(value_type x)
Update the moving average with a new observation (deprecated).
Definition ewma.hh:206
DirectEWMAX(value_type scaled_value)
Construct a EWMA with initial scaled average scaled_value.
Definition ewma.hh:103
DirectEWMAX()
Construct a EWMA with initial average 0.
Definition ewma.hh:98
value_type unscaled_average() const
Return the current moving average, rounded up.
Definition ewma.hh:115
Parameters for a EWMA with constant scaling factor and stability shift.
Definition ewma.hh:232
static value_type scaled_one()
Return this EWMA's scaled value for one.
Definition ewma.hh:251
static unsigned stability_shift()
Return this EWMA's stability shift.
Definition ewma.hh:239
static unsigned scale()
Return this EWMA's scaling factor (bits of fraction).
Definition ewma.hh:245
static unsigned compensation()
Return this EWMA's compensation.
Definition ewma.hh:257
An exponentially weighted moving average used to measure a rate.
Definition ewma.hh:392
RateEWMAX()
Create a rate EWMA with initial value(s) 0.
Definition ewma.hh:398
int rate(unsigned ratenum=0) const
Return the current rate in samples per second.
Definition ewma.hh:527
void update(signed_value_type delta, unsigned ratenum=0)
Update the sample count for the current epoch.
Definition ewma.hh:519
signed_value_type scaled_average(unsigned ratenum=0) const
Return the current scaled moving average.
Definition ewma.hh:411
unsigned scale(unsigned ratenum=0) const
Returns one of the average's scaling factors (bits of fraction).
Definition ewma.hh:418
String unparse_rate(unsigned ratenum=0) const
Unparse the current average into a String.
Definition ewma.hh:534
Parameters for a RateEWMA with constant scaling factor and alpha, one rate count, and epochs of jiffi...
Definition ewma.hh:475
static unsigned epoch()
Return the current epoch number.
Definition ewma.hh:482
static unsigned epoch_frequency()
Return the number of epochs (jiffies) per second.
Definition ewma.hh:487
Parameters for a EWMA with constant scaling factor and user-settable alpha.
Definition ewma.hh:290
static value_type scaled_one()
Return this EWMA's scaled value for one.
Definition ewma.hh:320
void set_stability_shift(unsigned stability_shift)
Set the current stability shift.
Definition ewma.hh:309
unsigned compensation() const
Return this EWMA's compensation.
Definition ewma.hh:326
static unsigned scale()
Return this EWMA's scaling factor (bits of fraction).
Definition ewma.hh:315
unsigned stability_shift() const
Return the current stability shift.
Definition ewma.hh:303
StabilityEWMAXParameters()
Construct a StabilityEWMAXParameters with initial alpha 1/16.
Definition ewma.hh:296
A string of characters.
Definition string.hh:22
Support for parsing configuration strings.
DirectEWMAX< FixedEWMAXParameters< 3, 10 > > FastDirectEWMA
A DirectEWMAX with stability shift 3 (alpha 1/8), scaling factor 10 (10 bits of fraction),...
Definition ewma.hh:269
RateEWMAX< RateEWMAXParameters< 4, 10, uint64_t, int64_t > > RateEWMA
A RateEWMAX with stability shift 4 (alpha 1/16), scaling factor 10 (10 bits of fraction),...
Definition ewma.hh:495
DirectEWMAX< FixedEWMAXParameters< 4, 10 > > DirectEWMA
A DirectEWMAX with stability shift 4 (alpha 1/16), scaling factor 10 (10 bits of fraction),...
Definition ewma.hh:265