/root/doris/be/src/util/concurrency_stats.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "util/concurrency_stats.h" |
19 | | |
20 | | #include <chrono> |
21 | | #include <sstream> |
22 | | |
23 | | #include "common/config.h" |
24 | | #include "common/logging.h" |
25 | | #include "util/thread.h" |
26 | | |
27 | | namespace doris { |
28 | 1 | ConcurrencyStatsManager::ConcurrencyStatsManager() : _running(false) { |
29 | | // Initialize all counters in the order of read path (top to bottom) |
30 | 1 | vscanner_get_block = new ConcurrencyCounter("vscanner"); |
31 | 1 | segment_iterator_next_batch = new ConcurrencyCounter("segment_iterator"); |
32 | 1 | column_reader_read_page = new ConcurrencyCounter("column_reader"); |
33 | 1 | page_io_decompress = new ConcurrencyCounter("page_io.decompress"); |
34 | 1 | page_io_pre_decode = new ConcurrencyCounter("page_io.pre_decode"); |
35 | 1 | page_io_insert_page_cache = new ConcurrencyCounter("page_io.insert_page_cache"); |
36 | 1 | cached_remote_reader_read_at = new ConcurrencyCounter("file_cache.read_at"); |
37 | 1 | cached_remote_reader_get_or_set = new ConcurrencyCounter("file_cache.get_or_set"); |
38 | 1 | cached_remote_reader_get_or_set_wait_lock = |
39 | 1 | new ConcurrencyCounter("file_cache.get_or_set_wait_lock"); |
40 | 1 | cached_remote_reader_write_back = new ConcurrencyCounter("file_cache.write_back"); |
41 | 1 | cached_remote_reader_blocking = new ConcurrencyCounter("file_cache.blocking"); |
42 | 1 | cached_remote_reader_local_read = new ConcurrencyCounter("file_cache.local_read"); |
43 | 1 | s3_file_reader_read = new ConcurrencyCounter("s3.read"); |
44 | | |
45 | | // Add to vector in the order they should be printed |
46 | 1 | _counters.push_back(vscanner_get_block); |
47 | 1 | _counters.push_back(segment_iterator_next_batch); |
48 | 1 | _counters.push_back(column_reader_read_page); |
49 | 1 | _counters.push_back(page_io_decompress); |
50 | 1 | _counters.push_back(page_io_pre_decode); |
51 | 1 | _counters.push_back(page_io_insert_page_cache); |
52 | 1 | _counters.push_back(cached_remote_reader_read_at); |
53 | 1 | _counters.push_back(cached_remote_reader_get_or_set); |
54 | 1 | _counters.push_back(cached_remote_reader_get_or_set_wait_lock); |
55 | 1 | _counters.push_back(cached_remote_reader_write_back); |
56 | 1 | _counters.push_back(cached_remote_reader_blocking); |
57 | 1 | _counters.push_back(cached_remote_reader_local_read); |
58 | 1 | _counters.push_back(s3_file_reader_read); |
59 | 1 | } |
60 | | |
61 | 1 | ConcurrencyStatsManager::~ConcurrencyStatsManager() { |
62 | 1 | stop(); |
63 | | |
64 | | // Clean up counters |
65 | 13 | for (auto* counter : _counters) { |
66 | 13 | delete counter; |
67 | 13 | } |
68 | 1 | _counters.clear(); |
69 | 1 | } |
70 | | |
71 | 104k | ConcurrencyStatsManager& ConcurrencyStatsManager::instance() { |
72 | 104k | static ConcurrencyStatsManager instance; |
73 | 104k | return instance; |
74 | 104k | } |
75 | | |
76 | 0 | void ConcurrencyStatsManager::start() { |
77 | 0 | if (_running.exchange(true)) { |
78 | 0 | return; // Already running |
79 | 0 | } |
80 | | |
81 | 0 | _dump_thread = std::make_unique<std::thread>([this]() { _dump_thread_func(); }); |
82 | 0 | } |
83 | | |
84 | 1 | void ConcurrencyStatsManager::stop() { |
85 | 1 | if (!_running.exchange(false)) { |
86 | 1 | return; // Not running |
87 | 1 | } |
88 | | |
89 | 0 | if (_dump_thread && _dump_thread->joinable()) { |
90 | 0 | _dump_thread->join(); |
91 | 0 | } |
92 | 0 | _dump_thread.reset(); |
93 | 0 | } |
94 | | |
95 | 0 | void ConcurrencyStatsManager::dump_to_log() { |
96 | 0 | if (_counters.empty()) { |
97 | 0 | return; |
98 | 0 | } |
99 | | |
100 | | // Build single line output: CONCURRENCY_STATS name1=value1 name2=value2 ... |
101 | 0 | std::stringstream ss; |
102 | 0 | ss << "CONCURRENCY_STATS"; |
103 | |
|
104 | 0 | for (const auto* counter : _counters) { |
105 | 0 | int64_t value = counter->value(); |
106 | 0 | ss << " " << counter->name() << "=" << value; |
107 | 0 | } |
108 | |
|
109 | 0 | LOG(INFO) << ss.str(); |
110 | 0 | } |
111 | | |
112 | 0 | void ConcurrencyStatsManager::_dump_thread_func() { |
113 | 0 | Thread::set_self_name("ConcurrencyStatsManager_dump_thread"); |
114 | 0 | while (_running.load(std::memory_order_relaxed)) { |
115 | | // Check if dumping is enabled |
116 | 0 | if (config::enable_concurrency_stats_dump) { |
117 | 0 | dump_to_log(); |
118 | 0 | } |
119 | | |
120 | | // Sleep for the configured interval |
121 | 0 | int32_t interval_ms = config::concurrency_stats_dump_interval_ms; |
122 | 0 | if (interval_ms <= 0) { |
123 | 0 | interval_ms = 100; // Default to 100ms if invalid |
124 | 0 | } |
125 | |
|
126 | 0 | std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms)); |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | } // namespace doris |