Initialize query profiler for all threads in a group

This commit is contained in:
Ivan Lezhankin 2020-01-24 17:21:50 +03:00
parent e96021cefe
commit 9d30f3c876
2 changed files with 35 additions and 36 deletions

View File

@ -203,6 +203,9 @@ protected:
/// Set to non-nullptr only if we have enough capabilities. /// Set to non-nullptr only if we have enough capabilities.
std::unique_ptr<TaskStatsInfoGetter> taskstats_getter; std::unique_ptr<TaskStatsInfoGetter> taskstats_getter;
private:
void setupState(const ThreadGroupStatusPtr & thread_group_);
}; };
} }

View File

@ -50,40 +50,11 @@ void CurrentThread::defaultThreadDeleter()
current_thread->detachQuery(true, true); current_thread->detachQuery(true, true);
} }
void ThreadStatus::initializeQuery() void ThreadStatus::setupState(const ThreadGroupStatusPtr & thread_group_)
{ {
assertState({ThreadState::DetachedFromQuery}, __PRETTY_FUNCTION__); assertState({ThreadState::DetachedFromQuery}, __PRETTY_FUNCTION__);
thread_group = std::make_shared<ThreadGroupStatus>(); /// Attach or init current thread to thread group and copy useful information from it
performance_counters.setParent(&thread_group->performance_counters);
memory_tracker.setParent(&thread_group->memory_tracker);
thread_group->memory_tracker.setDescription("(for query)");
thread_group->thread_numbers.emplace_back(thread_number);
thread_group->os_thread_ids.emplace_back(os_thread_id);
thread_group->master_thread_number = thread_number;
thread_group->master_thread_os_id = os_thread_id;
initPerformanceCounters();
thread_state = ThreadState::AttachedToQuery;
}
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;
}
assertState({ThreadState::DetachedFromQuery}, __PRETTY_FUNCTION__);
if (!thread_group_)
throw Exception("Attempt to attach to nullptr thread group", ErrorCodes::LOGICAL_ERROR);
/// Attach current thread to thread group and copy useful information from it
thread_group = thread_group_; thread_group = thread_group_;
performance_counters.setParent(&thread_group->performance_counters); performance_counters.setParent(&thread_group->performance_counters);
@ -92,22 +63,22 @@ void ThreadStatus::attachQuery(const ThreadGroupStatusPtr & thread_group_, bool
{ {
std::lock_guard lock(thread_group->mutex); std::lock_guard lock(thread_group->mutex);
/// NOTE: thread may be attached multiple times if it is reused from a thread pool.
thread_group->thread_numbers.emplace_back(thread_number);
thread_group->os_thread_ids.emplace_back(os_thread_id);
logs_queue_ptr = thread_group->logs_queue_ptr; logs_queue_ptr = thread_group->logs_queue_ptr;
query_context = thread_group->query_context; query_context = thread_group->query_context;
if (!global_context) if (!global_context)
global_context = thread_group->global_context; global_context = thread_group->global_context;
/// NOTE: A thread may be attached multiple times if it is reused from a thread pool.
thread_group->thread_numbers.emplace_back(thread_number);
thread_group->os_thread_ids.emplace_back(os_thread_id);
} }
if (query_context) if (query_context)
{ {
query_id = query_context->getCurrentQueryId(); query_id = query_context->getCurrentQueryId();
#if defined(__linux__) #if defined(OS_LINUX)
/// Set "nice" value if required. /// Set "nice" value if required.
Int32 new_os_thread_priority = query_context->getSettingsRef().os_thread_priority; Int32 new_os_thread_priority = query_context->getSettingsRef().os_thread_priority;
if (new_os_thread_priority && hasLinuxCapability(CAP_SYS_NICE)) if (new_os_thread_priority && hasLinuxCapability(CAP_SYS_NICE))
@ -128,6 +99,31 @@ void ThreadStatus::attachQuery(const ThreadGroupStatusPtr & thread_group_, bool
thread_state = ThreadState::AttachedToQuery; thread_state = ThreadState::AttachedToQuery;
} }
void ThreadStatus::initializeQuery()
{
setupState(std::make_shared<ThreadGroupStatus>());
/// No need to lock on mutex here
thread_group->memory_tracker.setDescription("(for query)");
thread_group->master_thread_number = thread_number;
thread_group->master_thread_os_id = os_thread_id;
}
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_);
}
void ThreadStatus::finalizePerformanceCounters() void ThreadStatus::finalizePerformanceCounters()
{ {
if (performance_counters_finalized) if (performance_counters_finalized)