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