thread_status_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(const Env* env,
  28. 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. static void SetEnableTracking(bool enable_tracking);
  45. // Update the thread status to indicate the current thread is doing
  46. // something related to the specified column family.
  47. //
  48. // REQUIRES: cfd != nullptr
  49. static void SetColumnFamily(const ColumnFamilyData* cfd);
  50. static void SetThreadOperation(ThreadStatus::OperationType type);
  51. static ThreadStatus::OperationType GetThreadOperation();
  52. static ThreadStatus::OperationStage SetThreadOperationStage(
  53. ThreadStatus::OperationStage stage);
  54. static void SetThreadOperationProperty(int code, uint64_t value);
  55. static void IncreaseThreadOperationProperty(int code, uint64_t delta);
  56. static void SetThreadState(ThreadStatus::StateType type);
  57. static void ResetThreadStatus();
  58. #ifndef NDEBUG
  59. static void TEST_SetStateDelay(const ThreadStatus::StateType state,
  60. int micro);
  61. static void TEST_StateDelay(const ThreadStatus::StateType state);
  62. static Env::IOActivity TEST_GetExpectedIOActivity(
  63. ThreadStatus::OperationType thread_op);
  64. #endif
  65. protected:
  66. // Initialize the thread-local ThreadStatusUpdater when it finds
  67. // the cached value is nullptr. Returns true if it has cached
  68. // a non-null pointer.
  69. static bool MaybeInitThreadLocalUpdater(const Env* env);
  70. #ifdef ROCKSDB_USING_THREAD_STATUS
  71. // A boolean flag indicating whether thread_updater_local_cache_
  72. // is initialized. It is set to true when an Env uses any
  73. // ThreadStatusUtil functions using the current thread other
  74. // than UnregisterThread(). It will be set to false when
  75. // UnregisterThread() is called.
  76. //
  77. // When this variable is set to true, thread_updater_local_cache_
  78. // will not be updated until this variable is again set to false
  79. // in UnregisterThread().
  80. static thread_local bool thread_updater_initialized_;
  81. // The thread-local cached ThreadStatusUpdater that caches the
  82. // thread_status_updater_ of the first Env that uses any ThreadStatusUtil
  83. // function other than UnregisterThread(). This variable will
  84. // be cleared when UnregisterThread() is called.
  85. //
  86. // When this variable is set to a non-null pointer, then the status
  87. // of the current thread will be updated when a function of
  88. // ThreadStatusUtil is called. Otherwise, all functions of
  89. // ThreadStatusUtil will be no-op.
  90. //
  91. // When thread_updater_initialized_ is set to true, this variable
  92. // will not be updated until this thread_updater_initialized_ is
  93. // again set to false in UnregisterThread().
  94. static thread_local ThreadStatusUpdater* thread_updater_local_cache_;
  95. #else
  96. static bool thread_updater_initialized_;
  97. static ThreadStatusUpdater* thread_updater_local_cache_;
  98. #endif
  99. };
  100. // A helper class for updating thread state. It will set the
  101. // thread state according to the input parameter in its constructor
  102. // and set the thread state to the previous state in its destructor.
  103. class AutoThreadOperationStageUpdater {
  104. public:
  105. explicit AutoThreadOperationStageUpdater(ThreadStatus::OperationStage stage);
  106. ~AutoThreadOperationStageUpdater();
  107. #ifdef ROCKSDB_USING_THREAD_STATUS
  108. private:
  109. ThreadStatus::OperationStage prev_stage_;
  110. #endif
  111. };
  112. } // namespace ROCKSDB_NAMESPACE