2014-11-20 19:49:32 +01:00
|
|
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
2015-01-09 21:57:11 +01:00
|
|
|
#include <memory>
|
2015-03-24 00:35:04 +01:00
|
|
|
#include "rocksdb/env.h"
|
2014-11-20 19:49:32 +01:00
|
|
|
#include "port/likely.h"
|
|
|
|
#include "util/mutexlock.h"
|
2014-12-22 21:20:17 +01:00
|
|
|
#include "util/thread_status_updater.h"
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
#if ROCKSDB_USING_THREAD_STATUS
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
__thread ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = nullptr;
|
2014-11-21 01:02:03 +01:00
|
|
|
|
2015-06-03 02:07:16 +02:00
|
|
|
uint64_t ThreadStatusUpdater::GetThreadID() {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
return data->thread_id;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::SetThreadType(
|
2014-11-20 19:49:32 +01:00
|
|
|
ThreadStatus::ThreadType ttype) {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
data->thread_type.store(ttype, std::memory_order_relaxed);
|
2015-05-07 07:50:35 +02:00
|
|
|
ClearThreadOperationProperties();
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
void ThreadStatusUpdater::ResetThreadStatus() {
|
|
|
|
ClearThreadState();
|
|
|
|
ClearThreadOperation();
|
|
|
|
SetColumnFamilyInfoKey(nullptr);
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(
|
2014-11-20 19:49:32 +01:00
|
|
|
const void* cf_key) {
|
|
|
|
auto* data = InitAndGet();
|
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);
|
2014-11-20 19:49:32 +01:00
|
|
|
data->cf_key.store(cf_key, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
const void* ThreadStatusUpdater::GetColumnFamilyInfoKey() {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
if (data->enable_tracking == false) {
|
|
|
|
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) {
|
2014-11-20 19:49:32 +01:00
|
|
|
auto* data = InitAndGet();
|
2015-01-13 09:04:08 +01:00
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
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,
|
|
|
|
std::memory_order_relaxed);
|
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::SetThreadOperationProperty(
|
|
|
|
int i, uint64_t value) {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_properties[i].store(value, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(
|
|
|
|
int i, uint64_t delta) {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
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-03-10 22:51:28 +01:00
|
|
|
auto* data = InitAndGet();
|
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->op_start_time.store(start_time, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
void ThreadStatusUpdater::ClearThreadOperation() {
|
|
|
|
auto* data = InitAndGet();
|
2015-01-13 09:04:08 +01:00
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-13 18:45:40 +01:00
|
|
|
data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
|
|
|
|
std::memory_order_relaxed);
|
2014-12-30 19:39:13 +01:00
|
|
|
data->operation_type.store(
|
|
|
|
ThreadStatus::OP_UNKNOWN, std::memory_order_relaxed);
|
2015-05-07 07:50:35 +02:00
|
|
|
ClearThreadOperationProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadOperationProperties() {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
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) {
|
|
|
|
auto* data = InitAndGet();
|
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
return ThreadStatus::STAGE_UNKNOWN;
|
|
|
|
}
|
|
|
|
return data->operation_stage.exchange(
|
|
|
|
stage, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
void ThreadStatusUpdater::SetThreadState(
|
|
|
|
const ThreadStatus::StateType type) {
|
|
|
|
auto* data = InitAndGet();
|
2015-01-13 09:04:08 +01:00
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-30 19:39:13 +01:00
|
|
|
data->state_type.store(type, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadState() {
|
|
|
|
auto* data = InitAndGet();
|
2015-01-13 09:04:08 +01:00
|
|
|
if (!data->enable_tracking) {
|
|
|
|
assert(data->cf_key.load(std::memory_order_relaxed) == nullptr);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-30 19:39:13 +01: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;
|
2015-03-24 00:35:04 +01:00
|
|
|
uint64_t now_micros = Env::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);
|
|
|
|
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.
|
2014-11-20 19:49:32 +01:00
|
|
|
auto cf_key = thread_data->cf_key.load(
|
|
|
|
std::memory_order_relaxed);
|
2014-11-20 22:44:37 +01:00
|
|
|
auto iter = cf_info_map_.find(cf_key);
|
2014-11-20 19:49:32 +01:00
|
|
|
assert(cf_key == 0 || iter != cf_info_map_.end());
|
|
|
|
auto* cf_info = iter != cf_info_map_.end() ?
|
2014-11-22 09:04:41 +01:00
|
|
|
iter->second.get() : nullptr;
|
2014-11-20 19:49:32 +01:00
|
|
|
const std::string* db_name = nullptr;
|
|
|
|
const std::string* cf_name = nullptr;
|
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};
|
2014-11-20 19:49:32 +01:00
|
|
|
if (cf_info != nullptr) {
|
|
|
|
db_name = &cf_info->db_name;
|
|
|
|
cf_name = &cf_info->cf_name;
|
2014-12-30 19:39:13 +01:00
|
|
|
op_type = thread_data->operation_type.load(
|
2015-05-07 07:50:35 +02:00
|
|
|
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(
|
2015-03-10 22:51:28 +01:00
|
|
|
std::memory_order_relaxed);
|
2015-03-13 18:45:40 +01:00
|
|
|
op_stage = thread_data->operation_stage.load(
|
|
|
|
std::memory_order_relaxed);
|
2014-12-30 19:39:13 +01:00
|
|
|
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) {
|
|
|
|
op_props[i] = thread_data->op_properties[i].load(
|
|
|
|
std::memory_order_relaxed);
|
|
|
|
}
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
thread_list->emplace_back(
|
|
|
|
thread_data->thread_id, thread_type,
|
|
|
|
db_name ? *db_name : "",
|
|
|
|
cf_name ? *cf_name : "",
|
2015-05-07 07:50:35 +02:00
|
|
|
op_type, op_elapsed_micros, op_stage, op_props,
|
|
|
|
state_type);
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
ThreadStatusData* ThreadStatusUpdater::InitAndGet() {
|
2014-11-20 19:49:32 +01:00
|
|
|
if (UNLIKELY(thread_status_data_ == nullptr)) {
|
|
|
|
thread_status_data_ = new ThreadStatusData();
|
|
|
|
thread_status_data_->thread_id = reinterpret_cast<uint64_t>(
|
|
|
|
thread_status_data_);
|
|
|
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
|
|
|
thread_data_set_.insert(thread_status_data_);
|
|
|
|
}
|
|
|
|
return thread_status_data_;
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::NewColumnFamilyInfo(
|
2014-11-20 19:49:32 +01:00
|
|
|
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_);
|
|
|
|
|
2014-11-22 09:04:41 +01:00
|
|
|
cf_info_map_[cf_key].reset(
|
|
|
|
new ConstantColumnFamilyInfo(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_);
|
|
|
|
auto cf_pair = cf_info_map_.find(cf_key);
|
|
|
|
assert(cf_pair != cf_info_map_.end());
|
|
|
|
|
2014-11-22 09:04:41 +01:00
|
|
|
auto* cf_info = cf_pair->second.get();
|
2014-11-20 19:49:32 +01:00
|
|
|
assert(cf_info);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
auto db_pair = db_key_map_.find(cf_info->db_key);
|
|
|
|
assert(db_pair != db_key_map_.end());
|
2014-11-20 20:00:21 +01:00
|
|
|
size_t result __attribute__((unused)) = db_pair->second.erase(cf_key);
|
2014-11-20 19:49:32 +01:00
|
|
|
assert(result);
|
|
|
|
|
2014-11-22 09:04:41 +01:00
|
|
|
cf_pair->second.reset();
|
2014-11-20 19:49:32 +01:00
|
|
|
result = cf_info_map_.erase(cf_key);
|
|
|
|
assert(result);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-20 20:00:21 +01:00
|
|
|
size_t result __attribute__((unused)) = 0;
|
2014-11-20 19:49:32 +01:00
|
|
|
for (auto cf_key : db_pair->second) {
|
|
|
|
auto cf_pair = cf_info_map_.find(cf_key);
|
|
|
|
assert(cf_pair != cf_info_map_.end());
|
2014-11-22 09:04:41 +01:00
|
|
|
cf_pair->second.reset();
|
2014-11-20 20:45:30 +01:00
|
|
|
result = cf_info_map_.erase(cf_key);
|
2014-11-20 19:49:32 +01:00
|
|
|
assert(result);
|
|
|
|
}
|
|
|
|
db_key_map_.erase(db_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::UnregisterThread() {
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
void ThreadStatusUpdater::ResetThreadStatus() {
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:07:16 +02:00
|
|
|
uint64_t ThreadStatusUpdater::GetThreadID() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::SetThreadType(
|
2014-11-20 19:49:32 +01:00
|
|
|
ThreadStatus::ThreadType ttype) {
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(
|
2014-11-20 19:49:32 +01:00
|
|
|
const void* cf_key) {
|
|
|
|
}
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
void ThreadStatusUpdater::SetThreadOperation(
|
|
|
|
const ThreadStatus::OperationType type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadOperation() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::SetThreadState(
|
|
|
|
const ThreadStatus::StateType type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::ClearThreadState() {
|
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
|
|
|
return Status::NotSupported(
|
|
|
|
"GetThreadList is not supported in the current running environment.");
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::NewColumnFamilyInfo(
|
2014-11-20 19:49:32 +01:00
|
|
|
const void* db_key, const std::string& db_name,
|
|
|
|
const void* cf_key, const std::string& cf_name) {
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* cf_key) {
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2015-05-07 07:50:35 +02:00
|
|
|
void ThreadStatusUpdater::SetThreadOperationProperty(
|
|
|
|
int i, uint64_t value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(
|
|
|
|
int i, uint64_t delta) {
|
|
|
|
}
|
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
|
|
|
} // namespace rocksdb
|