blob: f8ffb1d8e55412c8bf26ca921ffd5e6761ef9748 [file] [log] [blame]
Mark Salyzyn12717162014-04-29 15:49:14 -07001/*
San Mehat493dad92009-09-12 10:06:57 -07002** Copyright 2007, 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*/
16
Jeff Brownbff8f3f2012-05-08 15:05:42 -070017#define LOG_TAG "SchedPolicy"
18
San Mehat493dad92009-09-12 10:06:57 -070019#include <errno.h>
20#include <fcntl.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
Jeff Brownbff8f3f2012-05-08 15:05:42 -070026#include <cutils/sched_policy.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070027#include <log/log.h>
28
29#define UNUSED __attribute__((__unused__))
Raphael0384a982009-09-15 17:10:17 -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 */
35static inline SchedPolicy _policy(SchedPolicy p)
36{
37 return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
38}
San Mehatd2e4e462009-10-29 11:48:00 -070039
Jeff Brownbff8f3f2012-05-08 15:05:42 -070040#if defined(HAVE_ANDROID_OS) && defined(HAVE_SCHED_H) && defined(HAVE_PTHREADS)
Raphael0384a982009-09-15 17:10:17 -070041
Brad Fitzpatrick86b12152010-05-08 11:51:13 -070042#include <pthread.h>
Elliott Hughese07d77e2014-07-11 20:57:03 -070043#include <sched.h>
44#include <sys/prctl.h>
San Mehat3cd5b662009-09-14 16:05:24 -070045
San Mehat805d67a2009-10-29 13:56:26 -070046#define POLICY_DEBUG 0
San Mehatd2e4e462009-10-29 11:48:00 -070047
Glenn Kasten10ec3c72012-04-19 15:25:58 -070048#define CAN_SET_SP_SYSTEM 0 // non-zero means to implement set_sched_policy(tid, SP_SYSTEM)
49
Ruchi Kandoi422852e2014-04-22 18:55:08 -070050// timer slack value in nS enforced when the thread moves to background
51#define TIMER_SLACK_BG 40000000
52
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070053static pthread_once_t the_once = PTHREAD_ONCE_INIT;
54
San Mehatc0dfca72009-10-27 11:52:55 -070055static int __sys_supports_schedgroups = -1;
56
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070057// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070058static int bg_cgroup_fd = -1;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070059static int fg_cgroup_fd = -1;
60#if CAN_SET_SP_SYSTEM
61static int system_cgroup_fd = -1;
62#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070063
64/* Add tid to the scheduling group defined by the policy */
65static int add_tid_to_cgroup(int tid, SchedPolicy policy)
San Mehat493dad92009-09-12 10:06:57 -070066{
67 int fd;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070068
Glenn Kasten10ec3c72012-04-19 15:25:58 -070069 switch (policy) {
70 case SP_BACKGROUND:
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070071 fd = bg_cgroup_fd;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070072 break;
73 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -070074 case SP_AUDIO_APP:
75 case SP_AUDIO_SYS:
Glenn Kasten10ec3c72012-04-19 15:25:58 -070076 fd = fg_cgroup_fd;
77 break;
78#if CAN_SET_SP_SYSTEM
79 case SP_SYSTEM:
80 fd = system_cgroup_fd;
81 break;
82#endif
Glenn Kasten10ec3c72012-04-19 15:25:58 -070083 default:
84 fd = -1;
85 break;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070086 }
87
88 if (fd < 0) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -070089 SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
San Mehat493dad92009-09-12 10:06:57 -070090 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070091 }
San Mehat493dad92009-09-12 10:06:57 -070092
Brad Fitzpatrick253e27a2010-05-06 10:00:37 -070093 // specialized itoa -- works for tid > 0
94 char text[22];
95 char *end = text + sizeof(text) - 1;
96 char *ptr = end;
97 *ptr = '\0';
98 while (tid > 0) {
99 *--ptr = '0' + (tid % 10);
100 tid = tid / 10;
101 }
102
103 if (write(fd, ptr, end - ptr) < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700104 /*
105 * If the thread is in the process of exiting,
106 * don't flag an error
107 */
108 if (errno == ESRCH)
109 return 0;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700110 SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
111 ptr, strerror(errno), policy);
San Mehat493dad92009-09-12 10:06:57 -0700112 return -1;
113 }
114
San Mehat493dad92009-09-12 10:06:57 -0700115 return 0;
116}
117
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700118static void __initialize(void) {
119 char* filename;
120 if (!access("/dev/cpuctl/tasks", F_OK)) {
121 __sys_supports_schedgroups = 1;
122
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700123#if CAN_SET_SP_SYSTEM
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700124 filename = "/dev/cpuctl/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700125 system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700126 if (system_cgroup_fd < 0) {
127 SLOGV("open of %s failed: %s\n", filename, strerror(errno));
128 }
129#endif
130
Dima Zavin13ed76b2012-06-04 10:42:38 -0700131 filename = "/dev/cpuctl/apps/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700132 fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700133 if (fg_cgroup_fd < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700134 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -0700135 }
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700136
Dima Zavin13ed76b2012-06-04 10:42:38 -0700137 filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700138 bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700139 if (bg_cgroup_fd < 0) {
140 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
141 }
142 } else {
143 __sys_supports_schedgroups = 0;
San Mehat493dad92009-09-12 10:06:57 -0700144 }
San Mehatc0dfca72009-10-27 11:52:55 -0700145}
146
147/*
148 * Try to get the scheduler group.
149 *
San Mehat503df202010-03-02 17:09:56 -0800150 * The data from /proc/<pid>/cgroup looks (something) like:
San Mehatc0dfca72009-10-27 11:52:55 -0700151 * 2:cpu:/bg_non_interactive
San Mehat503df202010-03-02 17:09:56 -0800152 * 1:cpuacct:/
San Mehatc0dfca72009-10-27 11:52:55 -0700153 *
154 * We return the part after the "/", which will be an empty string for
155 * the default cgroup. If the string is longer than "bufLen", the string
156 * will be truncated.
157 */
158static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
159{
160#ifdef HAVE_ANDROID_OS
161 char pathBuf[32];
San Mehat503df202010-03-02 17:09:56 -0800162 char lineBuf[256];
163 FILE *fp;
San Mehatc0dfca72009-10-27 11:52:55 -0700164
165 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
San Mehat503df202010-03-02 17:09:56 -0800166 if (!(fp = fopen(pathBuf, "r"))) {
San Mehatc0dfca72009-10-27 11:52:55 -0700167 return -1;
168 }
169
San Mehat503df202010-03-02 17:09:56 -0800170 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
171 char *next = lineBuf;
172 char *subsys;
173 char *grp;
174 size_t len;
San Mehatc0dfca72009-10-27 11:52:55 -0700175
San Mehat503df202010-03-02 17:09:56 -0800176 /* Junk the first field */
177 if (!strsep(&next, ":")) {
178 goto out_bad_data;
179 }
San Mehatc0dfca72009-10-27 11:52:55 -0700180
San Mehat503df202010-03-02 17:09:56 -0800181 if (!(subsys = strsep(&next, ":"))) {
182 goto out_bad_data;
183 }
184
185 if (strcmp(subsys, "cpu")) {
186 /* Not the subsys we're looking for */
187 continue;
188 }
189
190 if (!(grp = strsep(&next, ":"))) {
191 goto out_bad_data;
192 }
193 grp++; /* Drop the leading '/' */
194 len = strlen(grp);
195 grp[len-1] = '\0'; /* Drop the trailing '\n' */
196
197 if (bufLen <= len) {
198 len = bufLen - 1;
199 }
200 strncpy(buf, grp, len);
201 buf[len] = '\0';
202 fclose(fp);
203 return 0;
San Mehatc0dfca72009-10-27 11:52:55 -0700204 }
205
San Mehat7e8529a2010-03-25 09:31:42 -0700206 SLOGE("Failed to find cpu subsys");
San Mehat503df202010-03-02 17:09:56 -0800207 fclose(fp);
208 return -1;
209 out_bad_data:
San Mehat7e8529a2010-03-25 09:31:42 -0700210 SLOGE("Bad cgroup data {%s}", lineBuf);
San Mehat503df202010-03-02 17:09:56 -0800211 fclose(fp);
212 return -1;
San Mehatc0dfca72009-10-27 11:52:55 -0700213#else
214 errno = ENOSYS;
215 return -1;
216#endif
217}
218
219int get_sched_policy(int tid, SchedPolicy *policy)
220{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700221#ifdef HAVE_GETTID
222 if (tid == 0) {
223 tid = gettid();
224 }
225#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700226 pthread_once(&the_once, __initialize);
San Mehatc0dfca72009-10-27 11:52:55 -0700227
228 if (__sys_supports_schedgroups) {
229 char grpBuf[32];
230 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
231 return -1;
232 if (grpBuf[0] == '\0') {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700233 *policy = SP_SYSTEM;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700234 } else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
San Mehatc0dfca72009-10-27 11:52:55 -0700235 *policy = SP_BACKGROUND;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700236 } else if (!strcmp(grpBuf, "apps")) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700237 *policy = SP_FOREGROUND;
San Mehatc0dfca72009-10-27 11:52:55 -0700238 } else {
239 errno = ERANGE;
240 return -1;
241 }
242 } else {
243 int rc = sched_getscheduler(tid);
244 if (rc < 0)
245 return -1;
246 else if (rc == SCHED_NORMAL)
247 *policy = SP_FOREGROUND;
248 else if (rc == SCHED_BATCH)
249 *policy = SP_BACKGROUND;
250 else {
251 errno = ERANGE;
252 return -1;
253 }
254 }
255 return 0;
256}
257
258int set_sched_policy(int tid, SchedPolicy policy)
259{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700260#ifdef HAVE_GETTID
261 if (tid == 0) {
262 tid = gettid();
263 }
264#endif
265 policy = _policy(policy);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700266 pthread_once(&the_once, __initialize);
San Mehat493dad92009-09-12 10:06:57 -0700267
San Mehatd2e4e462009-10-29 11:48:00 -0700268#if POLICY_DEBUG
269 char statfile[64];
270 char statline[1024];
271 char thread_name[255];
272 int fd;
273
274 sprintf(statfile, "/proc/%d/stat", tid);
275 memset(thread_name, 0, sizeof(thread_name));
276
277 fd = open(statfile, O_RDONLY);
278 if (fd >= 0) {
279 int rc = read(fd, statline, 1023);
280 close(fd);
281 statline[rc] = 0;
282 char *p = statline;
283 char *q;
284
285 for (p = statline; *p != '('; p++);
286 p++;
287 for (q = p; *q != ')'; q++);
288
289 strncpy(thread_name, p, (q-p));
290 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700291 switch (policy) {
292 case SP_BACKGROUND:
San Mehat7e8529a2010-03-25 09:31:42 -0700293 SLOGD("vvv tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700294 break;
295 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -0700296 case SP_AUDIO_APP:
297 case SP_AUDIO_SYS:
San Mehat7e8529a2010-03-25 09:31:42 -0700298 SLOGD("^^^ tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700299 break;
300 case SP_SYSTEM:
301 SLOGD("/// tid %d (%s)", tid, thread_name);
302 break;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700303 default:
San Mehat7e8529a2010-03-25 09:31:42 -0700304 SLOGD("??? tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700305 break;
San Mehatd2e4e462009-10-29 11:48:00 -0700306 }
307#endif
308
San Mehat493dad92009-09-12 10:06:57 -0700309 if (__sys_supports_schedgroups) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700310 if (add_tid_to_cgroup(tid, policy)) {
San Mehat493dad92009-09-12 10:06:57 -0700311 if (errno != ESRCH && errno != ENOENT)
312 return -errno;
313 }
314 } else {
315 struct sched_param param;
316
317 param.sched_priority = 0;
318 sched_setscheduler(tid,
319 (policy == SP_BACKGROUND) ?
320 SCHED_BATCH : SCHED_NORMAL,
321 &param);
322 }
323
Ruchi Kandoi422852e2014-04-22 18:55:08 -0700324 prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : 0, tid);
325
San Mehat493dad92009-09-12 10:06:57 -0700326 return 0;
327}
Raphael0384a982009-09-15 17:10:17 -0700328
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700329#else
330
331/* Stubs for non-Android targets. */
332
Mark Salyzyn12717162014-04-29 15:49:14 -0700333int set_sched_policy(int tid UNUSED, SchedPolicy policy UNUSED)
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700334{
335 return 0;
336}
337
Mark Salyzyn12717162014-04-29 15:49:14 -0700338int get_sched_policy(int tid UNUSED, SchedPolicy *policy)
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700339{
340 *policy = SP_SYSTEM_DEFAULT;
341 return 0;
342}
343
344#endif
345
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800346const char *get_sched_policy_name(SchedPolicy policy)
347{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700348 policy = _policy(policy);
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800349 static const char * const strings[SP_CNT] = {
350 [SP_BACKGROUND] = "bg",
351 [SP_FOREGROUND] = "fg",
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700352 [SP_SYSTEM] = " ",
353 [SP_AUDIO_APP] = "aa",
354 [SP_AUDIO_SYS] = "as",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800355 };
356 if ((policy < SP_CNT) && (strings[policy] != NULL))
357 return strings[policy];
358 else
359 return "error";
360}
361