thread_status_util.cc 7.1 KB

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