Coverage Report

Created: 2025-04-30 15:20

/root/doris/be/src/util/runtime_profile.cpp
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.cc
19
// and modified by Doris
20
21
#include "util/runtime_profile.h"
22
23
#include <gen_cpp/RuntimeProfile_types.h>
24
#include <rapidjson/encodings.h>
25
#include <rapidjson/stringbuffer.h>
26
#include <rapidjson/writer.h>
27
28
#include <algorithm>
29
#include <iomanip>
30
#include <iostream>
31
#include <memory>
32
#include <string>
33
34
#include "common/logging.h"
35
#include "common/object_pool.h"
36
#include "util/container_util.hpp"
37
#include "util/runtime_profile_counter_tree_node.h"
38
#ifdef BE_TEST
39
#include "common/status.h" // For ErrorCode
40
#endif
41
42
namespace doris {
43
44
// Thread counters name
45
static const std::string THREAD_TOTAL_TIME = "TotalWallClockTime";
46
static const std::string THREAD_VOLUNTARY_CONTEXT_SWITCHES = "VoluntaryContextSwitches";
47
static const std::string THREAD_INVOLUNTARY_CONTEXT_SWITCHES = "InvoluntaryContextSwitches";
48
49
const std::string RuntimeProfile::ROOT_COUNTER;
50
51
6
std::unique_ptr<RuntimeProfile> RuntimeProfile::from_thrift(const TRuntimeProfileTree& node) {
52
6
    if (node.nodes.empty()) {
53
0
        return std::make_unique<RuntimeProfile>("");
54
0
    }
55
6
    TRuntimeProfileNode root_node = node.nodes.front();
56
6
    std::unique_ptr<RuntimeProfile> res = std::make_unique<RuntimeProfile>(root_node.name);
57
6
    res->update(node);
58
6
    return res;
59
6
}
60
61
RuntimeProfile::RuntimeProfile(const std::string& name, bool is_averaged_profile)
62
        : _pool(new ObjectPool()),
63
          _name(name),
64
          _metadata(-1),
65
          _timestamp(-1),
66
          _is_averaged_profile(is_averaged_profile),
67
          _counter_total_time(TUnit::TIME_NS, 0, 3),
68
1.51M
          _local_time_percent(0) {
69
    // TotalTime counter has level3 to disable it from plan profile, because
70
    // it contains its child running time, we use exec time instead.
71
1.51M
    _counter_map["TotalTime"] = &_counter_total_time;
72
1.51M
}
73
74
1.51M
RuntimeProfile::~RuntimeProfile() = default;
75
76
4
bool RuntimeProfile::Counter::operator==(const Counter& other) const {
77
4
    return _value.load(std::memory_order_relaxed) == other._value.load(std::memory_order_relaxed) &&
78
4
           _type == other._type && _level == other._level;
79
4
}
80
81
24
void RuntimeProfile::merge(RuntimeProfile* other) {
82
24
    DCHECK(other != nullptr);
83
84
    // Merge this level
85
24
    {
86
24
        CounterMap::iterator dst_iter;
87
24
        CounterMap::const_iterator src_iter;
88
24
        std::lock_guard<std::mutex> l(_counter_map_lock);
89
24
        std::lock_guard<std::mutex> m(other->_counter_map_lock);
90
91
68
        for (src_iter = other->_counter_map.begin(); src_iter != other->_counter_map.end();
92
44
             ++src_iter) {
93
44
            dst_iter = _counter_map.find(src_iter->first);
94
95
44
            if (dst_iter == _counter_map.end()) {
96
8
                _counter_map[src_iter->first] = _pool->add(src_iter->second->clone());
97
36
            } else {
98
36
                DCHECK(dst_iter->second->type() == src_iter->second->type());
99
100
36
                if (dst_iter->second->type() == TUnit::DOUBLE_VALUE) {
101
0
                    double new_val =
102
0
                            dst_iter->second->double_value() + src_iter->second->double_value();
103
0
                    dst_iter->second->set(new_val);
104
36
                } else {
105
36
                    dst_iter->second->update(src_iter->second->value());
106
36
                }
107
36
            }
108
44
        }
109
110
24
        ChildCounterMap::const_iterator child_counter_src_itr;
111
112
24
        for (child_counter_src_itr = other->_child_counter_map.begin();
113
42
             child_counter_src_itr != other->_child_counter_map.end(); ++child_counter_src_itr) {
114
18
            std::set<std::string>* child_counters = find_or_insert(
115
18
                    &_child_counter_map, child_counter_src_itr->first, std::set<std::string>());
116
18
            child_counters->insert(child_counter_src_itr->second.begin(),
117
18
                                   child_counter_src_itr->second.end());
118
18
        }
119
24
    }
120
121
24
    {
122
24
        std::lock_guard<std::mutex> l(_children_lock);
123
24
        std::lock_guard<std::mutex> m(other->_children_lock);
124
125
        // Recursively merge children with matching names
126
40
        for (int i = 0; i < other->_children.size(); ++i) {
127
16
            RuntimeProfile* other_child = other->_children[i].first;
128
16
            ChildMap::iterator j = _child_map.find(other_child->_name);
129
16
            RuntimeProfile* child = nullptr;
130
131
16
            if (j != _child_map.end()) {
132
10
                child = j->second;
133
10
            } else {
134
6
                child = _pool->add(new RuntimeProfile(other_child->_name));
135
6
                child->_local_time_percent = other_child->_local_time_percent;
136
6
                child->_metadata = other_child->_metadata;
137
6
                child->_timestamp = other_child->_timestamp;
138
6
                bool indent_other_child = other->_children[i].second;
139
6
                _child_map[child->_name] = child;
140
6
                _children.push_back(std::make_pair(child, indent_other_child));
141
6
            }
142
143
16
            child->merge(other_child);
144
16
        }
145
24
    }
146
24
}
147
148
18
void RuntimeProfile::update(const TRuntimeProfileTree& thrift_profile) {
149
18
    int idx = 0;
150
18
    update(thrift_profile.nodes, &idx);
151
18
    DCHECK_EQ(idx, thrift_profile.nodes.size());
152
18
}
153
154
42
void RuntimeProfile::update(const std::vector<TRuntimeProfileNode>& nodes, int* idx) {
155
42
    DCHECK_LT(*idx, nodes.size());
156
42
    const TRuntimeProfileNode& node = nodes[*idx];
157
42
    {
158
42
        std::lock_guard<std::mutex> l(_counter_map_lock);
159
        // update this level
160
42
        std::map<std::string, Counter*>::iterator dst_iter;
161
162
82
        for (int i = 0; i < node.counters.size(); ++i) {
163
40
            const TCounter& tcounter = node.counters[i];
164
40
            CounterMap::iterator j = _counter_map.find(tcounter.name);
165
166
40
            if (j == _counter_map.end()) {
167
28
                _counter_map[tcounter.name] =
168
28
                        _pool->add(new Counter(tcounter.type, tcounter.value));
169
28
            } else {
170
12
                if (j->second->type() != tcounter.type) {
171
0
                    LOG(ERROR) << "Cannot update counters with the same name (" << j->first
172
0
                               << ") but different types.";
173
12
                } else {
174
12
                    j->second->set(tcounter.value);
175
12
                }
176
12
            }
177
40
        }
178
179
42
        ChildCounterMap::const_iterator child_counter_src_itr;
180
181
42
        for (child_counter_src_itr = node.child_counters_map.begin();
182
70
             child_counter_src_itr != node.child_counters_map.end(); ++child_counter_src_itr) {
183
28
            std::set<std::string>* child_counters = find_or_insert(
184
28
                    &_child_counter_map, child_counter_src_itr->first, std::set<std::string>());
185
28
            child_counters->insert(child_counter_src_itr->second.begin(),
186
28
                                   child_counter_src_itr->second.end());
187
28
        }
188
42
    }
189
190
42
    {
191
42
        std::lock_guard<std::mutex> l(_info_strings_lock);
192
42
        const InfoStrings& info_strings = node.info_strings;
193
42
        for (const std::string& key : node.info_strings_display_order) {
194
            // Look for existing info strings and update in place. If there
195
            // are new strings, add them to the end of the display order.
196
            // TODO: Is nodes.info_strings always a superset of
197
            // _info_strings? If so, can just copy the display order.
198
8
            InfoStrings::const_iterator it = info_strings.find(key);
199
8
            DCHECK(it != info_strings.end());
200
8
            InfoStrings::iterator existing = _info_strings.find(key);
201
202
8
            if (existing == _info_strings.end()) {
203
6
                _info_strings.insert(std::make_pair(key, it->second));
204
6
                _info_strings_display_order.push_back(key);
205
6
            } else {
206
2
                _info_strings[key] = it->second;
207
2
            }
208
8
        }
209
42
    }
210
211
42
    ++*idx;
212
42
    {
213
42
        std::lock_guard<std::mutex> l(_children_lock);
214
215
        // update children with matching names; create new ones if they don't match
216
66
        for (int i = 0; i < node.num_children; ++i) {
217
24
            const TRuntimeProfileNode& tchild = nodes[*idx];
218
24
            ChildMap::iterator j = _child_map.find(tchild.name);
219
24
            RuntimeProfile* child = nullptr;
220
221
24
            if (j != _child_map.end()) {
222
10
                child = j->second;
223
14
            } else {
224
14
                child = _pool->add(new RuntimeProfile(tchild.name));
225
14
                child->_metadata = tchild.metadata;
226
14
                child->_timestamp = tchild.timestamp;
227
14
                _child_map[tchild.name] = child;
228
14
                _children.push_back(std::make_pair(child, tchild.indent));
229
14
            }
230
231
24
            child->update(nodes, idx);
232
24
        }
233
42
    }
234
42
}
235
236
0
void RuntimeProfile::divide(int n) {
237
0
    DCHECK_GT(n, 0);
238
0
    std::map<std::string, Counter*>::iterator iter;
239
0
    {
240
0
        std::lock_guard<std::mutex> l(_counter_map_lock);
241
242
0
        for (iter = _counter_map.begin(); iter != _counter_map.end(); ++iter) {
243
0
            if (iter->second->type() == TUnit::DOUBLE_VALUE) {
244
0
                iter->second->set(iter->second->double_value() / n);
245
0
            } else {
246
0
                int64_t value = iter->second->_value.load();
247
0
                value = value / n;
248
0
                iter->second->_value.store(value);
249
0
            }
250
0
        }
251
0
    }
252
0
    {
253
0
        std::lock_guard<std::mutex> l(_children_lock);
254
255
0
        for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
256
0
            i->second->divide(n);
257
0
        }
258
0
    }
259
0
}
260
261
0
void RuntimeProfile::clear_children() {
262
0
    std::lock_guard<std::mutex> l(_children_lock);
263
0
    _children.clear();
264
0
}
265
266
0
void RuntimeProfile::compute_time_in_profile() {
267
0
    compute_time_in_profile(total_time_counter()->value());
268
0
}
269
270
0
void RuntimeProfile::compute_time_in_profile(int64_t total) {
271
0
    if (total == 0) {
272
0
        return;
273
0
    }
274
275
    // Add all the total times in all the children
276
0
    int64_t total_child_time = 0;
277
0
    std::lock_guard<std::mutex> l(_children_lock);
278
279
0
    for (int i = 0; i < _children.size(); ++i) {
280
0
        total_child_time += _children[i].first->total_time_counter()->value();
281
0
    }
282
283
0
    int64_t local_time = total_time_counter()->value() - total_child_time;
284
    // Counters have some margin, set to 0 if it was negative.
285
0
    local_time = std::max<int64_t>(0L, local_time);
286
0
    _local_time_percent = static_cast<double>(local_time) / total;
287
0
    _local_time_percent = std::min(1.0, _local_time_percent) * 100;
288
289
    // Recurse on children
290
0
    for (int i = 0; i < _children.size(); ++i) {
291
0
        _children[i].first->compute_time_in_profile(total);
292
0
    }
293
0
}
294
295
230
RuntimeProfile* RuntimeProfile::create_child(const std::string& name, bool indent, bool prepend) {
296
230
    std::lock_guard<std::mutex> l(_children_lock);
297
230
    DCHECK(_child_map.find(name) == _child_map.end()) << ", name: " << name;
298
230
    RuntimeProfile* child = _pool->add(new RuntimeProfile(name));
299
230
    if (this->is_set_metadata()) {
300
0
        child->set_metadata(this->metadata());
301
0
    }
302
303
230
    if (_children.empty()) {
304
154
        add_child_unlock(child, indent, nullptr);
305
154
    } else {
306
76
        auto* pos = prepend ? _children.begin()->first : nullptr;
307
76
        add_child_unlock(child, indent, pos);
308
76
    }
309
230
    return child;
310
230
}
311
312
209k
void RuntimeProfile::add_child_unlock(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
313
209k
    DCHECK(child != nullptr);
314
209k
    _child_map[child->_name] = child;
315
316
209k
    if (loc == nullptr) {
317
209k
        _children.push_back(std::make_pair(child, indent));
318
209k
    } else {
319
66
        for (ChildVector::iterator it = _children.begin(); it != _children.end(); ++it) {
320
66
            if (it->first == loc) {
321
66
                _children.insert(it, std::make_pair(child, indent));
322
66
                return;
323
66
            }
324
66
        }
325
0
        DCHECK(false) << "Invalid loc";
326
0
    }
327
209k
}
328
329
209k
void RuntimeProfile::add_child(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
330
209k
    std::lock_guard<std::mutex> l(_children_lock);
331
209k
    add_child_unlock(child, indent, loc);
332
209k
}
333
334
4
void RuntimeProfile::get_children(std::vector<RuntimeProfile*>* children) {
335
4
    children->clear();
336
4
    std::lock_guard<std::mutex> l(_children_lock);
337
338
16
    for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
339
12
        children->push_back(i->second);
340
12
    }
