blob: d20d217b7f955e70cbee9c54a8ce5592aebc1107 [file] [log] [blame]
Raphael0384a982009-09-15 17:10:17 -07001
San Mehat493dad92009-09-12 10:06:57 -07002/* libs/cutils/sched_policy.c
3**
4** Copyright 2007, The Android Open Source Project
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
Jeff Brownbff8f3f2012-05-08 15:05:42 -070019#define LOG_TAG "SchedPolicy"
20
San Mehat493dad92009-09-12 10:06:57 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <string.h>
25#include <errno.h>
26#include <fcntl.h>
Jeff Brownbff8f3f2012-05-08 15:05:42 -070027#include <cutils/sched_policy.h>
28#include <cutils/log.h>
Raphael0384a982009-09-15 17:10:17 -070029
Jeff Brownbff8f3f2012-05-08 15:05:42 -070030/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
31 * Call this any place a SchedPolicy is used as an input parameter.
32 * Returns the possibly re-mapped policy.
33 */
34static inline SchedPolicy _policy(SchedPolicy p)
35{
36 return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
37}
San Mehatd2e4e462009-10-29 11:48:00 -070038
Jeff Brownbff8f3f2012-05-08 15:05:42 -070039#if defined(HAVE_ANDROID_OS) && defined(HAVE_SCHED_H) && defined(HAVE_PTHREADS)
Raphael0384a982009-09-15 17:10:17 -070040
San Mehat493dad92009-09-12 10:06:57 -070041#include <sched.h>
Brad Fitzpatrick86b12152010-05-08 11:51:13 -070042#include <pthread.h>
San Mehat493dad92009-09-12 10:06:57 -070043
San Mehat3cd5b662009-09-14 16:05:24 -070044#ifndef SCHED_NORMAL
45 #define SCHED_NORMAL 0
46#endif
47
48#ifndef SCHED_BATCH
49 #define SCHED_BATCH 3
50#endif
51
San Mehat805d67a2009-10-29 13:56:26 -070052#define POLICY_DEBUG 0
San Mehatd2e4e462009-10-29 11:48:00 -070053
Glenn Kasten10ec3c72012-04-19 15:25:58 -070054#define CAN_SET_SP_SYSTEM 0 // non-zero means to implement set_sched_policy(tid, SP_SYSTEM)
55
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070056static pthread_once_t the_once = PTHREAD_ONCE_INIT;
57
San Mehatc0dfca72009-10-27 11:52:55 -070058static int __sys_supports_schedgroups = -1;
59
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070060// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070061static int bg_cgroup_fd = -1;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070062static int fg_cgroup_fd = -1;
63#if CAN_SET_SP_SYSTEM
64static int system_cgroup_fd = -1;
65#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070066
67/* Add tid to the scheduling group defined by the policy */
68static int add_tid_to_cgroup(int tid, SchedPolicy policy)
San Mehat493dad92009-09-12 10:06:57 -070069{
70 int fd;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070071
Glenn Kasten10ec3c72012-04-19 15:25:58 -070072 switch (policy) {
73 case SP_BACKGROUND:
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070074 fd = bg_cgroup_fd;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070075 break;
76 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -070077 case SP_AUDIO_APP:
78 case SP_AUDIO_SYS:
Glenn Kasten10ec3c72012-04-19 15:25:58 -070079 fd = fg_cgroup_fd;
80 break;
81#if CAN_SET_SP_SYSTEM
82 case SP_SYSTEM:
83 fd = system_cgroup_fd;
84 break;
85#endif
Glenn Kasten10ec3c72012-04-19 15:25:58 -070086 default:
87 fd = -1;
88 break;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070089 }
90
91 if (fd < 0) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -070092 SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
San Mehat493dad92009-09-12 10:06:57 -070093 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070094 }
San Mehat493dad92009-09-12 10:06:57 -070095
Brad Fitzpatrick253e27a2010-05-06 10:00:37 -070096 // specialized itoa -- works for tid > 0
97 char text[22];
98 char *end = text + sizeof(text) - 1;
99 char *ptr = end;
100 *ptr = '\0';
101 while (tid > 0) {
102 *--ptr = '0' + (tid % 10);
103 tid = tid / 10;
104 }
105
106 if (write(fd, ptr, end - ptr) < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700107 /*
108 * If the thread is in the process of exiting,
109 * don't flag an error
110 */
111 if (errno == ESRCH)
112 return 0;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700113 SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
114 ptr, strerror(errno), policy);
San Mehat493dad92009-09-12 10:06:57 -0700115 return -1;
116 }
117
San Mehat493dad92009-09-12 10:06:57 -0700118 return 0;
119}
120
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700121static void __initialize(void) {
122 char* filename;
123 if (!access("/dev/cpuctl/tasks", F_OK)) {
124 __sys_supports_schedgroups = 1;
125
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700126#if CAN_SET_SP_SYSTEM
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700127 filename = "/dev/cpuctl/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700128 system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700129 if (system_cgroup_fd < 0) {
130 SLOGV("open of %s failed: %s\n", filename, strerror(errno));
131 }
132#endif
133
Dima Zavin13ed76b2012-06-04 10:42:38 -0700134 filename = "/dev/cpuctl/apps/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700135 fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700136 if (fg_cgroup_fd < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700137 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -0700138 }
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700139
Dima Zavin13ed76b2012-06-04 10:42:38 -0700140 filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700141 bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700142 if (bg_cgroup_fd < 0) {
143 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
144 }
145 } else {
146 __sys_supports_schedgroups = 0;
San Mehat493dad92009-09-12 10:06:57 -0700147 }
San Mehatc0dfca72009-10-27 11:52:55 -0700148}
149
150/*
151 * Try to get the scheduler group.
152 *
San Mehat503df202010-03-02 17:09:56 -0800153 * The data from /proc/<pid>/cgroup looks (something) like:
San Mehatc0dfca72009-10-27 11:52:55 -0700154 * 2:cpu:/bg_non_interactive
San Mehat503df202010-03-02 17:09:56 -0800155 * 1:cpuacct:/
San Mehatc0dfca72009-10-27 11:52:55 -0700156 *
157 * We return the part after the "/", which will be an empty string for
158 * the default cgroup. If the string is longer than "bufLen", the string
159 * will be truncated.
160 */
161static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
162{
163#ifdef HAVE_ANDROID_OS
164 char pathBuf[32];
San Mehat503df202010-03-02 17:09:56 -0800165 char lineBuf[256];
166 FILE *fp;
San Mehatc0dfca72009-10-27 11:52:55 -0700167
168 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
San Mehat503df202010-03-02 17:09:56 -0800169 if (!(fp = fopen(pathBuf, "r"))) {
San Mehatc0dfca72009-10-27 11:52:55 -0700170 return -1;
171 }
172
San Mehat503df202010-03-02 17:09:56 -0800173 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
174 char *next = lineBuf;
175 char *subsys;
176 char *grp;
177 size_t len;
San Mehatc0dfca72009-10-27 11:52:55 -0700178
San Mehat503df202010-03-02 17:09:56 -0800179 /* Junk the first field */
180 if (!strsep(&next, ":")) {
181 goto out_bad_data;
182 }
San Mehatc0dfca72009-10-27 11:52:55 -0700183
San Mehat503df202010-03-02 17:09:56 -0800184 if (!(subsys = strsep(&next, ":"))) {
185 goto out_bad_data;
186 }
187
188 if (strcmp(subsys, "cpu")) {
189 /* Not the subsys we're looking for */
190 continue;
191 }
192
193 if (!(grp = strsep(&next, ":"))) {
194 goto out_bad_data;
195 }
196 grp++; /* Drop the leading '/' */
197 len = strlen(grp);
198 grp[len-1] = '\0'; /* Drop the trailing '\n' */
199
200 if (bufLen <= len) {
201 len = bufLen - 1;
202 }
203 strncpy(buf, grp, len);
204 buf[len] = '\0';
205 fclose(fp);
206 return 0;
San Mehatc0dfca72009-10-27 11:52:55 -0700207 }
208
San Mehat7e8529a2010-03-25 09:31:42 -0700209 SLOGE("Failed to find cpu subsys");
San Mehat503df202010-03-02 17:09:56 -0800210 fclose(fp);
211 return -1;
212 out_bad_data:
San Mehat7e8529a2010-03-25 09:31:42 -0700213 SLOGE("Bad cgroup data {%s}", lineBuf);
San Mehat503df202010-03-02 17:09:56 -0800214 fclose(fp);
215 return -1;
San Mehatc0dfca72009-10-27 11:52:55 -0700216#else
217 errno = ENOSYS;
218 return -1;
219#endif
220}
221
222int get_sched_policy(int tid, SchedPolicy *policy)
223{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700224#ifdef HAVE_GETTID
225 if (tid == 0) {
226 tid = gettid();
227 }
228#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700229 pthread_once(&the_once, __initialize);
San Mehatc0dfca72009-10-27 11:52:55 -0700230
231 if (__sys_supports_schedgroups) {
232 char grpBuf[32];
233 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
234 return -1;
235 if (grpBuf[0] == '\0') {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700236 *policy = SP_SYSTEM;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700237 } else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
San Mehatc0dfca72009-10-27 11:52:55 -0700238 *policy = SP_BACKGROUND;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700239 } else if (!strcmp(grpBuf, "apps")) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700240 *policy = SP_FOREGROUND;
San Mehatc0dfca72009-10-27 11:52:55 -0700241 } else {
242 errno = ERANGE;
243 return -1;
244 }
245 } else {
246 int rc = sched_getscheduler(tid);
247 if (rc < 0)
248 return -1;
249 else if (rc == SCHED_NORMAL)
250 *policy = SP_FOREGROUND;
251 else if (rc == SCHED_BATCH)
252 *policy = SP_BACKGROUND;
253 else {
254 errno = ERANGE;
255 return -1;
256 }
257 }
258 return 0;
259}
260
261int set_sched_policy(int tid, SchedPolicy policy)
262{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700263#ifdef HAVE_GETTID
264 if (tid == 0) {
265 tid = gettid();
266 }
267#endif
268 policy = _policy(policy);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700269 pthread_once(&the_once, __initialize);
San Mehat493dad92009-09-12 10:06:57 -0700270
San Mehatd2e4e462009-10-29 11:48:00 -0700271#if POLICY_DEBUG
272 char statfile[64];
273 char statline[1024];
274 char thread_name[255];
275 int fd;
276
277 sprintf(statfile, "/proc/%d/stat", tid);
278 memset(thread_name, 0, sizeof(thread_name));
279
280 fd = open(statfile, O_RDONLY);
281 if (fd >= 0) {
282 int rc = read(fd, statline, 1023);
283 close(fd);
284 statline[rc] = 0;
285 char *p = statline;
286 char *q;
287
288 for (p = statline; *p != '('; p++);
289 p++;
290 for (q = p; *q != ')'; q++);
291
292 strncpy(thread_name, p, (q-p));
293 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700294 switch (policy) {
295 case SP_BACKGROUND:
San Mehat7e8529a2010-03-25 09:31:42 -0700296 SLOGD("vvv tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700297 break;
298 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -0700299 case SP_AUDIO_APP:
300 case SP_AUDIO_SYS:
San Mehat7e8529a2010-03-25 09:31:42 -0700301 SLOGD("^^^ tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700302 break;
303 case SP_SYSTEM:
304 SLOGD("/// tid %d (%s)", tid, thread_name);
305 break;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700306 default:
San Mehat7e8529a2010-03-25 09:31:42 -0700307 SLOGD("??? tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700308 break;
San Mehatd2e4e462009-10-29 11:48:00 -0700309 }
310#endif
311
San Mehat493dad92009-09-12 10:06:57 -0700312 if (__sys_supports_schedgroups) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700313 if (add_tid_to_cgroup(tid, policy)) {
San Mehat493dad92009-09-12 10:06:57 -0700314 if (errno != ESRCH && errno != ENOENT)
315 return -errno;
316 }
317 } else {
318 struct sched_param param;
319
320 param.sched_priority = 0;
321 sched_setscheduler(tid,
322 (policy == SP_BACKGROUND) ?
323 SCHED_BATCH : SCHED_NORMAL,
324 &param);
325 }
326
327 return 0;
328}
Raphael0384a982009-09-15 17:10:17 -0700329
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700330#else
331
332/* Stubs for non-Android targets. */
333
334int set_sched_policy(int tid, SchedPolicy policy)
335{
336 return 0;
337}
338
339int get_sched_policy(int tid, SchedPolicy *policy)
340{
341 *policy = SP_SYSTEM_DEFAULT;
342 return 0;
343}
344
345#endif
346
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800347const char *get_sched_policy_name(SchedPolicy policy)
348{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700349 policy = _policy(policy);
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800350 static const char * const strings[SP_CNT] = {
351 [SP_BACKGROUND] = "bg",
352 [SP_FOREGROUND] = "fg",
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700353 [SP_SYSTEM] = " ",
354 [SP_AUDIO_APP] = "aa",
355 [SP_AUDIO_SYS] = "as",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800356 };
357 if ((policy < SP_CNT) && (strings[policy] != NULL))
358 return strings[policy];
359 else
360 return "error";
361}
362