| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/statistics.cc |
| Date: | 2025-11-30 02:35:17 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 158 | 160 | 98.8% |
| Branches: | 88 | 131 | 67.2% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "statistics.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cassert> | ||
| 9 | |||
| 10 | #include "json_document_write.h" | ||
| 11 | #include "util/concurrency.h" | ||
| 12 | #include "util/platform.h" | ||
| 13 | #include "util/smalloc.h" | ||
| 14 | #include "util/string.h" | ||
| 15 | |||
| 16 | using namespace std; // NOLINT | ||
| 17 | |||
| 18 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 19 | namespace CVMFS_NAMESPACE_GUARD { | ||
| 20 | #endif | ||
| 21 | |||
| 22 | namespace perf { | ||
| 23 | |||
| 24 | 46 | std::string Counter::ToString() { return StringifyInt(Get()); } | |
| 25 | 47 | std::string Counter::Print() { return StringifyInt(Get()); } | |
| 26 | 47 | std::string Counter::PrintK() { return StringifyInt(Get() / 1000); } | |
| 27 | 47 | std::string Counter::PrintKi() { return StringifyInt(Get() / 1024); } | |
| 28 | 47 | std::string Counter::PrintM() { return StringifyInt(Get() / (1000 * 1000)); } | |
| 29 | 47 | std::string Counter::PrintMi() { return StringifyInt(Get() / (1024 * 1024)); } | |
| 30 | 94 | std::string Counter::PrintRatio(Counter divider) { | |
| 31 | 94 | const double enumerator_value = Get(); | |
| 32 | 94 | const double divider_value = divider.Get(); | |
| 33 | 94 | return StringifyDouble(enumerator_value / divider_value); | |
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | //----------------------------------------------------------------------------- | ||
| 38 | |||
| 39 | |||
| 40 | /** | ||
| 41 | * Creates a new Statistics binder which maintains the same Counters as the | ||
| 42 | * existing one. Changes to those counters are visible in both Statistics | ||
| 43 | * objects. The child can then independently add more counters. CounterInfo | ||
| 44 | * objects are reference counted and deleted when all the statistics objects | ||
| 45 | * dealing with it are destroyed. | ||
| 46 | */ | ||
| 47 | 620 | Statistics *Statistics::Fork() { | |
| 48 |
1/2✓ Branch 1 taken 620 times.
✗ Branch 2 not taken.
|
620 | Statistics *child = new Statistics(); |
| 49 | |||
| 50 | 620 | const MutexLockGuard lock_guard(lock_); | |
| 51 | 1240 | for (map<string, CounterInfo *>::iterator i = counters_.begin(), | |
| 52 | 620 | iEnd = counters_.end(); | |
| 53 |
2/2✓ Branch 1 taken 23006 times.
✓ Branch 2 taken 620 times.
|
23626 | i != iEnd; |
| 54 | 23006 | ++i) { | |
| 55 | 23006 | atomic_inc32(&i->second->refcnt); | |
| 56 | } | ||
| 57 |
1/2✓ Branch 1 taken 620 times.
✗ Branch 2 not taken.
|
620 | child->counters_ = counters_; |
| 58 | |||
| 59 | 620 | return child; | |
| 60 | 620 | } | |
| 61 | |||
| 62 | |||
| 63 | 681 | Counter *Statistics::Lookup(const std::string &name) const { | |
| 64 | 681 | const MutexLockGuard lock_guard(lock_); | |
| 65 |
1/2✓ Branch 1 taken 681 times.
✗ Branch 2 not taken.
|
681 | const map<string, CounterInfo *>::const_iterator i = counters_.find(name); |
| 66 |
2/2✓ Branch 2 taken 595 times.
✓ Branch 3 taken 86 times.
|
681 | if (i != counters_.end()) |
| 67 | 595 | return &i->second->counter; | |
| 68 | 86 | return NULL; | |
| 69 | 681 | } | |
| 70 | |||
| 71 | |||
| 72 | 46 | string Statistics::LookupDesc(const std::string &name) { | |
| 73 | 46 | const MutexLockGuard lock_guard(lock_); | |
| 74 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | const map<string, CounterInfo *>::const_iterator i = counters_.find(name); |
| 75 |
1/2✓ Branch 3 taken 46 times.
✗ Branch 4 not taken.
|
46 | if (i != counters_.end()) |
| 76 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | return i->second->desc; |
| 77 | ✗ | return ""; | |
| 78 | 46 | } | |
| 79 | |||
| 80 | 46 | string Statistics::PrintList(const PrintOptions print_options) { | |
| 81 | 46 | string result; | |
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (print_options == kPrintHeader) |
| 83 | ✗ | result += "Name|Value|Description\n"; | |
| 84 | |||
| 85 | 46 | const MutexLockGuard lock_guard(lock_); | |
| 86 | 46 | for (map<string, CounterInfo *>::const_iterator i = counters_.begin(), | |
| 87 | 46 | iEnd = counters_.end(); | |
| 88 |
2/2✓ Branch 1 taken 46 times.
✓ Branch 2 taken 46 times.
|
92 | i != iEnd; |
| 89 | 46 | ++i) { | |
| 90 |
4/8✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 46 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 46 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 46 times.
✗ Branch 12 not taken.
|
92 | result += i->first + "|" + i->second->counter.ToString() + "|" |
| 91 |
3/6✓ Branch 3 taken 46 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 46 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 46 times.
✗ Branch 10 not taken.
|
92 | + i->second->desc + "\n"; |
| 92 | } | ||
| 93 | 92 | return result; | |
| 94 | 46 | } | |
| 95 | |||
| 96 | /** | ||
| 97 | * Converts statistics counters into JSON string in following format | ||
| 98 | * { | ||
| 99 | * "name_major1": { | ||
| 100 | * "counter1": val1, | ||
| 101 | * "counter2": val2 | ||
| 102 | * }, | ||
| 103 | * "name_major2": { | ||
| 104 | * "counter3": val3 | ||
| 105 | * } | ||
| 106 | * } | ||
| 107 | */ | ||
| 108 | 98 | string Statistics::PrintJSON() { | |
| 109 | 98 | const MutexLockGuard lock_guard(lock_); | |
| 110 | |||
| 111 | 98 | JsonStringGenerator json_statistics; | |
| 112 | |||
| 113 | // Make use of std::map key ordering and add counters namespace by namespace | ||
| 114 | 98 | JsonStringGenerator json_statistics_namespace; | |
| 115 |
1/2✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
|
98 | std::string last_namespace = ""; |
| 116 | 98 | for (map<string, CounterInfo *>::const_iterator i = counters_.begin(), | |
| 117 | 98 | iEnd = counters_.end(); | |
| 118 |
2/2✓ Branch 1 taken 138 times.
✓ Branch 2 taken 98 times.
|
236 | i != iEnd; |
| 119 | 138 | ++i) { | |
| 120 |
1/2✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
|
138 | std::vector<std::string> tokens = SplitString(i->first, '.'); |
| 121 | |||
| 122 |
2/2✓ Branch 2 taken 92 times.
✓ Branch 3 taken 46 times.
|
138 | if (tokens[0] != last_namespace) { |
| 123 |
3/4✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
✓ Branch 4 taken 46 times.
|
92 | if (last_namespace != "") { |
| 124 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | json_statistics.AddJsonObject( |
| 125 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
92 | last_namespace, json_statistics_namespace.GenerateString()); |
| 126 | } | ||
| 127 | 92 | json_statistics_namespace.Clear(); | |
| 128 | } | ||
| 129 | |||
| 130 |
1/2✓ Branch 4 taken 138 times.
✗ Branch 5 not taken.
|
138 | json_statistics_namespace.Add(tokens[1], i->second->counter.Get()); |
| 131 | |||
| 132 |
1/2✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
|
138 | last_namespace = tokens[0]; |
| 133 | 138 | } | |
| 134 |
3/4✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
✓ Branch 4 taken 52 times.
|
98 | if (last_namespace != "") { |
| 135 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | json_statistics.AddJsonObject(last_namespace, |
| 136 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
92 | json_statistics_namespace.GenerateString()); |
| 137 | } | ||
| 138 | |||
| 139 |
1/2✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
|
196 | return json_statistics.GenerateString(); |
| 140 | 98 | } | |
| 141 | |||
| 142 | /** | ||
| 143 | * Snapshot current state of the counters. | ||
| 144 | * Elements will either be updated or inserted into the map. | ||
| 145 | * | ||
| 146 | * Note: This function does NOT clear previous elements part of the map. | ||
| 147 | * | ||
| 148 | * Returns map of the updated counters and the timestamp of the snapshot. | ||
| 149 | */ | ||
| 150 | 376 | void Statistics::SnapshotCounters(std::map<std::string, int64_t> *counters, | |
| 151 | uint64_t *timestamp_ns) { | ||
| 152 | 376 | const MutexLockGuard lock_guard(lock_); | |
| 153 | 376 | *timestamp_ns = platform_realtime_ns(); | |
| 154 | 376 | for (map<string, CounterInfo *>::const_iterator i = counters_.begin(), | |
| 155 | 376 | iEnd = counters_.end(); | |
| 156 |
2/2✓ Branch 1 taken 1128 times.
✓ Branch 2 taken 376 times.
|
1504 | i != iEnd; |
| 157 | 1128 | ++i) { | |
| 158 | // modify or insert | ||
| 159 |
1/2✓ Branch 4 taken 1128 times.
✗ Branch 5 not taken.
|
1128 | (*counters)[i->first] = (*i->second).counter.Get(); |
| 160 | } | ||
| 161 | 376 | } | |
| 162 | |||
| 163 | 120662 | Counter *Statistics::Register(const string &name, const string &desc) { | |
| 164 | 120662 | const MutexLockGuard lock_guard(lock_); | |
| 165 |
2/5✓ Branch 2 taken 120662 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 120662 times.
|
120662 | assert(counters_.find(name) == counters_.end()); |
| 166 |
2/4✓ Branch 1 taken 120662 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 120662 times.
✗ Branch 5 not taken.
|
120662 | CounterInfo *counter_info = new CounterInfo(desc); |
| 167 |
1/2✓ Branch 1 taken 120662 times.
✗ Branch 2 not taken.
|
120662 | counters_[name] = counter_info; |
| 168 | 120662 | return &counter_info->counter; | |
| 169 | 120662 | } | |
| 170 | |||
| 171 | |||
| 172 | 4924 | Statistics::Statistics() { | |
| 173 | 4924 | lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t))); | |
| 174 | 4924 | const int retval = pthread_mutex_init(lock_, NULL); | |
| 175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4924 times.
|
4924 | assert(retval == 0); |
| 176 | 4924 | } | |
| 177 | |||
| 178 | |||
| 179 | 4914 | Statistics::~Statistics() { | |
| 180 | 9828 | for (map<string, CounterInfo *>::iterator i = counters_.begin(), | |
| 181 | 4914 | iEnd = counters_.end(); | |
| 182 |
2/2✓ Branch 1 taken 143467 times.
✓ Branch 2 taken 4914 times.
|
148381 | i != iEnd; |
| 183 | 143467 | ++i) { | |
| 184 | 143467 | const int32_t old_value = atomic_xadd32(&i->second->refcnt, -1); | |
| 185 |
2/2✓ Branch 0 taken 120461 times.
✓ Branch 1 taken 23006 times.
|
143467 | if (old_value == 1) |
| 186 |
1/2✓ Branch 1 taken 120461 times.
✗ Branch 2 not taken.
|
120461 | delete i->second; |
| 187 | } | ||
| 188 | 4914 | pthread_mutex_destroy(lock_); | |
| 189 | 4914 | free(lock_); | |
| 190 | 4914 | } | |
| 191 | |||
| 192 | |||
| 193 | //------------------------------------------------------------------------------ | ||
| 194 | |||
| 195 | |||
| 196 | /** | ||
| 197 | * If necessary, capacity_s is extended to be a multiple of resolution_s | ||
| 198 | */ | ||
| 199 | 6173 | Recorder::Recorder(uint32_t resolution_s, uint32_t capacity_s) | |
| 200 | 6173 | : last_timestamp_(0), capacity_s_(capacity_s), resolution_s_(resolution_s) { | |
| 201 |
2/4✓ Branch 0 taken 6173 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6173 times.
✗ Branch 3 not taken.
|
6173 | assert((resolution_s > 0) && (capacity_s > resolution_s)); |
| 202 | 6173 | const bool has_remainder = (capacity_s_ % resolution_s_) != 0; | |
| 203 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 6079 times.
|
6173 | if (has_remainder) { |
| 204 | 94 | capacity_s_ += resolution_s_ - (capacity_s_ % resolution_s_); | |
| 205 | } | ||
| 206 | 6173 | no_bins_ = capacity_s_ / resolution_s_; | |
| 207 |
1/2✓ Branch 1 taken 6173 times.
✗ Branch 2 not taken.
|
6173 | bins_.reserve(no_bins_); |
| 208 |
2/2✓ Branch 0 taken 470427 times.
✓ Branch 1 taken 6173 times.
|
476600 | for (unsigned i = 0; i < no_bins_; ++i) |
| 209 |
1/2✓ Branch 1 taken 470427 times.
✗ Branch 2 not taken.
|
470427 | bins_.push_back(0); |
| 210 | 6173 | } | |
| 211 | |||
| 212 | |||
| 213 | 94 | void Recorder::Tick() { TickAt(platform_monotonic_time()); } | |
| 214 | |||
| 215 | |||
| 216 | 5039 | void Recorder::TickAt(uint64_t timestamp) { | |
| 217 | 5039 | const uint64_t bin_abs = timestamp / resolution_s_; | |
| 218 | 5039 | const uint64_t last_bin_abs = last_timestamp_ / resolution_s_; | |
| 219 | |||
| 220 | // timestamp in the past: don't update last_timestamp_ | ||
| 221 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 4945 times.
|
5039 | if (bin_abs < last_bin_abs) { |
| 222 | // Do we still remember this event? | ||
| 223 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 47 times.
|
94 | if ((last_bin_abs - bin_abs) < no_bins_) |
| 224 | 47 | bins_[bin_abs % no_bins_]++; | |
| 225 | 94 | return; | |
| 226 | } | ||
| 227 | |||
| 228 |
2/2✓ Branch 0 taken 1107 times.
✓ Branch 1 taken 3838 times.
|
4945 | if (last_bin_abs == bin_abs) { |
| 229 | 1107 | bins_[bin_abs % no_bins_]++; | |
| 230 | } else { | ||
| 231 | // When clearing bins between last_timestamp_ and now, avoid cycling the | ||
| 232 | // ring buffer multiple times. | ||
| 233 | 7676 | const unsigned max_bins_clear = std::min(bin_abs, | |
| 234 | 3838 | last_bin_abs + no_bins_ + 1); | |
| 235 |
2/2✓ Branch 0 taken 31815 times.
✓ Branch 1 taken 3838 times.
|
35653 | for (uint64_t i = last_bin_abs + 1; i < max_bins_clear; ++i) |
| 236 | 31815 | bins_[i % no_bins_] = 0; | |
| 237 | 3838 | bins_[bin_abs % no_bins_] = 1; | |
| 238 | } | ||
| 239 | |||
| 240 | 4945 | last_timestamp_ = timestamp; | |
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | 859 | uint64_t Recorder::GetNoTicks(uint32_t retrospect_s) const { | |
| 245 | 859 | const uint64_t now = platform_monotonic_time(); | |
| 246 |
2/2✓ Branch 0 taken 470 times.
✓ Branch 1 taken 389 times.
|
859 | if (retrospect_s > now) |
| 247 | 470 | retrospect_s = now; | |
| 248 | |||
| 249 | 859 | const uint64_t last_bin_abs = last_timestamp_ / resolution_s_; | |
| 250 | 859 | const uint64_t past_bin_abs = (now - retrospect_s) / resolution_s_; | |
| 251 | 1718 | const int64_t min_bin_abs = std::max( | |
| 252 | past_bin_abs, | ||
| 253 |
2/2✓ Branch 0 taken 579 times.
✓ Branch 1 taken 280 times.
|
859 | (last_bin_abs < no_bins_) ? 0 : (last_bin_abs - (no_bins_ - 1))); |
| 254 | 859 | uint64_t result = 0; | |
| 255 |
2/2✓ Branch 0 taken 5145 times.
✓ Branch 1 taken 859 times.
|
6004 | for (int64_t i = last_bin_abs; i >= min_bin_abs; --i) { |
| 256 | 5145 | result += bins_[i % no_bins_]; | |
| 257 | } | ||
| 258 | |||
| 259 | 859 | return result; | |
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | //------------------------------------------------------------------------------ | ||
| 264 | |||
| 265 | |||
| 266 | 5750 | void MultiRecorder::AddRecorder(uint32_t resolution_s, uint32_t capacity_s) { | |
| 267 |
1/2✓ Branch 2 taken 5750 times.
✗ Branch 3 not taken.
|
5750 | recorders_.push_back(Recorder(resolution_s, capacity_s)); |
| 268 | 5750 | } | |
| 269 | |||
| 270 | |||
| 271 | 248 | uint64_t MultiRecorder::GetNoTicks(uint32_t retrospect_s) const { | |
| 272 | 248 | const unsigned N = recorders_.size(); | |
| 273 |
2/2✓ Branch 0 taken 295 times.
✓ Branch 1 taken 47 times.
|
342 | for (unsigned i = 0; i < N; ++i) { |
| 274 |
6/6✓ Branch 2 taken 188 times.
✓ Branch 3 taken 107 times.
✓ Branch 4 taken 94 times.
✓ Branch 5 taken 94 times.
✓ Branch 6 taken 201 times.
✓ Branch 7 taken 94 times.
|
295 | if ((recorders_[i].capacity_s() >= retrospect_s) || (i == (N - 1))) { |
| 275 | 201 | return recorders_[i].GetNoTicks(retrospect_s); | |
| 276 | } | ||
| 277 | } | ||
| 278 | 47 | return 0; | |
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | 214 | void MultiRecorder::Tick() { | |
| 283 | 214 | const uint64_t now = platform_monotonic_time(); | |
| 284 |
2/2✓ Branch 1 taken 574 times.
✓ Branch 2 taken 214 times.
|
788 | for (unsigned i = 0; i < recorders_.size(); ++i) |
| 285 | 574 | recorders_[i].TickAt(now); | |
| 286 | 214 | } | |
| 287 | |||
| 288 | |||
| 289 | 940 | void MultiRecorder::TickAt(uint64_t timestamp) { | |
| 290 |
2/2✓ Branch 1 taken 1880 times.
✓ Branch 2 taken 940 times.
|
2820 | for (unsigned i = 0; i < recorders_.size(); ++i) |
| 291 | 1880 | recorders_[i].TickAt(timestamp); | |
| 292 | 940 | } | |
| 293 | |||
| 294 | } // namespace perf | ||
| 295 | |||
| 296 | |||
| 297 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 298 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 299 | #endif | ||
| 300 |