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).
6 // This file defines the structures for exposing run-time status of any
7 // rocksdb-related thread. Such run-time status can be obtained via
8 // GetThreadList() API.
10 // Note that all thread-status features are still under-development, and
11 // thus APIs and class definitions might subject to change at this point.
12 // Will remove this comment once the APIs have been finalized.
23 #if !defined(ROCKSDB_LITE) && !defined(NROCKSDB_THREAD_STATUS) && \
24 defined(ROCKSDB_SUPPORT_THREAD_LOCAL)
25 #define ROCKSDB_USING_THREAD_STATUS
30 // TODO(yhchiang): remove this function once c++14 is available
31 // as std::max will be able to cover this.
32 // Current MS compiler does not support constexpr
33 template <int A
, int B
>
34 struct constexpr_max
{
35 static const int result
= (A
> B
) ? A
: B
;
38 // A structure that describes the current status of a thread.
39 // The status of active threads can be fetched using
40 // rocksdb::GetThreadList().
42 // The type of a thread.
43 enum ThreadType
: int {
44 HIGH_PRIORITY
= 0, // RocksDB BG thread in high-pri thread pool
45 LOW_PRIORITY
, // RocksDB BG thread in low-pri thread pool
46 USER
, // User thread (Non-RocksDB BG thread)
47 BOTTOM_PRIORITY
, // RocksDB BG thread in bottom-pri thread pool
51 // The type used to refer to a thread operation.
52 // A thread operation describes high-level action of a thread.
53 // Examples include compaction and flush.
54 enum OperationType
: int {
61 enum OperationStage
: int {
65 STAGE_COMPACTION_PREPARE
,
67 STAGE_COMPACTION_PROCESS_KV
,
68 STAGE_COMPACTION_INSTALL
,
69 STAGE_COMPACTION_SYNC_FILE
,
70 STAGE_PICK_MEMTABLES_TO_FLUSH
,
71 STAGE_MEMTABLE_ROLLBACK
,
72 STAGE_MEMTABLE_INSTALL_FLUSH_RESULTS
,
76 enum CompactionPropertyType
: int {
77 COMPACTION_JOB_ID
= 0,
78 COMPACTION_INPUT_OUTPUT_LEVEL
,
79 COMPACTION_PROP_FLAGS
,
80 COMPACTION_TOTAL_INPUT_BYTES
,
81 COMPACTION_BYTES_READ
,
82 COMPACTION_BYTES_WRITTEN
,
83 NUM_COMPACTION_PROPERTIES
86 enum FlushPropertyType
: int {
88 FLUSH_BYTES_MEMTABLES
,
93 // The maximum number of properties of an operation.
94 // This number should be set to the biggest NUM_XXX_PROPERTIES.
95 static const int kNumOperationProperties
=
96 constexpr_max
<NUM_COMPACTION_PROPERTIES
, NUM_FLUSH_PROPERTIES
>::result
;
98 // The type used to refer to a thread state.
99 // A state describes lower-level action of a thread
100 // such as reading / writing a file or waiting for a mutex.
101 enum StateType
: int {
103 STATE_MUTEX_WAIT
= 1,
107 ThreadStatus(const uint64_t _id
, const ThreadType _thread_type
,
108 const std::string
& _db_name
, const std::string
& _cf_name
,
109 const OperationType _operation_type
,
110 const uint64_t _op_elapsed_micros
,
111 const OperationStage _operation_stage
,
112 const uint64_t _op_props
[], const StateType _state_type
)
114 thread_type(_thread_type
),
117 operation_type(_operation_type
),
118 op_elapsed_micros(_op_elapsed_micros
),
119 operation_stage(_operation_stage
),
120 state_type(_state_type
) {
121 for (int i
= 0; i
< kNumOperationProperties
; ++i
) {
122 op_properties
[i
] = _op_props
[i
];
126 // An unique ID for the thread.
127 const uint64_t thread_id
;
129 // The type of the thread, it could be HIGH_PRIORITY,
130 // LOW_PRIORITY, and USER
131 const ThreadType thread_type
;
133 // The name of the DB instance where the thread is currently
134 // involved with. It would be set to empty string if the thread
135 // does not involve in any DB operation.
136 const std::string db_name
;
138 // The name of the column family where the thread is currently
139 // It would be set to empty string if the thread does not involve
140 // in any column family.
141 const std::string cf_name
;
143 // The operation (high-level action) that the current thread is involved.
144 const OperationType operation_type
;
146 // The elapsed time of the current thread operation in microseconds.
147 const uint64_t op_elapsed_micros
;
149 // An integer showing the current stage where the thread is involved
150 // in the current operation.
151 const OperationStage operation_stage
;
153 // A list of properties that describe some details about the current
154 // operation. Same field in op_properties[] might have different
155 // meanings for different operations.
156 uint64_t op_properties
[kNumOperationProperties
];
158 // The state (lower-level action) that the current thread is involved.
159 const StateType state_type
;
161 // The followings are a set of utility functions for interpreting
162 // the information of ThreadStatus
164 static std::string
GetThreadTypeName(ThreadType thread_type
);
166 // Obtain the name of an operation given its type.
167 static const std::string
& GetOperationName(OperationType op_type
);
169 static const std::string
MicrosToString(uint64_t op_elapsed_time
);
171 // Obtain a human-readable string describing the specified operation stage.
172 static const std::string
& GetOperationStageName(OperationStage stage
);
174 // Obtain the name of the "i"th operation property of the
175 // specified operation.
176 static const std::string
& GetOperationPropertyName(OperationType op_type
,
179 // Translate the "i"th property of the specified operation given
181 static std::map
<std::string
, uint64_t> InterpretOperationProperties(
182 OperationType op_type
, const uint64_t* op_properties
);
184 // Obtain the name of a state given its type.
185 static const std::string
& GetStateName(StateType state_type
);
188 } // namespace rocksdb