1 | #include <memory> |
---|---|
2 | |
3 | #include "CurrentThread.h" |
4 | #include <common/logger_useful.h> |
5 | #include <common/likely.h> |
6 | #include <Common/ThreadStatus.h> |
7 | #include <Common/TaskStatsInfoGetter.h> |
8 | #include <Interpreters/ProcessList.h> |
9 | #include <Interpreters/Context.h> |
10 | #include <common/getThreadNumber.h> |
11 | #include <Poco/Logger.h> |
12 | |
13 | |
14 | namespace DB |
15 | { |
16 | |
17 | namespace ErrorCodes |
18 | { |
19 | extern const int LOGICAL_ERROR; |
20 | } |
21 | |
22 | void CurrentThread::updatePerformanceCounters() |
23 | { |
24 | if (unlikely(!current_thread)) |
25 | return; |
26 | current_thread->updatePerformanceCounters(); |
27 | } |
28 | |
29 | bool CurrentThread::isInitialized() |
30 | { |
31 | return current_thread; |
32 | } |
33 | |
34 | ThreadStatus & CurrentThread::get() |
35 | { |
36 | if (unlikely(!current_thread)) |
37 | throw Exception("Thread #"+ std::to_string(getThreadNumber()) + " status was not initialized", ErrorCodes::LOGICAL_ERROR); |
38 | |
39 | return *current_thread; |
40 | } |
41 | |
42 | ProfileEvents::Counters & CurrentThread::getProfileEvents() |
43 | { |
44 | return current_thread ? current_thread->performance_counters : ProfileEvents::global_counters; |
45 | } |
46 | |
47 | MemoryTracker * CurrentThread::getMemoryTracker() |
48 | { |
49 | if (unlikely(!current_thread)) |
50 | return nullptr; |
51 | return ¤t_thread->memory_tracker; |
52 | } |
53 | |
54 | void CurrentThread::updateProgressIn(const Progress & value) |
55 | { |
56 | if (unlikely(!current_thread)) |
57 | return; |
58 | current_thread->progress_in.incrementPiecewiseAtomically(value); |
59 | } |
60 | |
61 | void CurrentThread::updateProgressOut(const Progress & value) |
62 | { |
63 | if (unlikely(!current_thread)) |
64 | return; |
65 | current_thread->progress_out.incrementPiecewiseAtomically(value); |
66 | } |
67 | |
68 | void CurrentThread::attachInternalTextLogsQueue(const std::shared_ptr<InternalTextLogsQueue> & logs_queue, |
69 | LogsLevel client_logs_level) |
70 | { |
71 | if (unlikely(!current_thread)) |
72 | return; |
73 | current_thread->attachInternalTextLogsQueue(logs_queue, client_logs_level); |
74 | } |
75 | |
76 | std::shared_ptr<InternalTextLogsQueue> CurrentThread::getInternalTextLogsQueue() |
77 | { |
78 | /// NOTE: this method could be called at early server startup stage |
79 | if (unlikely(!current_thread)) |
80 | return nullptr; |
81 | |
82 | if (current_thread->getCurrentState() == ThreadStatus::ThreadState::Died) |
83 | return nullptr; |
84 | |
85 | return current_thread->getInternalTextLogsQueue(); |
86 | } |
87 | |
88 | ThreadGroupStatusPtr CurrentThread::getGroup() |
89 | { |
90 | if (unlikely(!current_thread)) |
91 | return nullptr; |
92 | |
93 | return current_thread->getThreadGroup(); |
94 | } |
95 | |
96 | } |
97 |