thread_status_updater.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "rocksdb/status.h"
  39. #include "rocksdb/thread_status.h"
  40. #include "port/port.h"
  41. #include "util/thread_operation.h"
  42. namespace ROCKSDB_NAMESPACE {
  43. class ColumnFamilyHandle;
  44. // The structure that keeps constant information about a column family.
  45. struct ConstantColumnFamilyInfo {
  46. #ifdef ROCKSDB_USING_THREAD_STATUS
  47. public:
  48. ConstantColumnFamilyInfo(
  49. const void* _db_key,
  50. const std::string& _db_name,
  51. const std::string& _cf_name) :
  52. db_key(_db_key), db_name(_db_name), cf_name(_cf_name) {}
  53. const void* db_key;
  54. const std::string db_name;
  55. const std::string cf_name;
  56. #endif // ROCKSDB_USING_THREAD_STATUS
  57. };
  58. // the internal data-structure that is used to reflect the current
  59. // status of a thread using a set of atomic pointers.
  60. struct ThreadStatusData {
  61. #ifdef ROCKSDB_USING_THREAD_STATUS
  62. explicit ThreadStatusData() : enable_tracking(false) {
  63. thread_id.store(0);
  64. thread_type.store(ThreadStatus::USER);
  65. cf_key.store(nullptr);
  66. operation_type.store(ThreadStatus::OP_UNKNOWN);
  67. op_start_time.store(0);
  68. state_type.store(ThreadStatus::STATE_UNKNOWN);
  69. }
  70. // A flag to indicate whether the thread tracking is enabled
  71. // in the current thread. This value will be updated based on whether
  72. // the associated Options::enable_thread_tracking is set to true
  73. // in ThreadStatusUtil::SetColumnFamily().
  74. //
  75. // If set to false, then SetThreadOperation and SetThreadState
  76. // will be no-op.
  77. bool enable_tracking;
  78. std::atomic<uint64_t> thread_id;
  79. std::atomic<ThreadStatus::ThreadType> thread_type;
  80. std::atomic<void*> cf_key;
  81. std::atomic<ThreadStatus::OperationType> operation_type;
  82. std::atomic<uint64_t> op_start_time;
  83. std::atomic<ThreadStatus::OperationStage> operation_stage;
  84. std::atomic<uint64_t> op_properties[ThreadStatus::kNumOperationProperties];
  85. std::atomic<ThreadStatus::StateType> state_type;
  86. #endif // ROCKSDB_USING_THREAD_STATUS
  87. };
  88. // The class that stores and updates the status of the current thread
  89. // using a thread-local ThreadStatusData.
  90. //
  91. // In most of the case, you should use ThreadStatusUtil to update
  92. // the status of the current thread instead of using ThreadSatusUpdater
  93. // directly.
  94. //
  95. // @see ThreadStatusUtil
  96. class ThreadStatusUpdater {
  97. public:
  98. ThreadStatusUpdater() {}
  99. // Releases all ThreadStatusData of all active threads.
  100. virtual ~ThreadStatusUpdater() {}
  101. // Unregister the current thread.
  102. void UnregisterThread();
  103. // Reset the status of the current thread. This includes resetting
  104. // ColumnFamilyInfoKey, ThreadOperation, and ThreadState.
  105. void ResetThreadStatus();
  106. // Set the id of the current thread.
  107. void SetThreadID(uint64_t thread_id);
  108. // Register the current thread for tracking.
  109. void RegisterThread(ThreadStatus::ThreadType ttype, uint64_t thread_id);
  110. // Update the column-family info of the current thread by setting
  111. // its thread-local pointer of ThreadStateInfo to the correct entry.
  112. void SetColumnFamilyInfoKey(const void* cf_key);
  113. // returns the column family info key.
  114. const void* GetColumnFamilyInfoKey();
  115. // Update the thread operation of the current thread.
  116. void SetThreadOperation(const ThreadStatus::OperationType type);
  117. // The start time of the current thread operation. It is in the format
  118. // of micro-seconds since some fixed point in time.
  119. void SetOperationStartTime(const uint64_t start_time);
  120. // Set the "i"th property of the current operation.
  121. //
  122. // NOTE: Our practice here is to set all the thread operation properties
  123. // and stage before we set thread operation, and thread operation
  124. // will be set in std::memory_order_release. This is to ensure
  125. // whenever a thread operation is not OP_UNKNOWN, we will always
  126. // have a consistent information on its properties.
  127. void SetThreadOperationProperty(
  128. int i, uint64_t value);
  129. // Increase the "i"th property of the current operation with
  130. // the specified delta.
  131. void IncreaseThreadOperationProperty(
  132. int i, uint64_t delta);
  133. // Update the thread operation stage of the current thread.
  134. ThreadStatus::OperationStage SetThreadOperationStage(
  135. const ThreadStatus::OperationStage stage);
  136. // Clear thread operation of the current thread.
  137. void ClearThreadOperation();
  138. // Reset all thread-operation-properties to 0.
  139. void ClearThreadOperationProperties();
  140. // Update the thread state of the current thread.
  141. void SetThreadState(const ThreadStatus::StateType type);
  142. // Clear the thread state of the current thread.
  143. void ClearThreadState();
  144. // Obtain the status of all active registered threads.
  145. Status GetThreadList(
  146. std::vector<ThreadStatus>* thread_list);
  147. // Create an entry in the global ColumnFamilyInfo table for the
  148. // specified column family. This function should be called only
  149. // when the current thread does not hold db_mutex.
  150. void NewColumnFamilyInfo(
  151. const void* db_key, const std::string& db_name,
  152. const void* cf_key, const std::string& cf_name);
  153. // Erase all ConstantColumnFamilyInfo that is associated with the
  154. // specified db instance. This function should be called only when
  155. // the current thread does not hold db_mutex.
  156. void EraseDatabaseInfo(const void* db_key);
  157. // Erase the ConstantColumnFamilyInfo that is associated with the
  158. // specified ColumnFamilyData. This function should be called only
  159. // when the current thread does not hold db_mutex.
  160. void EraseColumnFamilyInfo(const void* cf_key);
  161. // Verifies whether the input ColumnFamilyHandles matches
  162. // the information stored in the current cf_info_map.
  163. void TEST_VerifyColumnFamilyInfoMap(
  164. const std::vector<ColumnFamilyHandle*>& handles,
  165. bool check_exist);
  166. protected:
  167. #ifdef ROCKSDB_USING_THREAD_STATUS
  168. // The thread-local variable for storing thread status.
  169. static __thread ThreadStatusData* thread_status_data_;
  170. // Returns the pointer to the thread status data only when the
  171. // thread status data is non-null and has enable_tracking == true.
  172. ThreadStatusData* GetLocalThreadStatus();
  173. // Directly returns the pointer to thread_status_data_ without
  174. // checking whether enabling_tracking is true of not.
  175. ThreadStatusData* Get() {
  176. return thread_status_data_;
  177. }
  178. // The mutex that protects cf_info_map and db_key_map.
  179. std::mutex thread_list_mutex_;
  180. // The current status data of all active threads.
  181. std::unordered_set<ThreadStatusData*> thread_data_set_;
  182. // A global map that keeps the column family information. It is stored
  183. // globally instead of inside DB is to avoid the situation where DB is
  184. // closing while GetThreadList function already get the pointer to its
  185. // CopnstantColumnFamilyInfo.
  186. std::unordered_map<const void*, ConstantColumnFamilyInfo> cf_info_map_;
  187. // A db_key to cf_key map that allows erasing elements in cf_info_map
  188. // associated to the same db_key faster.
  189. std::unordered_map<
  190. const void*, std::unordered_set<const void*>> db_key_map_;
  191. #else
  192. static ThreadStatusData* thread_status_data_;
  193. #endif // ROCKSDB_USING_THREAD_STATUS
  194. };
  195. } // namespace ROCKSDB_NAMESPACE