2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2014-12-22 21:20:17 +01:00
|
|
|
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/thread_status_util.h"
|
2016-01-26 01:26:53 +01:00
|
|
|
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/thread_status_updater.h"
|
2014-12-22 21:20:17 +01:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-12-22 21:20:17 +01:00
|
|
|
|
2016-12-14 03:22:00 +01:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2018-04-13 02:55:14 +02:00
|
|
|
__thread ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ =
|
|
|
|
nullptr;
|
2014-12-22 21:20:17 +01:00
|
|
|
__thread bool ThreadStatusUtil::thread_updater_initialized_ = false;
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::RegisterThread(const Env* env,
|
|
|
|
ThreadStatus::ThreadType thread_type) {
|
2014-12-22 21:20:17 +01:00
|
|
|
if (!MaybeInitThreadLocalUpdater(env)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(thread_updater_local_cache_);
|
2018-04-13 02:55:14 +02:00
|
|
|
thread_updater_local_cache_->RegisterThread(thread_type, env->GetThreadID());
|
2014-12-22 21:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUtil::UnregisterThread() {
|
|
|
|
thread_updater_initialized_ = false;
|
|
|
|
if (thread_updater_local_cache_ != nullptr) {
|
|
|
|
thread_updater_local_cache_->UnregisterThread();
|
|
|
|
thread_updater_local_cache_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 01:26:53 +01:00
|
|
|
void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* cfd,
|
|
|
|
const Env* env,
|
|
|
|
bool enable_thread_tracking) {
|
|
|
|
if (!MaybeInitThreadLocalUpdater(env)) {
|
2014-12-22 21:20:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(thread_updater_local_cache_);
|
2016-01-26 01:26:53 +01:00
|
|
|
if (cfd != nullptr && enable_thread_tracking) {
|
2015-01-13 09:04:08 +01:00
|
|
|
thread_updater_local_cache_->SetColumnFamilyInfoKey(cfd);
|
|
|
|
} else {
|
|
|
|
// When cfd == nullptr or enable_thread_tracking == false, we set
|
|
|
|
// ColumnFamilyInfoKey to nullptr, which makes SetThreadOperation
|
|
|
|
// and SetThreadState become no-op.
|
|
|
|
thread_updater_local_cache_->SetColumnFamilyInfoKey(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType op) {
|
|
|
|
if (thread_updater_local_cache_ == nullptr) {
|
|
|
|
// thread_updater_local_cache_ must be set in SetColumnFamily
|
|
|
|
// or other ThreadStatusUtil functions.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-10 22:51:28 +01:00
|
|
|
if (op != ThreadStatus::OP_UNKNOWN) {
|
2015-03-24 00:35:04 +01:00
|
|
|
uint64_t current_time = Env::Default()->NowMicros();
|
2015-03-10 22:51:28 +01:00
|
|
|
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);
|
|
|
|
}
|
2015-01-13 09:04:08 +01:00
|
|
|
thread_updater_local_cache_->SetThreadOperation(op);
|
|
|
|
}
|
|
|
|
|
2015-03-13 18:45:40 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::SetThreadOperationProperty(int code, uint64_t value) {
|
2015-05-07 07:50:35 +02:00
|
|
|
if (thread_updater_local_cache_ == nullptr) {
|
|
|
|
// thread_updater_local_cache_ must be set in SetColumnFamily
|
|
|
|
// or other ThreadStatusUtil functions.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
thread_updater_local_cache_->SetThreadOperationProperty(code, value);
|
2015-05-07 07:50:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::IncreaseThreadOperationProperty(int code,
|
|
|
|
uint64_t delta) {
|
2015-05-07 07:50:35 +02:00
|
|
|
if (thread_updater_local_cache_ == nullptr) {
|
|
|
|
// thread_updater_local_cache_ must be set in SetColumnFamily
|
|
|
|
// or other ThreadStatusUtil functions.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
thread_updater_local_cache_->IncreaseThreadOperationProperty(code, delta);
|
2015-05-07 07:50:35 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
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();
|
2014-12-22 21:20:17 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:26:53 +01:00
|
|
|
void ThreadStatusUtil::NewColumnFamilyInfo(const DB* db,
|
|
|
|
const ColumnFamilyData* cfd,
|
|
|
|
const std::string& cf_name,
|
|
|
|
const Env* env) {
|
|
|
|
if (!MaybeInitThreadLocalUpdater(env)) {
|
2014-12-22 21:20:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(thread_updater_local_cache_);
|
|
|
|
if (thread_updater_local_cache_) {
|
2016-01-26 01:26:53 +01:00
|
|
|
thread_updater_local_cache_->NewColumnFamilyInfo(db, db->GetName(), cfd,
|
|
|
|
cf_name);
|
2014-12-22 21:20:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::EraseColumnFamilyInfo(const ColumnFamilyData* cfd) {
|
2014-12-22 21:20:17 +01:00
|
|
|
if (thread_updater_local_cache_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
thread_updater_local_cache_->EraseColumnFamilyInfo(cfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUtil::EraseDatabaseInfo(const DB* db) {
|
2017-03-30 19:31:26 +02:00
|
|
|
ThreadStatusUpdater* thread_updater = db->GetEnv()->GetThreadStatusUpdater();
|
|
|
|
if (thread_updater == nullptr) {
|
2014-12-22 21:20:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-03-30 19:31:26 +02:00
|
|
|
thread_updater->EraseDatabaseInfo(db);
|
2014-12-22 21:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-13 18:45:40 +01:00
|
|
|
AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater(
|
|
|
|
ThreadStatus::OperationStage stage) {
|
|
|
|
prev_stage_ = ThreadStatusUtil::SetThreadOperationStage(stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {
|
|
|
|
ThreadStatusUtil::SetThreadOperationStage(prev_stage_);
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ = nullptr;
|
|
|
|
bool ThreadStatusUtil::thread_updater_initialized_ = false;
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env* /*env*/) {
|
2014-12-22 21:20:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* /*cfd*/,
|
|
|
|
const Env* /*env*/,
|
|
|
|
bool /*enable_thread_tracking*/) {}
|
2014-12-22 21:20:17 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType /*op*/) {}
|
2015-01-13 09:04:08 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::SetThreadOperationProperty(int /*code*/,
|
|
|
|
uint64_t /*value*/) {}
|
2015-05-07 07:50:35 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::IncreaseThreadOperationProperty(int /*code*/,
|
|
|
|
uint64_t /*delta*/) {}
|
2015-05-07 07:50:35 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType /*state*/) {}
|
2015-01-13 09:04:08 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::NewColumnFamilyInfo(const DB* /*db*/,
|
|
|
|
const ColumnFamilyData* /*cfd*/,
|
|
|
|
const std::string& /*cf_name*/,
|
|
|
|
const Env* /*env*/) {}
|
2014-12-22 21:20:17 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::EraseColumnFamilyInfo(const ColumnFamilyData* /*cfd*/) {}
|
2014-12-22 21:20:17 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::EraseDatabaseInfo(const DB* /*db*/) {}
|
2014-12-22 21:20:17 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUtil::ResetThreadStatus() {}
|
2015-01-13 09:04:08 +01:00
|
|
|
|
2015-03-13 18:45:40 +01:00
|
|
|
AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater(
|
2018-04-13 02:55:14 +02:00
|
|
|
ThreadStatus::OperationStage /*stage*/) {}
|
2015-03-13 18:45:40 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {}
|
2015-03-13 18:45:40 +01:00
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|