Coverage Report

Created: 2025-04-15 14:04

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