Coverage Report

Created: 2025-03-12 00:38

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