/root/doris/be/src/util/concurrency_stats.h
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 | | #pragma once |
19 | | |
20 | | #include <atomic> |
21 | | #include <memory> |
22 | | #include <string> |
23 | | #include <thread> |
24 | | #include <vector> |
25 | | |
26 | | namespace doris { |
27 | | |
28 | | // A thread-safe counter for tracking concurrent operations |
29 | | // Uses atomic variable for high-performance concurrent access |
30 | | class ConcurrencyCounter { |
31 | | public: |
32 | 13 | explicit ConcurrencyCounter(std::string name) : _name(std::move(name)), _count(0) {} |
33 | | |
34 | | // Increment the counter |
35 | 93.7k | void increment() { _count.fetch_add(1, std::memory_order_relaxed); } |
36 | | |
37 | | // Decrement the counter |
38 | 93.7k | void decrement() { _count.fetch_sub(1, std::memory_order_relaxed); } |
39 | | |
40 | | // Get current value |
41 | 0 | int64_t value() const { return _count.load(std::memory_order_relaxed); } |
42 | | |
43 | 0 | const std::string& name() const { return _name; } |
44 | | |
45 | | // RAII helper for automatic increment/decrement |
46 | | class Guard { |
47 | | public: |
48 | 82.3k | explicit Guard(ConcurrencyCounter* counter) : _counter(counter) { |
49 | 82.3k | if (_counter) { |
50 | 82.3k | _counter->increment(); |
51 | 82.3k | } |
52 | 82.3k | } |
53 | | |
54 | 82.3k | ~Guard() { |
55 | 82.3k | if (_counter) { |
56 | 82.3k | _counter->decrement(); |
57 | 82.3k | } |
58 | 82.3k | } |
59 | | |
60 | | Guard(const Guard&) = delete; |
61 | | Guard& operator=(const Guard&) = delete; |
62 | | |
63 | | private: |
64 | | ConcurrencyCounter* _counter; |
65 | | }; |
66 | | |
67 | | private: |
68 | | std::string _name; |
69 | | std::atomic<int64_t> _count; |
70 | | }; |
71 | | |
72 | | // Singleton manager for all concurrency counters |
73 | | // All counters are defined here in order |
74 | | class ConcurrencyStatsManager { |
75 | | public: |
76 | | static ConcurrencyStatsManager& instance(); |
77 | | |
78 | | // Start the background thread for periodic logging |
79 | | void start(); |
80 | | |
81 | | // Stop the background thread |
82 | | void stop(); |
83 | | |
84 | | // Manually dump all counters to log |
85 | | void dump_to_log(); |
86 | | |
87 | | // Access to individual counters (defined in order of read path from top to bottom) |
88 | | ConcurrencyCounter* vscanner_get_block; |
89 | | ConcurrencyCounter* segment_iterator_next_batch; |
90 | | ConcurrencyCounter* column_reader_read_page; |
91 | | ConcurrencyCounter* page_io_decompress; |
92 | | ConcurrencyCounter* page_io_pre_decode; |
93 | | ConcurrencyCounter* page_io_insert_page_cache; |
94 | | ConcurrencyCounter* cached_remote_reader_read_at; |
95 | | ConcurrencyCounter* cached_remote_reader_get_or_set; |
96 | | ConcurrencyCounter* cached_remote_reader_get_or_set_wait_lock; |
97 | | ConcurrencyCounter* cached_remote_reader_write_back; |
98 | | ConcurrencyCounter* cached_remote_reader_blocking; |
99 | | ConcurrencyCounter* cached_remote_reader_local_read; |
100 | | ConcurrencyCounter* s3_file_reader_read; |
101 | | |
102 | | private: |
103 | | ConcurrencyStatsManager(); |
104 | | ~ConcurrencyStatsManager(); |
105 | | |
106 | | ConcurrencyStatsManager(const ConcurrencyStatsManager&) = delete; |
107 | | ConcurrencyStatsManager& operator=(const ConcurrencyStatsManager&) = delete; |
108 | | |
109 | | void _dump_thread_func(); |
110 | | |
111 | | // All counters in the order they should be printed |
112 | | std::vector<ConcurrencyCounter*> _counters; |
113 | | |
114 | | std::atomic<bool> _running; |
115 | | std::unique_ptr<std::thread> _dump_thread; |
116 | | }; |
117 | | |
118 | | // Macro for scoped counting |
119 | | #define SCOPED_CONCURRENCY_COUNT_IMPL(counter_ptr, unique_id) \ |
120 | 82.3k | doris::ConcurrencyCounter::Guard _concurrency_guard_##unique_id(counter_ptr) |
121 | | |
122 | | #define SCOPED_CONCURRENCY_COUNT_HELPER(counter_ptr, line) \ |
123 | 82.3k | SCOPED_CONCURRENCY_COUNT_IMPL(counter_ptr, line) |
124 | | |
125 | 82.3k | #define SCOPED_CONCURRENCY_COUNT(counter_ptr) SCOPED_CONCURRENCY_COUNT_HELPER(counter_ptr, __LINE__) |
126 | | |
127 | | } // namespace doris |