1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
6 #include "monitoring/thread_status_util.h"
8 #include "monitoring/thread_status_updater.h"
9 #include "rocksdb/env.h"
14 #ifdef ROCKSDB_USING_THREAD_STATUS
15 __thread ThreadStatusUpdater
*
16 ThreadStatusUtil::thread_updater_local_cache_
= nullptr;
17 __thread
bool ThreadStatusUtil::thread_updater_initialized_
= false;
19 void ThreadStatusUtil::RegisterThread(
20 const Env
* env
, ThreadStatus::ThreadType thread_type
) {
21 if (!MaybeInitThreadLocalUpdater(env
)) {
24 assert(thread_updater_local_cache_
);
25 thread_updater_local_cache_
->RegisterThread(
26 thread_type
, env
->GetThreadID());
29 void ThreadStatusUtil::UnregisterThread() {
30 thread_updater_initialized_
= false;
31 if (thread_updater_local_cache_
!= nullptr) {
32 thread_updater_local_cache_
->UnregisterThread();
33 thread_updater_local_cache_
= nullptr;
37 void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData
* cfd
,
39 bool enable_thread_tracking
) {
40 if (!MaybeInitThreadLocalUpdater(env
)) {
43 assert(thread_updater_local_cache_
);
44 if (cfd
!= nullptr && enable_thread_tracking
) {
45 thread_updater_local_cache_
->SetColumnFamilyInfoKey(cfd
);
47 // When cfd == nullptr or enable_thread_tracking == false, we set
48 // ColumnFamilyInfoKey to nullptr, which makes SetThreadOperation
49 // and SetThreadState become no-op.
50 thread_updater_local_cache_
->SetColumnFamilyInfoKey(nullptr);
54 void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType op
) {
55 if (thread_updater_local_cache_
== nullptr) {
56 // thread_updater_local_cache_ must be set in SetColumnFamily
57 // or other ThreadStatusUtil functions.
61 if (op
!= ThreadStatus::OP_UNKNOWN
) {
62 uint64_t current_time
= Env::Default()->NowMicros();
63 thread_updater_local_cache_
->SetOperationStartTime(current_time
);
65 // TDOO(yhchiang): we could report the time when we set operation to
66 // OP_UNKNOWN once the whole instrumentation has been done.
67 thread_updater_local_cache_
->SetOperationStartTime(0);
69 thread_updater_local_cache_
->SetThreadOperation(op
);
72 ThreadStatus::OperationStage
ThreadStatusUtil::SetThreadOperationStage(
73 ThreadStatus::OperationStage stage
) {
74 if (thread_updater_local_cache_
== nullptr) {
75 // thread_updater_local_cache_ must be set in SetColumnFamily
76 // or other ThreadStatusUtil functions.
77 return ThreadStatus::STAGE_UNKNOWN
;
80 return thread_updater_local_cache_
->SetThreadOperationStage(stage
);
83 void ThreadStatusUtil::SetThreadOperationProperty(
84 int code
, uint64_t value
) {
85 if (thread_updater_local_cache_
== nullptr) {
86 // thread_updater_local_cache_ must be set in SetColumnFamily
87 // or other ThreadStatusUtil functions.
91 thread_updater_local_cache_
->SetThreadOperationProperty(
95 void ThreadStatusUtil::IncreaseThreadOperationProperty(
96 int code
, uint64_t delta
) {
97 if (thread_updater_local_cache_
== nullptr) {
98 // thread_updater_local_cache_ must be set in SetColumnFamily
99 // or other ThreadStatusUtil functions.
103 thread_updater_local_cache_
->IncreaseThreadOperationProperty(
107 void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType state
) {
108 if (thread_updater_local_cache_
== nullptr) {
109 // thread_updater_local_cache_ must be set in SetColumnFamily
110 // or other ThreadStatusUtil functions.
114 thread_updater_local_cache_
->SetThreadState(state
);
117 void ThreadStatusUtil::ResetThreadStatus() {
118 if (thread_updater_local_cache_
== nullptr) {
121 thread_updater_local_cache_
->ResetThreadStatus();
124 void ThreadStatusUtil::NewColumnFamilyInfo(const DB
* db
,
125 const ColumnFamilyData
* cfd
,
126 const std::string
& cf_name
,
128 if (!MaybeInitThreadLocalUpdater(env
)) {
131 assert(thread_updater_local_cache_
);
132 if (thread_updater_local_cache_
) {
133 thread_updater_local_cache_
->NewColumnFamilyInfo(db
, db
->GetName(), cfd
,
138 void ThreadStatusUtil::EraseColumnFamilyInfo(
139 const ColumnFamilyData
* cfd
) {
140 if (thread_updater_local_cache_
== nullptr) {
143 thread_updater_local_cache_
->EraseColumnFamilyInfo(cfd
);
146 void ThreadStatusUtil::EraseDatabaseInfo(const DB
* db
) {
147 ThreadStatusUpdater
* thread_updater
= db
->GetEnv()->GetThreadStatusUpdater();
148 if (thread_updater
== nullptr) {
151 thread_updater
->EraseDatabaseInfo(db
);
154 bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env
* env
) {
155 if (!thread_updater_initialized_
&& env
!= nullptr) {
156 thread_updater_initialized_
= true;
157 thread_updater_local_cache_
= env
->GetThreadStatusUpdater();
159 return (thread_updater_local_cache_
!= nullptr);
162 AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater(
163 ThreadStatus::OperationStage stage
) {
164 prev_stage_
= ThreadStatusUtil::SetThreadOperationStage(stage
);
167 AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {
168 ThreadStatusUtil::SetThreadOperationStage(prev_stage_
);
173 ThreadStatusUpdater
* ThreadStatusUtil::thread_updater_local_cache_
= nullptr;
174 bool ThreadStatusUtil::thread_updater_initialized_
= false;
176 bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env
* env
) {
180 void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData
* cfd
,
182 bool enable_thread_tracking
) {}
184 void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType op
) {
187 void ThreadStatusUtil::SetThreadOperationProperty(
188 int code
, uint64_t value
) {
191 void ThreadStatusUtil::IncreaseThreadOperationProperty(
192 int code
, uint64_t delta
) {
195 void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType state
) {
198 void ThreadStatusUtil::NewColumnFamilyInfo(const DB
* db
,
199 const ColumnFamilyData
* cfd
,
200 const std::string
& cf_name
,
203 void ThreadStatusUtil::EraseColumnFamilyInfo(
204 const ColumnFamilyData
* cfd
) {
207 void ThreadStatusUtil::EraseDatabaseInfo(const DB
* db
) {
210 void ThreadStatusUtil::ResetThreadStatus() {
213 AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater(
214 ThreadStatus::OperationStage stage
) {
217 AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {
220 #endif // ROCKSDB_USING_THREAD_STATUS
222 } // namespace rocksdb