#pragma once
#include <atomic>
#include <list>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "port/port.h"
#include "rocksdb/status.h"
#include "rocksdb/thread_status.h"
#include "util/thread_operation.h"
namespace ROCKSDB_NAMESPACE {
class ColumnFamilyHandle;
struct ConstantColumnFamilyInfo {
#ifndef NROCKSDB_THREAD_STATUS
public:
ConstantColumnFamilyInfo(const void* _db_key, const std::string& _db_name,
const std::string& _cf_name)
: db_key(_db_key), db_name(_db_name), cf_name(_cf_name) {}
const void* db_key;
const std::string db_name;
const std::string cf_name;
#endif };
struct ThreadStatusData {
#ifndef NROCKSDB_THREAD_STATUS
explicit ThreadStatusData() {
enable_tracking.store(false);
thread_id.store(0);
thread_type.store(ThreadStatus::USER);
cf_key.store(nullptr);
operation_type.store(ThreadStatus::OP_UNKNOWN);
op_start_time.store(0);
state_type.store(ThreadStatus::STATE_UNKNOWN);
}
std::atomic<bool> enable_tracking;
std::atomic<uint64_t> thread_id;
std::atomic<ThreadStatus::ThreadType> thread_type;
std::atomic<void*> cf_key;
std::atomic<ThreadStatus::OperationType> operation_type;
std::atomic<uint64_t> op_start_time;
std::atomic<ThreadStatus::OperationStage> operation_stage;
std::atomic<uint64_t> op_properties[ThreadStatus::kNumOperationProperties];
std::atomic<ThreadStatus::StateType> state_type;
#endif };
class ThreadStatusUpdater {
public:
ThreadStatusUpdater() {}
virtual ~ThreadStatusUpdater() {}
void UnregisterThread();
void ResetThreadStatus();
void SetThreadID(uint64_t thread_id);
void RegisterThread(ThreadStatus::ThreadType ttype, uint64_t thread_id);
void SetEnableTracking(bool enable_tracking);
void SetColumnFamilyInfoKey(const void* cf_key);
const void* GetColumnFamilyInfoKey();
void SetThreadOperation(const ThreadStatus::OperationType type);
ThreadStatus::OperationType GetThreadOperation();
void SetOperationStartTime(const uint64_t start_time);
void SetThreadOperationProperty(int i, uint64_t value);
void IncreaseThreadOperationProperty(int i, uint64_t delta);
ThreadStatus::OperationStage SetThreadOperationStage(
const ThreadStatus::OperationStage stage);
void ClearThreadOperation();
void ClearThreadOperationProperties();
void SetThreadState(const ThreadStatus::StateType type);
void ClearThreadState();
Status GetThreadList(std::vector<ThreadStatus>* thread_list);
void NewColumnFamilyInfo(const void* db_key, const std::string& db_name,
const void* cf_key, const std::string& cf_name);
void EraseDatabaseInfo(const void* db_key);
void EraseColumnFamilyInfo(const void* cf_key);
void TEST_VerifyColumnFamilyInfoMap(
const std::vector<ColumnFamilyHandle*>& handles, bool check_exist);
protected:
#ifndef NROCKSDB_THREAD_STATUS
static thread_local ThreadStatusData* thread_status_data_;
ThreadStatusData* GetLocalThreadStatus();
ThreadStatusData* Get() { return thread_status_data_; }
std::mutex thread_list_mutex_;
std::unordered_set<ThreadStatusData*> thread_data_set_;
std::unordered_map<const void*, ConstantColumnFamilyInfo> cf_info_map_;
std::unordered_map<const void*, std::unordered_set<const void*>> db_key_map_;
#else
static ThreadStatusData* thread_status_data_;
#endif };
}