341
4
}
342
343
0
void RuntimeProfile::get_all_children(std::vector<RuntimeProfile*>* children) {
344
0
    std::lock_guard<std::mutex> l(_children_lock);
345
346
0
    for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
347
0
        children->push_back(i->second);
348
0
        i->second->get_all_children(children);
349
0
    }
350
0
}
351
352
625k
void RuntimeProfile::add_info_string(const std::string& key, const std::string& value) {
353
625k
    std::lock_guard<std::mutex> l(_info_strings_lock);
354
625k
    InfoStrings::iterator it = _info_strings.find(key);
355
356
625k
    if (it == _info_strings.end()) {
357
625k
        _info_strings.insert(std::make_pair(key, value));
358
625k
        _info_strings_display_order.push_back(key);
359
625k
    } else {
360
330
        it->second = value;
361
330
    }
362
625k
}
363
364
32
const std::string* RuntimeProfile::get_info_string(const std::string& key) {
365
32
    std::lock_guard<std::mutex> l(_info_strings_lock);
366
32
    InfoStrings::const_iterator it = _info_strings.find(key);
367
368
32
    if (it == _info_strings.end()) {
369
2
        return nullptr;
370
2
    }
371
372
30
    return &it->second;
373
32
}
374
375
RuntimeProfile::HighWaterMarkCounter* RuntimeProfile::AddHighWaterMarkCounter(
376
        const std::string& name, TUnit::type unit, const std::string& parent_counter_name,
377
261k
        int64_t level) {
378
261k
    DCHECK_EQ(_is_averaged_profile, false);
379
261k
    std::lock_guard<std::mutex> l(_counter_map_lock);
380
261k
    if (_counter_map.find(name) != _counter_map.end()) {
381
102
        return reinterpret_cast<RuntimeProfile::HighWaterMarkCounter*>(_counter_map[name]);
382
102
    }
383
261k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
384
261k
           _counter_map.find(parent_counter_name) != _counter_map.end());
