Coverage Report

Created: 2024-11-22 16:10

/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
123k
#define CONCAT_IMPL(x, y) x##y
51
123k
#define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
52
53
0
#define ADD_LABEL_COUNTER(profile, name) (profile)->add_counter(name, TUnit::NONE)
54
#define ADD_LABEL_COUNTER_WITH_LEVEL(profile, name, level) \
55
0
    (profile)->add_counter_with_level(name, TUnit::NONE, level)
56
1.99k
#define ADD_COUNTER(profile, name, type) (profile)->add_counter(name, type)
57
#define ADD_COUNTER_WITH_LEVEL(profile, name, type, level) \
58
9
    (profile)->add_counter_with_level(name, type, level)
59
745
#define ADD_TIMER(profile, name) (profile)->add_counter(name, TUnit::TIME_NS)
60
#define ADD_TIMER_WITH_LEVEL(profile, name, level) \
61
20
    (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
12
    (profile)->add_counter(name, type, parent, level)
65
182
#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
6
    (profile)->add_counter(name, TUnit::TIME_NS, parent, level)
68
526
#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
    ScopedTimer<MonotonicStopWatch> MACRO_CONCAT(SCOPED_TIMER, __COUNTER__)(c, is_cancelled)
75
#define SCOPED_RAW_TIMER(c)                                                                  \
76
123k
    doris::ScopedRawTimer<doris::MonotonicStopWatch, int64_t> MACRO_CONCAT(SCOPED_RAW_TIMER, \
77
123k
                                                                           __COUNTER__)(c)
78
#define SCOPED_ATOMIC_TIMER(c)                                                                 \
79
0
    ScopedRawTimer<MonotonicStopWatch, std::atomic<int64_t>> MACRO_CONCAT(SCOPED_ATOMIC_TIMER, \
80
0
                                                                          __COUNTER__)(c)
81
1.08k
#define COUNTER_UPDATE(c, v) (c)->update(v)
82
192
#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
26.7k
                : _value(value), _type(type), _level(level) {}
100
26.6k
        virtual ~Counter() = default;
101
102
1.57k
        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
192
        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
33
        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(std::move(counter));
127
0
        }
128
129
494
        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
6
                : 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
