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