thread_status_updater.h 8.5 KB

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