Coverage Report

Created: 2025-04-11 14:35

/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
3
std::unique_ptr<RuntimeProfile> RuntimeProfile::from_thrift(const TRuntimeProfileTree& node) {
52
3
    if (node.nodes.empty()) {
53
0
        return std::make_unique<RuntimeProfile>("");
54
0
    }
55
3
    TRuntimeProfileNode root_node = node.nodes.front();
56
3
    std::unique_ptr<RuntimeProfile> res = std::make_unique<RuntimeProfile>(root_node.name);
57
3
    res->update(node);
58
3
    return res;
59
3
}
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
28.3k
          _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
28.3k
    _counter_map["TotalTime"] = &_counter_total_time;
72
28.3k
}
73
74
28.2k
RuntimeProfile::~RuntimeProfile() = default;
75
76
2
bool RuntimeProfile::Counter::operator==(const Counter& other) const {
77
2
    return _value.load(std::memory_order_relaxed) == other._value.load(std::memory_order_relaxed) &&
78
2
           _type == other._type && _level == other._level;
79
2
}
80
81
12
void RuntimeProfile::merge(RuntimeProfile* other) {
82
12
    DCHECK(other != nullptr);
83
84
    // Merge this level
85
12
    {
86
12
        CounterMap::iterator dst_iter;
87
12
        CounterMap::const_iterator src_iter;
88
12
        std::lock_guard<std::mutex> l(_counter_map_lock);
89
12
        std::lock_guard<std::mutex> m(other->_counter_map_lock);
90
91
34
        for (src_iter = other->_counter_map.begin(); src_iter != other->_counter_map.end();
92
22
             ++src_iter) {
93
22
            dst_iter = _counter_map.find(src_iter->first);
94
95
22
            if (dst_iter == _counter_map.end()) {
96
4
                _counter_map[src_iter->first] = _pool->add(src_iter->second->clone());
97
18
            } else {
98
18
                DCHECK(dst_iter->second->type() == src_iter->second->type());
99
100
18
                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
18
                } else {
105
18
                    dst_iter->second->update(src_iter->second->value());
106
18
                }
107
18
            }
108
22
        }
109
110
12
        ChildCounterMap::const_iterator child_counter_src_itr;
111
112
12
        for (child_counter_src_itr = other->_child_counter_map.begin();
113
21
             child_counter_src_itr != other->_child_counter_map.end(); ++child_counter_src_itr) {
114
9
            std::set<std::string>* child_counters = find_or_insert(
115
9
                    &_child_counter_map, child_counter_src_itr->first, std::set<std::string>());
116
9
            child_counters->insert(child_counter_src_itr->second.begin(),
117
9
                                   child_counter_src_itr->second.end());
118
9
        }
119
12
    }
120
121
12
    {
122
12
        std::lock_guard<std::mutex> l(_children_lock);
123
12
        std::lock_guard<std::mutex> m(other->_children_lock);
124
125
        // Recursively merge children with matching names
126
20
        for (int i = 0; i < other->_children.size(); ++i) {
127
8
            RuntimeProfile* other_child = other->_children[i].first;
128
8
            ChildMap::iterator j = _child_map.find(other_child->_name);
129
8
            RuntimeProfile* child = nullptr;
130
131
8
            if (j != _child_map.end()) {
132
5
                child = j->second;
133
5
            } else {
134
3
                child = _pool->add(new RuntimeProfile(other_child->_name));
135
3
                child->_local_time_percent = other_child->_local_time_percent;
136
3
                child->_metadata = other_child->_metadata;
137
3
                child->_timestamp = other_child->_timestamp;
138
3
                bool indent_other_child = other->_children[i].second;
139
3
                _child_map[child->_name] = child;
140
3
                _children.push_back(std::make_pair(child, indent_other_child));
141
3
            }
142
143
8
            child->merge(other_child);
144
8
        }
145
12
    }
