thread_status_updater.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include "monitoring/thread_status_updater.h"
  6. #include <memory>
  7. #include "port/likely.h"
  8. #include "rocksdb/env.h"
  9. #include "util/mutexlock.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. #ifdef ROCKSDB_USING_THREAD_STATUS
  12. __thread ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = nullptr;
  13. void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType ttype,
  14. uint64_t thread_id) {
  15. if (UNLIKELY(thread_status_data_ == nullptr)) {
  16. thread_status_data_ = new ThreadStatusData();
  17. thread_status_data_->thread_type = ttype;
  18. thread_status_data_->thread_id = thread_id;
  19. std::lock_guard<std::mutex> lck(thread_list_mutex_);
  20. thread_data_set_.insert(thread_status_data_);
  21. }
  22. ClearThreadOperationProperties();
  23. }
  24. void ThreadStatusUpdater::UnregisterThread() {
  25. if (thread_status_data_ != nullptr) {
  26. std::lock_guard<std::mutex> lck(thread_list_mutex_);
  27. thread_data_set_.erase(thread_status_data_);
  28. delete thread_status_data_;
  29. thread_status_data_ = nullptr;
  30. }
  31. }
  32. void ThreadStatusUpdater::ResetThreadStatus() {
  33. ClearThreadState();
  34. ClearThreadOperation();
  35. SetColumnFamilyInfoKey(nullptr);
  36. }
  37. void ThreadStatusUpdater::SetColumnFamilyInfoKey(const void* cf_key) {
  38. auto* data = Get();
  39. if (data == nullptr) {
  40. return;
  41. }
  42. // set the tracking flag based on whether cf_key is non-null or not.
  43. // If enable_thread_tracking is set to false, the input cf_key
  44. // would be nullptr.
  45. data->enable_tracking = (cf_key != nullptr);
  46. data->cf_key.store(const_cast<void*>(cf_key), std::memory_order_relaxed);
  47. }
  48. const void* ThreadStatusUpdater::GetColumnFamilyInfoKey() {
  49. auto* data = GetLocalThreadStatus();
  50. if (data == nullptr) {
  51. return nullptr;
  52. }
  53. return data->cf_key.load(std::memory_order_relaxed);
  54. }
  55. void ThreadStatusUpdater::SetThreadOperation(
  56. const ThreadStatus::OperationType type) {
  57. auto* data = GetLocalThreadStatus();
  58. if (data == nullptr) {
  59. return;
  60. }
  61. // NOTE: Our practice here is to set all the thread operation properties
  62. // and stage before we set thread operation, and thread operation
  63. // will be set in std::memory_order_release. This is to ensure
  64. // whenever a thread operation is not OP_UNKNOWN, we will always
  65. // have a consistent information on its properties.
  66. data->operation_type.store(type, std::memory_order_release);
  67. if (type == ThreadStatus::OP_UNKNOWN) {
  68. data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
  69. std::memory_order_relaxed);
  70. ClearThreadOperationProperties();
  71. }
  72. }
  73. void ThreadStatusUpdater::SetThreadOperationProperty(int i, uint64_t value) {
  74. auto* data = GetLocalThreadStatus();
  75. if (data == nullptr) {
  76. return;
  77. }
  78. data->op_properties[i].store(value, std::memory_order_relaxed);
  79. }
  80. void ThreadStatusUpdater::IncreaseThreadOperationProperty(int i,
  81. uint64_t delta) {
  82. auto* data = GetLocalThreadStatus();
  83. if (data == nullptr) {
  84. return;
  85. }
  86. data->op_properties[i].fetch_add(delta, std::memory_order_relaxed);
  87. }
  88. void ThreadStatusUpdater::SetOperationStartTime(const uint64_t start_time) {
  89. auto* data = GetLocalThreadStatus();
  90. if (data == nullptr) {
  91. return;
  92. }
  93. data->op_start_time.store(start_time, std::memory_order_relaxed);
  94. }
  95. void ThreadStatusUpdater::ClearThreadOperation() {
  96. auto* data = GetLocalThreadStatus();
  97. if (data == nullptr) {
  98. return;
  99. }
  100. data->operation_stage.store(ThreadStatus::STAGE_UNKNOWN,
  101. std::memory_order_relaxed);
  102. data->operation_type.store(ThreadStatus::OP_UNKNOWN,
  103. std::memory_order_relaxed);
  104. ClearThreadOperationProperties();
  105. }
  106. void ThreadStatusUpdater::ClearThreadOperationProperties() {
  107. auto* data = GetLocalThreadStatus();
  108. if (data == nullptr) {
  109. return;
  110. }
  111. for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
  112. data->op_properties[i].store(0, std::memory_order_relaxed);
  113. }
  114. }
  115. ThreadStatus::OperationStage ThreadStatusUpdater::SetThreadOperationStage(
  116. ThreadStatus::OperationStage stage) {
  117. auto* data = GetLocalThreadStatus();
  118. if (data == nullptr) {
  119. return ThreadStatus::STAGE_UNKNOWN;
  120. }
  121. return data->operation_stage.exchange(stage, std::memory_order_relaxed);
  122. }
  123. void ThreadStatusUpdater::SetThreadState(const ThreadStatus::StateType type) {
  124. auto* data = GetLocalThreadStatus();
  125. if (data == nullptr) {
  126. return;
  127. }
  128. data->state_type.store(type, std::memory_order_relaxed);
  129. }
  130. void ThreadStatusUpdater::ClearThreadState() {
  131. auto* data = GetLocalThreadStatus();
  132. if (data == nullptr) {
  133. return;
  134. }
  135. data->state_type.store(ThreadStatus::STATE_UNKNOWN,
  136. std::memory_order_relaxed);
  137. }
  138. Status ThreadStatusUpdater::GetThreadList(
  139. std::vector<ThreadStatus>* thread_list) {
  140. thread_list->clear();
  141. std::vector<std::shared_ptr<ThreadStatusData>> valid_list;
  142. uint64_t now_micros = Env::Default()->NowMicros();
  143. std::lock_guard<std::mutex> lck(thread_list_mutex_);
  144. for (auto* thread_data : thread_data_set_) {
  145. assert(thread_data);
  146. auto thread_id = thread_data->thread_id.load(std::memory_order_relaxed);
  147. auto thread_type = thread_data->thread_type.load(std::memory_order_relaxed);
  148. // Since any change to cf_info_map requires thread_list_mutex,
  149. // which is currently held by GetThreadList(), here we can safely
  150. // use "memory_order_relaxed" to load the cf_key.
  151. auto cf_key = thread_data->cf_key.load(std::memory_order_relaxed);
  152. ThreadStatus::OperationType op_type = ThreadStatus::OP_UNKNOWN;
  153. ThreadStatus::OperationStage op_stage = ThreadStatus::STAGE_UNKNOWN;
  154. ThreadStatus::StateType state_type = ThreadStatus::STATE_UNKNOWN;
  155. uint64_t op_elapsed_micros = 0;
  156. uint64_t op_props[ThreadStatus::kNumOperationProperties] = {0};
  157. auto iter = cf_info_map_.find(cf_key);
  158. if (iter != cf_info_map_.end()) {
  159. op_type = thread_data->operation_type.load(std::memory_order_acquire);
  160. // display lower-level info only when higher-level info is available.
  161. if (op_type != ThreadStatus::OP_UNKNOWN) {
  162. op_elapsed_micros = now_micros - thread_data->op_start_time.load(
  163. std::memory_order_relaxed);
  164. op_stage = thread_data->operation_stage.load(std::memory_order_relaxed);
  165. state_type = thread_data->state_type.load(std::memory_order_relaxed);
  166. for (int i = 0; i < ThreadStatus::kNumOperationProperties; ++i) {
  167. op_props[i] =
  168. thread_data->op_properties[i].load(std::memory_order_relaxed);
  169. }
  170. }
  171. }
  172. thread_list->emplace_back(
  173. thread_id, thread_type,
  174. iter != cf_info_map_.end() ? iter->second.db_name : "",
  175. iter != cf_info_map_.end() ? iter->second.cf_name : "", op_type,
  176. op_elapsed_micros, op_stage, op_props, state_type);
  177. }
  178. return Status::OK();
  179. }
  180. ThreadStatusData* ThreadStatusUpdater::GetLocalThreadStatus() {
  181. if (thread_status_data_ == nullptr) {
  182. return nullptr;
  183. }
  184. if (!thread_status_data_->enable_tracking) {
  185. assert(thread_status_data_->cf_key.load(std::memory_order_relaxed) ==
  186. nullptr);
  187. return nullptr;
  188. }
  189. return thread_status_data_;
  190. }
  191. void ThreadStatusUpdater::NewColumnFamilyInfo(const void* db_key,
  192. const std::string& db_name,
  193. const void* cf_key,
  194. const std::string& cf_name) {
  195. // Acquiring same lock as GetThreadList() to guarantee
  196. // a consistent view of global column family table (cf_info_map).
  197. std::lock_guard<std::mutex> lck(thread_list_mutex_);
  198. cf_info_map_.emplace(std::piecewise_construct, std::make_tuple(cf_key),
  199. std::make_tuple(db_key, db_name, cf_name));
  200. db_key_map_[db_key].insert(cf_key);
  201. }
  202. void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* cf_key) {
  203. // Acquiring same lock as GetThreadList() to guarantee
  204. // a consistent view of global column family table (cf_info_map).
  205. std::lock_guard<std::mutex> lck(thread_list_mutex_);
  206. auto cf_pair = cf_info_map_.find(cf_key);
  207. if (cf_pair != cf_info_map_.end()) {
  208. // Remove its entry from db_key_map_ by the following steps:
  209. // 1. Obtain the entry in db_key_map_ whose set contains cf_key
  210. // 2. Remove it from the set.
  211. ConstantColumnFamilyInfo& cf_info = cf_pair->second;
  212. auto db_pair = db_key_map_.find(cf_info.db_key);
  213. assert(db_pair != db_key_map_.end());
  214. size_t result __attribute__((__unused__));
  215. result = db_pair->second.erase(cf_key);
  216. assert(result);
  217. cf_info_map_.erase(cf_pair);
  218. }
  219. }
  220. void ThreadStatusUpdater::EraseDatabaseInfo(const void* db_key) {
  221. // Acquiring same lock as GetThreadList() to guarantee
  222. // a consistent view of global column family table (cf_info_map).
  223. std::lock_guard<std::mutex> lck(thread_list_mutex_);
  224. auto db_pair = db_key_map_.find(db_key);
  225. if (UNLIKELY(db_pair == db_key_map_.end())) {
  226. // In some occasional cases such as DB::Open fails, we won't
  227. // register ColumnFamilyInfo for a db.
  228. return;
  229. }
  230. for (auto cf_key : db_pair->second) {
  231. auto cf_pair = cf_info_map_.find(cf_key);
  232. if (cf_pair != cf_info_map_.end()) {
  233. cf_info_map_.erase(cf_pair);
  234. }
  235. }
  236. db_key_map_.erase(db_key);
  237. }
  238. #else
  239. void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType /*ttype*/,
  240. uint64_t /*thread_id*/) {}
  241. void ThreadStatusUpdater::UnregisterThread() {}
  242. void ThreadStatusUpdater::ResetThreadStatus() {}
  243. void ThreadStatusUpdater::SetColumnFamilyInfoKey(const void* /*cf_key*/) {}
  244. void ThreadStatusUpdater::SetThreadOperation(
  245. const ThreadStatus::OperationType /*type*/) {}
  246. void ThreadStatusUpdater::ClearThreadOperation() {}
  247. void ThreadStatusUpdater::SetThreadState(
  248. const ThreadStatus::StateType /*type*/) {}
  249. void ThreadStatusUpdater::ClearThreadState() {}
  250. Status ThreadStatusUpdater::GetThreadList(
  251. std::vector<ThreadStatus>* /*thread_list*/) {
  252. return Status::NotSupported(
  253. "GetThreadList is not supported in the current running environment.");
  254. }
  255. void ThreadStatusUpdater::NewColumnFamilyInfo(const void* /*db_key*/,
  256. const std::string& /*db_name*/,
  257. const void* /*cf_key*/,
  258. const std::string& /*cf_name*/) {}
  259. void ThreadStatusUpdater::EraseColumnFamilyInfo(const void* /*cf_key*/) {}
  260. void ThreadStatusUpdater::EraseDatabaseInfo(const void* /*db_key*/) {}
  261. void ThreadStatusUpdater::SetThreadOperationProperty(int /*i*/,
  262. uint64_t /*value*/) {}
  263. void ThreadStatusUpdater::IncreaseThreadOperationProperty(int /*i*/,
  264. uint64_t /*delta*/) {}
  265. #endif // ROCKSDB_USING_THREAD_STATUS
  266. } // namespace ROCKSDB_NAMESPACE