blob: bc49e88419ebdc5b543523c6d097ea16a835b59d [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>
Ruchi Kandoi422852e2014-04-22 18:55:08 -070029#include <linux/prctl.h>
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
San Mehat493dad92009-09-12 10:06:57 -070042#include <sched.h>
Brad Fitzpatrick86b12152010-05-08 11:51:13 -070043#include <pthread.h>
San Mehat493dad92009-09-12 10:06:57 -070044
San Mehat3cd5b662009-09-14 16:05:24 -070045#ifndef SCHED_NORMAL
46 #define SCHED_NORMAL 0
47#endif
48
49#ifndef SCHED_BATCH
50 #define SCHED_BATCH 3
51#endif
52
San Mehat805d67a2009-10-29 13:56:26 -070053#define POLICY_DEBUG 0
San Mehatd2e4e462009-10-29 11:48:00 -070054
Glenn Kasten10ec3c72012-04-19 15:25:58 -070055#define CAN_SET_SP_SYSTEM 0 // non-zero means to implement set_sched_policy(tid, SP_SYSTEM)
56
Ruchi Kandoi422852e2014-04-22 18:55:08 -070057// timer slack value in nS enforced when the thread moves to background
58#define TIMER_SLACK_BG 40000000
59
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070060static pthread_once_t the_once = PTHREAD_ONCE_INIT;
61
San Mehatc0dfca72009-10-27 11:52:55 -070062static int __sys_supports_schedgroups = -1;
63
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070064// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070065static int bg_cgroup_fd = -1;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070066static int fg_cgroup_fd = -1;
67#if CAN_SET_SP_SYSTEM
68static int system_cgroup_fd = -1;
69#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070070
71/* Add tid to the scheduling group defined by the policy */
72static int add_tid_to_cgroup(int tid, SchedPolicy policy)
San Mehat493dad92009-09-12 10:06:57 -070073{
74 int fd;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070075
Glenn Kasten10ec3c72012-04-19 15:25:58 -070076 switch (policy) {
77 case SP_BACKGROUND:
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070078 fd = bg_cgroup_fd;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070079 break;
80 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -070081 case SP_AUDIO_APP:
82 case SP_AUDIO_SYS:
Glenn Kasten10ec3c72012-04-19 15:25:58 -070083 fd = fg_cgroup_fd;
84 break;
85#if CAN_SET_SP_SYSTEM
86 case SP_SYSTEM:
87 fd = system_cgroup_fd;
88 break;
89#endif
Glenn Kasten10ec3c72012-04-19 15:25:58 -070090 default:
91 fd = -1;
92 break;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070093 }
94
95 if (fd < 0) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -070096 SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
San Mehat493dad92009-09-12 10:06:57 -070097 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070098 }
San Mehat493dad92009-09-12 10:06:57 -070099
Brad Fitzpatrick253e27a2010-05-06 10:00:37 -0700100 // specialized itoa -- works for tid > 0
101 char text[22];
102 char *end = text + sizeof(text) - 1;
103 char *ptr = end;
104 *ptr = '\0';
105 while (tid > 0) {
106 *--ptr = '0' + (tid % 10);
107 tid = tid / 10;
108 }
109
110 if (write(fd, ptr, end - ptr) < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700111 /*
112 * If the thread is in the process of exiting,
113 * don't flag an error
114 */
115 if (errno == ESRCH)
116 return 0;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700117 SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
118 ptr, strerror(errno), policy);
San Mehat493dad92009-09-12 10:06:57 -0700119 return -1;
120 }
121
San Mehat493dad92009-09-12 10:06:57 -0700122 return 0;
123}
124
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700125static void __initialize(void) {
126 char* filename;
127 if (!access("/dev/cpuctl/tasks", F_OK)) {
128 __sys_supports_schedgroups = 1;
129
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700130#if CAN_SET_SP_SYSTEM
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700131 filename = "/dev/cpuctl/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700132 system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700133 if (system_cgroup_fd < 0) {
134 SLOGV("open of %s failed: %s\n", filename, strerror(errno));
135 }
136#endif
137
Dima Zavin13ed76b2012-06-04 10:42:38 -0700138 filename = "/dev/cpuctl/apps/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700139 fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700140 if (fg_cgroup_fd < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700141 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -0700142 }
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700143
Dima Zavin13ed76b2012-06-04 10:42:38 -0700144 filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700145 bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700146 if (bg_cgroup_fd < 0) {
147 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
148 }
149 } else {
150 __sys_supports_schedgroups = 0;
San Mehat493dad92009-09-12 10:06:57 -0700151 }
San Mehatc0dfca72009-10-27 11:52:55 -0700152}
153
154/*
155 * Try to get the scheduler group.
156 *
San Mehat503df202010-03-02 17:09:56 -0800157 * The data from /proc/<pid>/cgroup looks (something) like:
San Mehatc0dfca72009-10-27 11:52:55 -0700158 * 2:cpu:/bg_non_interactive
San Mehat503df202010-03-02 17:09:56 -0800159 * 1:cpuacct:/
San Mehatc0dfca72009-10-27 11:52:55 -0700160 *
161 * We return the part after the "/", which will be an empty string for
162 * the default cgroup. If the string is longer than "bufLen", the string
163 * will be truncated.
164 */
165static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
166{
167#ifdef HAVE_ANDROID_OS
168 char pathBuf[32];
San Mehat503df202010-03-02 17:09:56 -0800169 char lineBuf[256];
170 FILE *fp;
San Mehatc0dfca72009-10-27 11:52:55 -0700171
172 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
San Mehat503df202010-03-02 17:09:56 -0800173 if (!(fp = fopen(pathBuf, "r"))) {
San Mehatc0dfca72009-10-27 11:52:55 -0700174 return -1;
175 }
176
San Mehat503df202010-03-02 17:09:56 -0800177 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
178 char *next = lineBuf;
179 char *subsys;
180 char *grp;
181 size_t len;
San Mehatc0dfca72009-10-27 11:52:55 -0700182
San Mehat503df202010-03-02 17:09:56 -0800183 /* Junk the first field */
184 if (!strsep(&next, ":")) {
185 goto out_bad_data;
186 }
San Mehatc0dfca72009-10-27 11:52:55 -0700187
San Mehat503df202010-03-02 17:09:56 -0800188 if (!(subsys = strsep(&next, ":"))) {
189 goto out_bad_data;
190 }
191
192 if (strcmp(subsys, "cpu")) {
193 /* Not the subsys we're looking for */
194 continue;
195 }
196
197 if (!(grp = strsep(&next, ":"))) {
198 goto out_bad_data;
199 }
200 grp++; /* Drop the leading '/' */
201 len = strlen(grp);
202 grp[len-1] = '\0'; /* Drop the trailing '\n' */
203
204 if (bufLen <= len) {
205 len = bufLen - 1;
206 }
207 strncpy(buf, grp, len);
208 buf[len] = '\0';
209 fclose(fp);
210 return 0;
San Mehatc0dfca72009-10-27 11:52:55 -0700211 }
212
San Mehat7e8529a2010-03-25 09:31:42 -0700213 SLOGE("Failed to find cpu subsys");
San Mehat503df202010-03-02 17:09:56 -0800214 fclose(fp);
215 return -1;
216 out_bad_data:
San Mehat7e8529a2010-03-25 09:31:42 -0700217 SLOGE("Bad cgroup data {%s}", lineBuf);
San Mehat503df202010-03-02 17:09:56 -0800218 fclose(fp);
219 return -1;
San Mehatc0dfca72009-10-27 11:52:55 -0700220#else
221 errno = ENOSYS;
222 return -1;
223#endif
224}
225
226int get_sched_policy(int tid, SchedPolicy *policy)
227{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700228#ifdef HAVE_GETTID
229 if (tid == 0) {
230 tid = gettid();
231 }
232#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700233 pthread_once(&the_once, __initialize);
San Mehatc0dfca72009-10-27 11:52:55 -0700234
235 if (__sys_supports_schedgroups) {
236 char grpBuf[32];
237 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
238 return -1;
239 if (grpBuf[0] == '\0') {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700240 *policy = SP_SYSTEM;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700241 } else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
San Mehatc0dfca72009-10-27 11:52:55 -0700242 *policy = SP_BACKGROUND;
Dima Zavin13ed76b2012-06-04 10:42:38 -0700243 } else if (!strcmp(grpBuf, "apps")) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700244 *policy = SP_FOREGROUND;
San Mehatc0dfca72009-10-27 11:52:55 -0700245 } else {
246 errno = ERANGE;
247 return -1;
248 }
249 } else {
250 int rc = sched_getscheduler(tid);
251 if (rc < 0)
252 return -1;
253 else if (rc == SCHED_NORMAL)
254 *policy = SP_FOREGROUND;
255 else if (rc == SCHED_BATCH)
256 *policy = SP_BACKGROUND;
257 else {
258 errno = ERANGE;
259 return -1;
260 }
261 }
262 return 0;
263}
264
265int set_sched_policy(int tid, SchedPolicy policy)
266{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700267#ifdef HAVE_GETTID
268 if (tid == 0) {
269 tid = gettid();
270 }
271#endif
272 policy = _policy(policy);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700273 pthread_once(&the_once, __initialize);
San Mehat493dad92009-09-12 10:06:57 -0700274
San Mehatd2e4e462009-10-29 11:48:00 -0700275#if POLICY_DEBUG
276 char statfile[64];
277 char statline[1024];
278 char thread_name[255];
279 int fd;
280
281 sprintf(statfile, "/proc/%d/stat", tid);
282 memset(thread_name, 0, sizeof(thread_name));
283
284 fd = open(statfile, O_RDONLY);
285 if (fd >= 0) {
286 int rc = read(fd, statline, 1023);
287 close(fd);
288 statline[rc] = 0;
289 char *p = statline;
290 char *q;
291
292 for (p = statline; *p != '('; p++);
293 p++;
294 for (q = p; *q != ')'; q++);
295
296 strncpy(thread_name, p, (q-p));
297 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700298 switch (policy) {
299 case SP_BACKGROUND:
San Mehat7e8529a2010-03-25 09:31:42 -0700300 SLOGD("vvv tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700301 break;
302 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -0700303 case SP_AUDIO_APP:
304 case SP_AUDIO_SYS:
San Mehat7e8529a2010-03-25 09:31:42 -0700305 SLOGD("^^^ tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700306 break;
307 case SP_SYSTEM:
308 SLOGD("/// tid %d (%s)", tid, thread_name);
309 break;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700310 default:
San Mehat7e8529a2010-03-25 09:31:42 -0700311 SLOGD("??? tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700312 break;
San Mehatd2e4e462009-10-29 11:48:00 -0700313 }
314#endif
315
San Mehat493dad92009-09-12 10:06:57 -0700316 if (__sys_supports_schedgroups) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700317 if (add_tid_to_cgroup(tid, policy)) {
San Mehat493dad92009-09-12 10:06:57 -0700318 if (errno != ESRCH && errno != ENOENT)
319 return -errno;
320 }
321 } else {
322 struct sched_param param;
323
324 param.sched_priority = 0;
325 sched_setscheduler(tid,
326 (policy == SP_BACKGROUND) ?
327 SCHED_BATCH : SCHED_NORMAL,
328 &param);
329 }
330
Ruchi Kandoi422852e2014-04-22 18:55:08 -0700331 prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : 0, tid);
332
San Mehat493dad92009-09-12 10:06:57 -0700333 return 0;
334}
Raphael0384a982009-09-15 17:10:17 -0700335
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700336#else
337
338/* Stubs for non-Android targets. */
339
340int set_sched_policy(int tid, SchedPolicy policy)
341{
342 return 0;
343}
344
345int get_sched_policy(int tid, SchedPolicy *policy)
346{
347 *policy = SP_SYSTEM_DEFAULT;
348 return 0;
349}
350
351#endif
352
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800353const char *get_sched_policy_name(SchedPolicy policy)
354{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700355 policy = _policy(policy);
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800356 static const char * const strings[SP_CNT] = {
357 [SP_BACKGROUND] = "bg",
358 [SP_FOREGROUND] = "fg",
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700359 [SP_SYSTEM] = " ",
360 [SP_AUDIO_APP] = "aa",
361 [SP_AUDIO_SYS] = "as",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800362 };
363 if ((policy < SP_CNT) && (strings[policy] != NULL))
364 return strings[policy];
365 else
366 return "error";
367}
368