blob: c7d0cca4c694c99046f17856c01c9ff259e08351 [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
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080041int set_cpuset_policy(int 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 Yiu2b3bf842019-02-21 14:37:36 +080049 return SetTaskProfiles(tid, {"HighEnergySaving", "ProcessCapacityLow", "LowIoPriority",
50 "TimerSlackHigh"})
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080051 ? 0
52 : -1;
53 case SP_FOREGROUND:
54 case SP_AUDIO_APP:
55 case SP_AUDIO_SYS:
Rick Yiu2b3bf842019-02-21 14:37:36 +080056 return SetTaskProfiles(tid, {"HighPerformance", "ProcessCapacityHigh", "HighIoPriority",
57 "TimerSlackNormal"})
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080058 ? 0
59 : -1;
60 case SP_TOP_APP:
Rick Yiu2b3bf842019-02-21 14:37:36 +080061 return SetTaskProfiles(tid, {"MaxPerformance", "ProcessCapacityMax", "MaxIoPriority",
62 "TimerSlackNormal"})
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080063 ? 0
64 : -1;
65 case SP_SYSTEM:
66 return SetTaskProfiles(tid, {"ServiceCapacityLow", "TimerSlackNormal"}) ? 0 : -1;
67 case SP_RESTRICTED:
68 return SetTaskProfiles(tid, {"ServiceCapacityRestricted", "TimerSlackNormal"}) ? 0 : -1;
69 default:
70 break;
Todd Kjosba8a4752015-10-26 16:22:11 -070071 }
72
Tim Murray99910262015-06-22 14:00:56 -070073 return 0;
Tim Murrayb769c8d2015-06-08 14:56:29 -070074}
75
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080076int set_sched_policy(int tid, SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070077 if (tid == 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080078 tid = GetThreadId();
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070079 }
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070080 policy = _policy(policy);
San Mehat493dad92009-09-12 10:06:57 -070081
San Mehatd2e4e462009-10-29 11:48:00 -070082#if POLICY_DEBUG
83 char statfile[64];
84 char statline[1024];
85 char thread_name[255];
San Mehatd2e4e462009-10-29 11:48:00 -070086
Raja Ma2f37e42016-04-19 23:55:14 +053087 snprintf(statfile, sizeof(statfile), "/proc/%d/stat", tid);
San Mehatd2e4e462009-10-29 11:48:00 -070088 memset(thread_name, 0, sizeof(thread_name));
89
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080090 unique_fd fd(TEMP_FAILURE_RETRY(open(statfile, O_RDONLY | O_CLOEXEC)));
San Mehatd2e4e462009-10-29 11:48:00 -070091 if (fd >= 0) {
92 int rc = read(fd, statline, 1023);
San Mehatd2e4e462009-10-29 11:48:00 -070093 statline[rc] = 0;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080094 char* p = statline;
95 char* q;
San Mehatd2e4e462009-10-29 11:48:00 -070096
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080097 for (p = statline; *p != '('; p++)
98 ;
San Mehatd2e4e462009-10-29 11:48:00 -070099 p++;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800100 for (q = p; *q != ')'; q++)
101 ;
San Mehatd2e4e462009-10-29 11:48:00 -0700102
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800103 strncpy(thread_name, p, (q - p));
San Mehatd2e4e462009-10-29 11:48:00 -0700104 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700105 switch (policy) {
Tim Murrayb769c8d2015-06-08 14:56:29 -0700106 case SP_BACKGROUND:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800107 SLOGD("vvv tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700108 break;
109 case SP_FOREGROUND:
110 case SP_AUDIO_APP:
111 case SP_AUDIO_SYS:
Tim Murray6647bb52016-01-11 16:16:35 -0800112 case SP_TOP_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800113 SLOGD("^^^ tid %d (%s)", tid, thread_name);
114 break;
115 case SP_SYSTEM:
116 SLOGD("/// tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700117 break;
Joel Fernandes88ef9f02017-03-25 22:46:10 -0700118 case SP_RT_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119 SLOGD("RT tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700120 break;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121 default:
122 SLOGD("??? tid %d (%s)", tid, thread_name);
123 break;
124 }
125#endif
Tim Murrayb769c8d2015-06-08 14:56:29 -0700126
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800127 switch (policy) {
128 case SP_BACKGROUND:
Rick Yiu2b3bf842019-02-21 14:37:36 +0800129 return SetTaskProfiles(tid, {"HighEnergySaving", "LowIoPriority", "TimerSlackHigh"})
130 ? 0
131 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800132 case SP_FOREGROUND:
133 case SP_AUDIO_APP:
134 case SP_AUDIO_SYS:
Rick Yiu2b3bf842019-02-21 14:37:36 +0800135 return SetTaskProfiles(tid, {"HighPerformance", "HighIoPriority", "TimerSlackNormal"})
136 ? 0
137 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800138 case SP_TOP_APP:
Rick Yiu2b3bf842019-02-21 14:37:36 +0800139 return SetTaskProfiles(tid, {"MaxPerformance", "MaxIoPriority", "TimerSlackNormal"})
140 ? 0
141 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800142 case SP_RT_APP:
Rick Yiu2b3bf842019-02-21 14:37:36 +0800143 return SetTaskProfiles(tid,
144 {"RealtimePerformance", "MaxIoPriority", "TimerSlackNormal"})
145 ? 0
146 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800147 default:
148 return SetTaskProfiles(tid, {"TimerSlackNormal"}) ? 0 : -1;
San Mehat493dad92009-09-12 10:06:57 -0700149 }
150
151 return 0;
152}
Raphael0384a982009-09-15 17:10:17 -0700153
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800154bool cpusets_enabled() {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700155 static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").HasValue());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800156 return enabled;
157}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700158
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800159bool schedboost_enabled() {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700160 static bool enabled = (CgroupMap::GetInstance().FindController("schedtune").HasValue());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800161 return enabled;
162}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700163
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800164static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700165 auto controller = CgroupMap::GetInstance().FindController(subsys);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800166
Yifan Hong53e0deb2019-03-22 17:01:08 -0700167 if (!controller.HasValue()) return -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800168
Yifan Hong53e0deb2019-03-22 17:01:08 -0700169 if (!controller.GetTaskGroup(tid, &subgroup)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800170 LOG(ERROR) << "Failed to find cgroup for tid " << tid;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800171 return -1;
172 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700173 return 0;
174}
175
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800176int get_sched_policy(int tid, SchedPolicy* policy) {
177 if (tid == 0) {
178 tid = GetThreadId();
179 }
180
181 std::string group;
182 if (schedboost_enabled()) {
183 if (getCGroupSubsys(tid, "schedtune", group) < 0) return -1;
184 }
185 if (group.empty() && cpusets_enabled()) {
186 if (getCGroupSubsys(tid, "cpuset", group) < 0) return -1;
187 }
188
189 // TODO: replace hardcoded directories
190 if (group.empty()) {
191 *policy = SP_FOREGROUND;
192 } else if (group == "foreground") {
193 *policy = SP_FOREGROUND;
194 } else if (group == "system-background") {
195 *policy = SP_SYSTEM;
196 } else if (group == "background") {
197 *policy = SP_BACKGROUND;
198 } else if (group == "top-app") {
199 *policy = SP_TOP_APP;
200 } else if (group == "restricted") {
201 *policy = SP_RESTRICTED;
202 } else {
203 errno = ERANGE;
204 return -1;
205 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700206 return 0;
207}
208
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800209#else
210
211/* Stubs for non-Android targets. */
212
213int set_sched_policy(int, SchedPolicy) {
214 return 0;
215}
216
217int get_sched_policy(int, SchedPolicy* policy) {
218 *policy = SP_SYSTEM_DEFAULT;
219 return 0;
220}
221
222#endif
223
Elliott Hughes9f495082018-04-25 14:52:50 -0700224const char* get_sched_policy_name(SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700225 policy = _policy(policy);
Elliott Hughes9f495082018-04-25 14:52:50 -0700226 static const char* const kSchedPolicyNames[] = {
Tim Murray419ba9e2018-04-13 10:15:49 -0700227 [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ",
228 [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta",
229 [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800230 };
Elliott Hughes9f495082018-04-25 14:52:50 -0700231 static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
232 if (policy < SP_BACKGROUND || policy >= SP_CNT) {
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800233 return "error";
Elliott Hughes9f495082018-04-25 14:52:50 -0700234 }
235 return kSchedPolicyNames[policy];
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800236}