blob: 15f8139b103ab91a495c6e70ed9e8549bf54862d [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:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080049 return SetTaskProfiles(tid,
50 {"HighEnergySaving", "ProcessCapacityLow", "LowIoPriority",
51 "TimerSlackHigh"},
52 true)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080053 ? 0
54 : -1;
55 case SP_FOREGROUND:
56 case SP_AUDIO_APP:
57 case SP_AUDIO_SYS:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080058 return SetTaskProfiles(tid,
59 {"HighPerformance", "ProcessCapacityHigh", "HighIoPriority",
60 "TimerSlackNormal"},
61 true)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080062 ? 0
63 : -1;
64 case SP_TOP_APP:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080065 return SetTaskProfiles(tid,
66 {"MaxPerformance", "ProcessCapacityMax", "MaxIoPriority",
67 "TimerSlackNormal"},
68 true)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080069 ? 0
70 : -1;
71 case SP_SYSTEM:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080072 return SetTaskProfiles(tid, {"ServiceCapacityLow", "TimerSlackNormal"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080073 case SP_RESTRICTED:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080074 return SetTaskProfiles(tid, {"ServiceCapacityRestricted", "TimerSlackNormal"}, true)
75 ? 0
76 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080077 default:
78 break;
Todd Kjosba8a4752015-10-26 16:22:11 -070079 }
80
Tim Murray99910262015-06-22 14:00:56 -070081 return 0;
Tim Murrayb769c8d2015-06-08 14:56:29 -070082}
83
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080084int set_sched_policy(int tid, SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070085 if (tid == 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080086 tid = GetThreadId();
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070087 }
Glenn Kasten69bfb1f2012-03-16 09:43:19 -070088 policy = _policy(policy);
San Mehat493dad92009-09-12 10:06:57 -070089
San Mehatd2e4e462009-10-29 11:48:00 -070090#if POLICY_DEBUG
91 char statfile[64];
92 char statline[1024];
93 char thread_name[255];
San Mehatd2e4e462009-10-29 11:48:00 -070094
Raja Ma2f37e42016-04-19 23:55:14 +053095 snprintf(statfile, sizeof(statfile), "/proc/%d/stat", tid);
San Mehatd2e4e462009-10-29 11:48:00 -070096 memset(thread_name, 0, sizeof(thread_name));
97
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080098 unique_fd fd(TEMP_FAILURE_RETRY(open(statfile, O_RDONLY | O_CLOEXEC)));
San Mehatd2e4e462009-10-29 11:48:00 -070099 if (fd >= 0) {
100 int rc = read(fd, statline, 1023);
San Mehatd2e4e462009-10-29 11:48:00 -0700101 statline[rc] = 0;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800102 char* p = statline;
103 char* q;
San Mehatd2e4e462009-10-29 11:48:00 -0700104
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800105 for (p = statline; *p != '('; p++)
106 ;
San Mehatd2e4e462009-10-29 11:48:00 -0700107 p++;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800108 for (q = p; *q != ')'; q++)
109 ;
San Mehatd2e4e462009-10-29 11:48:00 -0700110
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800111 strncpy(thread_name, p, (q - p));
San Mehatd2e4e462009-10-29 11:48:00 -0700112 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700113 switch (policy) {
Tim Murrayb769c8d2015-06-08 14:56:29 -0700114 case SP_BACKGROUND:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800115 SLOGD("vvv tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700116 break;
117 case SP_FOREGROUND:
118 case SP_AUDIO_APP:
119 case SP_AUDIO_SYS:
Tim Murray6647bb52016-01-11 16:16:35 -0800120 case SP_TOP_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121 SLOGD("^^^ tid %d (%s)", tid, thread_name);
122 break;
123 case SP_SYSTEM:
124 SLOGD("/// tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700125 break;
Joel Fernandes88ef9f02017-03-25 22:46:10 -0700126 case SP_RT_APP:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800127 SLOGD("RT tid %d (%s)", tid, thread_name);
Tim Murrayb769c8d2015-06-08 14:56:29 -0700128 break;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800129 default:
130 SLOGD("??? tid %d (%s)", tid, thread_name);
131 break;
132 }
133#endif
Tim Murrayb769c8d2015-06-08 14:56:29 -0700134
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800135 switch (policy) {
136 case SP_BACKGROUND:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800137 return SetTaskProfiles(tid, {"HighEnergySaving", "TimerSlackHigh"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800138 case SP_FOREGROUND:
139 case SP_AUDIO_APP:
140 case SP_AUDIO_SYS:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800141 return SetTaskProfiles(tid, {"HighPerformance", "TimerSlackNormal"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800142 case SP_TOP_APP:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800143 return SetTaskProfiles(tid, {"MaxPerformance", "TimerSlackNormal"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800144 case SP_RT_APP:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800145 return SetTaskProfiles(tid, {"RealtimePerformance", "TimerSlackNormal"}, true) ? 0 : -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800146 default:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800147 return SetTaskProfiles(tid, {"TimerSlackNormal"}, true) ? 0 : -1;
San Mehat493dad92009-09-12 10:06:57 -0700148 }
149
150 return 0;
151}
Raphael0384a982009-09-15 17:10:17 -0700152
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800153bool cpusets_enabled() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700154 static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").IsUsable());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800155 return enabled;
156}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700157
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800158bool schedboost_enabled() {
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700159 static bool enabled = (CgroupMap::GetInstance().FindController("schedtune").IsUsable());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800160 return enabled;
161}
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700162
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800163static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700164 auto controller = CgroupMap::GetInstance().FindController(subsys);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800165
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -0700166 if (!controller.IsUsable()) return -1;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800167
Yifan Hong53e0deb2019-03-22 17:01:08 -0700168 if (!controller.GetTaskGroup(tid, &subgroup)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800169 LOG(ERROR) << "Failed to find cgroup for tid " << tid;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170 return -1;
171 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700172 return 0;
173}
174
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800175int get_sched_policy(int tid, SchedPolicy* policy) {
176 if (tid == 0) {
177 tid = GetThreadId();
178 }
179
180 std::string group;
181 if (schedboost_enabled()) {
182 if (getCGroupSubsys(tid, "schedtune", group) < 0) return -1;
183 }
184 if (group.empty() && cpusets_enabled()) {
185 if (getCGroupSubsys(tid, "cpuset", group) < 0) return -1;
186 }
187
188 // TODO: replace hardcoded directories
189 if (group.empty()) {
190 *policy = SP_FOREGROUND;
191 } else if (group == "foreground") {
192 *policy = SP_FOREGROUND;
193 } else if (group == "system-background") {
194 *policy = SP_SYSTEM;
195 } else if (group == "background") {
196 *policy = SP_BACKGROUND;
197 } else if (group == "top-app") {
198 *policy = SP_TOP_APP;
199 } else if (group == "restricted") {
200 *policy = SP_RESTRICTED;
201 } else {
202 errno = ERANGE;
203 return -1;
204 }
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700205 return 0;
206}
207
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800208#else
209
210/* Stubs for non-Android targets. */
211
212int set_sched_policy(int, SchedPolicy) {
213 return 0;
214}
215
216int get_sched_policy(int, SchedPolicy* policy) {
217 *policy = SP_SYSTEM_DEFAULT;
218 return 0;
219}
220
221#endif
222
Elliott Hughes9f495082018-04-25 14:52:50 -0700223const char* get_sched_policy_name(SchedPolicy policy) {
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700224 policy = _policy(policy);
Elliott Hughes9f495082018-04-25 14:52:50 -0700225 static const char* const kSchedPolicyNames[] = {
Tim Murray419ba9e2018-04-13 10:15:49 -0700226 [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ",
227 [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta",
228 [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800229 };
Elliott Hughes9f495082018-04-25 14:52:50 -0700230 static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
231 if (policy < SP_BACKGROUND || policy >= SP_CNT) {
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800232 return "error";
Elliott Hughes9f495082018-04-25 14:52:50 -0700233 }
234 return kSchedPolicyNames[policy];
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800235}