2014-11-20 19:49:32 +01:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2014-12-22 21:20:17 +01:00
|
|
|
// The implementation of ThreadStatus.
|
2014-11-20 19:49:32 +01:00
|
|
|
//
|
|
|
|
// Note that we make get and set access to ThreadStatusData lockless.
|
|
|
|
// As a result, ThreadStatusData as a whole is not atomic. However,
|
|
|
|
// we guarantee consistent ThreadStatusData all the time whenever
|
|
|
|
// user call GetThreadList(). This consistency guarantee is done
|
|
|
|
// by having the following constraint in the internal implementation
|
|
|
|
// of set and get order:
|
|
|
|
//
|
|
|
|
// 1. When reset any information in ThreadStatusData, always start from
|
|
|
|
// clearing up the lower-level information first.
|
|
|
|
// 2. When setting any information in ThreadStatusData, always start from
|
|
|
|
// setting the higher-level information.
|
|
|
|
// 3. When returning ThreadStatusData to the user, fields are fetched from
|
|
|
|
// higher-level to lower-level. In addition, where there's a nullptr
|
|
|
|
// in one field, then all fields that has lower-level than that field
|
|
|
|
// should be ignored.
|
|
|
|
//
|
|
|
|
// The high to low level information would be:
|
2014-12-30 19:39:13 +01:00
|
|
|
// thread_id > thread_type > db > cf > operation > state
|
2014-11-20 19:49:32 +01:00
|
|
|
//
|
|
|
|
// This means user might not always get full information, but whenever
|
|
|
|
// returned by the GetThreadList() is guaranteed to be consistent.
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
2015-01-09 21:57:11 +01:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2014-11-20 19:49:32 +01:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2015-01-09 21:57:11 +01:00
|
|
|
#include <unordered_set>
|
2014-11-20 19:49:32 +01:00
|
|
|
#include <vector>
|
2015-01-09 21:57:11 +01:00
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "rocksdb/thread_status.h"
|
|
|
|
#include "port/port_posix.h"
|
2014-12-30 19:39:13 +01:00
|
|
|
#include "util/thread_operation.h"
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class ColumnFamilyHandle;
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
// The structure that keeps constant information about a column family.
|
2014-11-20 19:49:32 +01:00
|
|
|
struct ConstantColumnFamilyInfo {
|
|
|
|
#if ROCKSDB_USING_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 // ROCKSDB_USING_THREAD_STATUS
|
|
|
|
};
|
|
|
|
|
|
|
|
// the internal data-structure that is used to reflect the current
|
|
|
|
// status of a thread using a set of atomic pointers.
|
|
|
|
struct ThreadStatusData {
|
|
|
|
#if ROCKSDB_USING_THREAD_STATUS
|
2015-01-13 09:04:08 +01:00
|
|
|
explicit ThreadStatusData() : thread_id(0), enable_tracking(false) {
|
2014-12-30 19:39:13 +01:00
|
|
|
thread_type.store(ThreadStatus::USER);
|
2015-01-13 09:04:08 +01:00
|
|
|
cf_key.store(nullptr);
|
2014-12-30 19:39:13 +01:00
|
|
|
operation_type.store(ThreadStatus::OP_UNKNOWN);
|
2015-03-10 22:51:28 +01:00
|
|
|
op_start_time.store(0);
|
2014-12-30 19:39:13 +01:00
|
|
|
state_type.store(ThreadStatus::STATE_UNKNOWN);
|
2014-11-20 19:49:32 +01:00
|
|
|
}
|
2015-01-13 09:04:08 +01:00
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
uint64_t thread_id;
|
2015-01-13 09:04:08 +01:00
|
|
|
|
|
|
|
// A flag to indicate whether the thread tracking is enabled
|
|
|
|
// in the current thread. This value will be updated based on whether
|
|
|
|
// the associated Options::enable_thread_tracking is set to true
|
|
|
|
// in ThreadStatusUtil::SetColumnFamily().
|
|
|
|
//
|
|
|
|
// If set to false, then SetThreadOperation and SetThreadState
|
|
|
|
// will be no-op.
|
|
|
|
bool enable_tracking;
|
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
std::atomic<ThreadStatus::ThreadType> thread_type;
|
|
|
|
std::atomic<const void*> cf_key;
|
2014-12-30 19:39:13 +01:00
|
|
|
std::atomic<ThreadStatus::OperationType> operation_type;
|
2015-03-24 00:35:04 +01:00
|
|
|
std::atomic<uint64_t> op_start_time;
|
2015-03-13 18:45:40 +01:00
|
|
|
std::atomic<ThreadStatus::OperationStage> operation_stage;
|
2015-05-07 07:50:35 +02:00
|
|
|
std::atomic<uint64_t> op_properties[ThreadStatus::kNumOperationProperties];
|
2014-12-30 19:39:13 +01:00
|
|
|
std::atomic<ThreadStatus::StateType> state_type;
|
2014-11-20 19:49:32 +01:00
|
|
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
|
|
|
};
|
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
// The class that stores and updates the status of the current thread
|
|
|
|
// using a thread-local ThreadStatusData.
|
|
|
|
//
|
|
|
|
// In most of the case, you should use ThreadStatusUtil to update
|
|
|
|
// the status of the current thread instead of using ThreadSatusUpdater
|
|
|
|
// directly.
|
|
|
|
//
|
|
|
|
// @see ThreadStatusUtil
|
|
|
|
class ThreadStatusUpdater {
|
2014-11-20 19:49:32 +01:00
|
|
|
public:
|
2014-12-22 21:20:17 +01:00
|
|
|
ThreadStatusUpdater() {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// Releases all ThreadStatusData of all active threads.
|
2014-12-22 21:20:17 +01:00
|
|
|
virtual ~ThreadStatusUpdater() {}
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
// Unregister the current thread.
|
2014-11-20 19:49:32 +01:00
|
|
|
void UnregisterThread();
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
// Reset the status of the current thread. This includes resetting
|
|
|
|
// ColumnFamilyInfoKey, ThreadOperation, and ThreadState.
|
|
|
|
void ResetThreadStatus();
|
|
|
|
|
2014-11-20 19:49:32 +01:00
|
|
|
// Set the thread type of the current thread.
|
|
|
|
void SetThreadType(ThreadStatus::ThreadType ttype);
|
|
|
|
|
|
|
|
// Update the column-family info of the current thread by setting
|
2014-12-30 19:39:13 +01:00
|
|
|
// its thread-local pointer of ThreadStateInfo to the correct entry.
|
2014-11-20 19:49:32 +01:00
|
|
|
void SetColumnFamilyInfoKey(const void* cf_key);
|
|
|
|
|
2015-01-13 09:04:08 +01:00
|
|
|
// returns the column family info key.
|
|
|
|
const void* GetColumnFamilyInfoKey();
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
// Update the thread operation of the current thread.
|
|
|
|
void SetThreadOperation(const ThreadStatus::OperationType type);
|
|
|
|
|
2015-03-24 00:35:04 +01:00
|
|
|
// The start time of the current thread operation. It is in the format
|
|
|
|
// of micro-seconds since some fixed point in time.
|
|
|
|
void SetOperationStartTime(const uint64_t start_time);
|
2015-03-10 22:51:28 +01:00
|
|
|
|
2015-05-07 07:50:35 +02:00
|
|
|
// Set the "i"th property of the current operation.
|
|
|
|
//
|
|
|
|
// NOTE: Our practice here is to set all the thread operation properties
|
|
|
|
// and stage before we set thread operation, and thread operation
|
|
|
|
// will be set in std::memory_order_release. This is to ensure
|
|
|
|
// whenever a thread operation is not OP_UNKNOWN, we will always
|
|
|
|
// have a consistent information on its properties.
|
|
|
|
void SetThreadOperationProperty(
|
|
|
|
int i, uint64_t value);
|
|
|
|
|
|
|
|
// Increase the "i"th property of the current operation with
|
|
|
|
// the specified delta.
|
|
|
|
void IncreaseThreadOperationProperty(
|
|
|
|
int i, uint64_t delta);
|
|
|
|
|
2015-03-13 18:45:40 +01:00
|
|
|
// Update the thread operation stage of the current thread.
|
|
|
|
ThreadStatus::OperationStage SetThreadOperationStage(
|
|
|
|
const ThreadStatus::OperationStage stage);
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
// Clear thread operation of the current thread.
|
|
|
|
void ClearThreadOperation();
|
|
|
|
|
2015-05-07 07:50:35 +02:00
|
|
|
// Reset all thread-operation-properties to 0.
|
|
|
|
void ClearThreadOperationProperties();
|
|
|
|
|
2014-12-30 19:39:13 +01:00
|
|
|
// Update the thread state of the current thread.
|
|
|
|
void SetThreadState(const ThreadStatus::StateType type);
|
|
|
|
|
|
|
|
// Clear the thread state of the current thread.
|
|
|
|
void ClearThreadState();
|
2014-11-20 19:49:32 +01:00
|
|
|
|
2014-12-22 21:20:17 +01:00
|
|
|
// Obtain the status of all active registered threads.
|
2014-11-20 19:49:32 +01:00
|
|
|
Status GetThreadList(
|
2014-12-22 21:20:17 +01:00
|
|
|
std::vector<ThreadStatus>* thread_list);
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// Create an entry in the global ColumnFamilyInfo table for the
|
|
|
|
// specified column family. This function should be called only
|
|
|
|
// when the current thread does not hold db_mutex.
|
2014-12-22 21:20:17 +01:00
|
|
|
void NewColumnFamilyInfo(
|
2014-11-20 19:49:32 +01:00
|
|
|
const void* db_key, const std::string& db_name,
|
|
|
|
const void* cf_key, const std::string& cf_name);
|
|
|
|
|
|
|
|
// Erase all ConstantColumnFamilyInfo that is associated with the
|
|
|
|
// specified db instance. This function should be called only when
|
|
|
|
// the current thread does not hold db_mutex.
|
2014-12-22 21:20:17 +01:00
|
|
|
void EraseDatabaseInfo(const void* db_key);
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// Erase the ConstantColumnFamilyInfo that is associated with the
|
|
|
|
// specified ColumnFamilyData. This function should be called only
|
|
|
|
// when the current thread does not hold db_mutex.
|
2014-12-22 21:20:17 +01:00
|
|
|
void EraseColumnFamilyInfo(const void* cf_key);
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// Verifies whether the input ColumnFamilyHandles matches
|
|
|
|
// the information stored in the current cf_info_map.
|
2014-12-22 21:20:17 +01:00
|
|
|
void TEST_VerifyColumnFamilyInfoMap(
|
2014-11-21 06:13:18 +01:00
|
|
|
const std::vector<ColumnFamilyHandle*>& handles,
|
|
|
|
bool check_exist);
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
#if ROCKSDB_USING_THREAD_STATUS
|
2014-11-21 20:00:42 +01:00
|
|
|
// The thread-local variable for storing thread status.
|
|
|
|
static __thread ThreadStatusData* thread_status_data_;
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// Obtain the pointer to the thread status data. It also performs
|
|
|
|
// initialization when necessary.
|
|
|
|
ThreadStatusData* InitAndGet();
|
|
|
|
|
|
|
|
// The mutex that protects cf_info_map and db_key_map.
|
2014-12-22 21:20:17 +01:00
|
|
|
std::mutex thread_list_mutex_;
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// The current status data of all active threads.
|
2014-12-22 21:20:17 +01:00
|
|
|
std::unordered_set<ThreadStatusData*> thread_data_set_;
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// A global map that keeps the column family information. It is stored
|
|
|
|
// globally instead of inside DB is to avoid the situation where DB is
|
|
|
|
// closing while GetThreadList function already get the pointer to its
|
|
|
|
// CopnstantColumnFamilyInfo.
|
2014-12-22 21:20:17 +01:00
|
|
|
std::unordered_map<
|
2014-11-22 09:04:41 +01:00
|
|
|
const void*, std::unique_ptr<ConstantColumnFamilyInfo>> cf_info_map_;
|
2014-11-20 19:49:32 +01:00
|
|
|
|
|
|
|
// A db_key to cf_key map that allows erasing elements in cf_info_map
|
|
|
|
// associated to the same db_key faster.
|
2014-12-22 21:20:17 +01:00
|
|
|
std::unordered_map<
|
2014-11-20 19:49:32 +01:00
|
|
|
const void*, std::unordered_set<const void*>> db_key_map_;
|
2015-01-13 09:04:08 +01:00
|
|
|
|
2014-11-21 20:00:42 +01:00
|
|
|
#else
|
|
|
|
static ThreadStatusData* thread_status_data_;
|
2014-11-20 19:49:32 +01:00
|
|
|
#endif // ROCKSDB_USING_THREAD_STATUS
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|