2018-06-20 17:49:52 +00:00
|
|
|
#include <Common/ThreadStatus.h>
|
2020-01-28 11:59:06 +00:00
|
|
|
|
2020-01-30 14:31:01 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/ProcessList.h>
|
|
|
|
#include <Interpreters/QueryThreadLog.h>
|
2018-06-20 17:49:52 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
|
|
|
#include <Common/Exception.h>
|
2019-03-04 13:03:32 +00:00
|
|
|
#include <Common/QueryProfiler.h>
|
2020-01-30 14:31:01 +00:00
|
|
|
#include <Common/ThreadProfileEvents.h>
|
|
|
|
#include <Common/TraceCollector.h>
|
|
|
|
|
|
|
|
#include <ext/singleton.h>
|
2018-06-20 17:49:52 +00:00
|
|
|
|
2020-01-28 11:59:06 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
# include <Common/hasLinuxCapability.h>
|
2019-07-06 18:02:28 +00:00
|
|
|
|
2020-01-28 11:59:06 +00:00
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/resource.h>
|
2019-07-06 18:02:28 +00:00
|
|
|
#endif
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
|
|
|
|
/// Implement some methods of ThreadStatus and CurrentThread here to avoid extra linking dependencies in clickhouse_common_io
|
2019-02-04 15:39:08 +00:00
|
|
|
/// TODO It doesn't make sense.
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-07-06 18:02:28 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-07-06 18:02:28 +00:00
|
|
|
extern const int CANNOT_SET_THREAD_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
void ThreadStatus::attachQueryContext(Context & query_context_)
|
|
|
|
{
|
|
|
|
query_context = &query_context_;
|
2019-07-15 16:32:52 +00:00
|
|
|
query_id = query_context->getCurrentQueryId();
|
2018-06-20 17:49:52 +00:00
|
|
|
if (!global_context)
|
|
|
|
global_context = &query_context->getGlobalContext();
|
|
|
|
|
2019-02-04 15:39:08 +00:00
|
|
|
if (thread_group)
|
|
|
|
{
|
2019-02-08 13:23:10 +00:00
|
|
|
std::lock_guard lock(thread_group->mutex);
|
2020-01-28 11:59:06 +00:00
|
|
|
|
2019-02-04 15:39:08 +00:00
|
|
|
thread_group->query_context = query_context;
|
|
|
|
if (!thread_group->global_context)
|
|
|
|
thread_group->global_context = global_context;
|
|
|
|
}
|
2020-01-28 11:59:06 +00:00
|
|
|
|
|
|
|
initQueryProfiler();
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 21:01:43 +00:00
|
|
|
void CurrentThread::defaultThreadDeleter()
|
2018-06-20 17:49:52 +00:00
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->detachQuery(true, true);
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 12:01:45 +00:00
|
|
|
void ThreadStatus::setupState(const ThreadGroupStatusPtr & thread_group_)
|
2018-06-20 17:49:52 +00:00
|
|
|
{
|
|
|
|
assertState({ThreadState::DetachedFromQuery}, __PRETTY_FUNCTION__);
|
|
|
|
|
2020-01-28 12:01:45 +00:00
|
|
|
/// Attach or init current thread to thread group and copy useful information from it
|
2018-06-20 17:49:52 +00:00
|
|
|
thread_group = thread_group_;
|
|
|
|
|
|
|
|
performance_counters.setParent(&thread_group->performance_counters);
|
|
|
|
memory_tracker.setParent(&thread_group->memory_tracker);
|
|
|
|
|
|
|
|
{
|
2019-02-08 13:23:10 +00:00
|
|
|
std::lock_guard lock(thread_group->mutex);
|
2018-06-20 17:49:52 +00:00
|
|
|
|
2020-01-28 12:01:45 +00:00
|
|
|
/// NOTE: thread may be attached multiple times if it is reused from a thread pool.
|
2020-02-02 02:27:15 +00:00
|
|
|
thread_group->thread_ids.emplace_back(thread_id);
|
2020-01-28 12:01:45 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
logs_queue_ptr = thread_group->logs_queue_ptr;
|
|
|
|
query_context = thread_group->query_context;
|
|
|
|
|
|
|
|
if (!global_context)
|
|
|
|
global_context = thread_group->global_context;
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:07:01 +00:00
|
|
|
if (query_context)
|
2019-10-29 10:27:19 +00:00
|
|
|
{
|
2019-07-15 01:07:01 +00:00
|
|
|
query_id = query_context->getCurrentQueryId();
|
2020-01-28 11:59:06 +00:00
|
|
|
initQueryProfiler();
|
2019-07-15 01:07:01 +00:00
|
|
|
|
2020-01-28 12:01:45 +00:00
|
|
|
#if defined(OS_LINUX)
|
2019-10-29 10:27:19 +00:00
|
|
|
/// Set "nice" value if required.
|
2019-07-06 18:02:28 +00:00
|
|
|
Int32 new_os_thread_priority = query_context->getSettingsRef().os_thread_priority;
|
|
|
|
if (new_os_thread_priority && hasLinuxCapability(CAP_SYS_NICE))
|
|
|
|
{
|
|
|
|
LOG_TRACE(log, "Setting nice to " << new_os_thread_priority);
|
|
|
|
|
2020-02-02 02:27:15 +00:00
|
|
|
if (0 != setpriority(PRIO_PROCESS, thread_id, new_os_thread_priority))
|
2019-07-06 18:02:28 +00:00
|
|
|
throwFromErrno("Cannot 'setpriority'", ErrorCodes::CANNOT_SET_THREAD_PRIORITY);
|
|
|
|
|
|
|
|
os_thread_priority = new_os_thread_priority;
|
|
|
|
}
|
|
|
|
#endif
|
2019-10-29 10:27:19 +00:00
|
|
|
}
|
2019-07-06 18:02:28 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
initPerformanceCounters();
|
2019-01-18 13:10:43 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
thread_state = ThreadState::AttachedToQuery;
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:01:45 +00:00
|
|
|
void ThreadStatus::initializeQuery()
|
|
|
|
{
|
|
|
|
setupState(std::make_shared<ThreadGroupStatus>());
|
|
|
|
|
|
|
|
/// No need to lock on mutex here
|
|
|
|
thread_group->memory_tracker.setDescription("(for query)");
|
2020-02-02 02:27:15 +00:00
|
|
|
thread_group->master_thread_id = thread_id;
|
2020-01-28 12:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatus::attachQuery(const ThreadGroupStatusPtr & thread_group_, bool check_detached)
|
|
|
|
{
|
|
|
|
if (thread_state == ThreadState::AttachedToQuery)
|
|
|
|
{
|
|
|
|
if (check_detached)
|
|
|
|
throw Exception("Can't attach query to the thread, it is already attached", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!thread_group_)
|
|
|
|
throw Exception("Attempt to attach to nullptr thread group", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
setupState(thread_group_);
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
void ThreadStatus::finalizePerformanceCounters()
|
|
|
|
{
|
|
|
|
if (performance_counters_finalized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
performance_counters_finalized = true;
|
|
|
|
updatePerformanceCounters();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-09-28 03:40:09 +00:00
|
|
|
if (global_context && query_context)
|
|
|
|
{
|
|
|
|
auto & settings = query_context->getSettingsRef();
|
|
|
|
if (settings.log_queries && settings.log_query_threads)
|
|
|
|
if (auto thread_log = global_context->getQueryThreadLog())
|
|
|
|
logToQueryThreadLog(*thread_log);
|
|
|
|
}
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:40:47 +00:00
|
|
|
void ThreadStatus::initQueryProfiler()
|
|
|
|
{
|
2019-07-06 20:29:00 +00:00
|
|
|
/// query profilers are useless without trace collector
|
2020-02-03 17:06:59 +00:00
|
|
|
if (!global_context || !ext::Singleton<TraceCollector>::isInitialized())
|
2019-07-06 20:29:00 +00:00
|
|
|
return;
|
2019-01-18 13:10:43 +00:00
|
|
|
|
2019-07-10 20:47:39 +00:00
|
|
|
const auto & settings = query_context->getSettingsRef();
|
2019-01-18 13:10:43 +00:00
|
|
|
|
2020-01-26 11:19:04 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (settings.query_profiler_real_time_period_ns > 0)
|
2020-02-02 02:27:15 +00:00
|
|
|
query_profiler_real = std::make_unique<QueryProfilerReal>(thread_id,
|
2020-01-26 11:19:04 +00:00
|
|
|
/* period */ static_cast<UInt32>(settings.query_profiler_real_time_period_ns));
|
|
|
|
|
|
|
|
if (settings.query_profiler_cpu_time_period_ns > 0)
|
2020-02-02 02:27:15 +00:00
|
|
|
query_profiler_cpu = std::make_unique<QueryProfilerCpu>(thread_id,
|
2020-01-26 11:19:04 +00:00
|
|
|
/* period */ static_cast<UInt32>(settings.query_profiler_cpu_time_period_ns));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// QueryProfiler is optional.
|
|
|
|
tryLogCurrentException("ThreadStatus", "Cannot initialize QueryProfiler");
|
|
|
|
}
|
2018-12-25 00:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 22:40:47 +00:00
|
|
|
void ThreadStatus::finalizeQueryProfiler()
|
|
|
|
{
|
2019-07-06 20:29:00 +00:00
|
|
|
query_profiler_real.reset();
|
|
|
|
query_profiler_cpu.reset();
|
2018-12-25 00:19:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
void ThreadStatus::detachQuery(bool exit_if_already_detached, bool thread_exits)
|
|
|
|
{
|
|
|
|
if (exit_if_already_detached && thread_state == ThreadState::DetachedFromQuery)
|
|
|
|
{
|
|
|
|
thread_state = thread_exits ? ThreadState::Died : ThreadState::DetachedFromQuery;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertState({ThreadState::AttachedToQuery}, __PRETTY_FUNCTION__);
|
2018-12-25 00:19:17 +00:00
|
|
|
|
|
|
|
finalizeQueryProfiler();
|
2018-06-20 17:49:52 +00:00
|
|
|
finalizePerformanceCounters();
|
|
|
|
|
|
|
|
/// Detach from thread group
|
|
|
|
performance_counters.setParent(&ProfileEvents::global_counters);
|
2018-09-26 20:45:40 +00:00
|
|
|
memory_tracker.reset();
|
2018-10-05 19:29:49 +00:00
|
|
|
|
|
|
|
/// Must reset pointer to thread_group's memory_tracker, because it will be destroyed two lines below.
|
|
|
|
memory_tracker.setParent(nullptr);
|
|
|
|
|
2019-12-22 20:50:33 +00:00
|
|
|
query_id.clear();
|
2018-06-20 17:49:52 +00:00
|
|
|
query_context = nullptr;
|
|
|
|
thread_group.reset();
|
|
|
|
|
|
|
|
thread_state = thread_exits ? ThreadState::Died : ThreadState::DetachedFromQuery;
|
2019-07-06 18:02:28 +00:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
if (os_thread_priority)
|
|
|
|
{
|
|
|
|
LOG_TRACE(log, "Resetting nice");
|
|
|
|
|
2020-02-02 02:27:15 +00:00
|
|
|
if (0 != setpriority(PRIO_PROCESS, thread_id, 0))
|
2019-07-06 18:02:28 +00:00
|
|
|
LOG_ERROR(log, "Cannot 'setpriority' back to zero: " << errnoToString(ErrorCodes::CANNOT_SET_THREAD_PRIORITY, errno));
|
|
|
|
|
|
|
|
os_thread_priority = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadStatus::logToQueryThreadLog(QueryThreadLog & thread_log)
|
|
|
|
{
|
|
|
|
QueryThreadLogElement elem;
|
|
|
|
|
|
|
|
elem.event_time = time(nullptr);
|
|
|
|
elem.query_start_time = query_start_time;
|
|
|
|
elem.query_duration_ms = (getCurrentTimeNanoseconds() - query_start_time_nanoseconds) / 1000000U;
|
|
|
|
|
2019-05-20 11:37:41 +00:00
|
|
|
elem.read_rows = progress_in.read_rows.load(std::memory_order_relaxed);
|
|
|
|
elem.read_bytes = progress_in.read_bytes.load(std::memory_order_relaxed);
|
2019-05-21 04:06:36 +00:00
|
|
|
|
2019-05-20 11:37:41 +00:00
|
|
|
/// TODO: Use written_rows and written_bytes when run time progress is implemented
|
|
|
|
elem.written_rows = progress_out.read_rows.load(std::memory_order_relaxed);
|
|
|
|
elem.written_bytes = progress_out.read_bytes.load(std::memory_order_relaxed);
|
2018-06-20 17:49:52 +00:00
|
|
|
elem.memory_usage = memory_tracker.get();
|
|
|
|
elem.peak_memory_usage = memory_tracker.getPeak();
|
|
|
|
|
|
|
|
elem.thread_name = getThreadName();
|
2020-02-02 02:27:15 +00:00
|
|
|
elem.thread_id = thread_id;
|
2018-06-20 17:49:52 +00:00
|
|
|
|
|
|
|
if (thread_group)
|
|
|
|
{
|
|
|
|
{
|
2019-02-08 13:23:10 +00:00
|
|
|
std::lock_guard lock(thread_group->mutex);
|
2018-06-20 17:49:52 +00:00
|
|
|
|
2020-02-02 02:27:15 +00:00
|
|
|
elem.master_thread_id = thread_group->master_thread_id;
|
2018-06-20 17:49:52 +00:00
|
|
|
elem.query = thread_group->query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query_context)
|
|
|
|
{
|
|
|
|
elem.client_info = query_context->getClientInfo();
|
|
|
|
|
2019-08-09 13:02:01 +00:00
|
|
|
if (query_context->getSettingsRef().log_profile_events != 0)
|
2018-06-20 17:49:52 +00:00
|
|
|
{
|
|
|
|
/// NOTE: Here we are in the same thread, so we can make memcpy()
|
|
|
|
elem.profile_counters = std::make_shared<ProfileEvents::Counters>(performance_counters.getPartiallyAtomicSnapshot());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_log.add(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::initializeQuery()
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->initializeQuery();
|
|
|
|
current_thread->deleter = CurrentThread::defaultThreadDeleter;
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::attachTo(const ThreadGroupStatusPtr & thread_group)
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->attachQuery(thread_group, true);
|
|
|
|
current_thread->deleter = CurrentThread::defaultThreadDeleter;
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::attachToIfDetached(const ThreadGroupStatusPtr & thread_group)
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->attachQuery(thread_group, false);
|
|
|
|
current_thread->deleter = CurrentThread::defaultThreadDeleter;
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::attachQueryContext(Context & query_context)
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-15 16:32:52 +00:00
|
|
|
current_thread->attachQueryContext(query_context);
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::finalizePerformanceCounters()
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->finalizePerformanceCounters();
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::detachQuery()
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->detachQuery(false);
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::detachQueryIfNotDetached()
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return;
|
2019-07-05 14:15:05 +00:00
|
|
|
current_thread->detachQuery(true);
|
2018-06-20 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CurrentThread::QueryScope::QueryScope(Context & query_context)
|
|
|
|
{
|
|
|
|
CurrentThread::initializeQuery();
|
|
|
|
CurrentThread::attachQueryContext(query_context);
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:19:49 +00:00
|
|
|
void CurrentThread::QueryScope::logPeakMemoryUsage()
|
|
|
|
{
|
2019-03-14 18:03:35 +00:00
|
|
|
auto group = CurrentThread::getGroup();
|
|
|
|
if (!group)
|
|
|
|
return;
|
|
|
|
|
2018-09-26 21:19:49 +00:00
|
|
|
log_peak_memory_usage_in_destructor = false;
|
2019-03-14 18:03:35 +00:00
|
|
|
group->memory_tracker.logPeakMemoryUsage();
|
2018-09-26 21:19:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
CurrentThread::QueryScope::~QueryScope()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-09-26 21:19:49 +00:00
|
|
|
if (log_peak_memory_usage_in_destructor)
|
|
|
|
logPeakMemoryUsage();
|
2018-09-26 21:10:43 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
CurrentThread::detachQueryIfNotDetached();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException("CurrentThread", __PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|