385
261k
    RuntimeProfile::HighWaterMarkCounter* counter =
386
261k
            _pool->add(new RuntimeProfile::HighWaterMarkCounter(unit, level, parent_counter_name));
387
261k
    _counter_map[name] = counter;
388
261k
    std::set<std::string>* child_counters =
389
261k
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
390
261k
    child_counters->insert(name);
391
261k
    return counter;
392
261k
}
393
394
RuntimeProfile::Counter* RuntimeProfile::add_counter(const std::string& name, TUnit::type type,
395
                                                     const std::string& parent_counter_name,
396
6.20M
                                                     int64_t level) {
397
6.20M
    std::lock_guard<std::mutex> l(_counter_map_lock);
398
399
6.20M
    if (_counter_map.find(name) != _counter_map.end()) {
400
3.14k
        return _counter_map[name];
401
3.14k
    }
402
403
    // Parent counter must already exist.
404
6.19M
    DCHECK(parent_counter_name == ROOT_COUNTER ||
405
6.19M
           _counter_map.find(parent_counter_name) != _counter_map.end());
406
407
6.19M
    Counter* counter = _pool->add(new Counter(type, 0, level));
408
6.19M
    _counter_map[name] = counter;
409
6.19M
    std::set<std::string>* child_counters =
410
6.19M
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
411
6.19M
    child_counters->insert(name);
412
6.19M
    return counter;
413
6.20M
}
414
415
RuntimeProfile::NonZeroCounter* RuntimeProfile::add_nonzero_counter(
416
        const std::string& name, TUnit::type type, const std::string& parent_counter_name,
417
64
        int64_t level) {
418
64
    std::lock_guard<std::mutex> l(_counter_map_lock);
419
64
    if (_counter_map.find(name) != _counter_map.end()) {
420
0
        DCHECK(dynamic_cast<NonZeroCounter*>(_counter_map[name]));
421
0
        return static_cast<NonZeroCounter*>(_counter_map[name]);
422
0
    }
423
424
64
    DCHECK(parent_counter_name == ROOT_COUNTER ||
425
64
           _counter_map.find(parent_counter_name) != _counter_map.end());
426
64
    NonZeroCounter* counter = _pool->add(new NonZeroCounter(type, level, parent_counter_name));
427
64
    _counter_map[name] = counter;
428
64
    std::set<std::string>* child_counters =
429
64
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
430
64
    child_counters->insert(name);
431
64
    return counter;
432
64
}
433
434
RuntimeProfile::DerivedCounter* RuntimeProfile::add_derived_counter(
435
        const std::string& name, TUnit::type type, const DerivedCounterFunction& counter_fn,
436
16
        const std::string& parent_counter_name) {
437
16
    std::lock_guard<std::mutex> l(_counter_map_lock);
438
439
16
    if (_counter_map.find(name) != _counter_map.end()) {
440
0
        return nullptr;
441
0
    }
442
443
16
    DerivedCounter* counter = _pool->add(new DerivedCounter(type, counter_fn));
444
16
    _counter_map[name] = counter;
445
16
    std::set<std::string>* child_counters =
446
16
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
447
16
    child_counters->insert(name);
448
16
    return counter;
449
16
}
450
451
void RuntimeProfile::add_description(const std::string& name, const std::string& description,
452
156k
                                     std::string parent_counter_name) {
453
156k
    std::lock_guard<std::mutex> l(_counter_map_lock);
454
455
156k
    if (_counter_map.find(name) != _counter_map.end()) {
456
0
        Counter* counter = _counter_map[name];
457
0
        if (dynamic_cast<DescriptionEntry*>(counter) != nullptr) {
458
            // Do replace instead of update to avoid data race.
459
0
            _counter_map.erase(name);
460
0
        } else {
461
0
            DCHECK(false) << "Counter type mismatch, name: " << name
462
0
                          << ", type: " << counter->type() << ", description: " << description;
463
0
        }
464
0
    }
465
466
    // Parent counter must already exist.
467
156k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
468
156k
           _counter_map.find(parent_counter_name) != _counter_map.end());
