/src/rocksdb/monitoring/thread_status_util.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root directory). |
5 | | |
6 | | #include "monitoring/thread_status_util.h" |
7 | | |
8 | | #include "monitoring/thread_status_updater.h" |
9 | | #include "rocksdb/env.h" |
10 | | #include "rocksdb/system_clock.h" |
11 | | |
12 | | namespace ROCKSDB_NAMESPACE { |
13 | | |
14 | | #ifndef NROCKSDB_THREAD_STATUS |
15 | | thread_local ThreadStatusUpdater* |
16 | | ThreadStatusUtil::thread_updater_local_cache_ = nullptr; |
17 | | thread_local bool ThreadStatusUtil::thread_updater_initialized_ = false; |
18 | | |
19 | | void ThreadStatusUtil::RegisterThread(const Env* env, |
20 | 4 | ThreadStatus::ThreadType thread_type) { |
21 | 4 | if (!MaybeInitThreadLocalUpdater(env)) { |
22 | 0 | return; |
23 | 0 | } |
24 | 4 | assert(thread_updater_local_cache_); |
25 | 4 | thread_updater_local_cache_->RegisterThread(thread_type, env->GetThreadID()); |
26 | 4 | } |
27 | | |
28 | 4 | void ThreadStatusUtil::UnregisterThread() { |
29 | 4 | thread_updater_initialized_ = false; |
30 | 4 | if (thread_updater_local_cache_ != nullptr) { |
31 | 4 | thread_updater_local_cache_->UnregisterThread(); |
32 | 4 | thread_updater_local_cache_ = nullptr; |
33 | 4 | } |
34 | 4 | } |
35 | | |
36 | 60.8k | void ThreadStatusUtil::SetEnableTracking(bool enable_tracking) { |
37 | 60.8k | if (thread_updater_local_cache_ == nullptr) { |
38 | 53.3k | return; |
39 | 53.3k | } |
40 | 7.46k | thread_updater_local_cache_->SetEnableTracking(enable_tracking); |
41 | 7.46k | } |
42 | | |
43 | 11.4k | void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* cfd) { |
44 | 11.4k | if (thread_updater_local_cache_ == nullptr) { |
45 | 0 | return; |
46 | 0 | } |
47 | 11.4k | assert(cfd); |
48 | 11.4k | thread_updater_local_cache_->SetColumnFamilyInfoKey(cfd); |
49 | 11.4k | } |
50 | | |
51 | 441k | void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType op) { |
52 | 441k | if (thread_updater_local_cache_ == nullptr) { |
53 | 420k | return; |
54 | 420k | } |
55 | | |
56 | 21.2k | if (op != ThreadStatus::OP_UNKNOWN) { |
57 | 16.5k | uint64_t current_time = SystemClock::Default()->NowMicros(); |
58 | 16.5k | thread_updater_local_cache_->SetOperationStartTime(current_time); |
59 | 16.5k | } else { |
60 | | // TDOO(yhchiang): we could report the time when we set operation to |
61 | | // OP_UNKNOWN once the whole instrumentation has been done. |
62 | 4.67k | thread_updater_local_cache_->SetOperationStartTime(0); |
63 | 4.67k | } |
64 | 21.2k | thread_updater_local_cache_->SetThreadOperation(op); |
65 | 21.2k | } |
66 | | |
67 | 186k | ThreadStatus::OperationType ThreadStatusUtil::GetThreadOperation() { |
68 | 186k | if (thread_updater_local_cache_ == nullptr) { |
69 | 183k | return ThreadStatus::OperationType::OP_UNKNOWN; |
70 | 183k | } |
71 | 2.33k | return thread_updater_local_cache_->GetThreadOperation(); |
72 | 186k | } |
73 | | |
74 | | ThreadStatus::OperationStage ThreadStatusUtil::SetThreadOperationStage( |
75 | 64.9k | ThreadStatus::OperationStage stage) { |
76 | 64.9k | if (thread_updater_local_cache_ == nullptr) { |
77 | | // thread_updater_local_cache_ must be set in SetColumnFamily |
78 | | // or other ThreadStatusUtil functions. |
79 | 0 | return ThreadStatus::STAGE_UNKNOWN; |
80 | 0 | } |
81 | | |
82 | 64.9k | return thread_updater_local_cache_->SetThreadOperationStage(stage); |
83 | 64.9k | } |
84 | | |
85 | 88.5k | void ThreadStatusUtil::SetThreadOperationProperty(int code, uint64_t value) { |
86 | 88.5k | if (thread_updater_local_cache_ == nullptr) { |
87 | | // thread_updater_local_cache_ must be set in SetColumnFamily |
88 | | // or other ThreadStatusUtil functions. |
89 | 55.4k | return; |
90 | 55.4k | } |
91 | | |
92 | 33.0k | thread_updater_local_cache_->SetThreadOperationProperty(code, value); |
93 | 33.0k | } |
94 | | |
95 | | void ThreadStatusUtil::IncreaseThreadOperationProperty(int code, |
96 | 27.5k | uint64_t delta) { |
97 | 27.5k | if (thread_updater_local_cache_ == nullptr) { |
98 | | // thread_updater_local_cache_ must be set in SetColumnFamily |
99 | | // or other ThreadStatusUtil functions. |
100 | 0 | return; |
101 | 0 | } |
102 | | |
103 | 27.5k | thread_updater_local_cache_->IncreaseThreadOperationProperty(code, delta); |
104 | 27.5k | } |
105 | | |
106 | 0 | void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType state) { |
107 | 0 | if (thread_updater_local_cache_ == nullptr) { |
108 | | // thread_updater_local_cache_ must be set in SetColumnFamily |
109 | | // or other ThreadStatusUtil functions. |
110 | 0 | return; |
111 | 0 | } |
112 | | |
113 | 0 | thread_updater_local_cache_->SetThreadState(state); |
114 | 0 | } |
115 | | |
116 | 64.7k | void ThreadStatusUtil::ResetThreadStatus() { |
117 | 64.7k | if (thread_updater_local_cache_ == nullptr) { |
118 | 53.3k | return; |
119 | 53.3k | } |
120 | 11.4k | thread_updater_local_cache_->ResetThreadStatus(); |
121 | 11.4k | } |
122 | | |
123 | | void ThreadStatusUtil::NewColumnFamilyInfo(const DB* db, |
124 | | const ColumnFamilyData* cfd, |
125 | | const std::string& cf_name, |
126 | 0 | const Env* env) { |
127 | 0 | if (!MaybeInitThreadLocalUpdater(env)) { |
128 | 0 | return; |
129 | 0 | } |
130 | 0 | assert(thread_updater_local_cache_); |
131 | 0 | if (thread_updater_local_cache_) { |
132 | 0 | thread_updater_local_cache_->NewColumnFamilyInfo(db, db->GetName(), cfd, |
133 | 0 | cf_name); |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | 0 | void ThreadStatusUtil::EraseColumnFamilyInfo(const ColumnFamilyData* cfd) { |
138 | 0 | if (thread_updater_local_cache_ == nullptr) { |
139 | 0 | return; |
140 | 0 | } |
141 | 0 | thread_updater_local_cache_->EraseColumnFamilyInfo(cfd); |
142 | 0 | } |
143 | | |
144 | 0 | void ThreadStatusUtil::EraseDatabaseInfo(const DB* db) { |
145 | 0 | ThreadStatusUpdater* thread_updater = db->GetEnv()->GetThreadStatusUpdater(); |
146 | 0 | if (thread_updater == nullptr) { |
147 | 0 | return; |
148 | 0 | } |
149 | 0 | thread_updater->EraseDatabaseInfo(db); |
150 | 0 | } |
151 | | |
152 | 4 | bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env* env) { |
153 | 4 | if (!thread_updater_initialized_ && env != nullptr) { |
154 | 4 | thread_updater_initialized_ = true; |
155 | 4 | thread_updater_local_cache_ = env->GetThreadStatusUpdater(); |
156 | 4 | } |
157 | 4 | return (thread_updater_local_cache_ != nullptr); |
158 | 4 | } |
159 | | |
160 | | AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater( |
161 | 32.4k | ThreadStatus::OperationStage stage) { |
162 | 32.4k | prev_stage_ = ThreadStatusUtil::SetThreadOperationStage(stage); |
163 | 32.4k | } |
164 | | |
165 | 32.4k | AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() { |
166 | 32.4k | ThreadStatusUtil::SetThreadOperationStage(prev_stage_); |
167 | 32.4k | } |
168 | | |
169 | | #else |
170 | | |
171 | | ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ = nullptr; |
172 | | bool ThreadStatusUtil::thread_updater_initialized_ = false; |
173 | | |
174 | | void ThreadStatusUtil::RegisterThread( |
175 | | const Env* /*env*/, ThreadStatus::ThreadType /*thread_type*/) {} |
176 | | |
177 | | void ThreadStatusUtil::UnregisterThread() {} |
178 | | |
179 | | void ThreadStatusUtil::SetEnableTracking(bool /*enable_tracking*/) {} |
180 | | |
181 | | void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* /*cfd*/) {} |
182 | | |
183 | | ThreadStatus::OperationType ThreadStatusUtil::GetThreadOperation() { |
184 | | return ThreadStatus::OperationType::OP_UNKNOWN; |
185 | | } |
186 | | |
187 | | void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType /*op*/) {} |
188 | | |
189 | | void ThreadStatusUtil::SetThreadOperationProperty(int /*code*/, |
190 | | uint64_t /*value*/) {} |
191 | | |
192 | | void ThreadStatusUtil::IncreaseThreadOperationProperty(int /*code*/, |
193 | | uint64_t /*delta*/) {} |
194 | | |
195 | | void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType /*state*/) {} |
196 | | |
197 | | void ThreadStatusUtil::NewColumnFamilyInfo(const DB* /*db*/, |
198 | | const ColumnFamilyData* /*cfd*/, |
199 | | const std::string& /*cf_name*/, |
200 | | const Env* /*env*/) {} |
201 | | |
202 | | void ThreadStatusUtil::EraseColumnFamilyInfo(const ColumnFamilyData* /*cfd*/) {} |
203 | | |
204 | | void ThreadStatusUtil::EraseDatabaseInfo(const DB* /*db*/) {} |
205 | | |
206 | | void ThreadStatusUtil::ResetThreadStatus() {} |
207 | | |
208 | | bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env* /*env*/) { |
209 | | return false; |
210 | | } |
211 | | |
212 | | AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater( |
213 | | ThreadStatus::OperationStage /*stage*/) {} |
214 | | |
215 | | AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {} |
216 | | |
217 | | #endif // !NROCKSDB_THREAD_STATUS |
218 | | |
219 | | } // namespace ROCKSDB_NAMESPACE |