blob: 337b032f28a4f8e98af26d8e0a07744522bebc2c [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:
49 return SetTaskProfiles(tid,
50 {"HighEnergySaving", "ProcessCapacityLow", "TimerSlackHigh"})
51 ? 0
52 : -1;
53 case SP_FOREGROUND:
54 case SP_AUDIO_APP:
55 case SP_AUDIO_SYS:
56 return SetTaskProfiles(tid,
57 {"HighPerformance", "ProcessCapacityHigh", "TimerSlackNormal"})
58 ? 0
59 : -1;
60 case SP_TOP_APP:
61 return SetTaskProfiles(tid,
62 {"MaxPerformance", "ProcessCapacityMax", "TimerSlackNormal"})
63 ? 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:
129 return SetTaskProfiles(tid, {"HighEnergySaving", "TimerSlackHigh"}) ? 0 : -1;
130 case SP_FOREGROUND:
131 case SP_AUDIO_APP:
132 case SP_AUDIO_SYS:
133 return SetTaskProfiles(tid, {"HighPerformance", "TimerSlackNormal"}) ? 0 : -1;
134 case SP_TOP_APP:
135 return SetTaskProfiles(tid, {"MaxPerformance", "TimerSlackNormal"}) ? 0 : -1;
136 case SP_RT_APP:
137 return SetTaskProfiles(tid, {"RealtimePerformance", "TimerSlackNormal"}) ? 0 : -1;
138 default:
139 return SetTaskProfiles(tid, {"TimerSlackNormal"}) ? 0 : -1;
San Mehat493dad92009-09-12 10:06:57 -0700140 }
141
142 return 0;
143}
Raphael0384a982009-09-15 17:10:17 -0700144
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800145bool cpusets_enabled() {
146 static bool enabled = (CgroupMap::GetInstance().FindController("cpuset") != nullptr);
147 return enabled;
148}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700149
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800150bool schedboost_enabled() {
151 static bool enabled = (CgroupMap::GetInstance().FindController("schedtune") != nullptr);
152 return enabled;
153}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700154
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800155static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
156 const CgroupController* controller = CgroupMap::GetInstance().FindController(subsys);
157
158 if (!controller) return -1;
159
160 if (!controller->GetTaskGroup(tid, &subgroup)) {
161 PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
162 return -1;
163 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700164 return 0;
165}
166
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800167int get_sched_policy(int tid, SchedPolicy* policy) {
168 if (tid == 0) {
169 tid = GetThreadId();
170 }
171
172 std::string group;
173 if (schedboost_enabled()) {
174 if (getCGroupSubsys(tid, "schedtune", group) < 0) return -1;
175 }
176 if (group.empty() && cpusets_enabled()) {
177 if (getCGroupSubsys(tid, "cpuset", group) < 0) return -1;
178 }
179
180 // TODO: replace hardcoded directories
181 if (group.empty()) {
182 *policy = SP_FOREGROUND;
183 } else if (group == "foreground") {
184 *policy = SP_FOREGROUND;
185 } else if (group == "system-background") {
186 *policy = SP_SYSTEM;
187 } else if (group == "background") {
188 *policy = SP_BACKGROUND;
189 } else if (group == "top-app") {
190 *policy = SP_TOP_APP;
191 } else if (group == "restricted") {
192 *policy = SP_RESTRICTED;
193 } else {
194 errno = ERANGE;
195 return -1;
196 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700197 return 0;
198}
199
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800200#else
201
202/* Stubs for non-Android targets. */
203
204int set_sched_policy(int, SchedPolicy) {
205 return 0;
206}
207
208int get_sched_policy(int, SchedPolicy* policy) {
209 *policy = SP_SYSTEM_DEFAULT;
210 return 0;
211}
212
213#endif
214
Elliott Hughes9f495082018-04-25 14:52:50 -0700215const char* get_sched_policy_name(SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700216 policy = _policy(policy);
Elliott Hughes9f495082018-04-25 14:52:50 -0700217 static const char* const kSchedPolicyNames[] = {
Tim Murray419ba9e2018-04-13 10:15:49 -0700218 [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ",
219 [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta",
220 [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800221 };
Elliott Hughes9f495082018-04-25 14:52:50 -0700222 static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
223 if (policy < SP_BACKGROUND || policy >= SP_CNT) {
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800224 return "error";
Elliott Hughes9f495082018-04-25 14:52:50 -0700225 }
226 return kSchedPolicyNames[policy];
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800227}