146
12
}
147
148
9
void RuntimeProfile::update(const TRuntimeProfileTree& thrift_profile) {
149
9
    int idx = 0;
150
9
    update(thrift_profile.nodes, &idx);
151
9
    DCHECK_EQ(idx, thrift_profile.nodes.size());
152
9
}
153
154
21
void RuntimeProfile::update(const std::vector<TRuntimeProfileNode>& nodes, int* idx) {
155
21
    DCHECK_LT(*idx, nodes.size());
156
21
    const TRuntimeProfileNode& node = nodes[*idx];
157
21
    {
158
21
        std::lock_guard<std::mutex> l(_counter_map_lock);
159
        // update this level
160
21
        std::map<std::string, Counter*>::iterator dst_iter;
161
162
41
        for (int i = 0; i < node.counters.size(); ++i) {
163
20
            const TCounter& tcounter = node.counters[i];
164
20
            CounterMap::iterator j = _counter_map.find(tcounter.name);
165
166
20
            if (j == _counter_map.end()) {
167
14
                _counter_map[tcounter.name] =
168
14
                        _pool->add(new Counter(tcounter.type, tcounter.value));
169
14
            } else {
170
6
                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
6
                } else {
174
6
                    j->second->set(tcounter.value);
175
6
                }
176
6
            }
177
20
        }
178
179
21
        ChildCounterMap::const_iterator child_counter_src_itr;
180
181
21
        for (child_counter_src_itr = node.child_counters_map.begin();
182
35
             child_counter_src_itr != node.child_counters_map.end(); ++child_counter_src_itr) {
183
14
            std::set<std::string>* child_counters = find_or_insert(
184
14
                    &_child_counter_map, child_counter_src_itr->first, std::set<std::string>());
185
14
            child_counters->insert(child_counter_src_itr->second.begin(),
186
14
                                   child_counter_src_itr->second.end());
187
14
        }
188
21
    }
189
190
21
    {
191
21
        std::lock_guard<std::mutex> l(_info_strings_lock);
192
21
        const InfoStrings& info_strings = node.info_strings;
193
21
        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
4
            InfoStrings::const_iterator it = info_strings.find(key);
199
4
            DCHECK(it != info_strings.end());
200
4
            InfoStrings::iterator existing = _info_strings.find(key);
201
202
4
            if (existing == _info_strings.end()) {
203
3
                _info_strings.insert(std::make_pair(key, it->second));
204
3
                _info_strings_display_order.push_back(key);
205
3
            } else {
206
1
                _info_strings[key] = it->second;
207
1
            }
208
4
        }
209
21
    }
210
211
21
    ++*idx;
212
21
    {
213
21
        std::lock_guard<std::mutex> l(_children_lock);
214
215
        // update children with matching names; create new ones if they don't match
216
33
        for (int i = 0; i < node.num_children; ++i) {
217
12
            const TRuntimeProfileNode& tchild = nodes[*idx];
218
12
            ChildMap::iterator j = _child_map.find(tchild.name);
219
12
            RuntimeProfile* child = nullptr;
220
221
12
            if (j != _child_map.end()) {
222
5
                child = j->second;
223
7
            } else {
224
7
                child = _pool->add(new RuntimeProfile(tchild.name));
225
7
                child->_metadata = tchild.metadata;
226
7
                child->_timestamp = tchild.timestamp;
227
7
                _child_map[tchild.name] = child;
228
7
                _children.push_back(std::make_pair(child, tchild.indent));
229
7
            }
230
231
12
            child->update(nodes, idx);
232
12
        }
233
21
    }
