be/src/agent/cgroup_cpu_ctl.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 | | #include <fcntl.h> |
19 | | #include <sys/stat.h> |
20 | | #include <sys/types.h> |
21 | | #include <unistd.h> |
22 | | |
23 | | #include <shared_mutex> |
24 | | |
25 | | #include "common/config.h" |
26 | | #include "common/status.h" |
27 | | #include "util/cpu_info.h" |
28 | | |
29 | | namespace doris { |
30 | | |
31 | | class CgroupCpuCtl { |
32 | | public: |
33 | 14 | virtual ~CgroupCpuCtl() = default; |
34 | 14 | CgroupCpuCtl(uint64_t wg_id) { _wg_id = wg_id; } |
35 | | |
36 | | virtual Status init() = 0; |
37 | | |
38 | | virtual Status add_thread_to_cgroup() = 0; |
39 | | |
40 | | void update_cpu_hard_limit(int cpu_hard_limit); |
41 | | |
42 | | void update_cpu_soft_limit(int cpu_shares); |
43 | | |
44 | | // for log |
45 | | void get_cgroup_cpu_info(uint64_t* cpu_shares, int* cpu_hard_limit); |
46 | | |
47 | | static void init_doris_cgroup_path(); |
48 | | |
49 | | static Status delete_unused_cgroup_path(std::set<uint64_t>& used_wg_ids); |
50 | | |
51 | | static std::shared_ptr<CgroupCpuCtl> create_cgroup_cpu_ctl(uint64_t wg_id); |
52 | | |
53 | | static bool is_a_valid_cgroup_path(std::string cg_path); |
54 | | |
55 | | static uint64_t cpu_soft_limit_default_value(); |
56 | | |
57 | | protected: |
58 | | virtual Status modify_cg_cpu_hard_limit_no_lock(int cpu_hard_limit) = 0; |
59 | | |
60 | | virtual Status modify_cg_cpu_soft_limit_no_lock(int cpu_shares) = 0; |
61 | | |
62 | | Status add_thread_to_cgroup(std::string task_file); |
63 | | |
64 | | static Status write_cg_sys_file(std::string file_path, std::string value, std::string msg, |
65 | | bool is_append); |
66 | | |
67 | | static Status init_cgroup_v2_query_path_public_file(std::string home_path, |
68 | | std::string query_path); |
69 | | |
70 | | protected: |
71 | | inline static uint64_t _cpu_core_num; |
72 | | // TODO (yiguolei): Should echo this period to cpu.cfs_period_us??? |
73 | | inline const static uint64_t _cpu_cfs_period_us = 100000; |
74 | | inline static std::string _doris_cgroup_cpu_path = ""; |
75 | | inline static std::string _doris_cgroup_cpu_query_path = ""; |
76 | | inline static bool _is_enable_cgroup_v1_in_env = false; |
77 | | inline static bool _is_enable_cgroup_v2_in_env = false; |
78 | | inline static bool _is_cgroup_query_path_valid = false; |
79 | | |
80 | | // cgroup v2 public file |
81 | | inline static std::string _doris_cgroup_cpu_path_subtree_ctl_file = ""; |
82 | | inline static std::string _cgroup_v2_query_path_subtree_ctl_file = ""; |
83 | | inline static std::string _doris_cg_v2_procs_file = ""; |
84 | | |
85 | | protected: |
86 | | int _cpu_hard_limit = 0; |
87 | | std::shared_mutex _lock_mutex; |
88 | | bool _init_succ = false; |
89 | | uint64_t _wg_id = -1; // workload group id |
90 | | uint64_t _cpu_shares = 0; |
91 | | }; |
92 | | |
93 | | /* |
94 | | NOTE: directory structure |
95 | | 1 sys cgroup root path: |
96 | | /sys/fs/cgroup |
97 | | |
98 | | 2 sys cgroup cpu controller path: |
99 | | /sys/fs/cgroup/cpu |
100 | | |
101 | | 3 doris home path: |
102 | | /sys/fs/cgroup/cpu/{doris_home}/ |
103 | | |
104 | | 4 doris query path |
105 | | /sys/fs/cgroup/cpu/{doris_home}/query |
106 | | |
107 | | 5 workload group path |
108 | | /sys/fs/cgroup/cpu/{doris_home}/query/{workload group id} |
109 | | |
110 | | 6 workload group quota file: |
111 | | /sys/fs/cgroup/cpu/{doris_home}/query/{workload group id}/cpu.cfs_quota_us |
112 | | |
113 | | 7 workload group tasks file: |
114 | | /sys/fs/cgroup/cpu/{doris_home}/query/{workload group id}/tasks |
115 | | |
116 | | 8 workload group cpu.shares file: |
117 | | /sys/fs/cgroup/cpu/{doris_home}/query/{workload group id}/cpu.shares |
118 | | */ |
119 | | class CgroupV1CpuCtl : public CgroupCpuCtl { |
120 | | public: |
121 | 0 | CgroupV1CpuCtl(uint64_t tg_id) : CgroupCpuCtl(tg_id) {} |
122 | | Status init() override; |
123 | | Status modify_cg_cpu_hard_limit_no_lock(int cpu_hard_limit) override; |
124 | | Status modify_cg_cpu_soft_limit_no_lock(int cpu_shares) override; |
125 | | Status add_thread_to_cgroup() override; |
126 | | |
127 | | private: |
128 | | std::string _cgroup_v1_cpu_tg_path; // workload group path |
129 | | std::string _cgroup_v1_cpu_tg_quota_file; |
130 | | std::string _cgroup_v1_cpu_tg_shares_file; |
131 | | std::string _cgroup_v1_cpu_tg_task_file; |
132 | | }; |
133 | | |
134 | | /* |
135 | | NOTE: cgroup v2 directory structure |
136 | | 1 root path: |
137 | | /sys/fs/cgroup |
138 | | |
139 | | 2 doris home path: |
140 | | /sys/fs/cgroup/{doris_home}/ |
141 | | |
142 | | 3 doris home subtree_control file: |
143 | | /sys/fs/cgroup/{doris_home}/cgroup.subtree_control |
144 | | |
145 | | 4 query path: |
146 | | /sys/fs/cgroup/{doris_home}/query/ |
147 | | |
148 | | 5 query path subtree_control file: |
149 | | /sys/fs/cgroup/{doris_home}/query/cgroup.subtree_control |
150 | | |
151 | | 6 query path procs file: |
152 | | /sys/fs/cgroup/{doris_home}/query/cgroup.procs |
153 | | |
154 | | 7 workload group path: |
155 | | /sys/fs/cgroup/{doris_home}/query/{workload_group_id} |
156 | | |
157 | | 8 workload grou cpu.max file: |
158 | | /sys/fs/cgroup/{doris_home}/query/{workload_group_id}/cpu.max |
159 | | |
160 | | 9 workload grou cpu.weight file: |
161 | | /sys/fs/cgroup/{doris_home}/query/{workload_group_id}/cpu.weight |
162 | | |
163 | | 10 workload group cgroup type file: |
164 | | /sys/fs/cgroup/{doris_home}/query/{workload_group_id}/cgroup.type |
165 | | |
166 | | */ |
167 | | class CgroupV2CpuCtl : public CgroupCpuCtl { |
168 | | public: |
169 | 14 | CgroupV2CpuCtl(uint64_t tg_id) : CgroupCpuCtl(tg_id) {} |
170 | | Status init() override; |
171 | | Status modify_cg_cpu_hard_limit_no_lock(int cpu_hard_limit) override; |
172 | | Status modify_cg_cpu_soft_limit_no_lock(int cpu_shares) override; |
173 | | Status add_thread_to_cgroup() override; |
174 | | |
175 | | private: |
176 | | std::string _cgroup_v2_query_wg_path; |
177 | | std::string _cgroup_v2_query_wg_cpu_max_file; |
178 | | std::string _cgroup_v2_query_wg_cpu_weight_file; |
179 | | std::string _cgroup_v2_query_wg_thread_file; |
180 | | std::string _cgroup_v2_query_wg_type_file; |
181 | | }; |
182 | | |
183 | | } // namespace doris |