blob: fa0792fadd84db98cb9baf5499deb5895ebfb607 [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
Ruchi Kandoif4362cc2014-04-23 17:32:55 -070042#include <linux/prctl.h>
San Mehat493dad92009-09-12 10:06:57 -070043#include <sched.h>
Brad Fitzpatrick86b12152010-05-08 11:51:13 -070044#include <pthread.h>
San Mehat493dad92009-09-12 10:06:57 -070045
San Mehat3cd5b662009-09-14 16:05:24 -070046#ifndef SCHED_NORMAL
47 #define SCHED_NORMAL 0
48#endif
49
50#ifndef SCHED_BATCH
51 #define SCHED_BATCH 3
52#endif
53
San Mehat805d67a2009-10-29 13:56:26 -070054#define POLICY_DEBUG 0
San Mehatd2e4e462009-10-29 11:48:00 -070055
Glenn Kasten10ec3c72012-04-19 15:25:58 -070056#define CAN_SET_SP_SYSTEM 0 // non-zero means to implement set_sched_policy(tid, SP_SYSTEM)
57
Ruchi Kandoi422852e2014-04-22 18:55:08 -070058// timer slack value in nS enforced when the thread moves to background
59#define TIMER_SLACK_BG 40000000
60
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070061static pthread_once_t the_once = PTHREAD_ONCE_INIT;
62
San Mehatc0dfca72009-10-27 11:52:55 -070063static int __sys_supports_schedgroups = -1;
64
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070065// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070066static int bg_cgroup_fd = -1;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070067static int fg_cgroup_fd = -1;
68#if CAN_SET_SP_SYSTEM
69static int system_cgroup_fd = -1;
70#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070071
72/* Add tid to the scheduling group defined by the policy */
73static int add_tid_to_cgroup(int tid, SchedPolicy policy)
San Mehat493dad92009-09-12 10:06:57 -070074{
75 int fd;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070076
Glenn Kasten10ec3c72012-04-19 15:25:58 -070077 switch (policy) {
78 case SP_BACKGROUND:
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070079 fd = bg_cgroup_fd;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070080 break;
81 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -070082 case SP_AUDIO_APP:
83 case SP_AUDIO_SYS:
Glenn Kasten10ec3c72012-04-19 15:25:58 -070084 fd = fg_cgroup_fd;
85 break;
86#if CAN_SET_SP_SYSTEM
87 case SP_SYSTEM:
88 fd = system_cgroup_fd;
89 break;
90#endif
Glenn Kasten10ec3c72012-04-19 15:25:58 -070091 default:
92 fd = -1;
93 break;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070094 }
95
96 if (fd < 0) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -070097 SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
San Mehat493dad92009-09-12 10:06:57 -070098 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070099 }
San Mehat493dad92009-09-12 10:06:57 -0700100
Brad Fitzpatrick253e27a2010-05-06 10:00:37 -0700101 // specialized itoa -- works for tid > 0
102 char text[22];
103 char *end = text + sizeof(text) - 1;
104 char *ptr = end;
105 *ptr = '\0';
106 while (tid > 0) {
107 *--ptr = '0' + (tid % 10);
108 tid = tid / 10;
109 }
110
111 if (write(fd, ptr, end - ptr) < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700112 /*
113 * If the thread is in the process of exiting,
114 * don't flag an error
115 */
116 if (errno == ESRCH)
117 return 0;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700118 SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
119 ptr, strerror(errno), policy);
San Mehat493dad92009-09-12 10:06:57 -0700120 return -1;
121 }
122
San Mehat493dad92009-09-12 10:06:57 -0700123 return 0;
124}
125
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700126static void __initialize(void) {
127 char* filename;
128 if (!access("/dev/cpuctl/tasks", F_OK)) {
129 __sys_supports_schedgroups = 1;
130
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700131#if CAN_SET_SP_SYSTEM
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700132 filename = "/dev/cpuctl/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700133 system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700134 if (system_cgroup_fd < 0) {
135 SLOGV("open of %s failed: %s\n", filename, strerror(errno));
136 }
137#endif
138
Dima Zavin13ed76b2012-06-04 10:42:38 -0700139 filename = "/dev/cpuctl/apps/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700140 fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700141 if (fg_cgroup_fd < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700142 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -0700143 }
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700144
Dima Zavin13ed76b2012-06-04 10:42:38 -0700145 filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700146 bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700147 if (bg_cgroup_fd < 0) {
148 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
149 }
150 } else {
151 __sys_supports_schedgroups = 0;
San Mehat493dad92009-09-12 10:06:57 -0700152 }
San Mehatc0dfca72009-10-27 11:52:55 -0700153}
154
155/*
156 * Try to get the scheduler group.
157 *
San Mehat503df202010-03-02 17:09:56 -0800158 * The data from /proc/<pid>/cgroup looks (something) like:
San Mehatc0dfca72009-10-27 11:52:55 -0700159 * 2:cpu:/bg_non_interactive
San Mehat503df202010-03-02 17:09:56 -0800160 * 1:cpuacct:/
San Mehatc0dfca72009-10-27 11:52:55 -0700161 *
162 * We return the part after the "/", which will be an empty string for
163 * the default cgroup. If the string is longer than "bufLen", the string
164 * will be truncated.
165 */
166static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
167{
168#ifdef HAVE_ANDROID_OS
169 char pathBuf[32];
San Mehat503df202010-03-02 17:09:56 -0800170 char lineBuf[256];
171 FILE *fp;
San Mehatc0dfca72009-10-27 11:52:55 -0700172
173 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
San Mehat503df202010-03-02 17:09:56 -0800174 if (!(fp = fopen(pathBuf, "r"))) {
San Mehatc0dfca72009-10-27 11:52:55 -0700175 return -1;
176 }
177
San Mehat503df202010-03-02 17:09:56 -0800178 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
179 char *next = lineBuf;
180 char *subsys;
181 char *grp;
182 size_t len;
San Mehatc0dfca72009-10-27 11:52:55 -0700183
San Mehat503df202010-03-02 17:09:56 -0800184 /* Junk the first field */
185 if (!strsep(&next, ":")) {
186 goto out_bad_data;
187 }
San Mehatc0dfca72009-10-27 11:52:55 -0700188
San Mehat503df202010-03-02 17:09:56 -0800189 if (!(subsys = strsep(&next, ":"))) {
190 goto out_bad_data;
191 }
192
193 if (strcmp(subsys, "cpu")) {
194 /* Not the subsys we're looking for */
195 continue;
196 }
197
198 if (!(grp = strsep(&next, ":"))) {
199 goto out_bad_data;
200 }
201 grp++; /* Drop the leading '/' */
202 len = strlen(grp);
203 grp[len-1] = '\0'; /* Drop the trailing '\n' */
204
205 if (bufLen <= len) {
206 len = bufLen - 1;
207 }
208 strncpy(buf, grp, len);
209 buf[len] = '\0';
210 fclose(fp);
211 return 0;
San Mehatc0dfca72009-10-27 11:52:55 -0700212 }
213
San Mehat7e8529a2010-03-25 09:31:42 -0700214 SLOGE("Failed to find cpu subsys");
San Mehat503df202010-03-02 17:09:56 -0800215 fclose(fp);
216 return -1;
217 out_bad_data:
San Mehat7e8529a2010-03-25 09:31:42 -0700218 SLOGE("Bad cgroup data {%s}", lineBuf);
San Mehat503df202010-03-02 17:09:56 -0800219 fclose(fp);
220 return -1;
San Mehatc0dfca72009-10-27 11:52:55 -0700221#else
222 errno = ENOSYS;
223 return -1;
224#endif
225}
226
227int get_sched_policy(int tid, SchedPolicy *policy)
228{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700229#ifdef HAVE_GETTID
230 if (tid == 0) {
231 tid = gettid();
232 }
233#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700234 pthread_once(&the_once, __initialize);
San Mehatc0dfca72009-10-27 11:52:55 -0700235
236 if (__sys_supports_schedgroups) {
237 char grpBuf[32];
238 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
239 return -1;
240 if (grpBuf[0] == '\0') {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700241 *policy = SP_SYSTEM;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700242 } else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
San Mehatc0dfca72009-10-27 11:52:55 -0700243 *policy = SP_BACKGROUND;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700244 } else if (!strcmp(grpBuf, "apps")) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700245 *policy = SP_FOREGROUND;
San Mehatc0dfca72009-10-27 11:52:55 -0700246 } else {
247 errno = ERANGE;
248 return -1;
249 }
250 } else {
251 int rc = sched_getscheduler(tid);
252 if (rc < 0)
253 return -1;
254 else if (rc == SCHED_NORMAL)
255 *policy = SP_FOREGROUND;
256 else if (rc == SCHED_BATCH)
257 *policy = SP_BACKGROUND;
258 else {
259 errno = ERANGE;
260 return -1;
261 }
262 }
263 return 0;
264}
265
266int set_sched_policy(int tid, SchedPolicy policy)
267{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700268#ifdef HAVE_GETTID
269 if (tid == 0) {
270 tid = gettid();
271 }
272#endif
273 policy = _policy(policy);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700274 pthread_once(&the_once, __initialize);
San Mehat493dad92009-09-12 10:06:57 -0700275
San Mehatd2e4e462009-10-29 11:48:00 -0700276#if POLICY_DEBUG
277 char statfile[64];
278 char statline[1024];
279 char thread_name[255];
280 int fd;
281
282 sprintf(statfile, "/proc/%d/stat", tid);
283 memset(thread_name, 0, sizeof(thread_name));
284
285 fd = open(statfile, O_RDONLY);
286 if (fd >= 0) {
287 int rc = read(fd, statline, 1023);
288 close(fd);
289 statline[rc] = 0;
290 char *p = statline;
291 char *q;
292
293 for (p = statline; *p != '('; p++);
294 p++;
295 for (q = p; *q != ')'; q++);
296
297 strncpy(thread_name, p, (q-p));
298 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700299 switch (policy) {
300 case SP_BACKGROUND:
San Mehat7e8529a2010-03-25 09:31:42 -0700301 SLOGD("vvv tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700302 break;
303 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -0700304 case SP_AUDIO_APP:
305 case SP_AUDIO_SYS:
San Mehat7e8529a2010-03-25 09:31:42 -0700306 SLOGD("^^^ tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700307 break;
308 case SP_SYSTEM:
309 SLOGD("/// tid %d (%s)", tid, thread_name);
310 break;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700311 default:
San Mehat7e8529a2010-03-25 09:31:42 -0700312 SLOGD("??? tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700313 break;
San Mehatd2e4e462009-10-29 11:48:00 -0700314 }
315#endif
316
San Mehat493dad92009-09-12 10:06:57 -0700317 if (__sys_supports_schedgroups) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700318 if (add_tid_to_cgroup(tid, policy)) {
San Mehat493dad92009-09-12 10:06:57 -0700319 if (errno != ESRCH && errno != ENOENT)
320 return -errno;
321 }
322 } else {
323 struct sched_param param;
324
325 param.sched_priority = 0;
326 sched_setscheduler(tid,
327 (policy == SP_BACKGROUND) ?
328 SCHED_BATCH : SCHED_NORMAL,
329 &param);
330 }
331
Ruchi Kandoi422852e2014-04-22 18:55:08 -0700332 prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : 0, tid);
333
San Mehat493dad92009-09-12 10:06:57 -0700334 return 0;
335}
Raphael0384a982009-09-15 17:10:17 -0700336
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700337#else
338
339/* Stubs for non-Android targets. */
340
Mark Salyzyn12717162014-04-29 15:49:14 -0700341int set_sched_policy(int tid UNUSED, SchedPolicy policy UNUSED)
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700342{
343 return 0;
344}
345
Mark Salyzyn12717162014-04-29 15:49:14 -0700346int get_sched_policy(int tid UNUSED, SchedPolicy *policy)
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700347{
348 *policy = SP_SYSTEM_DEFAULT;
349 return 0;
350}
351
352#endif
353
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800354const char *get_sched_policy_name(SchedPolicy policy)
355{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700356 policy = _policy(policy);
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800357 static const char * const strings[SP_CNT] = {
358 [SP_BACKGROUND] = "bg",
359 [SP_FOREGROUND] = "fg",
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700360 [SP_SYSTEM] = " ",
361 [SP_AUDIO_APP] = "aa",
362 [SP_AUDIO_SYS] = "as",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800363 };
364 if ((policy < SP_CNT) && (strings[policy] != NULL))
365 return strings[policy];
366 else
367 return "error";
368}
369