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 // 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. 9 // 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. 13 14 #pragma once 15 16 #include <stdint.h> 17 #include <cstddef> 18 #include <map> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 #if !defined(ROCKSDB_LITE) && !defined(NROCKSDB_THREAD_STATUS) && \ 24 defined(ROCKSDB_SUPPORT_THREAD_LOCAL) 25 #define ROCKSDB_USING_THREAD_STATUS 26 #endif 27 28 namespace ROCKSDB_NAMESPACE { 29 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; 36 }; 37 38 // A structure that describes the current status of a thread. 39 // The status of active threads can be fetched using 40 // ROCKSDB_NAMESPACE::GetThreadList(). 41 struct ThreadStatus { 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 48 NUM_THREAD_TYPES 49 }; 50 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 { 55 OP_UNKNOWN = 0, 56 OP_COMPACTION, 57 OP_FLUSH, 58 NUM_OP_TYPES 59 }; 60 61 enum OperationStage : int { 62 STAGE_UNKNOWN = 0, 63 STAGE_FLUSH_RUN, 64 STAGE_FLUSH_WRITE_L0, 65 STAGE_COMPACTION_PREPARE, 66 STAGE_COMPACTION_RUN, 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, 73 NUM_OP_STAGES 74 }; 75 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 84 }; 85 86 enum FlushPropertyType : int { 87 FLUSH_JOB_ID = 0, 88 FLUSH_BYTES_MEMTABLES, 89 FLUSH_BYTES_WRITTEN, 90 NUM_FLUSH_PROPERTIES 91 }; 92 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; 97 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 { 102 STATE_UNKNOWN = 0, 103 STATE_MUTEX_WAIT = 1, 104 NUM_STATE_TYPES 105 }; 106 ThreadStatusThreadStatus107 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) 113 : thread_id(_id), 114 thread_type(_thread_type), 115 db_name(_db_name), 116 cf_name(_cf_name), 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]; 123 } 124 } 125 126 // An unique ID for the thread. 127 const uint64_t thread_id; 128 129 // The type of the thread, it could be HIGH_PRIORITY, 130 // LOW_PRIORITY, and USER 131 const ThreadType thread_type; 132 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; 137 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; 142 143 // The operation (high-level action) that the current thread is involved. 144 const OperationType operation_type; 145 146 // The elapsed time of the current thread operation in microseconds. 147 const uint64_t op_elapsed_micros; 148 149 // An integer showing the current stage where the thread is involved 150 // in the current operation. 151 const OperationStage operation_stage; 152 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]; 157 158 // The state (lower-level action) that the current thread is involved. 159 const StateType state_type; 160 161 // The followings are a set of utility functions for interpreting 162 // the information of ThreadStatus 163 164 static std::string GetThreadTypeName(ThreadType thread_type); 165 166 // Obtain the name of an operation given its type. 167 static const std::string& GetOperationName(OperationType op_type); 168 169 static const std::string MicrosToString(uint64_t op_elapsed_time); 170 171 // Obtain a human-readable string describing the specified operation stage. 172 static const std::string& GetOperationStageName(OperationStage stage); 173 174 // Obtain the name of the "i"th operation property of the 175 // specified operation. 176 static const std::string& GetOperationPropertyName(OperationType op_type, 177 int i); 178 179 // Translate the "i"th property of the specified operation given 180 // a property value. 181 static std::map<std::string, uint64_t> InterpretOperationProperties( 182 OperationType op_type, const uint64_t* op_properties); 183 184 // Obtain the name of a state given its type. 185 static const std::string& GetStateName(StateType state_type); 186 }; 187 188 } // namespace ROCKSDB_NAMESPACE 189