blob: 493511e57f5d7587c75ed2edaeacd0ed126ddc57 [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
Elliott Hughes400c3812014-07-17 17:15:14 -070048// This prctl is only available in Android kernels.
49#define PR_SET_TIMERSLACK_PID 41
50
Ruchi Kandoi422852e2014-04-22 18:55:08 -070051// timer slack value in nS enforced when the thread moves to background
52#define TIMER_SLACK_BG 40000000
53
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070054static pthread_once_t the_once = PTHREAD_ONCE_INIT;
55
San Mehatc0dfca72009-10-27 11:52:55 -070056static int __sys_supports_schedgroups = -1;
57
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070058// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070059static int bg_cgroup_fd = -1;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070060static int fg_cgroup_fd = -1;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070061
62/* Add tid to the scheduling group defined by the policy */
63static int add_tid_to_cgroup(int tid, SchedPolicy policy)
San Mehat493dad92009-09-12 10:06:57 -070064{
65 int fd;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070066
Glenn Kasten10ec3c72012-04-19 15:25:58 -070067 switch (policy) {
68 case SP_BACKGROUND:
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070069 fd = bg_cgroup_fd;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070070 break;
71 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -070072 case SP_AUDIO_APP:
73 case SP_AUDIO_SYS:
Glenn Kasten10ec3c72012-04-19 15:25:58 -070074 fd = fg_cgroup_fd;
75 break;
Glenn Kasten10ec3c72012-04-19 15:25:58 -070076 default:
77 fd = -1;
78 break;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070079 }
80
81 if (fd < 0) {
Glenn Kasten10ec3c72012-04-19 15:25:58 -070082 SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
San Mehat493dad92009-09-12 10:06:57 -070083 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070084 }
San Mehat493dad92009-09-12 10:06:57 -070085
Brad Fitzpatrick253e27a2010-05-06 10:00:37 -070086 // specialized itoa -- works for tid > 0
87 char text[22];
88 char *end = text + sizeof(text) - 1;
89 char *ptr = end;
90 *ptr = '\0';
91 while (tid > 0) {
92 *--ptr = '0' + (tid % 10);
93 tid = tid / 10;
94 }
95
96 if (write(fd, ptr, end - ptr) < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070097 /*
98 * If the thread is in the process of exiting,
99 * don't flag an error
100 */
101 if (errno == ESRCH)
102 return 0;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700103 SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
104 ptr, strerror(errno), policy);
San Mehat493dad92009-09-12 10:06:57 -0700105 return -1;
106 }
107
San Mehat493dad92009-09-12 10:06:57 -0700108 return 0;
109}
110
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700111static void __initialize(void) {
112 char* filename;
113 if (!access("/dev/cpuctl/tasks", F_OK)) {
114 __sys_supports_schedgroups = 1;
115
116 filename = "/dev/cpuctl/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700117 fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700118 if (fg_cgroup_fd < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700119 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -0700120 }
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700121
Riley Andrews522d72b2014-10-03 17:02:53 -0700122 filename = "/dev/cpuctl/bg_non_interactive/tasks";
Jeff Brown5b878d22012-05-08 15:07:58 -0700123 bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700124 if (bg_cgroup_fd < 0) {
125 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
126 }
127 } else {
128 __sys_supports_schedgroups = 0;
San Mehat493dad92009-09-12 10:06:57 -0700129 }
San Mehatc0dfca72009-10-27 11:52:55 -0700130}
131
132/*
133 * Try to get the scheduler group.
134 *
San Mehat503df202010-03-02 17:09:56 -0800135 * The data from /proc/<pid>/cgroup looks (something) like:
San Mehatc0dfca72009-10-27 11:52:55 -0700136 * 2:cpu:/bg_non_interactive
San Mehat503df202010-03-02 17:09:56 -0800137 * 1:cpuacct:/
San Mehatc0dfca72009-10-27 11:52:55 -0700138 *
139 * We return the part after the "/", which will be an empty string for
140 * the default cgroup. If the string is longer than "bufLen", the string
141 * will be truncated.
142 */
143static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
144{
145#ifdef HAVE_ANDROID_OS
146 char pathBuf[32];
San Mehat503df202010-03-02 17:09:56 -0800147 char lineBuf[256];
148 FILE *fp;
San Mehatc0dfca72009-10-27 11:52:55 -0700149
150 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
San Mehat503df202010-03-02 17:09:56 -0800151 if (!(fp = fopen(pathBuf, "r"))) {
San Mehatc0dfca72009-10-27 11:52:55 -0700152 return -1;
153 }
154
San Mehat503df202010-03-02 17:09:56 -0800155 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
156 char *next = lineBuf;
157 char *subsys;
158 char *grp;
159 size_t len;
San Mehatc0dfca72009-10-27 11:52:55 -0700160
San Mehat503df202010-03-02 17:09:56 -0800161 /* Junk the first field */
162 if (!strsep(&next, ":")) {
163 goto out_bad_data;
164 }
San Mehatc0dfca72009-10-27 11:52:55 -0700165
San Mehat503df202010-03-02 17:09:56 -0800166 if (!(subsys = strsep(&next, ":"))) {
167 goto out_bad_data;
168 }
169
170 if (strcmp(subsys, "cpu")) {
171 /* Not the subsys we're looking for */
172 continue;
173 }
174
175 if (!(grp = strsep(&next, ":"))) {
176 goto out_bad_data;
177 }
178 grp++; /* Drop the leading '/' */
179 len = strlen(grp);
180 grp[len-1] = '\0'; /* Drop the trailing '\n' */
181
182 if (bufLen <= len) {
183 len = bufLen - 1;
184 }
185 strncpy(buf, grp, len);
186 buf[len] = '\0';
187 fclose(fp);
188 return 0;
San Mehatc0dfca72009-10-27 11:52:55 -0700189 }
190
San Mehat7e8529a2010-03-25 09:31:42 -0700191 SLOGE("Failed to find cpu subsys");
San Mehat503df202010-03-02 17:09:56 -0800192 fclose(fp);
193 return -1;
194 out_bad_data:
San Mehat7e8529a2010-03-25 09:31:42 -0700195 SLOGE("Bad cgroup data {%s}", lineBuf);
San Mehat503df202010-03-02 17:09:56 -0800196 fclose(fp);
197 return -1;
San Mehatc0dfca72009-10-27 11:52:55 -0700198#else
199 errno = ENOSYS;
200 return -1;
201#endif
202}
203
204int get_sched_policy(int tid, SchedPolicy *policy)
205{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700206#ifdef HAVE_GETTID
207 if (tid == 0) {
208 tid = gettid();
209 }
210#endif
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700211 pthread_once(&the_once, __initialize);
San Mehatc0dfca72009-10-27 11:52:55 -0700212
213 if (__sys_supports_schedgroups) {
214 char grpBuf[32];
215 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
216 return -1;
217 if (grpBuf[0] == '\0') {
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700218 *policy = SP_FOREGROUND;
Riley Andrews522d72b2014-10-03 17:02:53 -0700219 } else if (!strcmp(grpBuf, "bg_non_interactive")) {
220 *policy = SP_BACKGROUND;
San Mehatc0dfca72009-10-27 11:52:55 -0700221 } else {
222 errno = ERANGE;
223 return -1;
224 }
225 } else {
226 int rc = sched_getscheduler(tid);
227 if (rc < 0)
228 return -1;
229 else if (rc == SCHED_NORMAL)
230 *policy = SP_FOREGROUND;
231 else if (rc == SCHED_BATCH)
232 *policy = SP_BACKGROUND;
233 else {
234 errno = ERANGE;
235 return -1;
236 }
237 }
238 return 0;
239}
240
241int set_sched_policy(int tid, SchedPolicy policy)
242{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700243#ifdef HAVE_GETTID
244 if (tid == 0) {
245 tid = gettid();
246 }
247#endif
248 policy = _policy(policy);
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700249 pthread_once(&the_once, __initialize);
San Mehat493dad92009-09-12 10:06:57 -0700250
San Mehatd2e4e462009-10-29 11:48:00 -0700251#if POLICY_DEBUG
252 char statfile[64];
253 char statline[1024];
254 char thread_name[255];
255 int fd;
256
257 sprintf(statfile, "/proc/%d/stat", tid);
258 memset(thread_name, 0, sizeof(thread_name));
259
260 fd = open(statfile, O_RDONLY);
261 if (fd >= 0) {
262 int rc = read(fd, statline, 1023);
263 close(fd);
264 statline[rc] = 0;
265 char *p = statline;
266 char *q;
267
268 for (p = statline; *p != '('; p++);
269 p++;
270 for (q = p; *q != ')'; q++);
271
272 strncpy(thread_name, p, (q-p));
273 }
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700274 switch (policy) {
275 case SP_BACKGROUND:
San Mehat7e8529a2010-03-25 09:31:42 -0700276 SLOGD("vvv tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700277 break;
278 case SP_FOREGROUND:
Dima Zavin29319a62012-06-04 13:14:36 -0700279 case SP_AUDIO_APP:
280 case SP_AUDIO_SYS:
San Mehat7e8529a2010-03-25 09:31:42 -0700281 SLOGD("^^^ tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700282 break;
283 case SP_SYSTEM:
284 SLOGD("/// tid %d (%s)", tid, thread_name);
285 break;
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700286 default:
San Mehat7e8529a2010-03-25 09:31:42 -0700287 SLOGD("??? tid %d (%s)", tid, thread_name);
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700288 break;
San Mehatd2e4e462009-10-29 11:48:00 -0700289 }
290#endif
291
San Mehat493dad92009-09-12 10:06:57 -0700292 if (__sys_supports_schedgroups) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700293 if (add_tid_to_cgroup(tid, policy)) {
San Mehat493dad92009-09-12 10:06:57 -0700294 if (errno != ESRCH && errno != ENOENT)
295 return -errno;
296 }
297 } else {
298 struct sched_param param;
299
300 param.sched_priority = 0;
301 sched_setscheduler(tid,
302 (policy == SP_BACKGROUND) ?
303 SCHED_BATCH : SCHED_NORMAL,
304 &param);
305 }
306
Ruchi Kandoi422852e2014-04-22 18:55:08 -0700307 prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : 0, tid);
308
San Mehat493dad92009-09-12 10:06:57 -0700309 return 0;
310}
Raphael0384a982009-09-15 17:10:17 -0700311
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700312#else
313
314/* Stubs for non-Android targets. */
315
Mark Salyzyn12717162014-04-29 15:49:14 -0700316int set_sched_policy(int tid UNUSED, SchedPolicy policy UNUSED)
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700317{
318 return 0;
319}
320
Mark Salyzyn12717162014-04-29 15:49:14 -0700321int get_sched_policy(int tid UNUSED, SchedPolicy *policy)
Jeff Brownbff8f3f2012-05-08 15:05:42 -0700322{
323 *policy = SP_SYSTEM_DEFAULT;
324 return 0;
325}
326
327#endif
328
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800329const char *get_sched_policy_name(SchedPolicy policy)
330{
Glenn Kasten69bfb1f2012-03-16 09:43:19 -0700331 policy = _policy(policy);
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800332 static const char * const strings[SP_CNT] = {
333 [SP_BACKGROUND] = "bg",
334 [SP_FOREGROUND] = "fg",
Glenn Kasten10ec3c72012-04-19 15:25:58 -0700335 [SP_SYSTEM] = " ",
336 [SP_AUDIO_APP] = "aa",
337 [SP_AUDIO_SYS] = "as",
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800338 };
339 if ((policy < SP_CNT) && (strings[policy] != NULL))
340 return strings[policy];
341 else
342 return "error";
343}
344