469
156k
    DescriptionEntry* counter = _pool->add(new DescriptionEntry(name, description));
470
156k
    _counter_map[name] = counter;
471
156k
    std::set<std::string>* child_counters =
472
156k
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
473
156k
    child_counters->insert(name);
474
156k
}
475
476
RuntimeProfile::ConditionCounter* RuntimeProfile::add_conditition_counter(
477
        const std::string& name, TUnit::type type, const ConditionCounterFunction& counter_fn,
478
0
        const std::string& parent_counter_name, int64_t level) {
479
0
    std::lock_guard<std::mutex> l(_counter_map_lock);
480
481
0
    if (_counter_map.find(name) != _counter_map.end()) {
482
0
        RuntimeProfile::ConditionCounter* contition_counter =
483
0
                dynamic_cast<ConditionCounter*>(_counter_map[name]);
484
0
        if (contition_counter == nullptr) {
485
0
            throw doris::Exception(doris::ErrorCode::INTERNAL_ERROR,
486
0
                                   "Failed to add a conditition counter that is duplicate and of a "
487
0
                                   "different type for {}.",
488
0
                                   name);
489
0
        }
490
0
        return contition_counter;
491
0
    }
492
493
0
    ConditionCounter* counter = _pool->add(new ConditionCounter(type, counter_fn, level));
494
0
    _counter_map[name] = counter;
495
0
    std::set<std::string>* child_counters =
496
0
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
497
0
    child_counters->insert(name);
498
0
    return counter;
499
0
}
500
501
6.50k
RuntimeProfile::Counter* RuntimeProfile::get_counter(const std::string& name) {
502
6.50k
    std::lock_guard<std::mutex> l(_counter_map_lock);
503
504
6.50k
    if (_counter_map.find(name) != _counter_map.end()) {
505
5.62k
        return _counter_map[name];
506
5.62k
    }
507
508
880
    return nullptr;
509
6.50k
}
510
511
0
void RuntimeProfile::get_counters(const std::string& name, std::vector<Counter*>* counters) {
512
0
    Counter* c = get_counter(name);
513
514
0
    if (c != nullptr) {
515
0
        counters->push_back(c);
516
0
    }
517
518
0
    std::lock_guard<std::mutex> l(_children_lock);
519
520
0
    for (int i = 0; i < _children.size(); ++i) {
521
0
        _children[i].first->get_counters(name, counters);
522
0
    }
523
0
}
524
525
// Print the profile:
526
//  1. Profile Name
527
//  2. Info Strings
528
//  3. Counters
529
//  4. Children
530
void RuntimeProfile::pretty_print(std::ostream* s, const std::string& prefix,
531
130
                                  int64_t profile_level) const {
532
130
    std::ostream& stream = *s;
533
534
    // create copy of _counter_map and _child_counter_map so we don't need to hold lock
535
    // while we call value() on the counters
536
130
    CounterMap counter_map;
537
130
    ChildCounterMap child_counter_map;
538
130
    {
539
130
        std::lock_guard<std::mutex> l(_counter_map_lock);
540
130
        counter_map = _counter_map;
541
130
        child_counter_map = _child_counter_map;
542
130
    }
543
544
130
    std::map<std::string, Counter*>::const_iterator total_time = counter_map.find("TotalTime");
545
130
    DCHECK(total_time != counter_map.end());
546
547
130
    stream.flags(std::ios::fixed);
548
130
    stream << prefix << _name << ":";
549
550
130
    if (total_time->second->value() != 0) {
551
0
        stream << "(Active: "
552
0
               << PrettyPrinter::print(total_time->second->value(), total_time->second->type())
553
0
               << ", non-child: " << std::setprecision(2) << _local_time_percent << "%)";
554
0
    }
555
556
130
    stream << std::endl;
557
558
130
    {
559
130
        std::lock_guard<std::mutex> l(_info_strings_lock);
560
130
        for (const std::string& key : _info_strings_display_order) {
561
4
            stream << prefix << "   - " << key << ": " << _info_strings.find(key)->second
562
4
                   << std::endl;
563
4
        }
564
130
    }
565
566
130
    RuntimeProfile::print_child_counters(prefix, ROOT_COUNTER, counter_map, child_counter_map, s);
567
568
    // create copy of _children so we don't need to hold lock while we call
569
    // pretty_print() on the children
570
130
    ChildVector children;
571
130
    {
572
130
        std::lock_guard<std::mutex> l(_children_lock);
573
130
        children = _children;
574
130
    }
575
576
210
    for (int i = 0; i < children.size(); ++i) {
577
80
        RuntimeProfile* profile = children[i].first;
578
80
        bool indent = children[i].second;
579
80
        profile->pretty_print(s, prefix + (indent ? "  " : ""));
580
80
    }
581
130
}
582
583
470
void RuntimeProfile::to_thrift(TRuntimeProfileTree* tree, int64 profile_level) {
584
470
    tree->nodes.clear();
585
470
    to_thrift(&tree->nodes, profile_level);
586
470
}
587
588
486
void RuntimeProfile::to_thrift(std::vector<TRuntimeProfileNode>* nodes, int64 profile_level) {
589
486
    int index = nodes->size();
590
486
    nodes->push_back(TRuntimeProfileNode());
591
486
    TRuntimeProfileNode& node = (*nodes)[index];
592
486
    node.name = _name;
593
486
    node.metadata = _metadata;
594
486
    node.timestamp = _timestamp;
595
486
    node.indent = true;
596
597
486
    {
598
486
        std::lock_guard<std::mutex> l(_counter_map_lock);
599
486
        RuntimeProfileCounterTreeNode conter_tree = RuntimeProfileCounterTreeNode::from_map(
600
486
                _counter_map, _child_counter_map, ROOT_COUNTER);
601
486
        conter_tree = RuntimeProfileCounterTreeNode::prune_the_tree(conter_tree, profile_level);
602
486
        conter_tree.to_thrift(node.counters, node.child_counters_map);
603
486
    }
604
605
486
    {
606
486
        std::lock_guard<std::mutex> l(_info_strings_lock);
607
486
        node.info_strings = _info_strings;
608
486
        node.info_strings_display_order = _info_strings_display_order;
609
486
    }
610
611
486
    ChildVector children;
612
486
    {
613
        // _children may be modified during to_thrift(),
614
        // so we have to lock and copy _children to avoid race condition
615
486
        std::lock_guard<std::mutex> l(_children_lock);
616
486
        children = _children;
617
486
    }
618
486
    node.num_children = children.size();
619
486
    nodes->reserve(nodes->size() + children.size());
620
621
498
    for (int i = 0; i < children.size(); ++i) {
622
12
        int child_idx = nodes->size();
623
12
        children[i].first->to_thrift(nodes, profile_level);
624
        // fix up indentation flag
625
12
        (*nodes)[child_idx].indent = children[i].second;
626
12
    }
627
486
}
628
629
int64_t RuntimeProfile::units_per_second(const RuntimeProfile::Counter* total_counter,
630
6
                                         const RuntimeProfile::Counter* timer) {
631
6
    DCHECK(total_counter->type() == TUnit::BYTES || total_counter->type() == TUnit::UNIT);
632
6
    DCHECK(timer->type() == TUnit::TIME_NS);
633
634
6
    if (timer->value() == 0) {
635
0
        return 0;
636
0
    }
637
638
6
    double secs = static_cast<double>(timer->value()) / 1000.0 / 1000.0 / 1000.0;
639
6
    return int64_t(total_counter->value() / secs);
640
6
}
641
642
0
int64_t RuntimeProfile::counter_sum(const std::vector<Counter*>* counters) {
643
0
    int64_t value = 0;
644
645
0
    for (int i = 0; i < counters->size(); ++i) {
646
0
        value += (*counters)[i]->value();
647
0
    }
648
649
0
    return value;
650
0
}
651
652
void RuntimeProfile::print_child_counters(const std::string& prefix,
653
                                          const std::string& counter_name,
654
                                          const CounterMap& counter_map,
655
                                          const ChildCounterMap& child_counter_map,
656
890
                                          std::ostream* s) {
657
890
    auto itr = child_counter_map.find(counter_name);
658
659
890
    if (itr != child_counter_map.end()) {
660
108
        const std::set<std::string>& child_counters = itr->second;
661
760
        for (const std::string& child_counter : child_counters) {
662
760
            auto iter = counter_map.find(child_counter);
663
760
            DCHECK(iter != counter_map.end());
664
760
            iter->second->pretty_print(s, prefix, iter->first);
665
760
            RuntimeProfile::print_child_counters(prefix + "  ", child_counter, counter_map,
666
760
                                                 child_counter_map, s);
667
760
        }
668
108
    }
669
890
}
670
671
} // namespace doris