1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
10 #include "monitoring/thread_status_updater.h"
11 #include "rocksdb/db.h"
12 #include "rocksdb/env.h"
13 #include "rocksdb/thread_status.h"
15 namespace ROCKSDB_NAMESPACE
{
17 class ColumnFamilyData
;
19 // The static utility class for updating thread-local status.
21 // The thread-local status is updated via the thread-local cached
22 // pointer thread_updater_local_cache_. During each function call,
23 // when ThreadStatusUtil finds thread_updater_local_cache_ is
24 // left uninitialized (determined by thread_updater_initialized_),
25 // it will tries to initialize it using the return value of
26 // Env::GetThreadStatusUpdater(). When thread_updater_local_cache_
27 // is initialized by a non-null pointer, each function call will
28 // then update the status of the current thread. Otherwise,
29 // all function calls to ThreadStatusUtil will be no-op.
30 class ThreadStatusUtil
{
32 // Register the current thread for tracking.
33 static void RegisterThread(const Env
* env
,
34 ThreadStatus::ThreadType thread_type
);
36 // Unregister the current thread.
37 static void UnregisterThread();
39 // Create an entry in the global ColumnFamilyInfo table for the
40 // specified column family. This function should be called only
41 // when the current thread does not hold db_mutex.
42 static void NewColumnFamilyInfo(const DB
* db
, const ColumnFamilyData
* cfd
,
43 const std::string
& cf_name
, const Env
* env
);
45 // Erase the ConstantColumnFamilyInfo that is associated with the
46 // specified ColumnFamilyData. This function should be called only
47 // when the current thread does not hold db_mutex.
48 static void EraseColumnFamilyInfo(const ColumnFamilyData
* cfd
);
50 // Erase all ConstantColumnFamilyInfo that is associated with the
51 // specified db instance. This function should be called only when
52 // the current thread does not hold db_mutex.
53 static void EraseDatabaseInfo(const DB
* db
);
55 // Update the thread status to indicate the current thread is doing
56 // something related to the specified column family.
57 static void SetColumnFamily(const ColumnFamilyData
* cfd
, const Env
* env
,
58 bool enable_thread_tracking
);
60 static void SetThreadOperation(ThreadStatus::OperationType type
);
62 static ThreadStatus::OperationStage
SetThreadOperationStage(
63 ThreadStatus::OperationStage stage
);
65 static void SetThreadOperationProperty(int code
, uint64_t value
);
67 static void IncreaseThreadOperationProperty(int code
, uint64_t delta
);
69 static void SetThreadState(ThreadStatus::StateType type
);
71 static void ResetThreadStatus();
74 static void TEST_SetStateDelay(const ThreadStatus::StateType state
,
76 static void TEST_StateDelay(const ThreadStatus::StateType state
);
80 // Initialize the thread-local ThreadStatusUpdater when it finds
81 // the cached value is nullptr. Returns true if it has cached
82 // a non-null pointer.
83 static bool MaybeInitThreadLocalUpdater(const Env
* env
);
85 #ifdef ROCKSDB_USING_THREAD_STATUS
86 // A boolean flag indicating whether thread_updater_local_cache_
87 // is initialized. It is set to true when an Env uses any
88 // ThreadStatusUtil functions using the current thread other
89 // than UnregisterThread(). It will be set to false when
90 // UnregisterThread() is called.
92 // When this variable is set to true, thread_updater_local_cache_
93 // will not be updated until this variable is again set to false
94 // in UnregisterThread().
95 static thread_local
bool thread_updater_initialized_
;
97 // The thread-local cached ThreadStatusUpdater that caches the
98 // thread_status_updater_ of the first Env that uses any ThreadStatusUtil
99 // function other than UnregisterThread(). This variable will
100 // be cleared when UnregisterThread() is called.
102 // When this variable is set to a non-null pointer, then the status
103 // of the current thread will be updated when a function of
104 // ThreadStatusUtil is called. Otherwise, all functions of
105 // ThreadStatusUtil will be no-op.
107 // When thread_updater_initialized_ is set to true, this variable
108 // will not be updated until this thread_updater_initialized_ is
109 // again set to false in UnregisterThread().
110 static thread_local ThreadStatusUpdater
* thread_updater_local_cache_
;
112 static bool thread_updater_initialized_
;
113 static ThreadStatusUpdater
* thread_updater_local_cache_
;
117 // A helper class for updating thread state. It will set the
118 // thread state according to the input parameter in its constructor
119 // and set the thread state to the previous state in its destructor.
120 class AutoThreadOperationStageUpdater
{
122 explicit AutoThreadOperationStageUpdater(ThreadStatus::OperationStage stage
);
123 ~AutoThreadOperationStageUpdater();
125 #ifdef ROCKSDB_USING_THREAD_STATUS
127 ThreadStatus::OperationStage prev_stage_
;
131 } // namespace ROCKSDB_NAMESPACE