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-11-20 18:49:32 +00:00
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "monitoring/thread_status_updater.h"
|
2015-01-09 20:57:11 +00:00
|
|
|
#include <memory>
|
2014-11-20 18:49:32 +00:00
|
|
|
#include "port/likely.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "rocksdb/env.h"
|
2014-11-20 18:49:32 +00:00
|
|
|
#include "util/mutexlock.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2016-12-14 02:22:00 +00:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
__thread ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = nullptr;
|
2014-11-21 00:02:03 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType ttype,
|
|
|
|
uint64_t thread_id) {
|
2015-06-17 18:21:18 +00: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 20:20:17 +00:00
|
|
|
void ThreadStatusUpdater::UnregisterThread() {
|
2014-11-20 18:49:32 +00: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-20 23:45:56 +00:00
|
|
|
thread_status_data_ = nullptr;
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 08:04:08 +00:00
|
|
|
void ThreadStatusUpdater::ResetThreadStatus() {
|
|
|
|
ClearThreadState();
|
|
|
|
ClearThreadOperation();
|
|
|
|
SetColumnFamilyInfoKey(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(const void* cf_key) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = Get();
|
|
|
|
if (data == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-13 08:04:08 +00: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-01 23:13:49 +00:00
|
|
|
data->cf_key.store(const_cast<void*>(cf_key), std::memory_order_relaxed);
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 08:04:08 +00:00
|
|
|
const void* ThreadStatusUpdater::GetColumnFamilyInfoKey() {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 08:04:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return data->cf_key.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:39:13 +00:00
|
|
|
void ThreadStatusUpdater::SetThreadOperation(
|
|
|
|
const ThreadStatus::OperationType type) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 08:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-07 05:50:35 +00: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 00:55:14 +00:00
|
|
|
std::memory_order_relaxed);
|
2015-05-07 05:50:35 +00:00
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::SetThreadOperationProperty(int i, uint64_t value) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-05-07 05:50:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_properties[i].store(value, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(int i,
|
|
|
|
uint64_t delta) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-05-07 05:50:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_properties[i].fetch_add(delta, std::memory_order_relaxed);
|
2014-12-30 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:35:04 +00:00
|
|
|
void ThreadStatusUpdater::SetOperationStartTime(const uint64_t start_time) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-03-10 21:51:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_start_time.store(start_time, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:39:13 +00:00
|
|
|
void ThreadStatusUpdater::ClearThreadOperation() {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 08:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-13 17:45:40 +00:00
|
|
|
data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
|
2018-04-13 00:55:14 +00:00
|
|
|
std::memory_order_relaxed);
|
|
|
|
data->operation_type.store(ThreadStatus::OP_UNKNOWN,
|
|
|
|
std::memory_order_relaxed);
|
2015-05-07 05:50:35 +00:00
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadOperationProperties() {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-05-07 05:50:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
|
|
|
|
data->op_properties[i].store(0, std::memory_order_relaxed);
|
|
|
|
}
|
2014-12-30 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 17:45:40 +00:00
|
|
|
ThreadStatus::OperationStage ThreadStatusUpdater::SetThreadOperationStage(
|
|
|
|
ThreadStatus::OperationStage stage) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-03-13 17:45:40 +00:00
|
|
|
return ThreadStatus::STAGE_UNKNOWN;
|
|
|
|
}
|
2018-04-13 00:55:14 +00:00
|
|
|
return data->operation_stage.exchange(stage, std::memory_order_relaxed);
|
2015-03-13 17:45:40 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::SetThreadState(const ThreadStatus::StateType type) {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 08:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-30 18:39:13 +00:00
|
|
|
data->state_type.store(type, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadState() {
|
2015-06-17 18:21:18 +00:00
|
|
|
auto* data = GetLocalThreadStatus();
|
|
|
|
if (data == nullptr) {
|
2015-01-13 08:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-13 00:55:14 +00:00
|
|
|
data->state_type.store(ThreadStatus::STATE_UNKNOWN,
|
|
|
|
std::memory_order_relaxed);
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
Status ThreadStatusUpdater::GetThreadList(
|
|
|
|
std::vector<ThreadStatus>* thread_list) {
|
2014-11-20 18:49:32 +00:00
|
|
|
thread_list->clear();
|
|
|
|
std::vector<std::shared_ptr<ThreadStatusData>> valid_list;
|
2015-03-23 23:35:04 +00:00
|
|
|
uint64_t now_micros = Env::Default()->NowMicros();
|
2014-11-20 18:49:32 +00:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
for (auto* thread_data : thread_data_set_) {
|
|
|
|
assert(thread_data);
|
2018-04-13 00:55:14 +00: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 18:39:13 +00: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 00:55:14 +00:00
|
|
|
auto cf_key = thread_data->cf_key.load(std::memory_order_relaxed);
|
|
|
|
|
2014-12-30 18:39:13 +00:00
|
|
|
ThreadStatus::OperationType op_type = ThreadStatus::OP_UNKNOWN;
|
2015-03-13 17:45:40 +00:00
|
|
|
ThreadStatus::OperationStage op_stage = ThreadStatus::STAGE_UNKNOWN;
|
2014-12-30 18:39:13 +00:00
|
|
|
ThreadStatus::StateType state_type = ThreadStatus::STATE_UNKNOWN;
|
2015-03-23 23:35:04 +00:00
|
|
|
uint64_t op_elapsed_micros = 0;
|
2015-05-07 05:50:35 +00:00
|
|
|
uint64_t op_props[ThreadStatus::kNumOperationProperties] = {0};
|
2017-09-28 21:22:06 +00:00
|
|
|
|
|
|
|
auto iter = cf_info_map_.find(cf_key);
|
|
|
|
if (iter != cf_info_map_.end()) {
|
2018-04-13 00:55:14 +00:00
|
|
|
op_type = thread_data->operation_type.load(std::memory_order_acquire);
|
2014-11-20 18:49:32 +00:00
|
|
|
// display lower-level info only when higher-level info is available.
|
2014-12-30 18:39:13 +00:00
|
|
|
if (op_type != ThreadStatus::OP_UNKNOWN) {
|
2015-03-23 23:35:04 +00:00
|
|
|
op_elapsed_micros = now_micros - thread_data->op_start_time.load(
|
2018-04-13 00:55:14 +00: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 05:50:35 +00:00
|
|
|
for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
|
2018-04-13 00:55:14 +00:00
|
|
|
op_props[i] =
|
|
|
|
thread_data->op_properties[i].load(std::memory_order_relaxed);
|
2015-05-07 05:50:35 +00:00
|
|
|
}
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 21:22:06 +00:00
|
|
|
|
2014-11-20 18:49:32 +00:00
|
|
|
thread_list->emplace_back(
|
2015-06-11 21:18:02 +00:00
|
|
|
thread_id, thread_type,
|
2017-09-28 21:22:06 +00:00
|
|
|
iter != cf_info_map_.end() ? iter->second.db_name : "",
|
2018-04-13 00:55:14 +00:00
|
|
|
iter != cf_info_map_.end() ? iter->second.cf_name : "", op_type,
|
|
|
|
op_elapsed_micros, op_stage, op_props, state_type);
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2015-06-17 18:21:18 +00:00
|
|
|
ThreadStatusData* ThreadStatusUpdater::GetLocalThreadStatus() {
|
|
|
|
if (thread_status_data_ == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!thread_status_data_->enable_tracking) {
|
2018-04-13 00:55:14 +00:00
|
|
|
assert(thread_status_data_->cf_key.load(std::memory_order_relaxed) ==
|
|
|
|
nullptr);
|
2015-06-17 18:21:18 +00:00
|
|
|
return nullptr;
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
return thread_status_data_;
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:55:14 +00: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 18:39:13 +00:00
|
|
|
// Acquiring same lock as GetThreadList() to guarantee
|
|
|
|
// a consistent view of global column family table (cf_info_map).
|
2014-11-20 18:49:32 +00:00
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
|
2018-04-13 00:55:14 +00: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 18:49:32 +00:00
|
|
|
db_key_map_[db_key].insert(cf_key);
|
|
|
|
}
|
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* cf_key) {
|
2014-12-30 18:39:13 +00:00
|
|
|
// Acquiring same lock as GetThreadList() to guarantee
|
|
|
|
// a consistent view of global column family table (cf_info_map).
|
2014-11-20 18:49:32 +00:00
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
2018-04-13 00:55:14 +00:00
|
|
|
|
2014-11-20 18:49:32 +00:00
|
|
|
auto cf_pair = cf_info_map_.find(cf_key);
|
2017-09-28 21:22:06 +00: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 20:14:42 +00:00
|
|
|
size_t result __attribute__((__unused__));
|
2017-10-23 21:20:53 +00:00
|
|
|
result = db_pair->second.erase(cf_key);
|
2017-09-28 21:22:06 +00:00
|
|
|
assert(result);
|
|
|
|
cf_info_map_.erase(cf_pair);
|
2016-03-05 00:03:31 +00:00
|
|
|
}
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
|
2014-12-30 18:39:13 +00:00
|
|
|
// Acquiring same lock as GetThreadList() to guarantee
|
|
|
|
// a consistent view of global column family table (cf_info_map).
|
2014-11-20 18:49:32 +00: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 21:22:06 +00:00
|
|
|
if (cf_pair != cf_info_map_.end()) {
|
|
|
|
cf_info_map_.erase(cf_pair);
|
2016-03-05 00:03:31 +00:00
|
|
|
}
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
|
|
|
db_key_map_.erase(db_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType /*ttype*/,
|
|
|
|
uint64_t /*thread_id*/) {}
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::UnregisterThread() {}
|
2015-06-03 00:07:16 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::ResetThreadStatus() {}
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(const void* /*cf_key*/) {}
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2014-12-30 18:39:13 +00:00
|
|
|
void ThreadStatusUpdater::SetThreadOperation(
|
2018-04-13 00:55:14 +00:00
|
|
|
const ThreadStatus::OperationType /*type*/) {}
|
2014-12-30 18:39:13 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::ClearThreadOperation() {}
|
2014-12-30 18:39:13 +00:00
|
|
|
|
|
|
|
void ThreadStatusUpdater::SetThreadState(
|
2018-04-13 00:55:14 +00:00
|
|
|
const ThreadStatus::StateType /*type*/) {}
|
2014-12-30 18:39:13 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::ClearThreadState() {}
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
Status ThreadStatusUpdater::GetThreadList(
|
2018-04-13 00:55:14 +00:00
|
|
|
std::vector<ThreadStatus>* /*thread_list*/) {
|
2014-11-20 18:49:32 +00:00
|
|
|
return Status::NotSupported(
|
|
|
|
"GetThreadList is not supported in the current running environment.");
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:55:14 +00: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 18:49:32 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* /*cf_key*/) {}
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* /*db_key*/) {}
|
2014-11-20 18:49:32 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::SetThreadOperationProperty(int /*i*/,
|
|
|
|
uint64_t /*value*/) {}
|
2015-05-07 05:50:35 +00:00
|
|
|
|
2018-04-13 00:55:14 +00:00
|
|
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(int /*i*/,
|
|
|
|
uint64_t /*delta*/) {}
|
2015-05-07 05:50:35 +00:00
|
|
|
|
2014-11-20 18:49:32 +00:00
|
|
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
|
|
|
} // namespace rocksdb
|