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).
6 // The implementation of ThreadStatus.
8 // Note that we make get and set access to ThreadStatusData lockless.
9 // As a result, ThreadStatusData as a whole is not atomic. However,
10 // we guarantee consistent ThreadStatusData all the time whenever
11 // user call GetThreadList(). This consistency guarantee is done
12 // by having the following constraint in the internal implementation
13 // of set and get order:
15 // 1. When reset any information in ThreadStatusData, always start from
16 // clearing up the lower-level information first.
17 // 2. When setting any information in ThreadStatusData, always start from
18 // setting the higher-level information.
19 // 3. When returning ThreadStatusData to the user, fields are fetched from
20 // higher-level to lower-level. In addition, where there's a nullptr
21 // in one field, then all fields that has lower-level than that field
24 // The high to low level information would be:
25 // thread_id > thread_type > db > cf > operation > state
27 // This means user might not always get full information, but whenever
28 // returned by the GetThreadList() is guaranteed to be consistent.
35 #include <unordered_map>
36 #include <unordered_set>
39 #include "port/port.h"
40 #include "rocksdb/status.h"
41 #include "rocksdb/thread_status.h"
42 #include "util/thread_operation.h"
44 namespace ROCKSDB_NAMESPACE
{
46 class ColumnFamilyHandle
;
48 // The structure that keeps constant information about a column family.
49 struct ConstantColumnFamilyInfo
{
50 #ifdef ROCKSDB_USING_THREAD_STATUS
52 ConstantColumnFamilyInfo(const void* _db_key
, const std::string
& _db_name
,
53 const std::string
& _cf_name
)
54 : db_key(_db_key
), db_name(_db_name
), cf_name(_cf_name
) {}
56 const std::string db_name
;
57 const std::string cf_name
;
58 #endif // ROCKSDB_USING_THREAD_STATUS
61 // the internal data-structure that is used to reflect the current
62 // status of a thread using a set of atomic pointers.
63 struct ThreadStatusData
{
64 #ifdef ROCKSDB_USING_THREAD_STATUS
65 explicit ThreadStatusData() : enable_tracking(false) {
67 thread_type
.store(ThreadStatus::USER
);
68 cf_key
.store(nullptr);
69 operation_type
.store(ThreadStatus::OP_UNKNOWN
);
70 op_start_time
.store(0);
71 state_type
.store(ThreadStatus::STATE_UNKNOWN
);
74 // A flag to indicate whether the thread tracking is enabled
75 // in the current thread. This value will be updated based on whether
76 // the associated Options::enable_thread_tracking is set to true
77 // in ThreadStatusUtil::SetColumnFamily().
79 // If set to false, then SetThreadOperation and SetThreadState
83 std::atomic
<uint64_t> thread_id
;
84 std::atomic
<ThreadStatus::ThreadType
> thread_type
;
85 std::atomic
<void*> cf_key
;
86 std::atomic
<ThreadStatus::OperationType
> operation_type
;
87 std::atomic
<uint64_t> op_start_time
;
88 std::atomic
<ThreadStatus::OperationStage
> operation_stage
;
89 std::atomic
<uint64_t> op_properties
[ThreadStatus::kNumOperationProperties
];
90 std::atomic
<ThreadStatus::StateType
> state_type
;
91 #endif // ROCKSDB_USING_THREAD_STATUS
94 // The class that stores and updates the status of the current thread
95 // using a thread-local ThreadStatusData.
97 // In most of the case, you should use ThreadStatusUtil to update
98 // the status of the current thread instead of using ThreadSatusUpdater
101 // @see ThreadStatusUtil
102 class ThreadStatusUpdater
{
104 ThreadStatusUpdater() {}
106 // Releases all ThreadStatusData of all active threads.
107 virtual ~ThreadStatusUpdater() {}
109 // Unregister the current thread.
110 void UnregisterThread();
112 // Reset the status of the current thread. This includes resetting
113 // ColumnFamilyInfoKey, ThreadOperation, and ThreadState.
114 void ResetThreadStatus();
116 // Set the id of the current thread.
117 void SetThreadID(uint64_t thread_id
);
119 // Register the current thread for tracking.
120 void RegisterThread(ThreadStatus::ThreadType ttype
, uint64_t thread_id
);
122 // Update the column-family info of the current thread by setting
123 // its thread-local pointer of ThreadStateInfo to the correct entry.
124 void SetColumnFamilyInfoKey(const void* cf_key
);
126 // returns the column family info key.
127 const void* GetColumnFamilyInfoKey();
129 // Update the thread operation of the current thread.
130 void SetThreadOperation(const ThreadStatus::OperationType type
);
132 // The start time of the current thread operation. It is in the format
133 // of micro-seconds since some fixed point in time.
134 void SetOperationStartTime(const uint64_t start_time
);
136 // Set the "i"th property of the current operation.
138 // NOTE: Our practice here is to set all the thread operation properties
139 // and stage before we set thread operation, and thread operation
140 // will be set in std::memory_order_release. This is to ensure
141 // whenever a thread operation is not OP_UNKNOWN, we will always
142 // have a consistent information on its properties.
143 void SetThreadOperationProperty(int i
, uint64_t value
);
145 // Increase the "i"th property of the current operation with
146 // the specified delta.
147 void IncreaseThreadOperationProperty(int i
, uint64_t delta
);
149 // Update the thread operation stage of the current thread.
150 ThreadStatus::OperationStage
SetThreadOperationStage(
151 const ThreadStatus::OperationStage stage
);
153 // Clear thread operation of the current thread.
154 void ClearThreadOperation();
156 // Reset all thread-operation-properties to 0.
157 void ClearThreadOperationProperties();
159 // Update the thread state of the current thread.
160 void SetThreadState(const ThreadStatus::StateType type
);
162 // Clear the thread state of the current thread.
163 void ClearThreadState();
165 // Obtain the status of all active registered threads.
166 Status
GetThreadList(std::vector
<ThreadStatus
>* thread_list
);
168 // Create an entry in the global ColumnFamilyInfo table for the
169 // specified column family. This function should be called only
170 // when the current thread does not hold db_mutex.
171 void NewColumnFamilyInfo(const void* db_key
, const std::string
& db_name
,
172 const void* cf_key
, const std::string
& cf_name
);
174 // Erase all ConstantColumnFamilyInfo that is associated with the
175 // specified db instance. This function should be called only when
176 // the current thread does not hold db_mutex.
177 void EraseDatabaseInfo(const void* db_key
);
179 // Erase the ConstantColumnFamilyInfo that is associated with the
180 // specified ColumnFamilyData. This function should be called only
181 // when the current thread does not hold db_mutex.
182 void EraseColumnFamilyInfo(const void* cf_key
);
184 // Verifies whether the input ColumnFamilyHandles matches
185 // the information stored in the current cf_info_map.
186 void TEST_VerifyColumnFamilyInfoMap(
187 const std::vector
<ColumnFamilyHandle
*>& handles
, bool check_exist
);
190 #ifdef ROCKSDB_USING_THREAD_STATUS
191 // The thread-local variable for storing thread status.
192 static thread_local ThreadStatusData
* thread_status_data_
;
194 // Returns the pointer to the thread status data only when the
195 // thread status data is non-null and has enable_tracking == true.
196 ThreadStatusData
* GetLocalThreadStatus();
198 // Directly returns the pointer to thread_status_data_ without
199 // checking whether enabling_tracking is true of not.
200 ThreadStatusData
* Get() { return thread_status_data_
; }
202 // The mutex that protects cf_info_map and db_key_map.
203 std::mutex thread_list_mutex_
;
205 // The current status data of all active threads.
206 std::unordered_set
<ThreadStatusData
*> thread_data_set_
;
208 // A global map that keeps the column family information. It is stored
209 // globally instead of inside DB is to avoid the situation where DB is
210 // closing while GetThreadList function already get the pointer to its
211 // CopnstantColumnFamilyInfo.
212 std::unordered_map
<const void*, ConstantColumnFamilyInfo
> cf_info_map_
;
214 // A db_key to cf_key map that allows erasing elements in cf_info_map
215 // associated to the same db_key faster.
216 std::unordered_map
<const void*, std::unordered_set
<const void*>> db_key_map_
;
219 static ThreadStatusData
* thread_status_data_
;
220 #endif // ROCKSDB_USING_THREAD_STATUS
223 } // namespace ROCKSDB_NAMESPACE