GCC Code Coverage Report


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