FadingCounter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "FadingCounter.h"
12 #include "SquidTime.h"
13 
14 #include <cmath>
15 
16 FadingCounter::FadingCounter(): horizon(-1), precision(10), delta(-1),
17  lastTime(0), total(0)
18 {
19  counters.reserve(precision);
20  while (counters.size() < static_cast<unsigned int>(precision))
21  counters.push_back(0);
22 }
23 
25 {
26  for (int i = 0; i < precision; ++i)
27  counters[i] = 0;
29  total = 0;
30 }
31 
32 void FadingCounter::configure(double newHorizon)
33 {
34  if (fabs(newHorizon - horizon) >= 1e-3) { // diff exceeds one millisecond
35  clear(); // for simplicity
36  horizon = newHorizon;
37  delta = horizon / precision; // may become negative or zero
38  }
39 }
40 
41 int FadingCounter::count(int howMany)
42 {
43  Must(howMany >= 0);
44 
45  if (delta < 0)
46  return total += howMany; // forget nothing
47 
48  if (horizon < 1e-3) // (e.g., zero)
49  return howMany; // remember nothing
50 
51  const double deltas = (current_dtime - lastTime) / delta;
52  if (deltas >= precision || current_dtime < lastTime) {
53  clear(); // forget all values
54  } else {
55  // forget stale values, if any
56  // fmod() or "current_dtime/delta" will overflow int for small deltas
57  const int lastSlot = static_cast<int>(fmod(lastTime, horizon) / delta);
58  const int staleSlots = static_cast<int>(deltas);
59  for (int i = 0, s = lastSlot + 1; i < staleSlots; ++i, ++s) {
60  const int idx = s % precision;
61  total -= counters[idx];
62  counters[idx] = 0;
63  Must(total >= 0);
64  }
65  }
66 
67  // apply new information
69  const int curSlot = static_cast<int>(fmod(lastTime, horizon) / delta);
70  counters[curSlot % precision] += howMany;
71  total += howMany;
72  Must(total >= 0);
73 
74  return total;
75 }
76 
int i
Definition: membanger.c:49
double horizon
read-only memory horizon in seconds; older events are forgotten
Definition: FadingCounter.h:29
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
#define Must(cond)
Definition: TextException.h:89
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