@ -15,6 +15,19 @@ namespace rocksdb {
__thread ThreadStatusData * ThreadStatusUpdater : : thread_status_data_ = nullptr ;
__thread ThreadStatusData * ThreadStatusUpdater : : thread_status_data_ = nullptr ;
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 ( ) ;
}
void ThreadStatusUpdater : : UnregisterThread ( ) {
void ThreadStatusUpdater : : UnregisterThread ( ) {
if ( thread_status_data_ ! = nullptr ) {
if ( thread_status_data_ ! = nullptr ) {
std : : lock_guard < std : : mutex > lck ( thread_list_mutex_ ) ;
std : : lock_guard < std : : mutex > lck ( thread_list_mutex_ ) ;
@ -24,18 +37,6 @@ void ThreadStatusUpdater::UnregisterThread() {
}
}
}
}
void ThreadStatusUpdater : : SetThreadID ( uint64_t thread_id ) {
auto * data = InitAndGet ( ) ;
data - > thread_id . store ( thread_id , std : : memory_order_relaxed ) ;
}
void ThreadStatusUpdater : : SetThreadType (
ThreadStatus : : ThreadType ttype ) {
auto * data = InitAndGet ( ) ;
data - > thread_type . store ( ttype , std : : memory_order_relaxed ) ;
ClearThreadOperationProperties ( ) ;
}
void ThreadStatusUpdater : : ResetThreadStatus ( ) {
void ThreadStatusUpdater : : ResetThreadStatus ( ) {
ClearThreadState ( ) ;
ClearThreadState ( ) ;
ClearThreadOperation ( ) ;
ClearThreadOperation ( ) ;
@ -44,7 +45,10 @@ void ThreadStatusUpdater::ResetThreadStatus() {
void ThreadStatusUpdater : : SetColumnFamilyInfoKey (
void ThreadStatusUpdater : : SetColumnFamilyInfoKey (
const void * cf_key ) {
const void * cf_key ) {
auto * data = InitAndGet ( ) ;
auto * data = Get ( ) ;
if ( data = = nullptr ) {
return ;
}
// set the tracking flag based on whether cf_key is non-null or not.
// 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
// If enable_thread_tracking is set to false, the input cf_key
// would be nullptr.
// would be nullptr.
@ -53,8 +57,8 @@ void ThreadStatusUpdater::SetColumnFamilyInfoKey(
}
}
const void * ThreadStatusUpdater : : GetColumnFamilyInfoKey ( ) {
const void * ThreadStatusUpdater : : GetColumnFamilyInfoKey ( ) {
auto * data = InitAnd Get( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( data - > enable_tracking = = false ) {
if ( data = = nullptr ) {
return nullptr ;
return nullptr ;
}
}
return data - > cf_key . load ( std : : memory_order_relaxed ) ;
return data - > cf_key . load ( std : : memory_order_relaxed ) ;
@ -62,9 +66,8 @@ const void* ThreadStatusUpdater::GetColumnFamilyInfoKey() {
void ThreadStatusUpdater : : SetThreadOperation (
void ThreadStatusUpdater : : SetThreadOperation (
const ThreadStatus : : OperationType type ) {
const ThreadStatus : : OperationType type ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
// NOTE: Our practice here is to set all the thread operation properties
// NOTE: Our practice here is to set all the thread operation properties
@ -82,9 +85,8 @@ void ThreadStatusUpdater::SetThreadOperation(
void ThreadStatusUpdater : : SetThreadOperationProperty (
void ThreadStatusUpdater : : SetThreadOperationProperty (
int i , uint64_t value ) {
int i , uint64_t value ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
data - > op_properties [ i ] . store ( value , std : : memory_order_relaxed ) ;
data - > op_properties [ i ] . store ( value , std : : memory_order_relaxed ) ;
@ -92,27 +94,24 @@ void ThreadStatusUpdater::SetThreadOperationProperty(
void ThreadStatusUpdater : : IncreaseThreadOperationProperty (
void ThreadStatusUpdater : : IncreaseThreadOperationProperty (
int i , uint64_t delta ) {
int i , uint64_t delta ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
data - > op_properties [ i ] . fetch_add ( delta , std : : memory_order_relaxed ) ;
data - > op_properties [ i ] . fetch_add ( delta , std : : memory_order_relaxed ) ;
}
}
void ThreadStatusUpdater : : SetOperationStartTime ( const uint64_t start_time ) {
void ThreadStatusUpdater : : SetOperationStartTime ( const uint64_t start_time ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
data - > op_start_time . store ( start_time , std : : memory_order_relaxed ) ;
data - > op_start_time . store ( start_time , std : : memory_order_relaxed ) ;
}
}
void ThreadStatusUpdater : : ClearThreadOperation ( ) {
void ThreadStatusUpdater : : ClearThreadOperation ( ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
data - > operation_stage . store ( ThreadStatus : : STAGE_UNKNOWN ,
data - > operation_stage . store ( ThreadStatus : : STAGE_UNKNOWN ,
@ -123,9 +122,8 @@ void ThreadStatusUpdater::ClearThreadOperation() {
}
}
void ThreadStatusUpdater : : ClearThreadOperationProperties ( ) {
void ThreadStatusUpdater : : ClearThreadOperationProperties ( ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
for ( int i = 0 ; i < ThreadStatus : : kNumOperationProperties ; + + i ) {
for ( int i = 0 ; i < ThreadStatus : : kNumOperationProperties ; + + i ) {
@ -135,9 +133,8 @@ void ThreadStatusUpdater::ClearThreadOperationProperties() {
ThreadStatus : : OperationStage ThreadStatusUpdater : : SetThreadOperationStage (
ThreadStatus : : OperationStage ThreadStatusUpdater : : SetThreadOperationStage (
ThreadStatus : : OperationStage stage ) {
ThreadStatus : : OperationStage stage ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ThreadStatus : : STAGE_UNKNOWN ;
return ThreadStatus : : STAGE_UNKNOWN ;
}
}
return data - > operation_stage . exchange (
return data - > operation_stage . exchange (
@ -146,18 +143,16 @@ ThreadStatus::OperationStage ThreadStatusUpdater::SetThreadOperationStage(
void ThreadStatusUpdater : : SetThreadState (
void ThreadStatusUpdater : : SetThreadState (
const ThreadStatus : : StateType type ) {
const ThreadStatus : : StateType type ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
data - > state_type . store ( type , std : : memory_order_relaxed ) ;
data - > state_type . store ( type , std : : memory_order_relaxed ) ;
}
}
void ThreadStatusUpdater : : ClearThreadState ( ) {
void ThreadStatusUpdater : : ClearThreadState ( ) {
auto * data = InitAndGet ( ) ;
auto * data = GetLocalThreadStatus ( ) ;
if ( ! data - > enable_tracking ) {
if ( data = = nullptr ) {
assert ( data - > cf_key . load ( std : : memory_order_relaxed ) = = nullptr ) ;
return ;
return ;
}
}
data - > state_type . store (
data - > state_type . store (
@ -222,11 +217,14 @@ Status ThreadStatusUpdater::GetThreadList(
return Status : : OK ( ) ;
return Status : : OK ( ) ;
}
}
ThreadStatusData * ThreadStatusUpdater : : InitAndGet ( ) {
ThreadStatusData * ThreadStatusUpdater : : GetLocalThreadStatus ( ) {
if ( UNLIKELY ( thread_status_data_ = = nullptr ) ) {
if ( thread_status_data_ = = nullptr ) {
thread_status_data_ = new ThreadStatusData ( ) ;
return nullptr ;
std : : lock_guard < std : : mutex > lck ( thread_list_mutex_ ) ;
}
thread_data_set_ . insert ( thread_status_data_ ) ;
if ( ! thread_status_data_ - > enable_tracking ) {
assert ( thread_status_data_ - > cf_key . load (
std : : memory_order_relaxed ) = = nullptr ) ;
return nullptr ;
}
}
return thread_status_data_ ;
return thread_status_data_ ;
}
}
@ -290,17 +288,14 @@ void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
# else
# else
void ThreadStatusUpdater : : UnregisterThread ( ) {
void ThreadStatusUpdater : : RegisterThread (
ThreadStatus : : ThreadType ttype , uint64_t thread_id ) {
}
}
void ThreadStatusUpdater : : ResetThreadStatus ( ) {
void ThreadStatusUpdater : : UnregisterThread ( ) {
}
void ThreadStatusUpdater : : SetThreadID ( uint64_t thread_id ) {
}
}
void ThreadStatusUpdater : : SetThreadType (
void ThreadStatusUpdater : : ResetThreadStatus ( ) {
ThreadStatus : : ThreadType ttype ) {
}
}
void ThreadStatusUpdater : : SetColumnFamilyInfoKey (
void ThreadStatusUpdater : : SetColumnFamilyInfoKey (