Coverage Report

Created: 2025-07-23 18:47

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