@@ -69,6 +69,7 @@ class ThreadGroup
6969 ThreadGroup ();
7070 using FatalErrorCallback = std::function<void ()>;
7171 explicit ThreadGroup (ContextPtr query_context_, FatalErrorCallback fatal_error_callback_ = {});
72+ explicit ThreadGroup (ThreadGroupPtr parent);
7273
7374 // / The first thread created this thread group
7475 const UInt64 master_thread_id;
@@ -79,7 +80,7 @@ class ThreadGroup
7980
8081 const FatalErrorCallback fatal_error_callback;
8182
82- MemorySpillScheduler memory_spill_scheduler;
83+ MemorySpillScheduler::Ptr memory_spill_scheduler;
8384 ProfileEvents::Counters performance_counters{VariableContext::Process};
8485 MemoryTracker memory_tracker{VariableContext::Process};
8586
@@ -118,11 +119,14 @@ class ThreadGroup
118119
119120 static ThreadGroupPtr createForBackgroundProcess (ContextPtr storage_context);
120121
122+ static ThreadGroupPtr createForMaterializedView ();
123+
121124 std::vector<UInt64> getInvolvedThreadIds () const ;
122125 size_t getPeakThreadsUsage () const ;
126+ UInt64 getThreadsTotalElapsedMs () const ;
123127
124128 void linkThread (UInt64 thread_id);
125- void unlinkThread ();
129+ void unlinkThread (UInt64 elapsed_thread_counter_ms );
126130
127131private:
128132 mutable std::mutex mutex;
@@ -138,6 +142,8 @@ class ThreadGroup
138142
139143 // / Peak threads count in the group
140144 size_t peak_threads_usage TSA_GUARDED_BY (mutex) = 0;
145+
146+ UInt64 elapsed_total_threads_counter_ms TSA_GUARDED_BY (mutex) = 0;
141147};
142148
143149/* *
@@ -240,10 +246,13 @@ class ThreadStatus : public boost::noncopyable
240246 UInt64 microseconds () const ;
241247 UInt64 seconds () const ;
242248
249+ UInt64 elapsedMilliseconds () const ;
250+ UInt64 elapsedMilliseconds (const TimePoint & current) const ;
251+
243252 std::chrono::time_point<std::chrono::system_clock> point;
244253 };
245254
246- TimePoint query_start_time {};
255+ TimePoint thread_attach_time {};
247256
248257 // CPU and Real time query profilers
249258 std::unique_ptr<QueryProfilerReal> query_profiler_real;
@@ -255,9 +264,6 @@ class ThreadStatus : public boost::noncopyable
255264 Stopwatch stopwatch{CLOCK_MONOTONIC_COARSE};
256265 UInt64 last_performance_counters_update_time = 0 ;
257266
258- // / See setInternalThread()
259- bool internal_thread = false ;
260-
261267 // / This is helpful for cut linking dependencies for clickhouse_common_io
262268 using Deleter = std::function<void ()>;
263269 Deleter deleter;
@@ -277,22 +283,6 @@ class ThreadStatus : public boost::noncopyable
277283 ContextPtr getQueryContext () const ;
278284 ContextPtr getGlobalContext () const ;
279285
280- // / "Internal" ThreadStatus is used for materialized views for separate
281- // / tracking into system.query_views_log
282- // /
283- // / You can have multiple internal threads, but only one non-internal with
284- // / the same thread_id.
285- // /
286- // / "Internal" thread:
287- // / - cannot have query profiler
288- // / since the running (main query) thread should already have one
289- // / - should not try to obtain latest counter on detach
290- // / because detaching of such threads will be done from a different
291- // / thread_id, and some counters are not available (i.e. getrusage()),
292- // / but anyway they are accounted correctly in the main ThreadStatus of a
293- // / query.
294- void setInternalThread ();
295-
296286 // / Attaches slave thread to existing thread group
297287 void attachToGroup (const ThreadGroupPtr & thread_group_, bool check_detached = true );
298288
0 commit comments