thread_status_util.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_util.h"
  6. #include "monitoring/thread_status_updater.h"
  7. #include "rocksdb/env.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. #ifdef ROCKSDB_USING_THREAD_STATUS
  10. __thread ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ =
  11. nullptr;
  12. __thread bool ThreadStatusUtil::thread_updater_initialized_ = false;
  13. void ThreadStatusUtil::RegisterThread(const Env* env,
  14. ThreadStatus::ThreadType thread_type) {
  15. if (!MaybeInitThreadLocalUpdater(env)) {
  16. return;
  17. }
  18. assert(thread_updater_local_cache_);
  19. thread_updater_local_cache_->RegisterThread(thread_type, env->GetThreadID());
  20. }
  21. void ThreadStatusUtil::UnregisterThread() {
  22. thread_updater_initialized_ = false;
  23. if (thread_updater_local_cache_ != nullptr) {
  24. thread_updater_local_cache_->UnregisterThread();
  25. thread_updater_local_cache_ = nullptr;
  26. }
  27. }
  28. void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* cfd,
  29. const Env* env,
  30. bool enable_thread_tracking) {
  31. if (!MaybeInitThreadLocalUpdater(env)) {
  32. return;
  33. }
  34. assert(thread_updater_local_cache_);
  35. if (cfd != nullptr && enable_thread_tracking) {
  36. thread_updater_local_cache_->SetColumnFamilyInfoKey(cfd);
  37. } else {
  38. // When cfd == nullptr or enable_thread_tracking == false, we set
  39. // ColumnFamilyInfoKey to nullptr, which makes SetThreadOperation
  40. // and SetThreadState become no-op.
  41. thread_updater_local_cache_->SetColumnFamilyInfoKey(nullptr);
  42. }
  43. }
  44. void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType op) {
  45. if (thread_updater_local_cache_ == nullptr) {
  46. // thread_updater_local_cache_ must be set in SetColumnFamily
  47. // or other ThreadStatusUtil functions.
  48. return;
  49. }
  50. if (op != ThreadStatus::OP_UNKNOWN) {
  51. uint64_t current_time = Env::Default()->NowMicros();
  52. thread_updater_local_cache_->SetOperationStartTime(current_time);
  53. } else {
  54. // TDOO(yhchiang): we could report the time when we set operation to
  55. // OP_UNKNOWN once the whole instrumentation has been done.
  56. thread_updater_local_cache_->SetOperationStartTime(0);
  57. }
  58. thread_updater_local_cache_->SetThreadOperation(op);
  59. }
  60. ThreadStatus::OperationStage ThreadStatusUtil::SetThreadOperationStage(
  61. ThreadStatus::OperationStage stage) {
  62. if (thread_updater_local_cache_ == nullptr) {
  63. // thread_updater_local_cache_ must be set in SetColumnFamily
  64. // or other ThreadStatusUtil functions.
  65. return ThreadStatus::STAGE_UNKNOWN;
  66. }
  67. return thread_updater_local_cache_->SetThreadOperationStage(stage);
  68. }
  69. void ThreadStatusUtil::SetThreadOperationProperty(int code, uint64_t value) {
  70. if (thread_updater_local_cache_ == nullptr) {
  71. // thread_updater_local_cache_ must be set in SetColumnFamily
  72. // or other ThreadStatusUtil functions.
  73. return;
  74. }
  75. thread_updater_local_cache_->SetThreadOperationProperty(code, value);
  76. }
  77. void ThreadStatusUtil::IncreaseThreadOperationProperty(int code,
  78. uint64_t delta) {
  79. if (thread_updater_local_cache_ == nullptr) {
  80. // thread_updater_local_cache_ must be set in SetColumnFamily
  81. // or other ThreadStatusUtil functions.
  82. return;
  83. }
  84. thread_updater_local_cache_->IncreaseThreadOperationProperty(code, delta);
  85. }
  86. void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType state) {
  87. if (thread_updater_local_cache_ == nullptr) {
  88. // thread_updater_local_cache_ must be set in SetColumnFamily
  89. // or other ThreadStatusUtil functions.
  90. return;
  91. }
  92. thread_updater_local_cache_->SetThreadState(state);
  93. }
  94. void ThreadStatusUtil::ResetThreadStatus() {
  95. if (thread_updater_local_cache_ == nullptr) {
  96. return;
  97. }
  98. thread_updater_local_cache_->ResetThreadStatus();
  99. }
  100. void ThreadStatusUtil::NewColumnFamilyInfo(const DB* db,
  101. const ColumnFamilyData* cfd,
  102. const std::string& cf_name,
  103. const Env* env) {
  104. if (!MaybeInitThreadLocalUpdater(env)) {
  105. return;
  106. }
  107. assert(thread_updater_local_cache_);
  108. if (thread_updater_local_cache_) {
  109. thread_updater_local_cache_->NewColumnFamilyInfo(db, db->GetName(), cfd,
  110. cf_name);
  111. }
  112. }
  113. void ThreadStatusUtil::EraseColumnFamilyInfo(const ColumnFamilyData* cfd) {
  114. if (thread_updater_local_cache_ == nullptr) {
  115. return;
  116. }
  117. thread_updater_local_cache_->EraseColumnFamilyInfo(cfd);
  118. }
  119. void ThreadStatusUtil::EraseDatabaseInfo(const DB* db) {
  120. ThreadStatusUpdater* thread_updater = db->GetEnv()->GetThreadStatusUpdater();
  121. if (thread_updater == nullptr) {
  122. return;
  123. }
  124. thread_updater->EraseDatabaseInfo(db);
  125. }
  126. bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env* env) {
  127. if (!thread_updater_initialized_ && env != nullptr) {
  128. thread_updater_initialized_ = true;
  129. thread_updater_local_cache_ = env->GetThreadStatusUpdater();
  130. }
  131. return (thread_updater_local_cache_ != nullptr);
  132. }
  133. AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater(
  134. ThreadStatus::OperationStage stage) {
  135. prev_stage_ = ThreadStatusUtil::SetThreadOperationStage(stage);
  136. }
  137. AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {
  138. ThreadStatusUtil::SetThreadOperationStage(prev_stage_);
  139. }
  140. #else
  141. ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ = nullptr;
  142. bool ThreadStatusUtil::thread_updater_initialized_ = false;
  143. bool ThreadStatusUtil::MaybeInitThreadLocalUpdater(const Env* /*env*/) {
  144. return false;
  145. }
  146. void ThreadStatusUtil::SetColumnFamily(const ColumnFamilyData* /*cfd*/,
  147. const Env* /*env*/,
  148. bool /*enable_thread_tracking*/) {}
  149. void ThreadStatusUtil::SetThreadOperation(ThreadStatus::OperationType /*op*/) {}
  150. void ThreadStatusUtil::SetThreadOperationProperty(int /*code*/,
  151. uint64_t /*value*/) {}
  152. void ThreadStatusUtil::IncreaseThreadOperationProperty(int /*code*/,
  153. uint64_t /*delta*/) {}
  154. void ThreadStatusUtil::SetThreadState(ThreadStatus::StateType /*state*/) {}
  155. void ThreadStatusUtil::NewColumnFamilyInfo(const DB* /*db*/,
  156. const ColumnFamilyData* /*cfd*/,
  157. const std::string& /*cf_name*/,
  158. const Env* /*env*/) {}
  159. void ThreadStatusUtil::EraseColumnFamilyInfo(const ColumnFamilyData* /*cfd*/) {}
  160. void ThreadStatusUtil::EraseDatabaseInfo(const DB* /*db*/) {}
  161. void ThreadStatusUtil::ResetThreadStatus() {}
  162. AutoThreadOperationStageUpdater::AutoThreadOperationStageUpdater(
  163. ThreadStatus::OperationStage /*stage*/) {}
  164. AutoThreadOperationStageUpdater::~AutoThreadOperationStageUpdater() {}
  165. #endif // ROCKSDB_USING_THREAD_STATUS
  166. } // namespace ROCKSDB_NAMESPACE