]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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. | |
5 | ||
6 | #include "monitoring/thread_status_util.h" | |
7 | ||
8 | #include "monitoring/thread_status_updater.h" | |
9 | #include "rocksdb/env.h" | |
10 | ||
11 | namespace rocksdb { | |
12 | ||
13 | ||
14 | #ifdef ROCKSDB_USING_THREAD_STATUS | |
15 | __thread ThreadStatusUpdater* | |
16 | ThreadStatusUtil::thread_updater_local_cache_ = nullptr; | |
17 | __thread bool ThreadStatusUtil::thread_updater_initialized_ = false; | |
18 | ||
19 | void ThreadStatusUtil::RegisterThread( | |
20 | const Env* env, ThreadStatus::ThreadType thread_type) { | |
21 | if (!MaybeInitThreadLocalUpdater(env)) { | |
22 | return; | |
23 | } | |
24 | assert(thread_updater_local_cache_); | |
25 | thread_updater_local_cache_->RegisterThread( | |
26 | thread_type, env->GetThreadID()); | |
27 | } | |
28 | ||
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; | |
34 | } | |
35 | } | |
36 | ||
37 | void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* cfd, | |
38 | const Env* env, | |
39 | bool enable_thread_tracking) { | |
40 | if (!MaybeInitThreadLocalUpdater(env)) { | |
41 | return; | |
42 | } | |
43 | assert(thread_updater_local_cache_); | |
44 | if (cfd != nullptr && enable_thread_tracking) { | |
45 | thread_updater_local_cache_->SetColumnFamilyInfoKey(cfd); | |
46 | } else { | |
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); | |
51 | } | |
52 | } | |
53 | ||
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. | |
58 | return; | |
59 | } | |
60 | ||
61 | if (op != ThreadStatus::OP_UNKNOWN) { | |
62 | uint64_t current_time = Env::Default()->NowMicros(); | |
63 | thread_updater_local_cache_->SetOperationStartTime(current_time); | |
64 | } else { | |
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); | |
68 | } | |
69 | thread_updater_local_cache_->SetThreadOperation(op); | |
70 | } | |
71 | ||
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; | |
78 | } | |
79 | ||
80 | return thread_updater_local_cache_->SetThreadOperationStage(stage); | |
81 | } | |
82 | ||
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. | |
88 | return; | |
89 | } | |
90 | ||
91 | thread_updater_local_cache_->SetThreadOperationProperty( | |
92 | code, value); | |
93 | } | |
94 | ||
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. | |
100 | return; | |
101 | } | |
102 | ||
103 | thread_updater_local_cache_->IncreaseThreadOperationProperty( | |
104 | code, delta); | |
105 | } | |
106 | ||
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. | |
111 | return; | |
112 | } | |
113 | ||
114 | thread_updater_local_cache_->SetThreadState(state); | |
115 | } | |
116 | ||
117 | void ThreadStatusUtil::ResetThreadStatus() { | |
118 | if (thread_updater_local_cache_ == nullptr) { | |
119 | return; | |
120 | } | |
121 | thread_updater_local_cache_->ResetThreadStatus(); | |
122 | } | |
123 | ||
124 | void ThreadStatusUtil::NewColumnFamilyInfo(const DB* db, | |
125 | const ColumnFamilyData* cfd, | |
126 | const std::string& cf_name, | |
127 | const Env* env) { | |
128 | if (!MaybeInitThreadLocalUpdater(env)) { | |
129 | return; | |
130 | } | |
131 | assert(thread_updater_local_cache_); | |
132 | if (thread_updater_local_cache_) { | |
133 | thread_updater_local_cache_->NewColumnFamilyInfo(db, db->GetName(), cfd, | |
134 | cf_name); | |
135 | } | |
136 | } | |
137 | ||
138 | void ThreadStatusUtil::EraseColumnFamilyInfo( | |
139 | const ColumnFamilyData* cfd) { | |
140 | if (thread_updater_local_cache_ == nullptr) { | |
141 | return; | |
142 | } | |
143 | thread_updater_local_cache_->EraseColumnFamilyInfo(cfd); | |
144 | } | |
145 | ||
146 | void ThreadStatusUtil::EraseDatabaseInfo(const DB* db) { | |
147 | ThreadStatusUpdater* thread_updater = db->GetEnv()->GetThreadStatusUpdater(); | |
148 | if (thread_updater == nullptr) { | |
149 | return; | |
150 | } | |
151 | thread_updater->EraseDatabaseInfo(db); | |
152 | } | |
153 | ||
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(); | |
158 | } | |
159 | return (thread_updater_local_cache_ != nullptr); | |
160 | } | |
161 | ||
162 | AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater( | |
163 | ThreadStatus::OperationStage stage) { | |
164 | prev_stage_ = ThreadStatusUtil::SetThreadOperationStage(stage); | |
165 | } | |
166 | ||
167 | AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() { | |
168 | ThreadStatusUtil::SetThreadOperationStage(prev_stage_); | |
169 | } | |
170 | ||
171 | #else | |
172 | ||
173 | ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ = nullptr; | |
174 | bool ThreadStatusUtil::thread_updater_initialized_ = false; | |
175 | ||
176 | bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env* env) { | |
177 | return false; | |
178 | } | |
179 | ||
180 | void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* cfd, | |
181 | const Env* env, | |
182 | bool enable_thread_tracking) {} | |
183 | ||
184 | void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType op) { | |
185 | } | |
186 | ||
187 | void ThreadStatusUtil::SetThreadOperationProperty( | |
188 | int code, uint64_t value) { | |
189 | } | |
190 | ||
191 | void ThreadStatusUtil::IncreaseThreadOperationProperty( | |
192 | int code, uint64_t delta) { | |
193 | } | |
194 | ||
195 | void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType state) { | |
196 | } | |
197 | ||
198 | void ThreadStatusUtil::NewColumnFamilyInfo(const DB* db, | |
199 | const ColumnFamilyData* cfd, | |
200 | const std::string& cf_name, | |
201 | const Env* env) {} | |
202 | ||
203 | void ThreadStatusUtil::EraseColumnFamilyInfo( | |
204 | const ColumnFamilyData* cfd) { | |
205 | } | |
206 | ||
207 | void ThreadStatusUtil::EraseDatabaseInfo(const DB* db) { | |
208 | } | |
209 | ||
210 | void ThreadStatusUtil::ResetThreadStatus() { | |
211 | } | |
212 | ||
213 | AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater( | |
214 | ThreadStatus::OperationStage stage) { | |
215 | } | |
216 | ||
217 | AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() { | |
218 | } | |
219 | ||
220 | #endif // ROCKSDB_USING_THREAD_STATUS | |
221 | ||
222 | } // namespace rocksdb |