Coverage Report

Created: 2026-03-24 20:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/thread_context.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "runtime/thread_context.h"
19
20
#include "common/signal_handler.h"
21
#include "runtime/query_context.h"
22
#include "runtime/runtime_state.h"
23
24
namespace doris {
25
class MemTracker;
26
27
888
void AttachTask::init(const std::shared_ptr<ResourceContext>& rc) {
28
888
    ThreadLocalHandle::create_thread_local_if_not_exits();
29
888
    signal::set_signal_task_id(rc->task_controller()->task_id());
30
888
    thread_context()->attach_task(rc);
31
888
}
32
33
396
AttachTask::AttachTask(const std::shared_ptr<ResourceContext>& rc) {
34
396
    init(rc);
35
396
}
36
37
348
AttachTask::AttachTask(const std::shared_ptr<MemTrackerLimiter>& mem_tracker) {
38
    // if parameter is `orphan_mem_tracker`, if you do not switch thraed mem tracker afterwards,
39
    // alloc or free memory from Allocator will fail DCHECK. unless you know for sure that
40
    // the thread will not alloc or free memory from Allocator later.
41
348
    std::shared_ptr<ResourceContext> rc = ResourceContext::create_shared();
42
348
    rc->memory_context()->set_mem_tracker(mem_tracker);
43
348
    init(rc);
44
348
}
45
46
144
AttachTask::AttachTask(RuntimeState* runtime_state) {
47
144
    signal::set_signal_is_nereids(runtime_state->is_nereids());
48
144
    init(runtime_state->get_query_ctx()->resource_ctx());
49
144
}
50
51
0
AttachTask::AttachTask(QueryContext* query_ctx) {
52
0
    init(query_ctx->resource_ctx());
53
0
}
54
55
888
AttachTask::~AttachTask() {
56
888
    signal::set_signal_task_id(TUniqueId());
57
888
    thread_context()->detach_task();
58
888
    ThreadLocalHandle::del_thread_local_if_count_is_zero();
59
888
}
60
61
10
SwitchResourceContext::SwitchResourceContext(const std::shared_ptr<ResourceContext>& rc) {
62
10
    DCHECK(rc != nullptr);
63
10
    doris::ThreadLocalHandle::create_thread_local_if_not_exits();
64
10
    DCHECK(thread_context()->is_attach_task());
65
10
    old_resource_ctx_ = thread_context()->resource_ctx();
66
10
    if (rc != old_resource_ctx_) {
67
8
        signal::set_signal_task_id(rc->task_controller()->task_id());
68
8
        thread_context()->resource_ctx_ = rc;
69
8
        thread_context()->thread_mem_tracker_mgr->attach_limiter_tracker(
70
8
                rc->memory_context()->mem_tracker(), rc->workload_group());
71
8
    }
72
10
}
73
74
10
SwitchResourceContext::~SwitchResourceContext() {
75
10
    if (old_resource_ctx_ != thread_context()->resource_ctx()) {
76
8
        DCHECK(old_resource_ctx_ != nullptr);
77
8
        signal::set_signal_task_id(old_resource_ctx_->task_controller()->task_id());
78
8
        thread_context()->resource_ctx_ = old_resource_ctx_;
79
8
        thread_context()->thread_mem_tracker_mgr->detach_limiter_tracker();
80
8
    }
81
10
    doris::ThreadLocalHandle::del_thread_local_if_count_is_zero();
82
10
}
83
84
SwitchThreadMemTrackerLimiter::SwitchThreadMemTrackerLimiter(
85
2.07M
        const std::shared_ptr<doris::MemTrackerLimiter>& mem_tracker) {
86
2.07M
    DCHECK(mem_tracker);
87
2.07M
    doris::ThreadLocalHandle::create_thread_local_if_not_exits();
88
2.07M
    if (mem_tracker != thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker_sptr()) {
89
1.89M
        thread_context()->thread_mem_tracker_mgr->attach_limiter_tracker(mem_tracker);
90
1.89M
        is_switched_ = true;
91
1.89M
    }
92
2.07M
}
93
94
2.07M
SwitchThreadMemTrackerLimiter::~SwitchThreadMemTrackerLimiter() {
95
2.07M
    if (is_switched_) {
96
1.89M
        thread_context()->thread_mem_tracker_mgr->detach_limiter_tracker();
97
1.89M
    }
98
2.07M
    doris::ThreadLocalHandle::del_thread_local_if_count_is_zero();
99
2.07M
}
100
101
180
AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer(MemTracker* mem_tracker) {
102
180
    ThreadLocalHandle::create_thread_local_if_not_exits();
103
180
    if (mem_tracker) {
104
180
        _need_pop = thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(mem_tracker);
105
180
    }
106
180
}
107
108
AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer(
109
        const std::shared_ptr<MemTracker>& mem_tracker)
110
130
        : _mem_tracker(mem_tracker) {
111
130
    ThreadLocalHandle::create_thread_local_if_not_exits();
112
130
    if (_mem_tracker) {
113
130
        _need_pop =
114
130
                thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(_mem_tracker.get());
115
130
    }
116
130
}
117
118
310
AddThreadMemTrackerConsumer::~AddThreadMemTrackerConsumer() {
119
310
    if (_need_pop) {
120
308
        thread_context()->thread_mem_tracker_mgr->pop_consumer_tracker();
121
308
    }
122
310
    ThreadLocalHandle::del_thread_local_if_count_is_zero();
123
310
}
124
125
} // namespace doris