/src/trafficserver/include/tsutil/Metrics.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | A brief file description |
4 | | |
5 | | @par License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <array> |
27 | | #include <unordered_map> |
28 | | #include <tuple> |
29 | | #include <memory> |
30 | | #include <mutex> |
31 | | #include <atomic> |
32 | | #include <cstdint> |
33 | | #include <limits> |
34 | | #include <string> |
35 | | #include <string_view> |
36 | | #include <utility> |
37 | | #include <variant> |
38 | | #include <optional> |
39 | | |
40 | | #include "tsutil/Assert.h" |
41 | | |
42 | | namespace ts |
43 | | { |
44 | | class Metrics |
45 | | { |
46 | | private: |
47 | | using self_type = Metrics; |
48 | | |
49 | | public: |
50 | | class AtomicType |
51 | | { |
52 | | friend class Metrics; |
53 | | |
54 | | public: |
55 | 2.04k | AtomicType() = default; |
56 | | |
57 | | int64_t |
58 | | load() const |
59 | 0 | { |
60 | 0 | return _value.load(); |
61 | 0 | } |
62 | | |
63 | | void |
64 | | increment(int64_t val) |
65 | 0 | { |
66 | 0 | _value.fetch_add(val, MEMORY_ORDER); |
67 | 0 | } |
68 | | |
69 | | // Use with care ... |
70 | | void |
71 | | store(int64_t val) |
72 | 0 | { |
73 | 0 | _value.store(val); |
74 | 0 | } |
75 | | |
76 | | void |
77 | | decrement(int64_t val) |
78 | 0 | { |
79 | 0 | _value.fetch_sub(val, MEMORY_ORDER); |
80 | 0 | } |
81 | | |
82 | | protected: |
83 | | std::atomic<int64_t> _value{0}; |
84 | | }; |
85 | | |
86 | | enum class MetricType : int { COUNTER = 0, GAUGE }; |
87 | | |
88 | | using IdType = int32_t; // Could be a tuple, but one way or another, they have to be combined to an int32_t. |
89 | | |
90 | | static constexpr uint16_t MAX_BLOBS = 8192; |
91 | | static constexpr uint16_t MAX_SIZE = 1024; // For a total of 8M metrics |
92 | | static constexpr IdType NOT_FOUND = std::numeric_limits<IdType>::min(); // <16-bit,16-bit> = <blob-index,offset> |
93 | | static const auto MEMORY_ORDER = std::memory_order_relaxed; |
94 | | static constexpr int METRIC_TYPE_BITS = 29; |
95 | | static constexpr int METRIC_TYPE_MASK = 0x1FFF; |
96 | | |
97 | | /// The reserved slot 0 of every store, returned when an id cannot be produced. Present under this |
98 | | /// name in both stores, so a consumer querying both has to expect it twice. |
99 | | static constexpr std::string_view BAD_ID_NAME{"proxy.process.api.metrics.bad_id"}; |
100 | | |
101 | | private: |
102 | | using NameAndId = std::tuple<std::string, IdType>; |
103 | | using LookupTable = std::unordered_map<std::string_view, IdType>; |
104 | | using NameStorage = std::array<NameAndId, MAX_SIZE>; |
105 | | using AtomicStorage = std::array<AtomicType, MAX_SIZE>; |
106 | | /// Per slot flag bits, see @c UNLISTED. A parallel array rather than a member of @c NameAndId |
107 | | /// because an atomic member would make that tuple neither copyable nor movable, and the slot is |
108 | | /// written there with a tuple assignment. |
109 | | using FlagStorage = std::array<std::atomic<uint8_t>, MAX_SIZE>; |
110 | | using NamesAndAtomics = std::tuple<NameStorage, AtomicStorage, FlagStorage>; |
111 | | using BlobStorage = std::array<std::unique_ptr<NamesAndAtomics>, MAX_BLOBS>; |
112 | | |
113 | | /// The slot exists and is still resolvable by name or id, but is skipped by iteration. |
114 | | static constexpr uint8_t UNLISTED = 0x01; |
115 | | |
116 | | public: |
117 | | Metrics(const self_type &) = delete; |
118 | | self_type &operator=(const self_type &) = delete; |
119 | | Metrics &operator=(Metrics &&) = delete; |
120 | | Metrics(Metrics &&) = delete; |
121 | | |
122 | 2 | virtual ~Metrics() {} |
123 | | |
124 | | // The singleton instance, owned by the Metrics class |
125 | | static Metrics &instance(); |
126 | | |
127 | | /** The hidden metrics instance. |
128 | | * |
129 | | * A completely separate storage from @c instance(). Metrics here are stored but never |
130 | | * published - they are structurally unreachable from the published store, so no consumer |
131 | | * (traffic_ctl, JSONRPC, stats_over_http) can expose them by omission. |
132 | | * |
133 | | * Intended for high cardinality intermediate values which feed @c Derived aggregates. |
134 | | * |
135 | | * @note An @c IdType from this instance is NOT interchangeable with one from @c instance(). |
136 | | * Ids are meaningful only relative to their store: passing a hidden id to the published |
137 | | * store yields a silently wrong metric, with no error and no crash, since @c valid() will |
138 | | * accept it. Prefer @c Gauge::createHiddenPtr / @c Counter::createHiddenPtr, which return |
139 | | * correctly typed pointers and never hand out an id. |
140 | | */ |
141 | | static Metrics &hidden_instance(); |
142 | | |
143 | | // Yes, we don't return objects here, but rather ID's and atomic's directly. Treat |
144 | | // the std::atomic<int64_t> as the underlying class for a single metric, and be happy. |
145 | | IdType |
146 | | lookup(const std::string_view name) const |
147 | 0 | { |
148 | 0 | return _storage->lookup(name); |
149 | 0 | } |
150 | | AtomicType * |
151 | | lookup(const std::string_view name, IdType *out_id) const |
152 | 2 | { |
153 | 2 | return _storage->lookup(name, out_id); |
154 | 2 | } |
155 | | AtomicType * |
156 | | lookup(IdType id, std::string_view *out_name = nullptr, Metrics::MetricType *type = nullptr) const |
157 | 192 | { |
158 | 192 | return _storage->lookup(id, out_name, type); |
159 | 192 | } |
160 | | |
161 | | /** Take @a id out of the store's listing. |
162 | | * |
163 | | * An unlisted metric keeps its slot, its name and its atomic. It is skipped by iteration, so it |
164 | | * vanishes from everything that enumerates the store, but it still resolves through @c lookup and |
165 | | * its value may still be read and written -- an unlisted number that still rings. Creating the |
166 | | * same name again relists it and returns the same id. |
167 | | * |
168 | | * @return @c false if @a id does not name an allocated slot. |
169 | | */ |
170 | | bool |
171 | | unlist(IdType id) |
172 | 0 | { |
173 | 0 | return _storage->set_listed(id, false); |
174 | 0 | } |
175 | | |
176 | | /// Put @a id back in the listing. @see unlist |
177 | | bool |
178 | | relist(IdType id) |
179 | 0 | { |
180 | 0 | return _storage->set_listed(id, true); |
181 | 0 | } |
182 | | |
183 | | /** Whether @a id is enumerated. |
184 | | * |
185 | | * @return @c false for an unlisted metric, and also for an id that names no allocated slot -- |
186 | | * neither appears in iteration. |
187 | | */ |
188 | | bool |
189 | | listed(IdType id) const |
190 | 0 | { |
191 | 0 | return _storage->listed(id); |
192 | 0 | } |
193 | | |
194 | | /// Convenience for callers that publish by name and do not retain the id. @see unlist |
195 | | bool |
196 | | unlist(std::string_view name) |
197 | 0 | { |
198 | 0 | auto id = lookup(name); |
199 | 0 |
|
200 | 0 | return id != NOT_FOUND && unlist(id); |
201 | 0 | } |
202 | | |
203 | | /// Convenience for callers that publish by name and do not retain the id. @see relist |
204 | | bool |
205 | | relist(std::string_view name) |
206 | 0 | { |
207 | 0 | auto id = lookup(name); |
208 | 0 |
|
209 | 0 | return id != NOT_FOUND && relist(id); |
210 | 0 | } |
211 | | AtomicType & |
212 | | operator[](IdType id) |
213 | 0 | { |
214 | 0 | return *lookup(id); |
215 | 0 | } |
216 | | |
217 | | IdType |
218 | | operator[](const std::string_view name) const |
219 | 0 | { |
220 | 0 | return lookup(name); |
221 | 0 | } |
222 | | |
223 | | int64_t |
224 | | increment(IdType id, uint64_t val = 1) |
225 | 0 | { |
226 | 0 | auto metric = lookup(id); |
227 | 0 |
|
228 | 0 | return (metric ? metric->_value.fetch_add(val, MEMORY_ORDER) : NOT_FOUND); |
229 | 0 | } |
230 | | |
231 | | int64_t |
232 | | decrement(IdType id, uint64_t val = 1) |
233 | 0 | { |
234 | 0 | auto metric = lookup(id); |
235 | 0 |
|
236 | 0 | return (metric ? metric->_value.fetch_sub(val, MEMORY_ORDER) : NOT_FOUND); |
237 | 0 | } |
238 | | |
239 | | std::string_view |
240 | | name(IdType id) const |
241 | 0 | { |
242 | 0 | return _storage->name(id); |
243 | 0 | } |
244 | | |
245 | | MetricType |
246 | | type(IdType id) const |
247 | 0 | { |
248 | 0 | return _storage->type(id); |
249 | 0 | } |
250 | | |
251 | | bool |
252 | | valid(IdType id) const |
253 | 0 | { |
254 | 0 | return _storage->valid(id); |
255 | 0 | } |
256 | | |
257 | | /** Visit every listed metric. |
258 | | * |
259 | | * @a func is called as <tt>func(std::string_view name, MetricType type, int64_t value)</tt> for |
260 | | * each listed metric, in creation order. Unlisted metrics are skipped, @see unlist. |
261 | | * |
262 | | * The set walked is fixed when the call begins: a metric created while it runs is not visited. |
263 | | * Enumeration is deliberately the whole store and nothing less. There is no cursor to hold, so |
264 | | * nothing can outlive the walk or name a slot the walk would not visit, and @a func may not |
265 | | * create a metric, which would be an attempt to grow the store from inside a pass over it. |
266 | | */ |
267 | | template <typename F> |
268 | | void |
269 | | for_each(F &&func) const |
270 | 0 | { |
271 | 0 | _storage->for_each(std::forward<F>(func)); |
272 | 0 | } Unexecuted instantiation: RecCore.cc:void ts::Metrics::for_each<RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_0>(RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_0&&) const Unexecuted instantiation: RecCore.cc:void ts::Metrics::for_each<RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_2>(RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_2&&) const Unexecuted instantiation: RecCore.cc:void ts::Metrics::for_each<RecDumpRecords(RecT, void (*)(RecT, void*, int, char const*, int, RecData*), void*)::$_0>(RecDumpRecords(RecT, void (*)(RecT, void*, int, char const*, int, RecData*), void*)::$_0&&) const |
273 | | |
274 | | private: |
275 | | // These are private, to assure that we don't use them by accident creating naked metrics |
276 | | IdType |
277 | | _create(const std::string_view name, MetricType type) |
278 | 192 | { |
279 | 192 | return _storage->create(name, type); |
280 | 192 | } |
281 | | |
282 | | // These are little helpers around managing the ID's |
283 | | static constexpr std::tuple<uint16_t, uint16_t> |
284 | | _splitID(IdType value) |
285 | 578 | { |
286 | 578 | return std::make_tuple(static_cast<uint16_t>(value >> 16) & METRIC_TYPE_MASK, static_cast<uint16_t>(value & 0xFFFF)); |
287 | 578 | } |
288 | | |
289 | | static constexpr MetricType |
290 | | _extractType(IdType value) |
291 | 0 | { |
292 | 0 | return MetricType{static_cast<int>((static_cast<uint32_t>(value) >> METRIC_TYPE_BITS) & 0x1)}; |
293 | 0 | } |
294 | | |
295 | | static constexpr IdType |
296 | | _makeId(uint16_t blob, uint16_t offset, const MetricType type) |
297 | 194 | { |
298 | 194 | int t = static_cast<int>(type); |
299 | 194 | return (t << METRIC_TYPE_BITS | blob << 16 | offset); |
300 | 194 | } |
301 | | |
302 | | /// As @c _makeId, without the type bits. |
303 | | static constexpr uint32_t |
304 | | _pack(uint16_t blob, uint16_t offset) |
305 | 386 | { |
306 | 386 | return static_cast<uint32_t>(blob) << 16 | offset; |
307 | 386 | } |
308 | | |
309 | | // A packed position must not reach the type bits, and an offset must fit its field. |
310 | | static_assert(MAX_SIZE <= 0x10000); |
311 | | static_assert(MAX_BLOBS <= (1 << (METRIC_TYPE_BITS - 16))); |
312 | | |
313 | | class Storage |
314 | | { |
315 | | /* The next free slot, packed as @c _makeId packs one. A single value because a reader that |
316 | | * caught a new offset against an old blob index, or the reverse, would reject ids that exist |
317 | | * or accept ids that do not. Release stored last, after the blob pointer or the slot's name it |
318 | | * publishes. Only ever increases, so an id is allocated exactly when it packs below it. |
319 | | * |
320 | | * A slot's name is written once, before the store that publishes it, and never changes, which |
321 | | * is what lets @c name and @c lookup hand out a view of it without the mutex. |
322 | | */ |
323 | | BlobStorage _blobs; |
324 | | std::atomic<uint32_t> _next_free{0}; |
325 | | LookupTable _lookups; |
326 | | mutable std::mutex _mutex; |
327 | | |
328 | | public: |
329 | | Storage(const Storage &) = delete; |
330 | | Storage &operator=(const Storage &) = delete; |
331 | | |
332 | | Storage() |
333 | 2 | { |
334 | 2 | _blobs[0] = std::make_unique<NamesAndAtomics>(); |
335 | 2 | release_assert(_blobs[0]); |
336 | | // Reserve slot 0 for errors, this should always be 0 |
337 | 2 | release_assert(0 == create(BAD_ID_NAME, MetricType::COUNTER)); |
338 | 2 | } |
339 | | |
340 | 2 | ~Storage() {} |
341 | | |
342 | | IdType create(const std::string_view name, const MetricType type = MetricType::COUNTER); |
343 | | void addBlob(); |
344 | | IdType lookup(const std::string_view name) const; |
345 | | AtomicType *lookup(const std::string_view name, IdType *out_id, MetricType *out_type = nullptr) const; |
346 | | AtomicType *lookup(Metrics::IdType id, std::string_view *out_name = nullptr, MetricType *out_type = nullptr) const; |
347 | | std::string_view name(IdType id) const; |
348 | | MetricType type(IdType id) const; |
349 | | bool set_listed(IdType id, bool listed); |
350 | | bool listed(IdType id) const; |
351 | | |
352 | | /** Visit every listed slot, in creation order. |
353 | | * |
354 | | * @see Metrics::for_each, which is how callers reach this. |
355 | | * |
356 | | * The bound is read once, up front. Acquiring it acquires every slot below it, which is what |
357 | | * lets the walk read names and values without the mutex: a slot's name is written before the |
358 | | * release store that publishes it, and never changes. |
359 | | */ |
360 | | template <typename F> |
361 | | void |
362 | | for_each(F &&func) const |
363 | 0 | { |
364 | 0 | auto const [last_blob, last_off] = _splitID(next_free_id()); |
365 | |
|
366 | 0 | for (uint16_t blob = 0; blob <= last_blob; ++blob) { |
367 | 0 | NamesAndAtomics const *entries = _blobs[blob].get(); |
368 | | |
369 | | // The bound covers every blob below it, so this is belt and braces. |
370 | 0 | if (entries == nullptr) { |
371 | 0 | break; |
372 | 0 | } |
373 | | |
374 | 0 | uint16_t const limit = blob == last_blob ? last_off : MAX_SIZE; |
375 | |
|
376 | 0 | for (uint16_t off = 0; off < limit; ++off) { |
377 | 0 | if ((std::get<2>(*entries)[off].load(MEMORY_ORDER) & UNLISTED) != 0) { |
378 | 0 | continue; |
379 | 0 | } |
380 | | |
381 | 0 | auto const &slot = std::get<0>(*entries)[off]; |
382 | | |
383 | | // The type comes from the slot's own id, not from the position, so it is the type the |
384 | | // metric was created with. |
385 | 0 | func(std::string_view{std::get<0>(slot)}, _extractType(std::get<1>(slot)), std::get<1>(*entries)[off].load()); |
386 | 0 | } |
387 | 0 | } |
388 | 0 | } Unexecuted instantiation: RecCore.cc:void ts::Metrics::Storage::for_each<RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_0>(RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_0&&) const Unexecuted instantiation: RecCore.cc:void ts::Metrics::Storage::for_each<RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_2>(RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_2&&) const Unexecuted instantiation: RecCore.cc:void ts::Metrics::Storage::for_each<RecDumpRecords(RecT, void (*)(RecT, void*, int, char const*, int, RecData*), void*)::$_0>(RecDumpRecords(RecT, void (*)(RecT, void*, int, char const*, int, RecData*), void*)::$_0&&) const |
389 | | |
390 | | /// The id the next slot will get, which is also iteration's exclusive bound. |
391 | | IdType |
392 | | next_free_id() const |
393 | 0 | { |
394 | 0 | return static_cast<IdType>(_next_free.load(std::memory_order_acquire)); |
395 | 0 | } |
396 | | |
397 | | bool |
398 | | valid(IdType id) const |
399 | 0 | { |
400 | 0 | return _is_allocated(id); |
401 | 0 | } |
402 | | |
403 | | private: |
404 | | /** Whether @a id names an allocated slot. |
405 | | * |
406 | | * The gate for every id based accessor, since ids from the @c TSStat* API are untrusted. An id |
407 | | * qualifies when it is non-negative, its offset is one @c _makeId could produce, and its slot |
408 | | * has been handed out. |
409 | | */ |
410 | | bool |
411 | | _is_allocated(IdType id) const |
412 | 192 | { |
413 | 192 | if (id < 0) { |
414 | 0 | return false; |
415 | 0 | } |
416 | | |
417 | 192 | auto [blob_ix, offset] = _splitID(id); |
418 | | |
419 | | // Not implied below: an earlier blob can name an offset past MAX_SIZE and still pack under. |
420 | 192 | if (offset >= MAX_SIZE) { |
421 | 0 | return false; |
422 | 0 | } |
423 | | |
424 | | // Acquiring the bound acquires the blob install, so _blobs needs no check of its own. |
425 | 192 | return _pack(blob_ix, offset) < _next_free.load(std::memory_order_acquire); |
426 | 192 | } |
427 | | }; |
428 | | |
429 | 2 | Metrics(std::shared_ptr<Storage> &str) : _storage(str) {} |
430 | | |
431 | | std::shared_ptr<Storage> _storage; |
432 | | |
433 | | public: |
434 | | // These are sort of factory classes, using the Metrics singleton for all storage etc. |
435 | | class Gauge |
436 | | { |
437 | | public: |
438 | | using self_type = Gauge; |
439 | | |
440 | | class AtomicType : public Metrics::AtomicType |
441 | | { |
442 | | }; |
443 | | |
444 | | static IdType |
445 | | lookup(const std::string_view name) |
446 | 0 | { |
447 | 0 | auto &instance = Metrics::instance(); |
448 | 0 |
|
449 | 0 | return instance.lookup(name); |
450 | 0 | } |
451 | | |
452 | | static AtomicType * |
453 | | lookup(const IdType id, std::string_view *out_name = nullptr) |
454 | 0 | { |
455 | 0 | auto &instance = Metrics::instance(); |
456 | 0 |
|
457 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(id, out_name)); |
458 | 0 | } |
459 | | |
460 | | static AtomicType * |
461 | | lookup(const std::string_view name, IdType *id) |
462 | 0 | { |
463 | 0 | auto &instance = Metrics::instance(); |
464 | 0 |
|
465 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(name, id)); |
466 | 0 | } |
467 | | |
468 | | static Metrics::IdType |
469 | | create(const std::string_view name) |
470 | 0 | { |
471 | 0 | auto &instance = Metrics::instance(); |
472 | 0 |
|
473 | 0 | return instance._create(name, MetricType::GAUGE); |
474 | 0 | } |
475 | | |
476 | | static AtomicType * |
477 | | createPtr(const std::string_view name) |
478 | 192 | { |
479 | 192 | auto &instance = Metrics::instance(); |
480 | | |
481 | 192 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(name, MetricType::GAUGE))); |
482 | 192 | } |
483 | | |
484 | | static AtomicType * |
485 | | createPtr(const std::string_view prefix, const std::string_view name) |
486 | 0 | { |
487 | 0 | auto &instance = Metrics::instance(); |
488 | 0 | std::string tmpname = std::string(prefix) + std::string(name); |
489 | 0 |
|
490 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(tmpname, MetricType::GAUGE))); |
491 | 0 | } |
492 | | |
493 | | /** Create a metric which is stored but never published. |
494 | | * |
495 | | * @see Metrics::hidden_instance() |
496 | | */ |
497 | | static AtomicType * |
498 | | createHiddenPtr(const std::string_view name) |
499 | 0 | { |
500 | 0 | auto &instance = Metrics::hidden_instance(); |
501 | 0 |
|
502 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(name, MetricType::GAUGE))); |
503 | 0 | } |
504 | | |
505 | | static AtomicType * |
506 | | createHiddenPtr(const std::string_view prefix, const std::string_view name) |
507 | 0 | { |
508 | 0 | auto &instance = Metrics::hidden_instance(); |
509 | 0 | std::string tmpname = std::string(prefix) + std::string(name); |
510 | 0 |
|
511 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(tmpname, MetricType::GAUGE))); |
512 | 0 | } |
513 | | |
514 | | static void |
515 | | increment(AtomicType *metric, uint64_t val = 1) |
516 | 0 | { |
517 | 0 | debug_assert(metric); |
518 | 0 | metric->_value.fetch_add(val, MEMORY_ORDER); |
519 | 0 | } Unexecuted instantiation: ts::Metrics::Gauge::increment(ts::Metrics::Gauge::AtomicType*, unsigned long) Unexecuted instantiation: ts::Metrics::Gauge::increment(ts::Metrics::Gauge::AtomicType*, unsigned long) |
520 | | |
521 | | static void |
522 | | decrement(AtomicType *metric, uint64_t val = 1) |
523 | 0 | { |
524 | 0 | debug_assert(metric); |
525 | 0 | metric->_value.fetch_sub(val, MEMORY_ORDER); |
526 | 0 | } Unexecuted instantiation: ts::Metrics::Gauge::decrement(ts::Metrics::Gauge::AtomicType*, unsigned long) Unexecuted instantiation: ts::Metrics::Gauge::decrement(ts::Metrics::Gauge::AtomicType*, unsigned long) |
527 | | |
528 | | static int64_t |
529 | | load(const AtomicType *metric) |
530 | 0 | { |
531 | 0 | debug_assert(metric); |
532 | 0 | return metric->_value.load(); |
533 | 0 | } Unexecuted instantiation: ts::Metrics::Gauge::load(ts::Metrics::Gauge::AtomicType const*) Unexecuted instantiation: ts::Metrics::Gauge::load(ts::Metrics::Gauge::AtomicType const*) |
534 | | |
535 | | static void |
536 | | store(AtomicType *metric, int64_t val) |
537 | 0 | { |
538 | 0 | debug_assert(metric); |
539 | 0 | return metric->_value.store(val); |
540 | 0 | } |
541 | | |
542 | | }; // class Gauge |
543 | | |
544 | | class Counter |
545 | | { |
546 | | public: |
547 | | using self_type = Counter; |
548 | | |
549 | | class AtomicType : public Metrics::AtomicType |
550 | | { |
551 | | }; |
552 | | |
553 | | static IdType |
554 | | lookup(const std::string_view name) |
555 | 0 | { |
556 | 0 | auto &instance = Metrics::instance(); |
557 | 0 |
|
558 | 0 | return instance.lookup(name); |
559 | 0 | } |
560 | | |
561 | | static AtomicType * |
562 | | lookup(const IdType id, std::string_view *out_name = nullptr) |
563 | 0 | { |
564 | 0 | auto &instance = Metrics::instance(); |
565 | 0 |
|
566 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(id, out_name)); |
567 | 0 | } |
568 | | |
569 | | static AtomicType * |
570 | | lookup(const std::string_view name, IdType *id) |
571 | 0 | { |
572 | 0 | auto &instance = Metrics::instance(); |
573 | 0 |
|
574 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(name, id)); |
575 | 0 | } |
576 | | |
577 | | static Metrics::IdType |
578 | | create(const std::string_view name) |
579 | 0 | { |
580 | 0 | auto &instance = Metrics::instance(); |
581 | 0 |
|
582 | 0 | return instance._create(name, MetricType::COUNTER); |
583 | 0 | } |
584 | | |
585 | | static AtomicType * |
586 | | createPtr(const std::string_view name) |
587 | 0 | { |
588 | 0 | auto &instance = Metrics::instance(); |
589 | 0 |
|
590 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(name, MetricType::COUNTER))); |
591 | 0 | } |
592 | | |
593 | | static AtomicType * |
594 | | createPtr(const std::string_view prefix, const std::string_view name) |
595 | 0 | { |
596 | 0 | auto &instance = Metrics::instance(); |
597 | 0 | std::string tmpname = std::string(prefix) + std::string(name); |
598 | 0 |
|
599 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(tmpname, MetricType::COUNTER))); |
600 | 0 | } |
601 | | |
602 | | /** Create a metric which is stored but never published. |
603 | | * |
604 | | * @see Metrics::hidden_instance() |
605 | | */ |
606 | | static AtomicType * |
607 | | createHiddenPtr(const std::string_view name) |
608 | 0 | { |
609 | 0 | auto &instance = Metrics::hidden_instance(); |
610 | 0 |
|
611 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(name, MetricType::COUNTER))); |
612 | 0 | } |
613 | | |
614 | | static AtomicType * |
615 | | createHiddenPtr(const std::string_view prefix, const std::string_view name) |
616 | 0 | { |
617 | 0 | auto &instance = Metrics::hidden_instance(); |
618 | 0 | std::string tmpname = std::string(prefix) + std::string(name); |
619 | 0 |
|
620 | 0 | return reinterpret_cast<AtomicType *>(instance.lookup(instance._create(tmpname, MetricType::COUNTER))); |
621 | 0 | } |
622 | | |
623 | | static void |
624 | | increment(AtomicType *metric, uint64_t val = 1) |
625 | 0 | { |
626 | 0 | debug_assert(metric); |
627 | 0 | metric->_value.fetch_add(val, MEMORY_ORDER); |
628 | 0 | } Unexecuted instantiation: ts::Metrics::Counter::increment(ts::Metrics::Counter::AtomicType*, unsigned long) Unexecuted instantiation: ts::Metrics::Counter::increment(ts::Metrics::Counter::AtomicType*, unsigned long) |
629 | | |
630 | | static int64_t |
631 | | load(const AtomicType *metric) |
632 | 0 | { |
633 | 0 | debug_assert(metric); |
634 | 0 | return metric->_value.load(); |
635 | 0 | } |
636 | | |
637 | | }; // class Counter |
638 | | |
639 | | /** |
640 | | * Static string metrics storage. |
641 | | * |
642 | | * All methods are thread-safe. |
643 | | */ |
644 | | class StaticString |
645 | | { |
646 | | public: |
647 | | using StringStorage = std::unordered_map<std::string, std::string>; |
648 | | |
649 | | static void |
650 | | createString(const std::string &name, const std::string_view value) |
651 | 0 | { |
652 | 0 | auto &instance = Metrics::StaticString::instance(); |
653 | 0 | return instance._createString(name, value); |
654 | 0 | } |
655 | | |
656 | | static StaticString &instance(); |
657 | | |
658 | | /** |
659 | | * Thread-safe iteration over all string metrics. |
660 | | * The callback is invoked for each metric while holding the mutex. |
661 | | */ |
662 | | template <typename Func> |
663 | | void |
664 | | for_each(Func &&func) const |
665 | 0 | { |
666 | 0 | std::lock_guard lock(_mutex); |
667 | 0 | for (const auto &[name, value] : _strings) { |
668 | 0 | func(name, value); |
669 | 0 | } |
670 | 0 | } Unexecuted instantiation: RecCore.cc:void ts::Metrics::StaticString::for_each<RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_1>(RecLookupMatchingRecords(unsigned int, char const*, void (*)(RecRecord const*, void*), void*, bool)::$_1&&) const Unexecuted instantiation: RecCore.cc:void ts::Metrics::StaticString::for_each<RecDumpRecords(RecT, void (*)(RecT, void*, int, char const*, int, RecData*), void*)::$_1>(RecDumpRecords(RecT, void (*)(RecT, void*, int, char const*, int, RecData*), void*)::$_1&&) const |
671 | | |
672 | | std::optional<std::string_view> lookup(const std::string &name) const; |
673 | | |
674 | | private: |
675 | | void _createString(const std::string &name, const std::string_view value); |
676 | | |
677 | | StringStorage _strings; |
678 | | mutable std::mutex _mutex; |
679 | | }; |
680 | | |
681 | | /** |
682 | | * Derive metrics by summing a set of other metrics. |
683 | | * |
684 | | */ |
685 | | class Derived |
686 | | { |
687 | | public: |
688 | | /// How the sources of a derived metric are combined into its value. |
689 | | enum class Op { SUM, MAX, MIN }; |
690 | | |
691 | | struct DerivedMetricSpec { |
692 | | using MetricSpec = std::variant<Metrics::AtomicType *, Metrics::IdType, std::string_view>; |
693 | | std::string_view derived_name; |
694 | | Metrics::MetricType derived_type; |
695 | | std::initializer_list<MetricSpec> derived_from; |
696 | | Op op{Op::SUM}; |
697 | | }; |
698 | | |
699 | | /** |
700 | | * Create new metrics derived from existing metrics. |
701 | | * |
702 | | * This function will create new metrics from a list of existing metrics. The existing metric can |
703 | | * be specified by name, id or a pointer to the metric. |
704 | | */ |
705 | | static void derive(const std::initializer_list<DerivedMetricSpec> &metrics); |
706 | | |
707 | | /** Add a source to a derived metric, creating the derived metric if needed. |
708 | | * |
709 | | * Unlike @c derive this may be called at any time, so aggregates can be built up as their |
710 | | * sources are discovered at runtime. |
711 | | * |
712 | | * @param derived_name Name of the derived metric, in the published store. |
713 | | * @param type Type of the derived metric. Ignored if the derived metric already exists. |
714 | | * @param source The source metric. May come from either the published or the hidden store. |
715 | | * @param op How to combine the sources. Ignored if the derived metric already exists. |
716 | | * |
717 | | * Adding a source which is already registered for @a derived_name is a no-op, so callers |
718 | | * which may re-register (e.g. an object recreated for the same key) need not track this. |
719 | | */ |
720 | | static void add_source(std::string_view derived_name, Metrics::MetricType type, Metrics::AtomicType *source, Op op = Op::SUM); |
721 | | |
722 | | /** Stop @a source contributing to a derived metric. |
723 | | * |
724 | | * The counterpart to @c add_source, for a contributor that goes away or stops wanting the |
725 | | * aggregate published. A derived metric is shared by its sources, so this does not unlist it |
726 | | * while any remain; when the last one is removed there is nothing left to report and the name |
727 | | * is unlisted. Re-adding a source relists it. |
728 | | * |
729 | | * A source that is not registered for @a derived_name, or a name with no derived metric, is a |
730 | | * no-op. |
731 | | * |
732 | | * Safe against a concurrent @c add_source for the same name: the listing change is made with |
733 | | * the membership change, so a metric cannot be left unlisted with a source still contributing. |
734 | | */ |
735 | | static void remove_source(std::string_view derived_name, Metrics::AtomicType *source); |
736 | | |
737 | | /** |
738 | | * Update derived metrics. |
739 | | * |
740 | | * This static function should be called periodically to update derived metrics. |
741 | | */ |
742 | | static void update_derived(); |
743 | | }; |
744 | | |
745 | | }; // class Metrics |
746 | | |
747 | | } // namespace ts |