ClickHouse/dbms/src/Common/ThreadStatus.h

116 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
#include <Common/ProfileEvents.h>
#include <Common/MemoryTracker.h>
#include <IO/Progress.h>
#include <memory>
#include <mutex>
namespace Poco
{
class Logger;
}
namespace DB
{
class Context;
class QueryStatus;
class ThreadStatus;
class QueryThreadLog;
using ThreadStatusPtr = std::shared_ptr<ThreadStatus>;
2018-05-29 18:14:31 +00:00
extern thread_local ThreadStatusPtr current_thread;
class ThreadStatus : public std::enable_shared_from_this<ThreadStatus>
{
public:
/// Poco's thread number (the same number is used in logs)
UInt32 thread_number = 0;
ProfileEvents::Counters performance_counters;
MemoryTracker memory_tracker;
Int32 os_thread_id = -1;
/// Statistics of read and write rows/bytes
Progress progress_in;
Progress progress_out;
2018-05-29 18:14:31 +00:00
public:
static ThreadStatusPtr create();
/// Called by master thread when the query finishes
void clean();
enum ThreadState
{
DetachedFromQuery = 0, /// We just created thread or it is background thread
QueryInitializing, /// We accepted a connection, but haven't enqueued a query to ProcessList
AttachedToQuery, /// Thread executes enqueued query
Died, /// Thread does not exist
};
int getCurrentState() const
{
return thread_state.load(std::memory_order_relaxed);
}
Context * getGlobalContext()
{
return global_context.load(std::memory_order_relaxed);
}
~ThreadStatus();
2018-05-29 18:14:31 +00:00
protected:
ThreadStatus();
void initializeQuery();
2018-05-29 18:14:31 +00:00
void attachQuery(
QueryStatus * parent_query_,
ProfileEvents::Counters * parent_counters,
MemoryTracker * parent_memory_tracker,
2018-05-29 18:14:31 +00:00
bool check_detached = true);
void detachQuery(bool thread_exits = false);
void logToQueryThreadLog(QueryThreadLog & thread_log);
2018-05-29 18:14:31 +00:00
void updatePerfomanceCountersImpl();
std::atomic<int> thread_state{ThreadState::DetachedFromQuery};
std::mutex mutex;
QueryStatus * parent_query = nullptr;
/// Is set once
std::atomic<Context *> global_context{nullptr};
/// Use it only from current thread
Context * query_context = nullptr;
UInt64 query_start_time_nanoseconds = 0;
time_t query_start_time = 0;
bool log_to_query_thread_log = true;
bool log_profile_events = true;
Poco::Logger * log = nullptr;
2018-05-29 18:14:31 +00:00
friend class CurrentThread;
friend struct TasksStatsCounters;
struct Impl;
2018-05-29 18:14:31 +00:00
std::unique_ptr<Impl> impl;
public:
class CurrentThreadScope;
};
}