FadingCounter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "base/TextException.h"
11 #include "Debug.h"
12 #include "FadingCounter.h"
13 #include "SquidTime.h"
14 
15 #include <cmath>
16 
17 FadingCounter::FadingCounter(): horizon(-1), precision(10), delta(-1),
18  lastTime(0), total(0)
19 {
20  counters.reserve(precision);
21  while (counters.size() < static_cast<unsigned int>(precision))
22  counters.push_back(0);
23 }
24 
26 {
27  for (int i = 0; i < precision; ++i)
28  counters[i] = 0;
30  total = 0;
31 }
32 
33 void FadingCounter::configure(double newHorizon)
34 {
35  if (fabs(newHorizon - horizon) >= 1e-3) { // diff exceeds one millisecond
36  clear(); // for simplicity
37  horizon = newHorizon;
38  delta = horizon / precision; // may become negative or zero
39  }
40 }
41 
42 int FadingCounter::count(int howMany)
43 {
44  Must(howMany >= 0);
45 
46  if (delta < 0)
47  return total += howMany; // forget nothing
48 
49  if (horizon < 1e-3) // (e.g., zero)
50  return howMany; // remember nothing
51 
52  const double deltas = (current_dtime - lastTime) / delta;
53  if (deltas >= precision || current_dtime < lastTime) {
54  clear(); // forget all values
55  } else {
56  // forget stale values, if any
57  // fmod() or "current_dtime/delta" will overflow int for small deltas
58  const int lastSlot = static_cast<int>(fmod(lastTime, horizon) / delta);
59  const int staleSlots = static_cast<int>(deltas);
60  for (int i = 0, s = lastSlot + 1; i < staleSlots; ++i, ++s) {
61  const int idx = s % precision;
62  total -= counters[idx];
63  counters[idx] = 0;
64  Must(total >= 0);
65  }
66  }
67 
68  // apply new information
70  const int curSlot = static_cast<int>(fmod(lastTime, horizon) / delta);
71  counters[curSlot % precision] += howMany;
72  total += howMany;
73  Must(total >= 0);
74 
75  return total;
76 }
77 
int i
Definition: membanger.c:49
double horizon
read-only memory horizon in seconds; older events are forgotten
Definition: FadingCounter.h:29
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
int count(int howMany)
count fresh, return #events remembered
void clear()
forgets all events
std::vector< int > counters
events per delta (possibly stale)
Definition: FadingCounter.h:36
void configure(double horizonSeconds)
0=remember nothing; -1=forget nothing; new value triggers clear()
double delta
sub-interval duration = horizon/precision
Definition: FadingCounter.h:33
double current_dtime
Definition: stub_time.cc:16
int total
number of remembered events (possibly stale)
Definition: FadingCounter.h:37
const int precision
#counting slots, controls measur. occuracy
Definition: FadingCounter.h:32
double lastTime
time of the last update
Definition: FadingCounter.h:35

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors