thread_status_util.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #pragma once
  6. #include <string>
  7. #include "monitoring/thread_status_updater.h"
  8. #include "rocksdb/db.h"
  9. #include "rocksdb/env.h"
  10. #include "rocksdb/thread_status.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class ColumnFamilyData;
  13. // The static utility class for updating thread-local status.
  14. //
  15. // The thread-local status is updated via the thread-local cached
  16. // pointer thread_updater_local_cache_. During each function call,
  17. // when ThreadStatusUtil finds thread_updater_local_cache_ is
  18. // left uninitialized (determined by thread_updater_initialized_),
  19. // it will tries to initialize it using the return value of
  20. // Env::GetThreadStatusUpdater(). When thread_updater_local_cache_
  21. // is initialized by a non-null pointer, each function call will
  22. // then update the status of the current thread. Otherwise,
  23. // all function calls to ThreadStatusUtil will be no-op.
  24. class ThreadStatusUtil {
  25. public:
  26. // Register the current thread for tracking.
  27. static void RegisterThread(
  28. const Env* env, ThreadStatus::ThreadType thread_type);
  29. // Unregister the current thread.
  30. static void UnregisterThread();
  31. // Create an entry in the global ColumnFamilyInfo table for the
  32. // specified column family. This function should be called only
  33. // when the current thread does not hold db_mutex.
  34. static void NewColumnFamilyInfo(const DB* db, const ColumnFamilyData* cfd,
  35. const std::string& cf_name, const Env* env);
  36. // Erase the ConstantColumnFamilyInfo that is associated with the
  37. // specified ColumnFamilyData. This function should be called only
  38. // when the current thread does not hold db_mutex.
  39. static void EraseColumnFamilyInfo(const ColumnFamilyData* cfd);
  40. // Erase all ConstantColumnFamilyInfo that is associated with the
  41. // specified db instance. This function should be called only when
  42. // the current thread does not hold db_mutex.
  43. static void EraseDatabaseInfo(const DB* db);
  44. // Update the thread status to indicate the current thread is doing
  45. // something related to the specified column family.
  46. static void SetColumnFamily(const ColumnFamilyData* cfd, const Env* env,
  47. bool enable_thread_tracking);
  48. static void SetThreadOperation(ThreadStatus::OperationType type);
  49. static ThreadStatus::OperationStage SetThreadOperationStage(
  50. ThreadStatus::OperationStage stage);
  51. static void SetThreadOperationProperty(
  52. int code, uint64_t value);
  53. static void IncreaseThreadOperationProperty(
  54. int code, uint64_t delta);
  55. static void SetThreadState(ThreadStatus::StateType type);
  56. static void ResetThreadStatus();
  57. #ifndef NDEBUG
  58. static void TEST_SetStateDelay(
  59. const ThreadStatus::StateType state, int micro);
  60. static void TEST_StateDelay(const ThreadStatus::StateType state);
  61. #endif
  62. protected:
  63. // Initialize the thread-local ThreadStatusUpdater when it finds
  64. // the cached value is nullptr. Returns true if it has cached
  65. // a non-null pointer.
  66. static bool MaybeInitThreadLocalUpdater(const Env* env);
  67. #ifdef ROCKSDB_USING_THREAD_STATUS
  68. // A boolean flag indicating whether thread_updater_local_cache_
  69. // is initialized. It is set to true when an Env uses any
  70. // ThreadStatusUtil functions using the current thread other
  71. // than UnregisterThread(). It will be set to false when
  72. // UnregisterThread() is called.
  73. //
  74. // When this variable is set to true, thread_updater_local_cache_
  75. // will not be updated until this variable is again set to false
  76. // in UnregisterThread().
  77. static __thread bool thread_updater_initialized_;
  78. // The thread-local cached ThreadStatusUpdater that caches the
  79. // thread_status_updater_ of the first Env that uses any ThreadStatusUtil
  80. // function other than UnregisterThread(). This variable will
  81. // be cleared when UnregisterThread() is called.
  82. //
  83. // When this variable is set to a non-null pointer, then the status
  84. // of the current thread will be updated when a function of
  85. // ThreadStatusUtil is called. Otherwise, all functions of
  86. // ThreadStatusUtil will be no-op.
  87. //
  88. // When thread_updater_initialized_ is set to true, this variable
  89. // will not be updated until this thread_updater_initialized_ is
  90. // again set to false in UnregisterThread().
  91. static __thread ThreadStatusUpdater* thread_updater_local_cache_;
  92. #else
  93. static bool thread_updater_initialized_;
  94. static ThreadStatusUpdater* thread_updater_local_cache_;
  95. #endif
  96. };
  97. // A helper class for updating thread state. It will set the
  98. // thread state according to the input parameter in its constructor
  99. // and set the thread state to the previous state in its destructor.
  100. class AutoThreadOperationStageUpdater {
  101. public:
  102. explicit AutoThreadOperationStageUpdater(
  103. ThreadStatus::OperationStage stage);
  104. ~AutoThreadOperationStageUpdater();
  105. #ifdef ROCKSDB_USING_THREAD_STATUS
  106. private:
  107. ThreadStatus::OperationStage prev_stage_;
  108. #endif
  109. };
  110. } // namespace ROCKSDB_NAMESPACE