blob: 1005b1e3e6d32196c931e3574ebfdfe0bdba74b4 [file] [log] [blame]
Mark Salyzyn12717162014-04-29 15:49:14 -07001/*
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08002 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
San Mehat493dad92009-09-12 10:06:57 -070016
Suren Baghdasaryan1bd127b2019-01-25 05:30:52 +000017#include <processgroup/sched_policy.h>
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080018
Jeff Brownbff8f3f2012-05-08 15:05:42 -070019#define LOG_TAG "SchedPolicy"
20
San Mehat493dad92009-09-12 10:06:57 -070021#include <errno.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070022#include <unistd.h>
23
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080024#include <android-base/logging.h>
25#include <android-base/threads.h>
26#include <cgroup_map.h>
27#include <processgroup/processgroup.h>
28
29using android::base::GetThreadId;
Mark Salyzyn12717162014-04-29 15:49:14 -070030
Jeff Brownbff8f3f2012-05-08 15:05:42 -070031/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
32 * Call this any place a SchedPolicy is used as an input parameter.
33 * Returns the possibly re-mapped policy.
34 */
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080035static inline SchedPolicy _policy(SchedPolicy p) {
36 return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
Jeff Brownbff8f3f2012-05-08 15:05:42 -070037}
San Mehatd2e4e462009-10-29 11:48:00 -070038
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080039#if defined(__ANDROID__)
40
T.J. Mercier1c007992024-01-25 16:29:54 +000041int set_cpuset_policy(pid_t tid, SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070042 if (tid == 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080043 tid = GetThreadId();
Tim Murrayb769c8d2015-06-08 14:56:29 -070044 }
45 policy = _policy(policy);
Tim Murrayb769c8d2015-06-08 14:56:29 -070046
Tim Murrayb769c8d2015-06-08 14:56:29 -070047 switch (policy) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080048 case SP_BACKGROUND:
Rick Yiu0b211fa2019-09-16 19:07:17 +080049 return SetTaskProfiles(tid, {"CPUSET_SP_BACKGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080050 case SP_FOREGROUND:
51 case SP_AUDIO_APP:
52 case SP_AUDIO_SYS:
Rick Yiu0b211fa2019-09-16 19:07:17 +080053 return SetTaskProfiles(tid, {"CPUSET_SP_FOREGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054 case SP_TOP_APP:
Rick Yiu0b211fa2019-09-16 19:07:17 +080055 return SetTaskProfiles(tid, {"CPUSET_SP_TOP_APP"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080056 case SP_SYSTEM:
Rick Yiu0b211fa2019-09-16 19:07:17 +080057 return SetTaskProfiles(tid, {"CPUSET_SP_SYSTEM"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080058 case SP_RESTRICTED:
Rick Yiu0b211fa2019-09-16 19:07:17 +080059 return SetTaskProfiles(tid, {"CPUSET_SP_RESTRICTED"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060 default:
61 break;
Todd Kjosba8a4752015-10-26 16:22:11 -070062 }
63
Tim Murray99910262015-06-22 14:00:56 -070064 return 0;
Tim Murrayb769c8d2015-06-08 14:56:29 -070065}
66
T.J. Mercier1c007992024-01-25 16:29:54 +000067int set_sched_policy(pid_t tid, SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070068 if (tid == 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080069 tid = GetThreadId();
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070070 }
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070071 policy = _policy(policy);
San Mehat493dad92009-09-12 10:06:57 -070072
San Mehatd2e4e462009-10-29 11:48:00 -070073#if POLICY_DEBUG
74 char statfile[64];
75 char statline[1024];
76 char thread_name[255];
San Mehatd2e4e462009-10-29 11:48:00 -070077
Raja Ma2f37e42016-04-19 23:55:14 +053078 snprintf(statfile, sizeof(statfile), "/proc/%d/stat", tid);
San Mehatd2e4e462009-10-29 11:48:00 -070079 memset(thread_name, 0, sizeof(thread_name));
80
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080081 unique_fd fd(TEMP_FAILURE_RETRY(open(statfile, O_RDONLY | O_CLOEXEC)));
San Mehatd2e4e462009-10-29 11:48:00 -070082 if (fd >= 0) {
83 int rc = read(fd, statline, 1023);
San Mehatd2e4e462009-10-29 11:48:00 -070084 statline[rc] = 0;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080085 char* p = statline;
86 char* q;
San Mehatd2e4e462009-10-29 11:48:00 -070087
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080088 for (p = statline; *p != '('; p++)
89 ;
San Mehatd2e4e462009-10-29 11:48:00 -070090 p++;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080091 for (q = p; *q != ')'; q++)
92 ;
San Mehatd2e4e462009-10-29 11:48:00 -070093
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080094 strncpy(thread_name, p, (q - p));
San Mehatd2e4e462009-10-29 11:48:00 -070095 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -070096 switch (policy) {
Tim Murrayb769c8d2015-06-08 14:56:29 -070097 case SP_BACKGROUND:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080098 SLOGD("vvv tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -070099 break;
100 case SP_FOREGROUND:
101 case SP_AUDIO_APP:
102 case SP_AUDIO_SYS:
Tim Murray6647bb52016-01-11 16:16:35 -0800103 case SP_TOP_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800104 SLOGD("^^^ tid %d (%s)", tid, thread_name);
105 break;
106 case SP_SYSTEM:
107 SLOGD("/// tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700108 break;
Joel Fernandes88ef9f02017-03-25 22:46:10 -0700109 case SP_RT_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800110 SLOGD("RT tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700111 break;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800112 default:
113 SLOGD("??? tid %d (%s)", tid, thread_name);
114 break;
115 }
116#endif
Tim Murrayb769c8d2015-06-08 14:56:29 -0700117
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800118 switch (policy) {
119 case SP_BACKGROUND:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800120 return SetTaskProfiles(tid, {"SCHED_SP_BACKGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121 case SP_FOREGROUND:
122 case SP_AUDIO_APP:
123 case SP_AUDIO_SYS:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800124 return SetTaskProfiles(tid, {"SCHED_SP_FOREGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800125 case SP_TOP_APP:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800126 return SetTaskProfiles(tid, {"SCHED_SP_TOP_APP"}, true) ? 0 : -1;
Wei Wangab879792020-11-24 00:26:40 -0800127 case SP_SYSTEM:
128 return SetTaskProfiles(tid, {"SCHED_SP_SYSTEM"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800129 case SP_RT_APP:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800130 return SetTaskProfiles(tid, {"SCHED_SP_RT_APP"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800131 default:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800132 return SetTaskProfiles(tid, {"SCHED_SP_DEFAULT"}, true) ? 0 : -1;
San Mehat493dad92009-09-12 10:06:57 -0700133 }
134
135 return 0;
136}
Raphael0384a982009-09-15 17:10:17 -0700137
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800138bool cpusets_enabled() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700139 static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").IsUsable());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800140 return enabled;
141}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700142
Quentin Perret64811b72020-01-14 12:55:57 +0000143static bool schedtune_enabled() {
144 return (CgroupMap::GetInstance().FindController("schedtune").IsUsable());
145}
146
147static bool cpuctl_enabled() {
148 return (CgroupMap::GetInstance().FindController("cpu").IsUsable());
149}
150
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800151bool schedboost_enabled() {
Quentin Perret64811b72020-01-14 12:55:57 +0000152 static bool enabled = schedtune_enabled() || cpuctl_enabled();
153
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800154 return enabled;
155}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700156
T.J. Mercier1c007992024-01-25 16:29:54 +0000157static int getCGroupSubsys(pid_t tid, const char* subsys, std::string& subgroup) {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700158 auto controller = CgroupMap::GetInstance().FindController(subsys);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800159
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700160 if (!controller.IsUsable()) return -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800161
Suren Baghdasaryane3d38962021-04-26 09:21:52 -0700162 if (!controller.GetTaskGroup(tid, &subgroup))
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800163 return -1;
Suren Baghdasaryane3d38962021-04-26 09:21:52 -0700164
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700165 return 0;
166}
167
Wei Wangcf4b15e2021-12-13 17:42:08 -0800168static int get_sched_policy_from_group(const std::string& group, SchedPolicy* policy) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800169 if (group.empty()) {
170 *policy = SP_FOREGROUND;
171 } else if (group == "foreground") {
172 *policy = SP_FOREGROUND;
173 } else if (group == "system-background") {
174 *policy = SP_SYSTEM;
175 } else if (group == "background") {
176 *policy = SP_BACKGROUND;
177 } else if (group == "top-app") {
178 *policy = SP_TOP_APP;
179 } else if (group == "restricted") {
180 *policy = SP_RESTRICTED;
181 } else {
182 errno = ERANGE;
183 return -1;
184 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700185 return 0;
186}
187
T.J. Mercier1c007992024-01-25 16:29:54 +0000188int get_sched_policy(pid_t tid, SchedPolicy* policy) {
Wei Wangcf4b15e2021-12-13 17:42:08 -0800189 if (tid == 0) {
190 tid = GetThreadId();
191 }
192
193 std::string group;
194 if (schedboost_enabled()) {
195 if ((getCGroupSubsys(tid, "schedtune", group) < 0) &&
196 (getCGroupSubsys(tid, "cpu", group) < 0)) {
197 LOG(ERROR) << "Failed to find cpu cgroup for tid " << tid;
198 return -1;
199 }
200 // Wipe invalid group to fallback to cpuset
201 if (!group.empty()) {
202 if (get_sched_policy_from_group(group, policy) < 0) {
203 group.clear();
204 } else {
205 return 0;
206 }
207 }
208 }
209
210 if (cpusets_enabled() && getCGroupSubsys(tid, "cpuset", group) < 0) {
211 LOG(ERROR) << "Failed to find cpuset cgroup for tid " << tid;
212 return -1;
213 }
214 return get_sched_policy_from_group(group, policy);
215}
216
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800217#else
218
219/* Stubs for non-Android targets. */
220
221int set_sched_policy(int, SchedPolicy) {
222 return 0;
223}
224
225int get_sched_policy(int, SchedPolicy* policy) {
226 *policy = SP_SYSTEM_DEFAULT;
227 return 0;
228}
229
230#endif
231
Elliott Hughes9f495082018-04-25 14:52:50 -0700232const char* get_sched_policy_name(SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700233 policy = _policy(policy);
Elliott Hughes9f495082018-04-25 14:52:50 -0700234 static const char* const kSchedPolicyNames[] = {
Tim Murray419ba9e2018-04-13 10:15:49 -0700235 [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ",
236 [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta",
237 [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800238 };
Elliott Hughes9f495082018-04-25 14:52:50 -0700239 static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
240 if (policy < SP_BACKGROUND || policy >= SP_CNT) {
Wei Wangee2f2602019-10-11 15:09:09 -0700241 return nullptr;
Elliott Hughes9f495082018-04-25 14:52:50 -0700242 }
243 return kSchedPolicyNames[policy];
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800244}
Wei Wangee2f2602019-10-11 15:09:09 -0700245
246const char* get_cpuset_policy_profile_name(SchedPolicy policy) {
247 /*
248 * cpuset profile array for:
249 * SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
250 * SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
251 * SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
252 * index is policy + 1
253 * this need keep in sync with SchedPolicy enum
254 */
255 static constexpr const char* kCpusetProfiles[SP_CNT + 1] = {
256 "CPUSET_SP_DEFAULT", "CPUSET_SP_BACKGROUND", "CPUSET_SP_FOREGROUND",
257 "CPUSET_SP_SYSTEM", "CPUSET_SP_FOREGROUND", "CPUSET_SP_FOREGROUND",
258 "CPUSET_SP_TOP_APP", "CPUSET_SP_DEFAULT", "CPUSET_SP_RESTRICTED"};
259 if (policy < SP_DEFAULT || policy >= SP_CNT) {
260 return nullptr;
261 }
262 return kCpusetProfiles[policy + 1];
263}
264
265const char* get_sched_policy_profile_name(SchedPolicy policy) {
266 /*
267 * sched profile array for:
268 * SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
269 * SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
270 * SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
271 * index is policy + 1
272 * this need keep in sync with SchedPolicy enum
273 */
274 static constexpr const char* kSchedProfiles[SP_CNT + 1] = {
275 "SCHED_SP_DEFAULT", "SCHED_SP_BACKGROUND", "SCHED_SP_FOREGROUND",
Wei Wangab879792020-11-24 00:26:40 -0800276 "SCHED_SP_SYSTEM", "SCHED_SP_FOREGROUND", "SCHED_SP_FOREGROUND",
Wei Wangee2f2602019-10-11 15:09:09 -0700277 "SCHED_SP_TOP_APP", "SCHED_SP_RT_APP", "SCHED_SP_DEFAULT"};
278 if (policy < SP_DEFAULT || policy >= SP_CNT) {
279 return nullptr;
280 }
281 return kSchedProfiles[policy + 1];
282}