3
        void set(int64_t v) override {
171
3
            current_value_.store(v, std::memory_order_relaxed);
172
3
            UpdateMax(v);
173
3
        }
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
3
        void UpdateMax(int64_t v) {
181
3
            while (true) {
182
3
                int64_t old_max = _value.load(std::memory_order_relaxed);
183
3
                int64_t new_max = std::max(old_max, v);
184
3
                if (new_max == old_max) {
185
3
                    break; // Avoid atomic update.
186
3
                }
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
3
        }
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
0
                : 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
    // Merges the src profile into this one, combining counters that have an identical
297
    // path. Info strings from profiles are not merged. 'src' would be a const if it
298
    // weren't for locking.
299
    // Calling this concurrently on two RuntimeProfiles in reverse order results in
300
    // undefined behavior.
301
    void merge(RuntimeProfile* src);
302
303
    // Updates this profile w/ the thrift profile: behaves like Merge(), except
304
    // that existing counters are updated rather than added up.
305
    // Info strings matched up by key and are updated or added, depending on whether
306
    // the key has already been registered.
307
    void update(const TRuntimeProfileTree& thrift_profile);
308
309
    // Add a counter with 'name'/'type'.  Returns a counter object that the caller can
310
    // update.  The counter is owned by the RuntimeProfile object.
311
    // If parent_counter_name is a non-empty string, the counter is added as a child of
312
    // parent_counter_name.
313
    // If the counter already exists, the existing counter object is returned.
314
    Counter* add_counter(const std::string& name, TUnit::type type,
315
                         const std::string& parent_counter_name, int64_t level = 2);
316
3.09k
    Counter* add_counter(const std::string& name, TUnit::type type) {
317
3.09k
        return add_counter(name, type, "");
318
3.09k
    }
319
320
29
    Counter* add_counter_with_level(const std::string& name, TUnit::type type, int64_t level) {
321
29
        return add_counter(name, type, "", level);
322
29
    }
323
324
    NonZeroCounter* add_nonzero_counter(const std::string& name, TUnit::type type,
325
                                        const std::string& parent_counter_name = "",
326
                                        int64_t level = 2);
327
328
    // Add a derived counter with 'name'/'type'. The counter is owned by the
329
    // RuntimeProfile object.
330
    // If parent_counter_name is a non-empty string, the counter is added as a child of
331
    // parent_counter_name.
332
    // Returns nullptr if the counter already exists.
333
    DerivedCounter* add_derived_counter(const std::string& name, TUnit::type type,
334
                                        const DerivedCounterFunction& counter_fn,
335
                                        const std::string& parent_counter_name);
336
337
    // Gets the counter object with 'name'.  Returns nullptr if there is no counter with
338
    // that name.
339
    Counter* get_counter(const std::string& name);
340
341
    // Adds all counters with 'name' that are registered either in this or
342
    // in any of the child profiles to 'counters'.
343
    void get_counters(const std::string& name, std::vector<Counter*>* counters);
344
345
    // Helper to append to the "ExecOption" info string.
346
0
    void append_exec_option(const std::string& option) { add_info_string("ExecOption", option); }
347
348
    // Adds a string to the runtime profile.  If a value already exists for 'key',
349
    // the value will be updated.
350
    void add_info_string(const std::string& key, const std::string& value);
351
352
    // Creates and returns a new EventSequence (owned by the runtime
353
    // profile) - unless a timer with the same 'key' already exists, in
354
    // which case it is returned.
355
    // TODO: EventSequences are not merged by Merge()
356
    EventSequence* add_event_sequence(const std::string& key);
357
358
    // Returns a pointer to the info string value for 'key'.  Returns nullptr if
359
    // the key does not exist.
360
    const std::string* get_info_string(const std::string& key);
361
362
    // Returns the counter for the total elapsed time.
363
0
    Counter* total_time_counter() { return &_counter_total_time; }
364
365
    // Prints the counters in a name: value format.
366
    // Does not hold locks when it makes any function calls.
367
    void pretty_print(std::ostream* s, const std::string& prefix = "") const;
368
369
    // Serializes profile to thrift.
370
    // Does not hold locks when it makes any function calls.
371
    void to_thrift(TRuntimeProfileTree* tree);
372
    void to_thrift(std::vector<TRuntimeProfileNode>* nodes);
373
374
    // Divides all counters by n
375
    void divide(int n);
376
377
    void get_children(std::vector<RuntimeProfile*>* children);
378
379
    // Gets all profiles in tree, including this one.
380
    void get_all_children(std::vector<RuntimeProfile*>* children);
381
382
    // Returns the number of counters in this profile
383
0
    int num_counters() const { return _counter_map.size(); }
384
385
    // Returns name of this profile
386
0
    const std::string& name() const { return _name; }
387
388
    // *only call this on top-level profiles*
389
    // (because it doesn't re-file child profiles)
390
0
    void set_name(const std::string& name) { _name = name; }
391
392
0
    int64_t metadata() const { return _metadata; }
393
3
    void set_metadata(int64_t md) {
394
3
        _is_set_metadata = true;
395
3
        _metadata = md;
396
3
    }
397
398
92
    bool is_set_metadata() const { return _is_set_metadata; }
399
400
3
    void set_is_sink(bool is_sink) {
401
3
        _is_set_sink = true;
402
3
        _is_sink = is_sink;
403
3
    }
404
405
0
    bool is_sink() const { return _is_sink; }
406
407
92
    bool is_set_sink() const { return _is_set_sink; }
408
409
0
    time_t timestamp() const { return _timestamp; }
410
0
    void set_timestamp(time_t ss) { _timestamp = ss; }
411
412
    // Derived counter function: return measured throughput as input_value/second.
413
    static int64_t units_per_second(const Counter* total_counter, const Counter* timer);
414
415
    // Derived counter function: return aggregated value
416
    static int64_t counter_sum(const std::vector<Counter*>* counters);
417
418
    // Function that returns a counter metric.
419
    // Note: this function should not block (or take a long time).
420
    using SampleFn = std::function<int64_t()>;
421
422
    // Add a rate counter to the current profile based on src_counter with name.
423
    // The rate counter is updated periodically based on the src counter.
424
    // The rate counter has units in src_counter unit per second.
425
    Counter* add_rate_counter(const std::string& name, Counter* src_counter);
426
427
    // Same as 'add_rate_counter' above except values are taken by calling fn.
428
    // The resulting counter will be of 'type'.
429
    Counter* add_rate_counter(const std::string& name, SampleFn fn, TUnit::type type);
430
431
    // Add a sampling counter to the current profile based on src_counter with name.
432
    // The sampling counter is updated periodically based on the src counter by averaging
433
    // the samples taken from the src counter.
434
    // The sampling counter has the same unit as src_counter unit.
435
    Counter* add_sampling_counter(const std::string& name, Counter* src_counter);
436
437
    // Same as 'add_sampling_counter' above except the samples are taken by calling fn.
438
    Counter* add_sampling_counter(const std::string& name, SampleFn fn);
439
440
    /// Adds a high water mark counter to the runtime profile. Otherwise, same behavior
441
    /// as AddCounter().
442
    HighWaterMarkCounter* AddHighWaterMarkCounter(const std::string& name, TUnit::type unit,
443
                                                  const std::string& parent_counter_name = "",
444
                                                  int64_t level = 2);
445
446
    // Only for create MemTracker(using profile's counter to calc consumption)
447
    std::shared_ptr<HighWaterMarkCounter> AddSharedHighWaterMarkCounter(
448
            const std::string& name, TUnit::type unit, const std::string& parent_counter_name = "");
449
450
    // Recursively compute the fraction of the 'total_time' spent in this profile and
451
    // its children.
452
    // This function updates _local_time_percent for each profile.
453
    void compute_time_in_profile();
454
455
    void clear_children();
456
457
private:
458
    // Pool for allocated counters. Usually owned by the creator of this
459
    // object, but occasionally allocated in the constructor.
460
    std::unique_ptr<ObjectPool> _pool;
461
462
    // Pool for allocated counters. These counters are shared with some other objects.
463
    std::map<std::string, std::shared_ptr<HighWaterMarkCounter>> _shared_counter_pool;
464
465
    // Name for this runtime profile.
466
    std::string _name;
467
468
    // user-supplied, uninterpreted metadata.
469
    int64_t _metadata;
470
    bool _is_set_metadata = false;
471
472
    bool _is_sink = false;
473
    bool _is_set_sink = false;
474
475
    // The timestamp when the profile was modified, make sure the update is up to date.
476
    time_t _timestamp;
477
478
    /// True if this profile is an average derived from other profiles.
479
    /// All counters in this profile must be of unit AveragedCounter.
480
    bool _is_averaged_profile;
481
482
    // Map from counter names to counters.  The profile owns the memory for the
483
    // counters.
484
    using CounterMap = std::map<std::string, Counter*>;
485
    CounterMap _counter_map;
486
487
    // Map from parent counter name to a set of child counter name.
488
    // All top level counters are the child of "" (root).
489
    using ChildCounterMap = std::map<std::string, std::set<std::string>>;
490
    ChildCounterMap _child_counter_map;
491
492
    // A set of bucket counters registered in this runtime profile.
493
    std::set<std::vector<Counter*>*> _bucketing_counters;
494
495
    // protects _counter_map, _counter_child_map and _bucketing_counters
496
    mutable std::mutex _counter_map_lock;
497
498
    // Child profiles.  Does not own memory.
499
    // We record children in both a map (to facilitate updates) and a vector
500
    // (to print things in the order they were registered)
501
    using ChildMap = std::map<std::string, RuntimeProfile*>;
502
    ChildMap _child_map;
503
    // vector of (profile, indentation flag)
504
    using ChildVector = std::vector<std::pair<RuntimeProfile*, bool>>;
505
    ChildVector _children;
506
    mutable std::mutex _children_lock; // protects _child_map and _children
507
508
    using InfoStrings = std::map<std::string, std::string>;
509
    InfoStrings _info_strings;
510
511
    // Keeps track of the order in which InfoStrings are displayed when printed
512
    using InfoStringsDisplayOrder = std::vector<std::string>;
513
    InfoStringsDisplayOrder _info_strings_display_order;
514
515
    // Protects _info_strings and _info_strings_display_order
516
    mutable std::mutex _info_strings_lock;
517
518
    using EventSequenceMap = std::map<std::string, EventSequence*>;
519
    EventSequenceMap _event_sequence_map;
520
    mutable std::mutex _event_sequences_lock;
521
522
    Counter _counter_total_time;
523
    // Time spent in just in this profile (i.e. not the children) as a fraction
524
    // of the total time in the entire profile tree.
525
    double _local_time_percent;
526
527
    enum PeriodicCounterType {
528
        RATE_COUNTER = 0,
529
        SAMPLING_COUNTER,
530
    };
531
532
    struct RateCounterInfo {
533
        Counter* src_counter = nullptr;
534
        SampleFn sample_fn;
535
        int64_t elapsed_ms;
536
    };
537
538
    struct SamplingCounterInfo {
539
        Counter* src_counter = nullptr; // the counter to be sampled
540
        SampleFn sample_fn;
541
        int64_t total_sampled_value; // sum of all sampled values;
542
        int64_t num_sampled;         // number of samples taken
543
    };
544
545
    struct BucketCountersInfo {
546
        Counter* src_counter = nullptr; // the counter to be sampled
547
        int64_t num_sampled;            // number of samples taken
548
        // TODO: customize bucketing
549
    };
550
551
    // update a subtree of profiles from nodes, rooted at *idx.
552
    // On return, *idx points to the node immediately following this subtree.
553
    void update(const std::vector<TRuntimeProfileNode>& nodes, int* idx);
554
555
    // Helper function to compute compute the fraction of the total time spent in
556
    // this profile and its children.
557
    // Called recursively.
558
    void compute_time_in_profile(int64_t total_time);
559
560
    // Print the child counters of the given counter name
561
    static void print_child_counters(const std::string& prefix, const std::string& counter_name,
562
                                     const CounterMap& counter_map,
563
                                     const ChildCounterMap& child_counter_map, std::ostream* s);
564
565
0
    static std::string print_counter(Counter* counter) {
566
0
        return PrettyPrinter::print(counter->value(), counter->type());
567
0
    }
568
};
569
570
// Utility class to update the counter at object construction and destruction.
571
// When the object is constructed, decrement the counter by val.
572
// When the object goes out of scope, increment the counter by val.
573
class ScopedCounter {
574
public:
575
0
    ScopedCounter(RuntimeProfile::Counter* counter, int64_t val) : _val(val), _counter(counter) {
576
0
        if (counter == nullptr) {
577
0
            return;
578
0
        }
579
0
580
0
        _counter->update(-1L * _val);
581
0
    }
582
583
    // Increment the counter when object is destroyed
584
0
    ~ScopedCounter() {
585
0
        if (_counter != nullptr) {
586
0
            _counter->update(_val);
587
0
        }
588
0
    }
589
590
    // Disable copy constructor and assignment
591
    ScopedCounter(const ScopedCounter& counter) = delete;
592
    ScopedCounter& operator=(const ScopedCounter& counter) = delete;
593
594
private:
595
    int64_t _val;
596
    RuntimeProfile::Counter* _counter = nullptr;
597
};
598
599
// Utility class to update time elapsed when the object goes out of scope.
600
// 'T' must implement the stopWatch "interface" (start,stop,elapsed_time) but
601
// we use templates not to pay for virtual function overhead.
602
template <class T, typename Bool = bool>
603
class ScopedTimer {
604
public:
605
    ScopedTimer(RuntimeProfile::Counter* counter, const Bool* is_cancelled = nullptr)
606
526
            : _counter(counter), _is_cancelled(is_cancelled) {
607
526
        if (counter == nullptr) {
608
35
            return;
609
35
        }
610
491
        DCHECK_EQ(counter->type(), TUnit::TIME_NS);
611
491
        _sw.start();
612
491
    }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbEC2EPNS_14RuntimeProfile7CounterEPKb
Line
Count
Source
606
526
            : _counter(counter), _is_cancelled(is_cancelled) {
607
526
        if (counter == nullptr) {
608
35
            return;
609
35
        }
610
491
        DCHECK_EQ(counter->type(), TUnit::TIME_NS);
611
491
        _sw.start();
612
491
    }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbEC2EPNS_14RuntimeProfile7CounterEPKb
613
614
    void stop() { _sw.stop(); }
615
616
    void start() { _sw.start(); }
617
618
491
    bool is_cancelled() { return _is_cancelled != nullptr && *_is_cancelled; }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbE12is_cancelledEv
Line
Count
Source
618
491
    bool is_cancelled() { return _is_cancelled != nullptr && *_is_cancelled; }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbE12is_cancelledEv
619
620
491
    void UpdateCounter() {
621
491
        if (_counter != nullptr && !is_cancelled()) {
622
491
            _counter->update(_sw.elapsed_time());
623
491
        }
624
491
    }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbE13UpdateCounterEv
Line
Count
Source
620
491
    void UpdateCounter() {
621
491
        if (_counter != nullptr && !is_cancelled()) {
622
491
            _counter->update(_sw.elapsed_time());
623
491
        }
624
491
    }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbE13UpdateCounterEv
625
626
    // Update counter when object is destroyed
627
526
    ~ScopedTimer() {
628
526
        if (_counter == nullptr) {
629
35
            return;
630
35
        }
631
491
        _sw.stop();
632
491
        UpdateCounter();
633
491
    }
_ZN5doris11ScopedTimerINS_15CustomStopWatchILi1EEEbED2Ev
Line
Count
Source
627
526
    ~ScopedTimer() {
628
526
        if (_counter == nullptr) {
629
35
            return;
630
35
        }
631
491
        _sw.stop();
632
491
        UpdateCounter();
633
491
    }
Unexecuted instantiation: _ZN5doris11ScopedTimerINS_15CustomStopWatchILi3EEEbED2Ev
634
635
    // Disable copy constructor and assignment
636
    ScopedTimer(const ScopedTimer& timer) = delete;
637
    ScopedTimer& operator=(const ScopedTimer& timer) = delete;
638
639
private:
640
    T _sw;
641
    RuntimeProfile::Counter* _counter = nullptr;
642
    const Bool* _is_cancelled = nullptr;
643
};
644
645
// Utility class to update time elapsed when the object goes out of scope.
646
// 'T' must implement the stopWatch "interface" (start,stop,elapsed_time) but
647
// we use templates not to pay for virtual function overhead.
648
template <class T, class C>
649
class ScopedRawTimer {
650
public:
651
123k
    ScopedRawTimer(C* counter) : _counter(counter) { _sw.start(); }
_ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEElEC2EPl
Line
Count
Source
651
123k
    ScopedRawTimer(C* counter) : _counter(counter) { _sw.start(); }
Unexecuted instantiation: _ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEESt6atomicIlEEC2EPS4_
652
    // Update counter when object is destroyed
653
123k
    ~ScopedRawTimer() { *_counter += _sw.elapsed_time(); }
_ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEElED2Ev
Line
Count
Source
653
123k
    ~ScopedRawTimer() { *_counter += _sw.elapsed_time(); }
Unexecuted instantiation: _ZN5doris14ScopedRawTimerINS_15CustomStopWatchILi1EEESt6atomicIlEED2Ev
654
655
    // Disable copy constructor and assignment
656
    ScopedRawTimer(const ScopedRawTimer& timer) = delete;
657
    ScopedRawTimer& operator=(const ScopedRawTimer& timer) = delete;
658
659
private:
660
    T _sw;
661
    C* _counter = nullptr;
662
};
663
664
} // namespace doris