]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/FadingCounter.cc
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
10 #include "base/TextException.h"
12 #include "FadingCounter.h"
13 #include "SquidTime.h"
17 FadingCounter::FadingCounter(): horizon(-1), precision(10), delta(-1),
20 counters
.reserve(precision
);
21 while (counters
.size() < static_cast<unsigned int>(precision
))
22 counters
.push_back(0);
25 void FadingCounter::clear()
27 for (int i
= 0; i
< precision
; ++i
)
29 lastTime
= current_dtime
;
33 void FadingCounter::configure(double newHorizon
)
35 if (fabs(newHorizon
- horizon
) >= 1e-3) { // diff exceeds one millisecond
36 clear(); // for simplicity
38 delta
= horizon
/ precision
; // may become negative or zero
42 int FadingCounter::count(int howMany
)
47 return total
+= howMany
; // forget nothing
49 if (horizon
< 1e-3) // (e.g., zero)
50 return howMany
; // remember nothing
52 const double deltas
= (current_dtime
- lastTime
) / delta
;
53 if (deltas
>= precision
|| current_dtime
< lastTime
) {
54 clear(); // forget all values
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
];
68 // apply new information
69 lastTime
= current_dtime
;
70 const int curSlot
= static_cast<int>(fmod(lastTime
, horizon
) / delta
);
71 counters
[curSlot
% precision
] += howMany
;