blob: 0f2640a0abcd6119f1d5cf924c4a08d795251b8b [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>
T.J. Mercier54bfde02024-06-04 23:25:29 +000022#include <fcntl.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070023#include <unistd.h>
24
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080025#include <android-base/logging.h>
26#include <android-base/threads.h>
27#include <cgroup_map.h>
28#include <processgroup/processgroup.h>
29
30using android::base::GetThreadId;
Mark Salyzyn12717162014-04-29 15:49:14 -070031
Jeff Brownbff8f3f2012-05-08 15:05:42 -070032/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
33 * Call this any place a SchedPolicy is used as an input parameter.
34 * Returns the possibly re-mapped policy.
35 */
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080036static inline SchedPolicy _policy(SchedPolicy p) {
37 return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
Jeff Brownbff8f3f2012-05-08 15:05:42 -070038}
San Mehatd2e4e462009-10-29 11:48:00 -070039
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080040#if defined(__ANDROID__)
41
T.J. Mercier1c007992024-01-25 16:29:54 +000042int set_cpuset_policy(pid_t tid, SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070043 if (tid == 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080044 tid = GetThreadId();
Tim Murrayb769c8d2015-06-08 14:56:29 -070045 }
46 policy = _policy(policy);
Tim Murrayb769c8d2015-06-08 14:56:29 -070047
Tim Murrayb769c8d2015-06-08 14:56:29 -070048 switch (policy) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080049 case SP_BACKGROUND:
Rick Yiu0b211fa2019-09-16 19:07:17 +080050 return SetTaskProfiles(tid, {"CPUSET_SP_BACKGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080051 case SP_FOREGROUND:
52 case SP_AUDIO_APP:
53 case SP_AUDIO_SYS:
Rick Yiu0b211fa2019-09-16 19:07:17 +080054 return SetTaskProfiles(tid, {"CPUSET_SP_FOREGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080055 case SP_TOP_APP:
Rick Yiu0b211fa2019-09-16 19:07:17 +080056 return SetTaskProfiles(tid, {"CPUSET_SP_TOP_APP"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080057 case SP_SYSTEM:
Rick Yiu0b211fa2019-09-16 19:07:17 +080058 return SetTaskProfiles(tid, {"CPUSET_SP_SYSTEM"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080059 case SP_RESTRICTED:
Rick Yiu0b211fa2019-09-16 19:07:17 +080060 return SetTaskProfiles(tid, {"CPUSET_SP_RESTRICTED"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080061 default:
62 break;
Todd Kjosba8a4752015-10-26 16:22:11 -070063 }
64
Tim Murray99910262015-06-22 14:00:56 -070065 return 0;
Tim Murrayb769c8d2015-06-08 14:56:29 -070066}
67
T.J. Mercier1c007992024-01-25 16:29:54 +000068int set_sched_policy(pid_t tid, SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070069 if (tid == 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080070 tid = GetThreadId();
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070071 }
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070072 policy = _policy(policy);
San Mehat493dad92009-09-12 10:06:57 -070073
San Mehatd2e4e462009-10-29 11:48:00 -070074#if POLICY_DEBUG
75 char statfile[64];
76 char statline[1024];
77 char thread_name[255];
San Mehatd2e4e462009-10-29 11:48:00 -070078
Raja Ma2f37e42016-04-19 23:55:14 +053079 snprintf(statfile, sizeof(statfile), "/proc/%d/stat", tid);
San Mehatd2e4e462009-10-29 11:48:00 -070080 memset(thread_name, 0, sizeof(thread_name));
81
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080082 unique_fd fd(TEMP_FAILURE_RETRY(open(statfile, O_RDONLY | O_CLOEXEC)));
San Mehatd2e4e462009-10-29 11:48:00 -070083 if (fd >= 0) {
84 int rc = read(fd, statline, 1023);
San Mehatd2e4e462009-10-29 11:48:00 -070085 statline[rc] = 0;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080086 char* p = statline;
87 char* q;
San Mehatd2e4e462009-10-29 11:48:00 -070088
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080089 for (p = statline; *p != '('; p++)
90 ;
San Mehatd2e4e462009-10-29 11:48:00 -070091 p++;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080092 for (q = p; *q != ')'; q++)
93 ;
San Mehatd2e4e462009-10-29 11:48:00 -070094
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080095 strncpy(thread_name, p, (q - p));
San Mehatd2e4e462009-10-29 11:48:00 -070096 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -070097 switch (policy) {
Tim Murrayb769c8d2015-06-08 14:56:29 -070098 case SP_BACKGROUND:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080099 SLOGD("vvv tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700100 break;
101 case SP_FOREGROUND:
102 case SP_AUDIO_APP:
103 case SP_AUDIO_SYS:
Tim Murray6647bb52016-01-11 16:16:35 -0800104 case SP_TOP_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800105 SLOGD("^^^ tid %d (%s)", tid, thread_name);
106 break;
107 case SP_SYSTEM:
108 SLOGD("/// tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700109 break;
Joel Fernandes88ef9f02017-03-25 22:46:10 -0700110 case SP_RT_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800111 SLOGD("RT tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700112 break;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800113 default:
114 SLOGD("??? tid %d (%s)", tid, thread_name);
115 break;
116 }
117#endif
Tim Murrayb769c8d2015-06-08 14:56:29 -0700118
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119 switch (policy) {
120 case SP_BACKGROUND:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800121 return SetTaskProfiles(tid, {"SCHED_SP_BACKGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800122 case SP_FOREGROUND:
123 case SP_AUDIO_APP:
124 case SP_AUDIO_SYS:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800125 return SetTaskProfiles(tid, {"SCHED_SP_FOREGROUND"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800126 case SP_TOP_APP:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800127 return SetTaskProfiles(tid, {"SCHED_SP_TOP_APP"}, true) ? 0 : -1;
Wei Wangab879792020-11-24 00:26:40 -0800128 case SP_SYSTEM:
129 return SetTaskProfiles(tid, {"SCHED_SP_SYSTEM"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800130 case SP_RT_APP:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800131 return SetTaskProfiles(tid, {"SCHED_SP_RT_APP"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800132 default:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800133 return SetTaskProfiles(tid, {"SCHED_SP_DEFAULT"}, true) ? 0 : -1;
San Mehat493dad92009-09-12 10:06:57 -0700134 }
135
136 return 0;
137}
Raphael0384a982009-09-15 17:10:17 -0700138
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800139bool cpusets_enabled() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700140 static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").IsUsable());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800141 return enabled;
142}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700143
Quentin Perret64811b72020-01-14 12:55:57 +0000144static bool schedtune_enabled() {
145 return (CgroupMap::GetInstance().FindController("schedtune").IsUsable());
146}
147
148static bool cpuctl_enabled() {
149 return (CgroupMap::GetInstance().FindController("cpu").IsUsable());
150}
151
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800152bool schedboost_enabled() {
Quentin Perret64811b72020-01-14 12:55:57 +0000153 static bool enabled = schedtune_enabled() || cpuctl_enabled();
154
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800155 return enabled;
156}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700157
T.J. Mercier1c007992024-01-25 16:29:54 +0000158static int getCGroupSubsys(pid_t tid, const char* subsys, std::string& subgroup) {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700159 auto controller = CgroupMap::GetInstance().FindController(subsys);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800160
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700161 if (!controller.IsUsable()) return -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800162
Suren Baghdasaryane3d38962021-04-26 09:21:52 -0700163 if (!controller.GetTaskGroup(tid, &subgroup))
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800164 return -1;
Suren Baghdasaryane3d38962021-04-26 09:21:52 -0700165
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700166 return 0;
167}
168
Wei Wangcf4b15e2021-12-13 17:42:08 -0800169static int get_sched_policy_from_group(const std::string& group, SchedPolicy* policy) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170 if (group.empty()) {
171 *policy = SP_FOREGROUND;
172 } else if (group == "foreground") {
173 *policy = SP_FOREGROUND;
174 } else if (group == "system-background") {
175 *policy = SP_SYSTEM;
176 } else if (group == "background") {
177 *policy = SP_BACKGROUND;
178 } else if (group == "top-app") {
179 *policy = SP_TOP_APP;
180 } else if (group == "restricted") {
181 *policy = SP_RESTRICTED;
182 } else {
183 errno = ERANGE;
184 return -1;
185 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700186 return 0;
187}
188
T.J. Mercier1c007992024-01-25 16:29:54 +0000189int get_sched_policy(pid_t tid, SchedPolicy* policy) {
Wei Wangcf4b15e2021-12-13 17:42:08 -0800190 if (tid == 0) {
191 tid = GetThreadId();
192 }
193
194 std::string group;
195 if (schedboost_enabled()) {
196 if ((getCGroupSubsys(tid, "schedtune", group) < 0) &&
197 (getCGroupSubsys(tid, "cpu", group) < 0)) {
198 LOG(ERROR) << "Failed to find cpu cgroup for tid " << tid;
199 return -1;
200 }
201 // Wipe invalid group to fallback to cpuset
202 if (!group.empty()) {
203 if (get_sched_policy_from_group(group, policy) < 0) {
204 group.clear();
205 } else {
206 return 0;
207 }
208 }
209 }
210
211 if (cpusets_enabled() && getCGroupSubsys(tid, "cpuset", group) < 0) {
212 LOG(ERROR) << "Failed to find cpuset cgroup for tid " << tid;
213 return -1;
214 }
215 return get_sched_policy_from_group(group, policy);
216}
217
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800218#else
219
220/* Stubs for non-Android targets. */
221
222int set_sched_policy(int, SchedPolicy) {
223 return 0;
224}
225
226int get_sched_policy(int, SchedPolicy* policy) {
227 *policy = SP_SYSTEM_DEFAULT;
228 return 0;
229}
230
231#endif
232
Elliott Hughes9f495082018-04-25 14:52:50 -0700233const char* get_sched_policy_name(SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700234 policy = _policy(policy);
Elliott Hughes9f495082018-04-25 14:52:50 -0700235 static const char* const kSchedPolicyNames[] = {
Tim Murray419ba9e2018-04-13 10:15:49 -0700236 [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ",
237 [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta",
238 [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800239 };
Elliott Hughes9f495082018-04-25 14:52:50 -0700240 static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
241 if (policy < SP_BACKGROUND || policy >= SP_CNT) {
Wei Wangee2f2602019-10-11 15:09:09 -0700242 return nullptr;
Elliott Hughes9f495082018-04-25 14:52:50 -0700243 }
244 return kSchedPolicyNames[policy];
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800245}
Wei Wangee2f2602019-10-11 15:09:09 -0700246
247const char* get_cpuset_policy_profile_name(SchedPolicy policy) {
248 /*
249 * cpuset profile array for:
250 * SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
251 * SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
252 * SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
253 * index is policy + 1
254 * this need keep in sync with SchedPolicy enum
255 */
256 static constexpr const char* kCpusetProfiles[SP_CNT + 1] = {
257 "CPUSET_SP_DEFAULT", "CPUSET_SP_BACKGROUND", "CPUSET_SP_FOREGROUND",
258 "CPUSET_SP_SYSTEM", "CPUSET_SP_FOREGROUND", "CPUSET_SP_FOREGROUND",
259 "CPUSET_SP_TOP_APP", "CPUSET_SP_DEFAULT", "CPUSET_SP_RESTRICTED"};
260 if (policy < SP_DEFAULT || policy >= SP_CNT) {
261 return nullptr;
262 }
263 return kCpusetProfiles[policy + 1];
264}
265
266const char* get_sched_policy_profile_name(SchedPolicy policy) {
267 /*
268 * sched profile array for:
269 * SP_DEFAULT(-1), SP_BACKGROUND(0), SP_FOREGROUND(1),
270 * SP_SYSTEM(2), SP_AUDIO_APP(3), SP_AUDIO_SYS(4),
271 * SP_TOP_APP(5), SP_RT_APP(6), SP_RESTRICTED(7)
272 * index is policy + 1
273 * this need keep in sync with SchedPolicy enum
274 */
275 static constexpr const char* kSchedProfiles[SP_CNT + 1] = {
276 "SCHED_SP_DEFAULT", "SCHED_SP_BACKGROUND", "SCHED_SP_FOREGROUND",
Wei Wangab879792020-11-24 00:26:40 -0800277 "SCHED_SP_SYSTEM", "SCHED_SP_FOREGROUND", "SCHED_SP_FOREGROUND",
Wei Wangee2f2602019-10-11 15:09:09 -0700278 "SCHED_SP_TOP_APP", "SCHED_SP_RT_APP", "SCHED_SP_DEFAULT"};
279 if (policy < SP_DEFAULT || policy >= SP_CNT) {
280 return nullptr;
281 }
282 return kSchedProfiles[policy + 1];
283}