234
21
}
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
110
RuntimeProfile* RuntimeProfile::create_child(const std::string& name, bool indent, bool prepend) {
296
110
    std::lock_guard<std::mutex> l(_children_lock);
297
110
    DCHECK(_child_map.find(name) == _child_map.end()) << ", name: " << name;
298
110
    RuntimeProfile* child = _pool->add(new RuntimeProfile(name));
299
110
    if (this->is_set_metadata()) {
300
0
        child->set_metadata(this->metadata());
301
0
    }
302
303
110
    if (_children.empty()) {
304
74
        add_child_unlock(child, indent, nullptr);
305
74
    } else {
306
36
        auto* pos = prepend ? _children.begin()->first : nullptr;
307
36
        add_child_unlock(child, indent, pos);
308
36
    }
309
110
    return child;
310
110
}
311
312
757
void RuntimeProfile::add_child_unlock(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
313
757
    DCHECK(child != nullptr);
314
757
    _child_map[child->_name] = child;
315
316
757
    if (loc == nullptr) {
317
726
        _children.push_back(std::make_pair(child, indent));
318
726
    } else {
319
31
        for (ChildVector::iterator it = _children.begin(); it != _children.end(); ++it) {
320
31
            if (it->first == loc) {
321
31
                _children.insert(it, std::make_pair(child, indent));
322
31
                return;
323
31
            }
324
31
        }
325
0
        DCHECK(false) << "Invalid loc";
326
0
    }
327
757
}
328
329
647
void RuntimeProfile::add_child(RuntimeProfile* child, bool indent, RuntimeProfile* loc) {
330
647
    std::lock_guard<std::mutex> l(_children_lock);
331
647
    add_child_unlock(child, indent, loc);
332
647
}
333
334
2
void RuntimeProfile::get_children(std::vector<RuntimeProfile*>* children) {
335
2
    children->clear();
336
2
    std::lock_guard<std::mutex> l(_children_lock);
337
338
8
    for (ChildMap::iterator i = _child_map.begin(); i != _child_map.end(); ++i) {
339
6
        children->push_back(i->second);
340
6
    }
341
2
}
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
1.02k
void RuntimeProfile::add_info_string(const std::string& key, const std::string& value) {
353
1.02k
    std::lock_guard<std::mutex> l(_info_strings_lock);
354
1.02k
    InfoStrings::iterator it = _info_strings.find(key);
355
356
1.02k
    if (it == _info_strings.end()) {
357
612
        _info_strings.insert(std::make_pair(key, value));
358
612
        _info_strings_display_order.push_back(key);
359
612
    } else {
360
408
        it->second = value;
361
408
    }
362
1.02k
}
363
364
16
const std::string* RuntimeProfile::get_info_string(const std::string& key) {
365
16
    std::lock_guard<std::mutex> l(_info_strings_lock);
366
16
    InfoStrings::const_iterator it = _info_strings.find(key);
367
368
16
    if (it == _info_strings.end()) {
369
1
        return nullptr;
370
1
    }
371
372
15
    return &it->second;
373
16
}
374
375
RuntimeProfile::HighWaterMarkCounter* RuntimeProfile::AddHighWaterMarkCounter(
376
        const std::string& name, TUnit::type unit, const std::string& parent_counter_name,
377
730
        int64_t level) {
378
730
    DCHECK_EQ(_is_averaged_profile, false);
379
730
    std::lock_guard<std::mutex> l(_counter_map_lock);
380
730
    if (_counter_map.find(name) != _counter_map.end()) {
381
51
        return reinterpret_cast<RuntimeProfile::HighWaterMarkCounter*>(_counter_map[name]);
382
51
    }
383
679
    DCHECK(parent_counter_name == ROOT_COUNTER ||
384
679
           _counter_map.find(parent_counter_name) != _counter_map.end());
385
679
    RuntimeProfile::HighWaterMarkCounter* counter =
386
679
            _pool->add(new RuntimeProfile::HighWaterMarkCounter(unit, level, parent_counter_name));
387
679
    _counter_map[name] = counter;
388
679
    std::set<std::string>* child_counters =
389
679
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
390
679
    child_counters->insert(name);
391
679
    return counter;
392
730
}
393
394
RuntimeProfile::Counter* RuntimeProfile::add_counter(const std::string& name, TUnit::type type,
395
                                                     const std::string& parent_counter_name,
396
29.1k
                                                     int64_t level) {
397
29.1k
    std::lock_guard<std::mutex> l(_counter_map_lock);
398
399
29.1k
    if (_counter_map.find(name) != _counter_map.end()) {
400
1.55k
        return _counter_map[name];
401
1.55k
    }
402
403
    // Parent counter must already exist.
404
27.5k
    DCHECK(parent_counter_name == ROOT_COUNTER ||
405
27.5k
           _counter_map.find(parent_counter_name) != _counter_map.end());
406
407
27.5k
    Counter* counter = _pool->add(new Counter(type, 0, level));
408
27.5k
    _counter_map[name] = counter;
409
27.5k
    std::set<std::string>* child_counters =
410
27.5k
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
411
27.5k
    child_counters->insert(name);
412
27.5k
    return counter;
413
29.1k
}
414
415
RuntimeProfile::NonZeroCounter* RuntimeProfile::add_nonzero_counter(
416
        const std::string& name, TUnit::type type, const std::string& parent_counter_name,
417
32
        int64_t level) {
418
32
    std::lock_guard<std::mutex> l(_counter_map_lock);
419
32
    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
32
    DCHECK(parent_counter_name == ROOT_COUNTER ||
425
32
           _counter_map.find(parent_counter_name) != _counter_map.end());
426
32
    NonZeroCounter* counter = _pool->add(new NonZeroCounter(type, level, parent_counter_name));
427
32
    _counter_map[name] = counter;
428
32
    std::set<std::string>* child_counters =
429
32
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
430
32
    child_counters->insert(name);
431
32
    return counter;
432
32
}
433
434
RuntimeProfile::DerivedCounter* RuntimeProfile::add_derived_counter(
435
        const std::string& name, TUnit::type type, const DerivedCounterFunction& counter_fn,
436
8
        const std::string& parent_counter_name) {
437
8
    std::lock_guard<std::mutex> l(_counter_map_lock);
438
439
8
    if (_counter_map.find(name) != _counter_map.end()) {
440
0
        return nullptr;
441
0
    }
442
443
8
    DerivedCounter* counter = _pool->add(new DerivedCounter(type, counter_fn));
444
8
    _counter_map[name] = counter;
445
8
    std::set<std::string>* child_counters =
446
8
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
447
8
    child_counters->insert(name);
448
8
    return counter;
449
8
}
450
451
void RuntimeProfile::add_description(const std::string& name, const std::string& description,
452
0
                                     std::string parent_counter_name) {
453
0
    std::lock_guard<std::mutex> l(_counter_map_lock);
454
455
0
    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
0
    DCHECK(parent_counter_name == ROOT_COUNTER ||
468
0
           _counter_map.find(parent_counter_name) != _counter_map.end());
469
0
    DescriptionEntry* counter = _pool->add(new DescriptionEntry(name, description));
470
0
    _counter_map[name] = counter;
471
0
    std::set<std::string>* child_counters =
472
0
            find_or_insert(&_child_counter_map, parent_counter_name, std::set<std::string>());
473
0
    child_counters->insert(name);
474
0
}
475
476
2.80k
RuntimeProfile::Counter* RuntimeProfile::get_counter(const std::string& name) {
477
2.80k
    std::lock_guard<std::mutex> l(_counter_map_lock);
478
479
2.80k
    if (_counter_map.find(name) != _counter_map.end()) {
480
2.39k
        return _counter_map[name];
481
2.39k
    }
482
483
410
    return nullptr;
484
2.80k
}
485
486
0
void RuntimeProfile::get_counters(const std::string& name, std::vector<Counter*>* counters) {
487
0
    Counter* c = get_counter(name);
488
489
0
    if (c != nullptr) {
490
0
        counters->push_back(c);
491
0
    }
492
493
0
    std::lock_guard<std::mutex> l(_children_lock);
494
495
0
    for (int i = 0; i < _children.size(); ++i) {
496
0
        _children[i].first->get_counters(name, counters);
497
0
    }
498
0
}
499
500
// Print the profile:
501
//  1. Profile Name
502
//  2. Info Strings
503
//  3. Counters
504
//  4. Children
505
void RuntimeProfile::pretty_print(std::ostream* s, const std::string& prefix,
506
53
                                  int64_t profile_level) const {
507
53
    std::ostream& stream = *s;
508
509
    // create copy of _counter_map and _child_counter_map so we don't need to hold lock
510
    // while we call value() on the counters
511
53
    CounterMap counter_map;
512
53
    ChildCounterMap child_counter_map;
513
53
    {
514
53
        std::lock_guard<std::mutex> l(_counter_map_lock);
515
53
        counter_map = _counter_map;
516
53
        child_counter_map = _child_counter_map;
517
53
    }
518
519
53
    std::map<std::string, Counter*>::const_iterator total_time = counter_map.find("TotalTime");
520
53
    DCHECK(total_time != counter_map.end());
521
522
53
    stream.flags(std::ios::fixed);
523
53
    stream << prefix << _name << ":";
524
525
53
    if (total_time->second->value() != 0) {
526
0
        stream << "(Active: "
527
0
               << PrettyPrinter::print(total_time->second->value(), total_time->second->type())
528
0
               << ", non-child: " << std::setprecision(2) << _local_time_percent << "%)";
529
0
    }
530
531
53
    stream << std::endl;
532
533
53
    {
534
53
        std::lock_guard<std::mutex> l(_info_strings_lock);
535
53
        for (const std::string& key : _info_strings_display_order) {
536
2
            stream << prefix << "   - " << key << ": " << _info_strings.find(key)->second
537
2
                   << std::endl;
538
2
        }
539
53
    }
540
541
53
    RuntimeProfile::print_child_counters(prefix, ROOT_COUNTER, counter_map, child_counter_map, s);
542
543
    // create copy of _children so we don't need to hold lock while we call
544
    // pretty_print() on the children
545
53
    ChildVector children;
546
53
    {
547
53
        std::lock_guard<std::mutex> l(_children_lock);
548
53
        children = _children;
549
53
    }
550
551
85
    for (int i = 0; i < children.size(); ++i) {
552
32
        RuntimeProfile* profile = children[i].first;
553
32
        bool indent = children[i].second;
554
32
        profile->pretty_print(s, prefix + (indent ? "  " : ""));
555
32
    }
556
53
}
557
558
234
void RuntimeProfile::to_thrift(TRuntimeProfileTree* tree, int64 profile_level) {
559
234
    tree->nodes.clear();
560
234
    to_thrift(&tree->nodes, profile_level);
561
234
}
562
563
242
void RuntimeProfile::to_thrift(std::vector<TRuntimeProfileNode>* nodes, int64 profile_level) {
564
242
    int index = nodes->size();
565
242
    nodes->push_back(TRuntimeProfileNode());
566
242
    TRuntimeProfileNode& node = (*nodes)[index];
567
242
    node.name = _name;
568
242
    node.metadata = _metadata;
569
242
    node.timestamp = _timestamp;
570
242
    node.indent = true;
571
572
242
    {
573
242
        std::lock_guard<std::mutex> l(_counter_map_lock);
574
242
        RuntimeProfileCounterTreeNode conter_tree = RuntimeProfileCounterTreeNode::from_map(
575
242
                _counter_map, _child_counter_map, ROOT_COUNTER);
576
242
        conter_tree = RuntimeProfileCounterTreeNode::prune_the_tree(conter_tree, profile_level);
577
242
        conter_tree.to_thrift(node.counters, node.child_counters_map);
578
242
    }
579
580
242
    {
581
242
        std::lock_guard<std::mutex> l(_info_strings_lock);
582
242
        node.info_strings = _info_strings;
583
242
        node.info_strings_display_order = _info_strings_display_order;
584
242
    }
585
586
242
    ChildVector children;
587
242
    {
588
        // _children may be modified during to_thrift(),
589
        // so we have to lock and copy _children to avoid race condition
590
242
        std::lock_guard<std::mutex> l(_children_lock);
591
242
        children = _children;
592
242
    }
593
242
    node.num_children = children.size();
594
242
    nodes->reserve(nodes->size() + children.size());
595
596
248
    for (int i = 0; i < children.size(); ++i) {
597
6
        int child_idx = nodes->size();
598
6
        children[i].first->to_thrift(nodes, profile_level);
599
        // fix up indentation flag
600
6
        (*nodes)[child_idx].indent = children[i].second;
601
6
    }
602
242
}
603
604
int64_t RuntimeProfile::units_per_second(const RuntimeProfile::Counter* total_counter,
605
3
                                         const RuntimeProfile::Counter* timer) {
606
3
    DCHECK(total_counter->type() == TUnit::BYTES || total_counter->type() == TUnit::UNIT);
607
3
    DCHECK(timer->type() == TUnit::TIME_NS);
608
609
3
    if (timer->value() == 0) {
610
0
        return 0;
611
0
    }
612
613
3
    double secs = static_cast<double>(timer->value()) / 1000.0 / 1000.0 / 1000.0;
614
3
    return int64_t(total_counter->value() / secs);
615
3
}
616
617
0
int64_t RuntimeProfile::counter_sum(const std::vector<Counter*>* counters) {
618
0
    int64_t value = 0;
619
620
0
    for (int i = 0; i < counters->size(); ++i) {
621
0
        value += (*counters)[i]->value();
622
0
    }
623
624
0
    return value;
625
0
}
626
627
void RuntimeProfile::print_child_counters(const std::string& prefix,
628
                                          const std::string& counter_name,
629
                                          const CounterMap& counter_map,
630
                                          const ChildCounterMap& child_counter_map,
631
371
                                          std::ostream* s) {
632
371
    auto itr = child_counter_map.find(counter_name);
633
634
371
    if (itr != child_counter_map.end()) {
635
47
        const std::set<std::string>& child_counters = itr->second;
636
318
        for (const std::string& child_counter : child_counters) {
637
318
            auto iter = counter_map.find(child_counter);
638
318
            DCHECK(iter != counter_map.end());
639
318
            iter->second->pretty_print(s, prefix, iter->first);
640
318
            RuntimeProfile::print_child_counters(prefix + "  ", child_counter, counter_map,
641
318
                                                 child_counter_map, s);
642
318
        }
643
47
    }
644
371
}
645
646
} // namespace doris