Coverage Report

Created: 2025-07-25 10:29

/root/doris/be/src/util/runtime_profile.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/runtime-profile.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/Metrics_types.h>
24
#include <glog/logging.h>
25
#include <stdint.h>
26
27
#include <algorithm>
28
#include <atomic>
29
#include <cstdint>
30
#include <functional>
31
#include <iostream>
32
#include <map>
33
#include <memory>
34
#include <mutex>
35
#include <set>
36
#include <string>
37
#include <utility>
38
#include <vector>
39
40
#include "common/compiler_util.h" // IWYU pragma: keep
41
#include "util/binary_cast.hpp"
42
#include "util/pretty_printer.h"
43
#include "util/stopwatch.hpp"
44
45
namespace doris {
46
class TRuntimeProfileNode;
47
class TRuntimeProfileTree;
48
49
// Some macro magic to generate unique ids using __COUNTER__
50
191k
#define CONCAT_IMPL(x, y) x##y
51
191k
#define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
52
53
4
#define ADD_LABEL_COUNTER(profile, name) (profile)->add_counter(name, TUnit::NONE)
54
#define ADD_LABEL_COUNTER_WITH_LEVEL(profile, name, type) \
55
0
    (profile)->add_counter_with_level(name, TUnit::NONE, type)
56
1.62k
#define ADD_COUNTER(profile, name, type) (profile)->add_counter(name, type)
57
#define ADD_COUNTER_WITH_LEVEL(profile, name, type, level) \
58
28
    (profile)->add_counter_with_level(name, type, level)
59
799
#define ADD_TIMER(profile, name) (profile)->add_counter(name, TUnit::TIME_NS)
60
#define ADD_TIMER_WITH_LEVEL(profile, name, level) \
61
32
    (profile)->add_counter_with_level(name, TUnit::TIME_NS, level)
62
0
#define ADD_CHILD_COUNTER(profile, name, type, parent) (profile)->add_counter(name, type, parent)
63
#define ADD_CHILD_COUNTER_WITH_LEVEL(profile, name, type, parent, level) \
64
4
    (profile)->add_counter(name, type, parent, level)
65
630
#define ADD_CHILD_TIMER(profile, name, parent) (profile)->add_counter(name, TUnit::TIME_NS, parent)
66
#define ADD_CHILD_TIMER_WITH_LEVEL(profile, name, parent, level) \
67
0
    (profile)->add_counter(name, TUnit::TIME_NS, parent, level)
68
1.07k
#define SCOPED_TIMER(c) ScopedTimer<MonotonicStopWatch> MACRO_CONCAT(SCOPED_TIMER, __COUNTER__)(c)
69
#define SCOPED_TIMER_ATOMIC(c) \
70
    ScopedTimer<MonotonicStopWatch, std::atomic_bool> MACRO_CONCAT(SCOPED_TIMER, __COUNTER__)(c)
71
#define SCOPED_CPU_TIMER(c) \
72
0
    ScopedTimer<ThreadCpuStopWatch> MACRO_CONCAT(SCOPED_TIMER, __COUNTER__)(c)
73
#define CANCEL_SAFE_SCOPED_TIMER(c, is_cancelled) \
74
0
    ScopedTimer<MonotonicStopWatch> MACRO_CONCAT(SCOPED_TIMER, __COUNTER__)(c, is_cancelled)
75
#define SCOPED_RAW_TIMER(c)                                                                  \
76
189k
    doris::ScopedRawTimer<doris::MonotonicStopWatch, int64_t> MACRO_CONCAT(SCOPED_RAW_TIMER, \
77
189k
                                                                           __COUNTER__)(c)
78
#define SCOPED_ATOMIC_TIMER(c)                                                                 \
79
20
    ScopedRawTimer<MonotonicStopWatch, std::atomic<int64_t>> MACRO_CONCAT(SCOPED_ATOMIC_TIMER, \
80
20
                                                                          __COUNTER__)(c)
81
3.42k
#define COUNTER_UPDATE(c, v) (c)->update(v)
82
217
#define COUNTER_SET(c, v) (c)->set(v)
83
84
class ObjectPool;
85
86
// Runtime profile is a group of profiling counters.  It supports adding named counters
87
// and being able to serialize and deserialize them.
88
// The profiles support a tree structure to form a hierarchy of counters.
89
// Runtime profiles supports measuring wall clock rate based counters.  There is a
90
// single thread per process that will convert an amount (i.e. bytes) counter to a
91
// corresponding rate based counter.  This thread wakes up at fixed intervals and updates
92
// all of the rate counters.
93
// Thread-safe.
94
class RuntimeProfile {
95
public:
96
    class Counter {
97
    public:
98
        Counter(TUnit::type type, int64_t value = 0, int64_t level = 3)
99
8.66k
                : _value(value), _type(type), _level(level) {}
100
8.59k
        virtual ~Counter() = default;
101
102
4.48k
        virtual void update(int64_t delta) { _value.fetch_add(delta, std::memory_order_relaxed); }
103
104
0
        void bit_or(int64_t delta) { _value.fetch_or(delta, std::memory_order_relaxed); }
105
106
217
        virtual void set(int64_t value) { _value.store(value, std::memory_order_relaxed); }
107
108
0
        virtual void set(double value) {
109
0
            DCHECK_EQ(sizeof(value), sizeof(int64_t));
110
0
            _value.store(binary_cast<double, int64_t>(value), std::memory_order_relaxed);
111
0
        }
112
113
4
        virtual int64_t value() const { return _value.load(std::memory_order_relaxed); }
114
115
0
        virtual double double_value() const {
116
0
            return binary_cast<int64_t, double>(_value.load(std::memory_order_relaxed));
117
0
        }
118
119
        virtual void to_thrift(const std::string& name, std::vector<TCounter>& tcounters,
120
0
                               std::map<std::string, std::set<std::string>>& child_counters_map) {
121
0
            TCounter counter;
122
0
            counter.name = name;
123
0
            counter.value = this->value();
124
0
            counter.type = this->type();
125
0
            counter.__set_level(this->level());
126
0
            tcounters.push_back(counter);
127
0
        }
128
129
1.06k
        TUnit::type type() const { return _type; }
130
131
0
        virtual int64_t level() { return _level; }
132
133
    private:
134
        friend class RuntimeProfile;
135
136
        std::atomic<int64_t> _value;
137
        TUnit::type _type;
138
        int64_t _level;
139
    };
140
141
    /// A counter that keeps track of the highest value seen (reporting that
142
    /// as value()) and the current value.
143
    class HighWaterMarkCounter : public Counter {
144
    public:
145
        HighWaterMarkCounter(TUnit::type unit, int64_t level = 2)
146
4
                : Counter(unit, 0, level), current_value_(0) {}
147
148
0
        virtual void add(int64_t delta) {
149
0
            current_value_.fetch_add(delta, std::memory_order_relaxed);
150
0
            if (delta > 0) {
151
0
                UpdateMax(current_value_);
152
0
            }
153
0
        }
154
155
        /// Tries to increase the current value by delta. If current_value() + delta
156
        /// exceeds max, return false and current_value is not changed.
157
0
        bool try_add(int64_t delta, int64_t max) {
158
0
            while (true) {
159
0
                int64_t old_val = current_value_.load(std::memory_order_relaxed);
160
0
                int64_t new_val = old_val + delta;
161
0
                if (UNLIKELY(new_val > max)) return false;
162
0
                if (LIKELY(current_value_.compare_exchange_weak(old_val, new_val,
163
0
                                                                std::memory_order_relaxed))) {
164
0
                    UpdateMax(new_val);
165
0
                    return true;
166
0
                }
167
0
            }
168
0
        }
169
170
2
        void set(int64_t v) override {
171
2
            current_value_.store(v, std::memory_order_relaxed);
172
2
            UpdateMax(v);
173
2
        }
174
175
0
        int64_t current_value() const { return current_value_.load(std::memory_order_relaxed); }
176
177
    private:
178
        /// Set '_value' to 'v' if 'v' is larger than '_value'. The entire operation is
179
        /// atomic.
180
2
        void UpdateMax(int64_t v) {
181
2
            while (true) {
182
2
                int64_t old_max = _value.load(std::memory_order_relaxed);
183
2
                int64_t new_max = std::max(old_max, v);
184
2
                if (new_max == old_max) {
185
2
                    break; // Avoid atomic update.
186
2
                }
187
0
                if (LIKELY(_value.compare_exchange_weak(old_max, new_max,
188
0
                                                        std::memory_order_relaxed))) {
189
0
                    break;
190
0
                }
191
0
            }
192
2
        }
193
194
        /// The current value of the counter. _value in the super class represents
195
        /// the high water mark.
196
        std::atomic<int64_t> current_value_;
197
    };
198
199
    using DerivedCounterFunction = std::function<int64_t()>;
200
201
    // A DerivedCounter also has a name and type, but the value is computed.
202
    // Do not call Set() and Update().
203
    class DerivedCounter : public Counter {
204
    public:
205
        DerivedCounter(TUnit::type type, const DerivedCounterFunction& counter_fn)
206
2
                : Counter(type, 0), _counter_fn(counter_fn) {}
207
208
0
        int64_t value() const override { return _counter_fn(); }
209
210
    private:
211
        DerivedCounterFunction _counter_fn;
212
    };
213
214
    // NonZeroCounter will not be converted to Thrift if the value is 0.
215
    class NonZeroCounter : public Counter {
216
    public:
217
        NonZeroCounter(TUnit::type type, int64_t level, const std::string& parent_name)
218
0
                : Counter(type, 0, level), _parent_name(parent_name) {}
219
220
        void to_thrift(const std::string& name, std::vector<TCounter>& tcounters,
221
0
                       std::map<std::string, std::set<std::string>>& child_counters_map) override {
222
0
            if (this->_value > 0) {
223
0
                Counter::to_thrift(name, tcounters, child_counters_map);
224
0
            } else {
225
                // remove it
226
0
                child_counters_map[_parent_name].erase(name);
227
0
            }
228
0
        }
229
230
    private:
231
        const std::string _parent_name;
232
    };
233
234
    // An EventSequence captures a sequence of events (each added by
235
    // calling MarkEvent). Each event has a text label, and a time
236
    // (measured relative to the moment start() was called as t=0). It is
237
    // useful for tracking the evolution of some serial process, such as
238
    // the query lifecycle.
239
    // Not thread-safe.
240
    class EventSequence {
241
    public:
242
0
        EventSequence() = default;
243
244
        // starts the timer without resetting it.
245
0
        void start() { _sw.start(); }
246
247
        // stops (or effectively pauses) the timer.
248
0
        void stop() { _sw.stop(); }
249
250
        // Stores an event in sequence with the given label and the
251
        // current time (relative to the first time start() was called) as
252
        // the timestamp.
253
0
        void mark_event(const std::string& label) {
254
0
            _events.push_back(make_pair(label, _sw.elapsed_time()));
255
0
        }
256
257
0
        int64_t elapsed_time() { return _sw.elapsed_time(); }
258
259
        // An Event is a <label, timestamp> pair
260
        using Event = std::pair<std::string, int64_t>;
261
262
        // An EventList is a sequence of Events, in increasing timestamp order
263
        using EventList = std::vector<Event>;
264
265
0
        const EventList& events() const { return _events; }
266
267
    private:
268
        // Stored in increasing time order
269
        EventList _events;
270
271
        // Timer which allows events to be timestamped when they are recorded.
272
        MonotonicStopWatch _sw;
273
    };
274
275
    // Create a runtime profile object with 'name'.
276
    RuntimeProfile(const std::string& name, bool is_averaged_profile = false);
277
278
    ~RuntimeProfile();
279
280
    // Adds a child profile.  This is thread safe.
281
    // 'indent' indicates whether the child will be printed w/ extra indentation
282
    // relative to the parent.
283
    // If location is non-null, child will be inserted after location.  Location must
284
    // already be added to the profile.
285
    void add_child(RuntimeProfile* child, bool indent, RuntimeProfile* location);
286
287
    void insert_child_head(RuntimeProfile* child, bool indent);
288
289
    void add_child_unlock(RuntimeProfile* child, bool indent, RuntimeProfile* loc);
290
291
    /// Creates a new child profile with the given 'name'. A child profile with that name
292
    /// must not already exist. If 'prepend' is true, prepended before other child profiles,
293
    /// otherwise appended after other child profiles.
294
    RuntimeProfile* create_child(const std::string& name, bool indent = true, bool prepend = false);
295
296
    // Sorts all children according to a custom comparator. Does not
297
    // invalidate pointers to profiles.
298
    template <class Compare>
299
    void sort_childer(const Compare& cmp) {
300
        std::lock_guard<std::mutex> l(_children_lock);
301
        std::sort(_children.begin(), _children.end(), cmp);
302
    }
303
304
    // Merges the src profile into this one, combining counters that have an identical
305
    // path. Info strings from profiles are not merged. 'src' would be a const if it
306
    // weren't for locking.
307
    // Calling this concurrently on two RuntimeProfiles in reverse order results in
308
    // undefined behavior.
309
    void merge(RuntimeProfile* src);
310
311
    // Updates this profile w/ the thrift profile: behaves like Merge(), except
312
    // that existing counters are updated rather than added up.
313
    // Info strings matched up by key and are updated or added, depending on whether
314
    // the key has already been registered.
315
    void update(const TRuntimeProfileTree& thrift_profile);
316
317
    // Add a counter with 'name'/'type'.  Returns a counter object that the caller can
318
    // update.  The counter is owned by the RuntimeProfile object.
319
    // If parent_counter_name is a non-empty string, the counter is added as a child of
320
    // parent_counter_name.
321
    // If the counter already exists, the existing counter object is returned.
322
    Counter* add_counter(const std::string& name, TUnit::type type,
323
                         const std::string& parent_counter_name, int64_t level = 2);
324
3.38k
    Counter* add_counter(const std::string& name, TUnit::type type) {
325
3.38k
        return add_counter(name, type, "");
326
3.38k
    }
327
328
60
    Counter* add_counter_with_level(const std::string& name, TUnit::type type, int64_t level) {
329
60
        return add_counter(name, type, "", level);
330
60
    }
331
332
    NonZeroCounter* add_nonzero_counter(const std::string& name, TUnit::type type,
333
                                        const std::string& parent_counter_name = "",
334
                                        int64_t level = 2);
335
336
    // Add a derived counter with 'name'/'type'. The counter is owned by the
337
    // RuntimeProfile object.
338
    // If parent_counter_name is a non-empty string, the counter is added as a child of
339
    // parent_counter_name.
340
    // Returns nullptr if the counter already exists.
341
    DerivedCounter* add_derived_counter(const std::string& name, TUnit::type type,
342
                                        const DerivedCounterFunction& counter_fn,
343
                                        const std::string& parent_counter_name);
344
345
    // Gets the counter object with 'name'.  Returns nullptr if there is no counter with
346
    // that name.
347
    Counter* get_counter(const std::string& name);
348
349
    // Adds all counters with 'name' that are registered either in this or
350
    // in any of the child profiles to 'counters'.
351
    void get_counters(const std::string& name, std::vector<Counter*>* counters);
352
353
    // Helper to append to the "ExecOption" info string.
354
0
    void append_exec_option(const std::string& option) { add_info_string("ExecOption", option); }
355
356
    // Adds a string to the runtime profile.  If a value already exists for 'key',
357
    // the value will be updated.
358
    void add_info_string(const std::string& key, const std::string& value);
359
360
    // Creates and returns a new EventSequence (owned by the runtime
361
    // profile) - unless a timer with the same 'key' already exists, in
362
    // which case it is returned.
363
    // TODO: EventSequences are not merged by Merge()
364
    EventSequence* add_event_sequence(const std::string& key);
365
366
    // Returns a pointer to the info string value for 'key'.  Returns nullptr if
367
    // the key does not exist.
368
    const std::string* get_info_string(const std::string& key);
369
370
    // Returns the counter for the total elapsed time.
371
34
    Counter* total_time_counter() { return &_counter_total_time; }
372
373
    // Prints the counters in a name: value format.
374
    // Does not hold locks when it makes any function calls.
375
    void pretty_print(std::ostream* s, const std::string& prefix = "") const;
376
377
    // Serializes profile to thrift.
378
    // Does not hold locks when it makes any function calls.
379
    void to_thrift(TRuntimeProfileTree* tree);
380
    void to_thrift(std::vector<TRuntimeProfileNode>* nodes);
381
382
    // Divides all counters by n
383
    void divide(int n);
384
385
    void get_children(std::vector<RuntimeProfile*>* children);
386
387
    // Gets all profiles in tree, including this one.
388
    void get_all_children(std::vector<RuntimeProfile*>* children);
389
390
    // Returns the number of counters in this profile
391
0
    int num_counters() const { return _counter_map.size(); }
392
393
    // Returns name of this profile
394
2
    const std::string& name() const { return _name; }
395
396
    // *only call this on top-level profiles*
397
    // (because it doesn't re-file child profiles)
398
0
    void set_name(const std::string& name) { _name = name; }
399
400
0
    int64_t metadata() const { return _metadata; }
401
2
    void set_metadata(int64_t md) {
402
2
        _is_set_metadata = true;
403
2
        _metadata = md;
404
2
    }
405
406
89
    bool is_set_metadata() const { return _is_set_metadata; }
407
408
0
    void set_is_sink(bool is_sink) {
409
0
        _is_set_sink = true;
410
0
        _is_sink = is_sink;
411
0
    }
412
413
0
    bool is_sink() const { return _is_sink; }
414
415
89
    bool is_set_sink() const { return _is_set_sink; }
416
417
0
    time_t timestamp() const { return _timestamp; }
418
0
    void set_timestamp(time_t ss) { _timestamp = ss; }
419
420
    // Derived counter function: return measured throughput as input_value/second.
421
    static int64_t units_per_second(const Counter* total_counter, const Counter* timer);
422
423
    // Derived counter function: return aggregated value
424
    static int64_t counter_sum(const std::vector<Counter*>* counters);
425
426
    // Function that returns a counter metric.
427
    // Note: this function should not block (or take a long time).
428
    using SampleFn = std::function<int64_t()>;
429
430
    // Add a rate counter to the current profile based on src_counter with name.
431
    // The rate counter is updated periodically based on the src counter.
432
    // The rate counter has units in src_counter unit per second.
433
    Counter* add_rate_counter(const std::string& name, Counter* src_counter);
434
435
    // Same as 'add_rate_counter' above except values are taken by calling fn.
436
    // The resulting counter will be of 'type'.
437
    Counter* add_rate_counter(const std::string& name, SampleFn fn, TUnit::type type);
438
439
    // Add a sampling counter to the current profile based on src_counter with name.
440
    // The sampling counter is updated periodically based on the src counter by averaging
441
    // the samples taken from the src counter.
442
    // The sampling counter has the same unit as src_counter unit.
443
    Counter* add_sampling_counter(const std::string& name, Counter* src_counter);
444
445
    // Same as 'add_sampling_counter' above except the samples are taken by calling fn.
446
    Counter* add_sampling_counter(const std::string& name, SampleFn fn);
447
448
    /// Adds a high water mark counter to the runtime profile. Otherwise, same behavior
449
    /// as AddCounter().
450
    HighWaterMarkCounter* AddHighWaterMarkCounter(const std::string& name, TUnit::type unit,
451
                                                  const std::string& parent_counter_name = "",
452
                                                  int64_t level = 2);
453
454
    // Only for create MemTracker(using profile's counter to calc consumption)
455
    std::shared_ptr<HighWaterMarkCounter> AddSharedHighWaterMarkCounter(
456
            const std::string& name, TUnit::type unit, const std::string& parent_counter_name = "");
457
458
    // Recursively compute the fraction of the 'total_time' spent in this profile and
459
    // its children.
460
    // This function updates _local_time_percent for each profile.
461
    void compute_time_in_profile();
462
463
    void clear_children();
464
465
private:
466
    // Pool for allocated counters. Usually owned by the creator of this
467
    // object, but occasionally allocated in the constructor.
468
    std::unique_ptr<ObjectPool> _pool;
469
470
    // Pool for allocated counters. These counters are shared with some other objects.
471
    std::map<std::string, std::shared_ptr<HighWaterMarkCounter>> _shared_counter_pool;
472
473
    // Name for this runtime profile.
474
    std::string _name;
475
476
    // user-supplied, uninterpreted metadata.
477
    int64_t _metadata;
478
    bool _is_set_metadata = false;
479
480
    bool _is_sink = false;
481
    bool _is_set_sink = false;
482
483
    // The timestamp when the profile was modified, make sure the update is up to date.
484
    time_t _timestamp;
485
486
    /// True if this profile is an average derived from other profiles.
487
    /// All counters in this profile must be of unit AveragedCounter.
488
    bool _is_averaged_profile;
489
490
    // Map from counter names to counters.  The profile owns the memory for the
491
    // counters.
492
    using CounterMap = std::map<std::string, Counter*>;
493
    CounterMap _counter_map;
494
495
    // Map from parent counter name to a set of child counter name.
496
    // All top level counters are the child of "" (root).
497
    using ChildCounterMap = std::map<std::string, std::set<std::string>>;
498
    ChildCounterMap _child_counter_map;
499
500
    // A set of bucket counters registered in this runtime profile.
501
    std::set<std::vector<Counter*>*> _bucketing_counters;
502
503
    // protects _counter_map, _counter_child_map and _bucketing_counters
504
    mutable std::mutex _counter_map_lock;
505
506
    // Child profiles.  Does not own memory.
507
    // We record children in both a map (to facilitate updates) and a vector
508
    // (to print things in the order they were registered)
509
    using ChildMap = std::map<std::string, RuntimeProfile*>;
510
    ChildMap _child_map;
511
    // vector of (profile, indentation flag)
512
    using ChildVector = std::vector<std::pair<RuntimeProfile*, bool>>;
513
    ChildVector _children;
514
    mutable std::mutex _children_lock; // protects _child_map and _children
515
516
    using InfoStrings = std::map<std::string, std::string>;
517
    InfoStrings _info_strings;
518
519
    // Keeps track of the order in which InfoStrings are displayed when printed
520
    using InfoStringsDisplayOrder = std::vector<std::string>;
521
    InfoStringsDisplayOrder _info_strings_display_order;
522
523
    // Protects _info_strings and _info_strings_display_order
524
    mutable std::mutex _info_strings_lock;
525
526
    using EventSequenceMap = std::map<std::string, EventSequence*>;
527
    EventSequenceMap _event_sequence_map;
528
    mutable std::mutex _event_sequences_lock;
529
530
    Counter _counter_total_time;
531
    // Time spent in just in this profile (i.e. not the children) as a fraction
532
    // of the total time in the entire profile tree.
533
    double _local_time_percent;
534
535
    enum PeriodicCounterType {
536
        RATE_COUNTER = 0,
537
        SAMPLING_COUNTER,
538
    };
539
540
    struct RateCounterInfo {
541
        Counter* src_counter = nullptr;
542
        SampleFn sample_fn;
543
        int64_t elapsed_ms;
544
    };
545
546
    struct SamplingCounterInfo {
547
        Counter* src_counter = nullptr; // the counter to be sampled
548
        SampleFn sample_fn;
549
        int64_t total_sampled_value; // sum of all sampled values;
550
        int64_t num_sampled;         // number of samples taken
551
    };
552
553
    struct BucketCountersInfo {
554
        Counter* src_counter = nullptr; // the counter to be sampled
555
        int64_t num_sampled;            // number of samples taken
556
        // TODO: customize bucketing
557
    };
558
559
    // update a subtree of profiles from nodes, rooted at *idx.
560
    // On return, *idx points to the node immediately following this subtree.
561
    void update(const std::vector<TRuntimeProfileNode>& nodes, int* idx);
562
563
    // Helper function to compute compute the fraction of the total time spent in
564
    // this profile and its children.
565
    // Called recursively.
566
    void compute_time_in_profile(int64_t total_time);
567
568
    // Print the child counters of the given counter name
569
    static void print_child_counters(const std::string& prefix, const std::string& counter_name,
570
                                     const CounterMap& counter_map,
571
                                     const ChildCounterMap& child_counter_map, std::ostream* s);
572
573
0
    static std::string print_counter(Counter* counter) {
574
0
        return PrettyPrinter::print(counter->value(), counter->type());
575
0
    }
576
};
577
578
// Utility class to update the counter at object construction and destruction.
579
// When the object is constructed, decrement the counter by val.
580
// When the object goes out of scope, increment the counter by val.
581
class ScopedCounter {
582
public:
583
0
    ScopedCounter(RuntimeProfile::Counter* counter, int64_t val) : _val(val), _counter(counter) {
584
0
        if (counter == nullptr) {
585
0
            return;
586
0
        }
587
0
588
0
        _counter->update(-1L * _val);
589
0
    }
590
591
    // Increment the counter when object is destroyed
592
0
    ~ScopedCounter() {
593
0
        if (_counter != nullptr) {
594
0
            _counter->update(_val);
595
0
        }
596
0
    }
597
598
    // Disable copy constructor and assignment
599
    ScopedCounter(const ScopedCounter& counter) = delete;
600
    ScopedCounter& operator=(const ScopedCounter& counter) = delete;
601
602
private:
603
    int64_t _val;
604
    RuntimeProfile::Counter* _counter = nullptr;
605
};
606
607
// Utility class to update time elapsed when the object goes out of scope.
608
// 'T' must implement the stopWatch "interface" (start,stop,elapsed_time) but
609
// we use templates not to pay for virtual function overhead.
610
template <class T, typename Bool = bool>
611
class ScopedTimer {
612
public:
613
    ScopedTimer(RuntimeProfile::Counter* counter, const Bool* is_cancelled = nullptr)
614
1.07k
            : _counter(counter), _is_cancelled(is_cancelled) {
615
1.07k
        if (counter == nullptr) {
616
14
            return;
617
14
        }
618
1.06k
        DCHECK_EQ(counter->type(), TUnit::TIME_NS);
619
1.06k
        _sw.start();
620
1.06k
    }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbEC2EPNS_14RuntimeProfile7CounterEPKb
Line
Count
Source
614
1.07k
            : _counter(counter), _is_cancelled(is_cancelled) {
615
1.07k
        if (counter == nullptr) {
616
14
            return;
617
14
        }
618
1.06k
        DCHECK_EQ(counter->type(), TUnit::TIME_NS);
619
1.06k
        _sw.start();
620
1.06k
    }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbEC2EPNS_14RuntimeProfile7CounterEPKb
621
622
    void stop() { _sw.stop(); }
623
624
    void start() { _sw.start(); }
625
626
1.06k
    bool is_cancelled() { return _is_cancelled != nullptr && *_is_cancelled; }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbE12is_cancelledEv
Line
Count
Source
626
1.06k
    bool is_cancelled() { return _is_cancelled != nullptr && *_is_cancelled; }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbE12is_cancelledEv
627
628
1.06k
    void UpdateCounter() {
629
1.06k
        if (_counter != nullptr && !is_cancelled()) {
630
1.06k
            _counter->update(_sw.elapsed_time());
631
1.06k
        }
632
1.06k
    }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbE13UpdateCounterEv
Line
Count
Source
628
1.06k
    void UpdateCounter() {
629
1.06k
        if (_counter != nullptr && !is_cancelled()) {
630
1.06k
            _counter->update(_sw.elapsed_time());
631
1.06k
        }
632
1.06k
    }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbE13UpdateCounterEv
633
634
    // Update counter when object is destroyed
635
1.07k
    ~ScopedTimer() {
636
1.07k
        if (_counter == nullptr) {
637
14
            return;
638
14
        }
639
1.06k
        _sw.stop();
640
1.06k
        UpdateCounter();
641
1.06k
    }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbED2Ev
Line
Count
Source
635
1.07k
    ~ScopedTimer() {
636
1.07k
        if (_counter == nullptr) {
637
14
            return;
638
14
        }
639
1.06k
        _sw.stop();
640
1.06k
        UpdateCounter();
641
1.06k
    }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbED2Ev
642
643
    // Disable copy constructor and assignment
644
    ScopedTimer(const ScopedTimer& timer) = delete;
645
    ScopedTimer& operator=(const ScopedTimer& timer) = delete;
646
647
private:
648
    T _sw;
649
    RuntimeProfile::Counter* _counter = nullptr;
650
    const Bool* _is_cancelled = nullptr;
651
};
652
653
// Utility class to update time elapsed when the object goes out of scope.
654
// 'T' must implement the stopWatch "interface" (start,stop,elapsed_time) but
655
// we use templates not to pay for virtual function overhead.
656
template <class T, class C>
657
class ScopedRawTimer {
658
public:
659
189k
    ScopedRawTimer(C* counter) : _counter(counter) { _sw.start(); }
_ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEElEC2EPl
Line
Count
Source
659
189k
    ScopedRawTimer(C* counter) : _counter(counter) { _sw.start(); }
_ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEESt6atomicIlEEC2EPS4_
Line
Count
Source
659
20
    ScopedRawTimer(C* counter) : _counter(counter) { _sw.start(); }
660
    // Update counter when object is destroyed
661
189k
    ~ScopedRawTimer() { *_counter += _sw.elapsed_time(); }
_ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEElED2Ev
Line
Count
Source
661
189k
    ~ScopedRawTimer() { *_counter += _sw.elapsed_time(); }
_ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEESt6atomicIlEED2Ev
Line
Count
Source
661
20
    ~ScopedRawTimer() { *_counter += _sw.elapsed_time(); }
662
663
    // Disable copy constructor and assignment
664
    ScopedRawTimer(const ScopedRawTimer& timer) = delete;
665
    ScopedRawTimer& operator=(const ScopedRawTimer& timer) = delete;
666
667
private:
668
    T _sw;
669
    C* _counter = nullptr;
670
};
671
672
} // namespace doris