7 - /blog/2261/getthreadlist/
10 We recently added a new API, called `GetThreadList()`, that exposes the RocksDB background thread activity. With this feature, developers will be able to obtain the real-time information about the currently running compactions and flushes such as the input / output size, elapsed time, the number of bytes it has written. Below is an example output of `GetThreadList`. To better illustrate the example, we have put a sample output of `GetThreadList` into a table where each column represents a thread status:
16 <tr style="border:2px solid #000000" >
18 <td style="padding:3px" >ThreadID
29 <td style="padding:3px" >DB
40 <td style="padding:3px" >CF
51 <td style="padding:3px" >ThreadType
62 <td style="padding:3px" >Operation
73 <td style="padding:3px" >ElapsedTime
84 <td style="padding:3px" >Stage
87 <td >FlushJob::WriteLevel0Table
90 <td >CompactionJob::Install
95 <td style="vertical-align:top;padding:3px" >OperationProperties
98 <td style="vertical-align:top;padding:3px" >
99 BytesMemtables 4092938
103 <td style="vertical-align:top" >
112 TotalInputBytes 4883044
118 In the above output, we can see `GetThreadList()` reports the activity of two threads: one thread running flush job (middle column) and the other thread running a compaction job (right-most column). In each thread status, it shows basic information about the thread such as thread id, it's target db / column family, and the job it is currently doing and the current status of the job. For instance, we can see thread 140716416169728 is doing compaction on the `picachu` column family in database `db2`. In addition, we can see the compaction has been running for 600 ms, and it has read 4876417 bytes out of 4883044 bytes. This indicates the compaction is about to complete. The stage property indicates which code block the thread is currently executing. For instance, thread 140716416169728 is currently running `CompactionJob::Install`, which further indicates the compaction job is almost done.
120 Below we briefly describe its API.
126 To enable thread-tracking of a rocksdb instance, simply set `enable_thread_tracking` to true in its DBOptions:
129 // If true, then the status of the threads involved in this DB will
130 // be tracked and available via GetThreadList() API.
133 bool enable_thread_tracking;
141 The GetThreadList API is defined in [include/rocksdb/env.h](https://github.com/facebook/rocksdb/blob/master/include/rocksdb/env.h#L317-L318), which is an Env
145 virtual Status GetThreadList(std::vector* thread_list)
148 Since an Env can be shared across multiple rocksdb instances, the output of
149 `GetThreadList()` include the background activity of all the rocksdb instances
150 that using the same Env.
152 The `GetThreadList()` API simply returns a vector of `ThreadStatus`, each describes
153 the current status of a thread. The `ThreadStatus` structure, defined in
154 [include/rocksdb/thread_status.h](https://github.com/facebook/rocksdb/blob/master/include/rocksdb/thread_status.h), contains the following information:
157 // An unique ID for the thread.
158 const uint64_t thread_id;
160 // The type of the thread, it could be HIGH_PRIORITY,
161 // LOW_PRIORITY, and USER
162 const ThreadType thread_type;
164 // The name of the DB instance where the thread is currently
165 // involved with. It would be set to empty string if the thread
166 // does not involve in any DB operation.
167 const std::string db_name;
169 // The name of the column family where the thread is currently
170 // It would be set to empty string if the thread does not involve
171 // in any column family.
172 const std::string cf_name;
174 // The operation (high-level action) that the current thread is involved.
175 const OperationType operation_type;
177 // The elapsed time in micros of the current thread operation.
178 const uint64_t op_elapsed_micros;
180 // An integer showing the current stage where the thread is involved
181 // in the current operation.
182 const OperationStage operation_stage;
184 // A list of properties that describe some details about the current
185 // operation. Same field in op_properties[] might have different
186 // meanings for different operations.
187 uint64_t op_properties[kNumOperationProperties];
189 // The state (lower-level action) that the current thread is involved.
190 const StateType state_type;
193 If you are interested in the background thread activity of your RocksDB application, please feel free to give `GetThreadList()` a try :)