]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/monitoring/thread_status_updater.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / monitoring / thread_status_updater.h
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).
5 //
6 // The implementation of ThreadStatus.
7 //
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:
14 //
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
22 // should be ignored.
23 //
24 // The high to low level information would be:
25 // thread_id > thread_type > db > cf > operation > state
26 //
27 // This means user might not always get full information, but whenever
28 // returned by the GetThreadList() is guaranteed to be consistent.
29 #pragma once
30 #include <atomic>
31 #include <list>
32 #include <memory>
33 #include <mutex>
34 #include <string>
35 #include <unordered_map>
36 #include <unordered_set>
37 #include <vector>
38
39 #include "port/port.h"
40 #include "rocksdb/status.h"
41 #include "rocksdb/thread_status.h"
42 #include "util/thread_operation.h"
43
44 namespace ROCKSDB_NAMESPACE {
45
46 class ColumnFamilyHandle;
47
48 // The structure that keeps constant information about a column family.
49 struct ConstantColumnFamilyInfo {
50 #ifdef ROCKSDB_USING_THREAD_STATUS
51 public:
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) {}
55 const void* db_key;
56 const std::string db_name;
57 const std::string cf_name;
58 #endif // ROCKSDB_USING_THREAD_STATUS
59 };
60
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) {
66 thread_id.store(0);
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);
72 }
73
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().
78 //
79 // If set to false, then SetThreadOperation and SetThreadState
80 // will be no-op.
81 bool enable_tracking;
82
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
92 };
93
94 // The class that stores and updates the status of the current thread
95 // using a thread-local ThreadStatusData.
96 //
97 // In most of the case, you should use ThreadStatusUtil to update
98 // the status of the current thread instead of using ThreadSatusUpdater
99 // directly.
100 //
101 // @see ThreadStatusUtil
102 class ThreadStatusUpdater {
103 public:
104 ThreadStatusUpdater() {}
105
106 // Releases all ThreadStatusData of all active threads.
107 virtual ~ThreadStatusUpdater() {}
108
109 // Unregister the current thread.
110 void UnregisterThread();
111
112 // Reset the status of the current thread. This includes resetting
113 // ColumnFamilyInfoKey, ThreadOperation, and ThreadState.
114 void ResetThreadStatus();
115
116 // Set the id of the current thread.
117 void SetThreadID(uint64_t thread_id);
118
119 // Register the current thread for tracking.
120 void RegisterThread(ThreadStatus::ThreadType ttype, uint64_t thread_id);
121
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);
125
126 // returns the column family info key.
127 const void* GetColumnFamilyInfoKey();
128
129 // Update the thread operation of the current thread.
130 void SetThreadOperation(const ThreadStatus::OperationType type);
131
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);
135
136 // Set the "i"th property of the current operation.
137 //
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);
144
145 // Increase the "i"th property of the current operation with
146 // the specified delta.
147 void IncreaseThreadOperationProperty(int i, uint64_t delta);
148
149 // Update the thread operation stage of the current thread.
150 ThreadStatus::OperationStage SetThreadOperationStage(
151 const ThreadStatus::OperationStage stage);
152
153 // Clear thread operation of the current thread.
154 void ClearThreadOperation();
155
156 // Reset all thread-operation-properties to 0.
157 void ClearThreadOperationProperties();
158
159 // Update the thread state of the current thread.
160 void SetThreadState(const ThreadStatus::StateType type);
161
162 // Clear the thread state of the current thread.
163 void ClearThreadState();
164
165 // Obtain the status of all active registered threads.
166 Status GetThreadList(std::vector<ThreadStatus>* thread_list);
167
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);
173
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);
178
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);
183
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);
188
189 protected:
190 #ifdef ROCKSDB_USING_THREAD_STATUS
191 // The thread-local variable for storing thread status.
192 static thread_local ThreadStatusData* thread_status_data_;
193
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();
197
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_; }
201
202 // The mutex that protects cf_info_map and db_key_map.
203 std::mutex thread_list_mutex_;
204
205 // The current status data of all active threads.
206 std::unordered_set<ThreadStatusData*> thread_data_set_;
207
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_;
213
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_;
217
218 #else
219 static ThreadStatusData* thread_status_data_;
220 #endif // ROCKSDB_USING_THREAD_STATUS
221 };
222
223 } // namespace ROCKSDB_NAMESPACE