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-11-20 19:49:32 +01:00
|
|
|
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/thread_status_updater.h"
|
2021-01-26 07:07:26 +01:00
|
|
|
|
2015-01-09 21:57:11 +01:00
|
|
|
#include <memory>
|
2021-01-26 07:07:26 +01:00
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
#include "port/likely.h"
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "rocksdb/env.h"
|
2021-01-26 07:07:26 +01:00
|
|
|
#include "rocksdb/system_clock.h"
|
2014-11-20 19:49:32 +01:00
|
|
|
#include "util/mutexlock.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2016-12-14 03:22:00 +01:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
__thread ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = nullptr;
|
2014-11-21 01:02:03 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType ttype,
|
|
|
|
uint64_t thread_id) {
|
2015-06-17 20:21:18 +02:00
|
|
|
if (UNLIKELY(thread_status_data_ == nullptr)) {
|
|
|
|
thread_status_data_ = new ThreadStatusData();
|
|
|
|
thread_status_data_->thread_type = ttype;
|
|
|
|
thread_status_data_->thread_id = thread_id;
|
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
thread_data_set_.insert(thread_status_data_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::UnregisterThread() {
|
2014-11-20 19:49:32 +01:00
|
|
|
if (thread_status_data_ != nullptr) {
|
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
thread_data_set_.erase(thread_status_data_);
|
|
|
|
delete thread_status_data_;
|
2014-11-21 00:45:56 +01:00
|
|
|
thread_status_data_ = nullptr;
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
void ThreadStatusUpdater::ResetThreadStatus() {
|
|
|
|
ClearThreadState();
|
|
|
|
ClearThreadOperation();
|
|
|
|
SetColumnFamilyInfoKey(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(const void* cf_key) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = Get();
|
|
|
|
if (data == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-13 09:04:08 +01:00
|
|
|
// set the tracking flag based on whether cf_key is non-null or not.
|
|
|
|
// If enable_thread_tracking is set to false, the input cf_key
|
|
|
|
// would be nullptr.
|
|
|
|
data->enable_tracking = (cf_key != nullptr);
|
2015-07-02 01:13:49 +02:00
|
|
|
data->cf_key.store(const_cast<void*>(cf_key), std::memory_order_relaxed);
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
const void* ThreadStatusUpdater::GetColumnFamilyInfoKey() {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 09:04:08 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return data->cf_key.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
void ThreadStatusUpdater::SetThreadOperation(
|
|
|
|
const ThreadStatus::OperationType type) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 09:04:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-05-07 07:50:35 +02:00
|
|
|
// NOTE: Our practice here is to set all the thread operation properties
|
|
|
|
// and stage before we set thread operation, and thread operation
|
|
|
|
// will be set in std::memory_order_release. This is to ensure
|
|
|
|
// whenever a thread operation is not OP_UNKNOWN, we will always
|
|
|
|
// have a consistent information on its properties.
|
|
|
|
data->operation_type.store(type, std::memory_order_release);
|
|
|
|
if (type == ThreadStatus::OP_UNKNOWN) {
|
|
|
|
data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
|
2018-04-13 02:55:14 +02:00
|
|
|
std::memory_order_relaxed);
|
2015-05-07 07:50:35 +02:00
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::SetThreadOperationProperty(int i, uint64_t value) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-05-07 07:50:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_properties[i].store(value, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(int i,
|
|
|
|
uint64_t delta) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-05-07 07:50:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_properties[i].fetch_add(delta, std::memory_order_relaxed);
|
2014-12-30 19:39:13 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 00:35:04 +01:00
|
|
|
void ThreadStatusUpdater::SetOperationStartTime(const uint64_t start_time) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-03-10 22:51:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_start_time.store(start_time, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
void ThreadStatusUpdater::ClearThreadOperation() {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 09:04:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-03-13 18:45:40 +01:00
|
|
|
data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
|
2018-04-13 02:55:14 +02:00
|
|
|
std::memory_order_relaxed);
|
|
|
|
data->operation_type.store(ThreadStatus::OP_UNKNOWN,
|
|
|
|
std::memory_order_relaxed);
|
2015-05-07 07:50:35 +02:00
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadOperationProperties() {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-05-07 07:50:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
|
|
|
|
data->op_properties[i].store(0, std::memory_order_relaxed);
|
|
|
|
}
|
2014-12-30 19:39:13 +01:00
|
|
|
}
|
|
|
|
|
2015-03-13 18:45:40 +01:00
|
|
|
ThreadStatus::OperationStage ThreadStatusUpdater::SetThreadOperationStage(
|
|
|
|
ThreadStatus::OperationStage stage) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-03-13 18:45:40 +01:00
|
|
|
return ThreadStatus::STAGE_UNKNOWN;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
return data->operation_stage.exchange(stage, std::memory_order_relaxed);
|
2015-03-13 18:45:40 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::SetThreadState(const ThreadStatus::StateType type) {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 09:04:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-12-30 19:39:13 +01:00
|
|
|
data->state_type.store(type, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadState() {
|
2015-06-17 20:21:18 +02:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 09:04:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
data->state_type.store(ThreadStatus::STATE_UNKNOWN,
|
|
|
|
std::memory_order_relaxed);
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
Status ThreadStatusUpdater::GetThreadList(
|
|
|
|
std::vector<ThreadStatus>* thread_list) {
|
2014-11-20 19:49:32 +01:00
|
|
|
thread_list->clear();
|
|
|
|
std::vector<std::shared_ptr<ThreadStatusData>> valid_list;
|
2021-01-26 07:07:26 +01:00
|
|
|
uint64_t now_micros = SystemClock::Default()->NowMicros();
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
for (auto* thread_data : thread_data_set_) {
|
|
|
|
assert(thread_data);
|
2018-04-13 02:55:14 +02:00
|
|
|
auto thread_id = thread_data->thread_id.load(std::memory_order_relaxed);
|
|
|
|
auto thread_type = thread_data->thread_type.load(std::memory_order_relaxed);
|
2014-12-30 19:39:13 +01:00
|
|
|
// Since any change to cf_info_map requires thread_list_mutex,
|
|
|
|
// which is currently held by GetThreadList(), here we can safely
|
|
|
|
// use "memory_order_relaxed" to load the cf_key.
|
2018-04-13 02:55:14 +02:00
|
|
|
auto cf_key = thread_data->cf_key.load(std::memory_order_relaxed);
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
ThreadStatus::OperationType op_type = ThreadStatus::OP_UNKNOWN;
|
2015-03-13 18:45:40 +01:00
|
|
|
ThreadStatus::OperationStage op_stage = ThreadStatus::STAGE_UNKNOWN;
|
2014-12-30 19:39:13 +01:00
|
|
|
ThreadStatus::StateType state_type = ThreadStatus::STATE_UNKNOWN;
|
2015-03-24 00:35:04 +01:00
|
|
|
uint64_t op_elapsed_micros = 0;
|
2015-05-07 07:50:35 +02:00
|
|
|
uint64_t op_props[ThreadStatus::kNumOperationProperties] = {0};
|
2017-09-28 23:22:06 +02:00
|
|
|
|
|
|
|
auto iter = cf_info_map_.find(cf_key);
|
|
|
|
if (iter != cf_info_map_.end()) {
|
2018-04-13 02:55:14 +02:00
|
|
|
op_type = thread_data->operation_type.load(std::memory_order_acquire);
|
2014-11-20 19:49:32 +01:00
|
|
|
// display lower-level info only when higher-level info is available.
|
2014-12-30 19:39:13 +01:00
|
|
|
if (op_type != ThreadStatus::OP_UNKNOWN) {
|
2015-03-24 00:35:04 +01:00
|
|
|
op_elapsed_micros = now_micros - thread_data->op_start_time.load(
|
2018-04-13 02:55:14 +02:00
|
|
|
std::memory_order_relaxed);
|
|
|
|
op_stage = thread_data->operation_stage.load(std::memory_order_relaxed);
|
|
|
|
state_type = thread_data->state_type.load(std::memory_order_relaxed);
|
2015-05-07 07:50:35 +02:00
|
|
|
for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
|
2018-04-13 02:55:14 +02:00
|
|
|
op_props[i] =
|
|
|
|
thread_data->op_properties[i].load(std::memory_order_relaxed);
|
2015-05-07 07:50:35 +02:00
|
|
|
}
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 23:22:06 +02:00
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
thread_list->emplace_back(
|
2015-06-11 23:18:02 +02:00
|
|
|
thread_id, thread_type,
|
2017-09-28 23:22:06 +02:00
|
|
|
iter != cf_info_map_.end() ? iter->second.db_name : "",
|
2018-04-13 02:55:14 +02:00
|
|
|
iter != cf_info_map_.end() ? iter->second.cf_name : "", op_type,
|
|
|
|
op_elapsed_micros, op_stage, op_props, state_type);
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2015-06-17 20:21:18 +02:00
|
|
|
ThreadStatusData* ThreadStatusUpdater::GetLocalThreadStatus() {
|
|
|
|
if (thread_status_data_ == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!thread_status_data_->enable_tracking) {
|
2018-04-13 02:55:14 +02:00
|
|
|
assert(thread_status_data_->cf_key.load(std::memory_order_relaxed) ==
|
|
|
|
nullptr);
|
2015-06-17 20:21:18 +02:00
|
|
|
return nullptr;
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
return thread_status_data_;
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::NewColumnFamilyInfo(const void* db_key,
|
|
|
|
const std::string& db_name,
|
|
|
|
const void* cf_key,
|
|
|
|
const std::string& cf_name) {
|
2014-12-30 19:39:13 +01:00
|
|
|
// Acquiring same lock as GetThreadList() to guarantee
|
|
|
|
// a consistent view of global column family table (cf_info_map).
|
2014-11-20 19:49:32 +01:00
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
cf_info_map_.emplace(std::piecewise_construct, std::make_tuple(cf_key),
|
|
|
|
std::make_tuple(db_key, db_name, cf_name));
|
2014-11-20 19:49:32 +01:00
|
|
|
db_key_map_[db_key].insert(cf_key);
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* cf_key) {
|
2014-12-30 19:39:13 +01:00
|
|
|
// Acquiring same lock as GetThreadList() to guarantee
|
|
|
|
// a consistent view of global column family table (cf_info_map).
|
2014-11-20 19:49:32 +01:00
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
2018-04-13 02:55:14 +02:00
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
auto cf_pair = cf_info_map_.find(cf_key);
|
2017-09-28 23:22:06 +02:00
|
|
|
if (cf_pair != cf_info_map_.end()) {
|
|
|
|
// Remove its entry from db_key_map_ by the following steps:
|
|
|
|
// 1. Obtain the entry in db_key_map_ whose set contains cf_key
|
|
|
|
// 2. Remove it from the set.
|
|
|
|
ConstantColumnFamilyInfo& cf_info = cf_pair->second;
|
|
|
|
auto db_pair = db_key_map_.find(cf_info.db_key);
|
|
|
|
assert(db_pair != db_key_map_.end());
|
2018-02-02 21:14:42 +01:00
|
|
|
size_t result __attribute__((__unused__));
|
2017-10-23 23:20:53 +02:00
|
|
|
result = db_pair->second.erase(cf_key);
|
2017-09-28 23:22:06 +02:00
|
|
|
assert(result);
|
|
|
|
cf_info_map_.erase(cf_pair);
|
2016-03-05 01:03:31 +01:00
|
|
|
}
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
|
2014-12-30 19:39:13 +01:00
|
|
|
// Acquiring same lock as GetThreadList() to guarantee
|
|
|
|
// a consistent view of global column family table (cf_info_map).
|
2014-11-20 19:49:32 +01:00
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
auto db_pair = db_key_map_.find(db_key);
|
|
|
|
if (UNLIKELY(db_pair == db_key_map_.end())) {
|
|
|
|
// In some occasional cases such as DB::Open fails, we won't
|
|
|
|
// register ColumnFamilyInfo for a db.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto cf_key : db_pair->second) {
|
|
|
|
auto cf_pair = cf_info_map_.find(cf_key);
|
2017-09-28 23:22:06 +02:00
|
|
|
if (cf_pair != cf_info_map_.end()) {
|
|
|
|
cf_info_map_.erase(cf_pair);
|
2016-03-05 01:03:31 +01:00
|
|
|
}
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
db_key_map_.erase(db_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType /*ttype*/,
|
|
|
|
uint64_t /*thread_id*/) {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::UnregisterThread() {}
|
2015-06-03 02:07:16 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::ResetThreadStatus() {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(const void* /*cf_key*/) {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
void ThreadStatusUpdater::SetThreadOperation(
|
2018-04-13 02:55:14 +02:00
|
|
|
const ThreadStatus::OperationType /*type*/) {}
|
2014-12-30 19:39:13 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::ClearThreadOperation() {}
|
2014-12-30 19:39:13 +01:00
|
|
|
|
|
|
|
void ThreadStatusUpdater::SetThreadState(
|
2018-04-13 02:55:14 +02:00
|
|
|
const ThreadStatus::StateType /*type*/) {}
|
2014-12-30 19:39:13 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::ClearThreadState() {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
Status ThreadStatusUpdater::GetThreadList(
|
2018-04-13 02:55:14 +02:00
|
|
|
std::vector<ThreadStatus>* /*thread_list*/) {
|
2014-11-20 19:49:32 +01:00
|
|
|
return Status::NotSupported(
|
|
|
|
"GetThreadList is not supported in the current running environment.");
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::NewColumnFamilyInfo(const void* /*db_key*/,
|
|
|
|
const std::string& /*db_name*/,
|
|
|
|
const void* /*cf_key*/,
|
|
|
|
const std::string& /*cf_name*/) {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* /*cf_key*/) {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* /*db_key*/) {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::SetThreadOperationProperty(int /*i*/,
|
|
|
|
uint64_t /*value*/) {}
|
2015-05-07 07:50:35 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(int /*i*/,
|
|
|
|
uint64_t /*delta*/) {}
|
2015-05-07 07:50:35 +02:00
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|