Coverage Report

Created: 2026-03-16 21:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/workload_management/resource_context.h
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
#pragma once
19
20
#include <gen_cpp/data.pb.h>
21
22
#include <memory>
23
24
#include "common/factory_creator.h"
25
#include "common/multi_version.h"
26
#include "runtime/runtime_profile.h"
27
#include "runtime/workload_group/workload_group.h"
28
#include "runtime/workload_management/cpu_context.h"
29
#include "runtime/workload_management/io_context.h"
30
#include "runtime/workload_management/memory_context.h"
31
#include "runtime/workload_management/task_controller.h"
32
33
namespace doris {
34
#include "common/compile_check_begin.h"
35
36
// Every task should have its own resource context. And BE may adjust the resource
37
// context during running.
38
// ResourceContext contains many contexts or controller, the task could implements their
39
// own implementation.
40
class ResourceContext : public std::enable_shared_from_this<ResourceContext> {
41
    ENABLE_FACTORY_CREATOR(ResourceContext);
42
43
public:
44
122k
    ResourceContext() {
45
        // These all default values, it may be reset.
46
122k
        cpu_context_ = CPUContext::create_unique();
47
122k
        memory_context_ = MemoryContext::create_unique();
48
122k
        io_context_ = IOContext::create_unique();
49
122k
        task_controller_ = TaskController::create_unique();
50
51
122k
        cpu_context_->set_resource_ctx(this);
52
122k
        memory_context_->set_resource_ctx(this);
53
122k
        io_context_->set_resource_ctx(this);
54
122k
        task_controller_->set_resource_ctx(this);
55
122k
    }
56
122k
    ~ResourceContext() = default;
57
58
    // Only return the raw pointer to the caller, so that the caller should not save it to other variables.
59
36
    CPUContext* cpu_context() const { return cpu_context_.get(); }
60
1.82M
    MemoryContext* memory_context() const { return memory_context_.get(); }
61
515
    IOContext* io_context() const { return io_context_.get(); }
62
1.44M
    TaskController* task_controller() const { return task_controller_.get(); }
63
123k
    WorkloadGroupPtr workload_group() const { return _workload_group; }
64
65
0
    void set_cpu_context(std::unique_ptr<CPUContext> cpu_context) {
66
0
        cpu_context_ = std::move(cpu_context);
67
0
        cpu_context_->set_resource_ctx(this);
68
0
    }
69
0
    void set_memory_context(std::unique_ptr<MemoryContext> memory_context) {
70
0
        memory_context_ = std::move(memory_context);
71
0
        memory_context_->set_resource_ctx(this);
72
0
    }
73
0
    void set_io_context(std::unique_ptr<IOContext> io_context) {
74
0
        io_context_ = std::move(io_context);
75
0
        io_context_->set_resource_ctx(this);
76
0
    }
77
121k
    void set_task_controller(std::unique_ptr<TaskController> task_controller) {
78
121k
        task_controller_ = std::move(task_controller);
79
121k
        task_controller_->set_resource_ctx(this);
80
121k
    }
81
18
    void set_workload_group(WorkloadGroupPtr wg) { _workload_group = wg; }
82
83
    void to_thrift_query_statistics(TQueryStatistics* statistics) const;
84
85
0
    std::string debug_string() { return resource_profile_.get()->pretty_print(); }
86
0
    void refresh_resource_profile() {
87
0
        std::unique_ptr<RuntimeProfile> resource_profile =
88
0
                std::make_unique<RuntimeProfile>("ResourceContext");
89
0
90
0
        RuntimeProfile* cpu_profile =
91
0
                resource_profile->create_child(cpu_context_->stats_profile()->name(), true, false);
92
0
        cpu_profile->merge(cpu_context_->stats_profile());
93
0
        RuntimeProfile* memory_profile = resource_profile->create_child(
94
0
                memory_context_->stats_profile()->name(), true, false);
95
0
        memory_profile->merge(memory_context_->stats_profile());
96
0
        RuntimeProfile* io_profile =
97
0
                resource_profile->create_child(io_context_->stats_profile()->name(), true, false);
98
0
        io_profile->merge(io_context_->stats_profile());
99
0
100
0
        resource_profile_.set(std::move(resource_profile));
101
0
    }
102
103
private:
104
    // The controller's init value is nullptr, it means the resource context will ignore this controller.
105
    std::unique_ptr<CPUContext> cpu_context_ = nullptr;
106
    std::unique_ptr<MemoryContext> memory_context_ = nullptr;
107
    std::unique_ptr<IOContext> io_context_ = nullptr;
108
    std::unique_ptr<TaskController> task_controller_ = nullptr;
109
110
    WorkloadGroupPtr _workload_group = nullptr;
111
    MultiVersion<RuntimeProfile> resource_profile_;
112
};
113
114
#include "common/compile_check_end.h"
115
} // namespace doris