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