fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
350 lines
11 KiB
350 lines
11 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
10 years ago
|
// 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.
|
||
|
|
||
8 years ago
|
#include "monitoring/thread_status_updater.h"
|
||
10 years ago
|
#include <memory>
|
||
10 years ago
|
#include "port/likely.h"
|
||
8 years ago
|
#include "rocksdb/env.h"
|
||
10 years ago
|
#include "util/mutexlock.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
8 years ago
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
||
10 years ago
|
|
||
10 years ago
|
__thread ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = nullptr;
|
||
10 years ago
|
|
||
10 years ago
|
void ThreadStatusUpdater::RegisterThread(
|
||
|
ThreadStatus::ThreadType ttype, uint64_t thread_id) {
|
||
|
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();
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::UnregisterThread() {
|
||
10 years ago
|
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_;
|
||
10 years ago
|
thread_status_data_ = nullptr;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::ResetThreadStatus() {
|
||
|
ClearThreadState();
|
||
|
ClearThreadOperation();
|
||
|
SetColumnFamilyInfoKey(nullptr);
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(
|
||
10 years ago
|
const void* cf_key) {
|
||
10 years ago
|
auto* data = Get();
|
||
|
if (data == nullptr) {
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
// 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);
|
||
10 years ago
|
data->cf_key.store(const_cast<void*>(cf_key), std::memory_order_relaxed);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
const void* ThreadStatusUpdater::GetColumnFamilyInfoKey() {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return nullptr;
|
||
|
}
|
||
|
return data->cf_key.load(std::memory_order_relaxed);
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetThreadOperation(
|
||
|
const ThreadStatus::OperationType type) {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
// 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) {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
data->op_properties[i].store(value, std::memory_order_relaxed);
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(
|
||
|
int i, uint64_t delta) {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
data->op_properties[i].fetch_add(delta, std::memory_order_relaxed);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetOperationStartTime(const uint64_t start_time) {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
data->op_start_time.store(start_time, std::memory_order_relaxed);
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::ClearThreadOperation() {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
|
||
|
std::memory_order_relaxed);
|
||
10 years ago
|
data->operation_type.store(
|
||
|
ThreadStatus::OP_UNKNOWN, std::memory_order_relaxed);
|
||
10 years ago
|
ClearThreadOperationProperties();
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::ClearThreadOperationProperties() {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
|
||
|
data->op_properties[i].store(0, std::memory_order_relaxed);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
ThreadStatus::OperationStage ThreadStatusUpdater::SetThreadOperationStage(
|
||
|
ThreadStatus::OperationStage stage) {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return ThreadStatus::STAGE_UNKNOWN;
|
||
|
}
|
||
|
return data->operation_stage.exchange(
|
||
|
stage, std::memory_order_relaxed);
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetThreadState(
|
||
|
const ThreadStatus::StateType type) {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
data->state_type.store(type, std::memory_order_relaxed);
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::ClearThreadState() {
|
||
10 years ago
|
auto* data = GetLocalThreadStatus();
|
||
|
if (data == nullptr) {
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
data->state_type.store(
|
||
|
ThreadStatus::STATE_UNKNOWN, std::memory_order_relaxed);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
Status ThreadStatusUpdater::GetThreadList(
|
||
|
std::vector<ThreadStatus>* thread_list) {
|
||
10 years ago
|
thread_list->clear();
|
||
|
std::vector<std::shared_ptr<ThreadStatusData>> valid_list;
|
||
10 years ago
|
uint64_t now_micros = Env::Default()->NowMicros();
|
||
10 years ago
|
|
||
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
||
|
for (auto* thread_data : thread_data_set_) {
|
||
|
assert(thread_data);
|
||
10 years ago
|
auto thread_id = thread_data->thread_id.load(
|
||
|
std::memory_order_relaxed);
|
||
10 years ago
|
auto thread_type = thread_data->thread_type.load(
|
||
|
std::memory_order_relaxed);
|
||
10 years ago
|
// 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.
|
||
10 years ago
|
auto cf_key = thread_data->cf_key.load(
|
||
|
std::memory_order_relaxed);
|
||
10 years ago
|
auto iter = cf_info_map_.find(cf_key);
|
||
10 years ago
|
auto* cf_info = iter != cf_info_map_.end() ?
|
||
10 years ago
|
iter->second.get() : nullptr;
|
||
10 years ago
|
const std::string* db_name = nullptr;
|
||
|
const std::string* cf_name = nullptr;
|
||
10 years ago
|
ThreadStatus::OperationType op_type = ThreadStatus::OP_UNKNOWN;
|
||
10 years ago
|
ThreadStatus::OperationStage op_stage = ThreadStatus::STAGE_UNKNOWN;
|
||
10 years ago
|
ThreadStatus::StateType state_type = ThreadStatus::STATE_UNKNOWN;
|
||
10 years ago
|
uint64_t op_elapsed_micros = 0;
|
||
10 years ago
|
uint64_t op_props[ThreadStatus::kNumOperationProperties] = {0};
|
||
10 years ago
|
if (cf_info != nullptr) {
|
||
|
db_name = &cf_info->db_name;
|
||
|
cf_name = &cf_info->cf_name;
|
||
10 years ago
|
op_type = thread_data->operation_type.load(
|
||
10 years ago
|
std::memory_order_acquire);
|
||
10 years ago
|
// display lower-level info only when higher-level info is available.
|
||
10 years ago
|
if (op_type != ThreadStatus::OP_UNKNOWN) {
|
||
10 years ago
|
op_elapsed_micros = now_micros - thread_data->op_start_time.load(
|
||
10 years ago
|
std::memory_order_relaxed);
|
||
10 years ago
|
op_stage = thread_data->operation_stage.load(
|
||
|
std::memory_order_relaxed);
|
||
10 years ago
|
state_type = thread_data->state_type.load(
|
||
|
std::memory_order_relaxed);
|
||
10 years ago
|
for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
|
||
|
op_props[i] = thread_data->op_properties[i].load(
|
||
|
std::memory_order_relaxed);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
thread_list->emplace_back(
|
||
10 years ago
|
thread_id, thread_type,
|
||
10 years ago
|
db_name ? *db_name : "",
|
||
|
cf_name ? *cf_name : "",
|
||
10 years ago
|
op_type, op_elapsed_micros, op_stage, op_props,
|
||
|
state_type);
|
||
10 years ago
|
}
|
||
|
|
||
|
return Status::OK();
|
||
|
}
|
||
|
|
||
10 years ago
|
ThreadStatusData* ThreadStatusUpdater::GetLocalThreadStatus() {
|
||
|
if (thread_status_data_ == nullptr) {
|
||
|
return nullptr;
|
||
|
}
|
||
|
if (!thread_status_data_->enable_tracking) {
|
||
|
assert(thread_status_data_->cf_key.load(
|
||
|
std::memory_order_relaxed) == nullptr);
|
||
|
return nullptr;
|
||
10 years ago
|
}
|
||
|
return thread_status_data_;
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::NewColumnFamilyInfo(
|
||
10 years ago
|
const void* db_key, const std::string& db_name,
|
||
|
const void* cf_key, const std::string& cf_name) {
|
||
10 years ago
|
// Acquiring same lock as GetThreadList() to guarantee
|
||
|
// a consistent view of global column family table (cf_info_map).
|
||
10 years ago
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
||
|
|
||
10 years ago
|
cf_info_map_[cf_key].reset(
|
||
|
new ConstantColumnFamilyInfo(db_key, db_name, cf_name));
|
||
10 years ago
|
db_key_map_[db_key].insert(cf_key);
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* cf_key) {
|
||
10 years ago
|
// Acquiring same lock as GetThreadList() to guarantee
|
||
|
// a consistent view of global column family table (cf_info_map).
|
||
10 years ago
|
std::lock_guard<std::mutex> lck(thread_list_mutex_);
|
||
|
auto cf_pair = cf_info_map_.find(cf_key);
|
||
9 years ago
|
if (cf_pair == cf_info_map_.end()) {
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
auto* cf_info = cf_pair->second.get();
|
||
10 years ago
|
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());
|
||
10 years ago
|
size_t result __attribute__((unused)) = db_pair->second.erase(cf_key);
|
||
10 years ago
|
assert(result);
|
||
|
|
||
10 years ago
|
cf_pair->second.reset();
|
||
10 years ago
|
result = cf_info_map_.erase(cf_key);
|
||
|
assert(result);
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
|
||
10 years ago
|
// Acquiring same lock as GetThreadList() to guarantee
|
||
|
// a consistent view of global column family table (cf_info_map).
|
||
10 years ago
|
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;
|
||
|
}
|
||
|
|
||
10 years ago
|
size_t result __attribute__((unused)) = 0;
|
||
10 years ago
|
for (auto cf_key : db_pair->second) {
|
||
|
auto cf_pair = cf_info_map_.find(cf_key);
|
||
9 years ago
|
if (cf_pair == cf_info_map_.end()) {
|
||
|
continue;
|
||
|
}
|
||
10 years ago
|
cf_pair->second.reset();
|
||
10 years ago
|
result = cf_info_map_.erase(cf_key);
|
||
10 years ago
|
assert(result);
|
||
|
}
|
||
|
db_key_map_.erase(db_key);
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::RegisterThread(
|
||
|
ThreadStatus::ThreadType ttype, uint64_t thread_id) {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::UnregisterThread() {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::ResetThreadStatus() {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetColumnFamilyInfoKey(
|
||
10 years ago
|
const void* cf_key) {
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetThreadOperation(
|
||
|
const ThreadStatus::OperationType type) {
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::ClearThreadOperation() {
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::SetThreadState(
|
||
|
const ThreadStatus::StateType type) {
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::ClearThreadState() {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
Status ThreadStatusUpdater::GetThreadList(
|
||
|
std::vector<ThreadStatus>* thread_list) {
|
||
10 years ago
|
return Status::NotSupported(
|
||
|
"GetThreadList is not supported in the current running environment.");
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::NewColumnFamilyInfo(
|
||
10 years ago
|
const void* db_key, const std::string& db_name,
|
||
|
const void* cf_key, const std::string& cf_name) {
|
||
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* cf_key) {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void ThreadStatusUpdater::SetThreadOperationProperty(
|
||
|
int i, uint64_t value) {
|
||
|
}
|
||
|
|
||
|
void ThreadStatusUpdater::IncreaseThreadOperationProperty(
|
||
|
int i, uint64_t delta) {
|
||
|
}
|
||
|
|
||
10 years ago
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
||
|
} // namespace rocksdb
|