1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <sys/time.h>
#include <array>
#include <set>
#include <unistd.h>
#include "lock.hh"
namespace pdns
{
// We keep three sets of related counters:
//
// 1. The current counters (thread local, updated individually by thread code very often)
// 2. The snapshot counters (thread local, updated by thread code in one single mutex protected copy)
// 3. The history counters (global) to keep track of the counters of deleted threads
// We have two main classes: one that holds the thread local counters
// (both current and snapshot ones) and one that aggregates the
// values for all threads and keeps the history counters.
// The thread local current counters are the ones updated by
// performance critical code. Every once in a while, all values in the
// current counters are copied to the snapshot thread local copies in
// a thread safe way.
// The snapshot counters are aggregated by the GlobalCounters
// class, as these can be accessed safely from multiple threads.
// Make sure to call the thread local tlocal.updatesAtomics() once
// in a while. This will fill the snapshot values for that thread if some
// time has passed since the last snap update.
// To fetch aggregate values, call globals.sum(counter1) or
// globals.avg(counter2), or any aggreggation function. If multiple
// counters need to be collected in a consistent way:
// auto data = globals.aggregatedSnap();
//
// Note that the aggregate values can mix somewhat older thread-local
// with newer thread-local info from another thread. So it is possible
// to see the following:
//
// If thread T1 increments "received" and then passes the packet to
// thread T2 that increments "processed", it may happen that the value
// of "processed" observed by sum() is higher than "received", as T1
// might not have called updateSnap() yet while T2 did. To avoid this
// inconsistency, be careful to update related counters in a single
// thread only.
// For an example of the use of these templates, see rec-tcounters.hh
template <typename Counters>
class TLocalCounters;
template <typename Counters>
class GlobalCounters
{
public:
// Register a thread local set of values
void subscribe(TLocalCounters<Counters>* ptr)
{
auto lock = d_guarded.lock();
lock->d_instances.emplace(ptr);
}
// Unregister, typically done when a thread exits
void unsubscribe(TLocalCounters<Counters>* ptr, const Counters& data)
{
auto lock = d_guarded.lock();
lock->d_instances.erase(ptr);
lock->d_history.merge(data);
}
// Two ways of computing aggregated values for a specific counter: simple additions of all thread data, or taking weighted averages into account
template <typename Enum>
auto sum(Enum index);
template <typename Enum>
auto avg(Enum index);
template <typename Enum>
auto max(Enum index);
// Aggregate all counter data for all threads
Counters aggregatedSnap();
// Reset history
void reset()
{
auto lock = d_guarded.lock();
lock->d_history = Counters();
}
private:
struct Guarded
{
// We have x instances, normally one per thread
std::set<TLocalCounters<Counters>*> d_instances;
// If an instance gets deleted because its thread is cleaned up, the values
// are accumulated in d_history
Counters d_history;
};
LockGuarded<Guarded> d_guarded;
};
template <typename Counters>
class TLocalCounters
{
public:
static constexpr suseconds_t defaultSnapUpdatePeriodus = 100000;
TLocalCounters(GlobalCounters<Counters>& collector, timeval interval = timeval{0, defaultSnapUpdatePeriodus}) :
d_collector(collector), d_interval(interval)
{
collector.subscribe(this);
}
~TLocalCounters()
{
d_collector.unsubscribe(this, d_current);
}
TLocalCounters(const TLocalCounters&) = delete;
TLocalCounters(TLocalCounters&&) = delete;
TLocalCounters& operator=(const TLocalCounters&) = delete;
TLocalCounters& operator=(TLocalCounters&&) = delete;
template <typename Enum>
auto& at(Enum index)
{
return d_current.at(index);
}
template <typename Enum>
// coverity[auto_causes_copy]
auto snapAt(Enum index)
{
return d_snapshot.lock()->at(index);
}
[[nodiscard]] Counters getSnap()
{
return *(d_snapshot.lock());
}
bool updateSnap(const timeval& tv_now, bool force = false)
{
timeval tv_diff{};
if (!force) {
timersub(&tv_now, &d_last, &tv_diff);
}
if (force || timercmp(&tv_diff, &d_interval, >=)) {
// It's a copy
*(d_snapshot.lock()) = d_current;
d_last = tv_now;
return true;
}
return false;
}
bool updateSnap(bool force = false)
{
timeval tv_now{};
if (!force) {
gettimeofday(&tv_now, nullptr);
}
return updateSnap(tv_now, force);
}
private:
GlobalCounters<Counters>& d_collector;
Counters d_current;
LockGuarded<Counters> d_snapshot;
timeval d_last{0, 0};
const timeval d_interval;
};
// Sum for a specific index
// In the future we might want to move the specifics of computing an aggregated value to the
// app specific Counters class
template <typename Counters>
template <typename Enum>
auto GlobalCounters<Counters>::sum(Enum index)
{
auto lock = d_guarded.lock();
auto sum = lock->d_history.at(index);
for (const auto& instance : lock->d_instances) {
sum += instance->snapAt(index);
}
return sum;
}
// Average for a specific index
// In the future we might want to move the specifics of computing an aggregated value to the
// app specific Counters class
template <typename Counters>
template <typename Enum>
auto GlobalCounters<Counters>::avg(Enum index)
{
auto lock = d_guarded.lock();
auto wavg = lock->d_history.at(index);
auto sum = wavg.avg * wavg.weight;
auto count = wavg.weight;
for (const auto& instance : lock->d_instances) {
auto val = instance->snapAt(index);
count += val.weight;
sum += val.avg * val.weight;
}
return count > 0 ? sum / count : 0;
}
// Max for a specific index
// In the future we might want to move the specifics of computing an aggregated value to the
// app specific Counters class
template <typename Counters>
template <typename Enum>
auto GlobalCounters<Counters>::max(Enum index)
{
auto lock = d_guarded.lock();
uint64_t max = 0; // ignore history
for (const auto& instance : lock->d_instances) {
max = std::max(instance->snapAt(index), max);
}
return max;
}
// Get a consistent snap of *all* aggregated values
template <typename Counters>
Counters GlobalCounters<Counters>::aggregatedSnap()
{
auto lock = d_guarded.lock();
Counters ret = lock->d_history;
for (const auto& instance : lock->d_instances) {
auto snap = instance->getSnap();
ret.merge(snap);
}
return ret;
}
}
|