blob: 35d362adad6bf3a08816a39e53127887fa7c4163 [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
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <string.h>
23#include <errno.h>
24#include <fcntl.h>
Raphael0384a982009-09-15 17:10:17 -070025
San Mehatd2e4e462009-10-29 11:48:00 -070026#define LOG_TAG "SchedPolicy"
27#include "cutils/log.h"
28
Raphael0384a982009-09-15 17:10:17 -070029#ifdef HAVE_SCHED_H
Brad Fitzpatrick86b12152010-05-08 11:51:13 -070030#ifdef HAVE_PTHREADS
Raphael0384a982009-09-15 17:10:17 -070031
San Mehat493dad92009-09-12 10:06:57 -070032#include <sched.h>
Brad Fitzpatrick86b12152010-05-08 11:51:13 -070033#include <pthread.h>
San Mehat493dad92009-09-12 10:06:57 -070034
35#include <cutils/sched_policy.h>
36
San Mehat3cd5b662009-09-14 16:05:24 -070037#ifndef SCHED_NORMAL
38 #define SCHED_NORMAL 0
39#endif
40
41#ifndef SCHED_BATCH
42 #define SCHED_BATCH 3
43#endif
44
San Mehat805d67a2009-10-29 13:56:26 -070045#define POLICY_DEBUG 0
San Mehatd2e4e462009-10-29 11:48:00 -070046
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070047static pthread_once_t the_once = PTHREAD_ONCE_INIT;
48
San Mehatc0dfca72009-10-27 11:52:55 -070049static int __sys_supports_schedgroups = -1;
50
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070051// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
52static int normal_cgroup_fd = -1;
53static int bg_cgroup_fd = -1;
54
55/* Add tid to the scheduling group defined by the policy */
56static int add_tid_to_cgroup(int tid, SchedPolicy policy)
San Mehat493dad92009-09-12 10:06:57 -070057{
58 int fd;
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070059
60 if (policy == SP_BACKGROUND) {
61 fd = bg_cgroup_fd;
62 } else {
63 fd = normal_cgroup_fd;
64 }
65
66 if (fd < 0) {
67 SLOGE("add_tid_to_cgroup failed; background=%d\n",
68 policy == SP_BACKGROUND ? 1 : 0);
San Mehat493dad92009-09-12 10:06:57 -070069 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070070 }
San Mehat493dad92009-09-12 10:06:57 -070071
Brad Fitzpatrick253e27a2010-05-06 10:00:37 -070072 // specialized itoa -- works for tid > 0
73 char text[22];
74 char *end = text + sizeof(text) - 1;
75 char *ptr = end;
76 *ptr = '\0';
77 while (tid > 0) {
78 *--ptr = '0' + (tid % 10);
79 tid = tid / 10;
80 }
81
82 if (write(fd, ptr, end - ptr) < 0) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070083 /*
84 * If the thread is in the process of exiting,
85 * don't flag an error
86 */
87 if (errno == ESRCH)
88 return 0;
89 SLOGW("add_tid_to_cgroup failed to write '%s' (%s); background=%d\n",
90 ptr, strerror(errno), policy == SP_BACKGROUND ? 1 : 0);
San Mehat493dad92009-09-12 10:06:57 -070091 return -1;
92 }
93
San Mehat493dad92009-09-12 10:06:57 -070094 return 0;
95}
96
Brad Fitzpatricke43c2482010-05-07 12:06:05 -070097static void __initialize(void) {
98 char* filename;
99 if (!access("/dev/cpuctl/tasks", F_OK)) {
100 __sys_supports_schedgroups = 1;
101
102 filename = "/dev/cpuctl/tasks";
103 normal_cgroup_fd = open(filename, O_WRONLY);
104 if (normal_cgroup_fd < 0) {
105 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -0700106 }
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700107
108 filename = "/dev/cpuctl/bg_non_interactive/tasks";
109 bg_cgroup_fd = open(filename, O_WRONLY);
110 if (bg_cgroup_fd < 0) {
111 SLOGE("open of %s failed: %s\n", filename, strerror(errno));
112 }
113 } else {
114 __sys_supports_schedgroups = 0;
San Mehat493dad92009-09-12 10:06:57 -0700115 }
San Mehatc0dfca72009-10-27 11:52:55 -0700116}
117
118/*
119 * Try to get the scheduler group.
120 *
San Mehat503df202010-03-02 17:09:56 -0800121 * The data from /proc/<pid>/cgroup looks (something) like:
San Mehatc0dfca72009-10-27 11:52:55 -0700122 * 2:cpu:/bg_non_interactive
San Mehat503df202010-03-02 17:09:56 -0800123 * 1:cpuacct:/
San Mehatc0dfca72009-10-27 11:52:55 -0700124 *
125 * We return the part after the "/", which will be an empty string for
126 * the default cgroup. If the string is longer than "bufLen", the string
127 * will be truncated.
128 */
129static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
130{
131#ifdef HAVE_ANDROID_OS
132 char pathBuf[32];
San Mehat503df202010-03-02 17:09:56 -0800133 char lineBuf[256];
134 FILE *fp;
San Mehatc0dfca72009-10-27 11:52:55 -0700135
136 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
San Mehat503df202010-03-02 17:09:56 -0800137 if (!(fp = fopen(pathBuf, "r"))) {
San Mehatc0dfca72009-10-27 11:52:55 -0700138 return -1;
139 }
140
San Mehat503df202010-03-02 17:09:56 -0800141 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
142 char *next = lineBuf;
143 char *subsys;
144 char *grp;
145 size_t len;
San Mehatc0dfca72009-10-27 11:52:55 -0700146
San Mehat503df202010-03-02 17:09:56 -0800147 /* Junk the first field */
148 if (!strsep(&next, ":")) {
149 goto out_bad_data;
150 }
San Mehatc0dfca72009-10-27 11:52:55 -0700151
San Mehat503df202010-03-02 17:09:56 -0800152 if (!(subsys = strsep(&next, ":"))) {
153 goto out_bad_data;
154 }
155
156 if (strcmp(subsys, "cpu")) {
157 /* Not the subsys we're looking for */
158 continue;
159 }
160
161 if (!(grp = strsep(&next, ":"))) {
162 goto out_bad_data;
163 }
164 grp++; /* Drop the leading '/' */
165 len = strlen(grp);
166 grp[len-1] = '\0'; /* Drop the trailing '\n' */
167
168 if (bufLen <= len) {
169 len = bufLen - 1;
170 }
171 strncpy(buf, grp, len);
172 buf[len] = '\0';
173 fclose(fp);
174 return 0;
San Mehatc0dfca72009-10-27 11:52:55 -0700175 }
176
San Mehat7e8529a2010-03-25 09:31:42 -0700177 SLOGE("Failed to find cpu subsys");
San Mehat503df202010-03-02 17:09:56 -0800178 fclose(fp);
179 return -1;
180 out_bad_data:
San Mehat7e8529a2010-03-25 09:31:42 -0700181 SLOGE("Bad cgroup data {%s}", lineBuf);
San Mehat503df202010-03-02 17:09:56 -0800182 fclose(fp);
183 return -1;
San Mehatc0dfca72009-10-27 11:52:55 -0700184#else
185 errno = ENOSYS;
186 return -1;
187#endif
188}
189
190int get_sched_policy(int tid, SchedPolicy *policy)
191{
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700192 pthread_once(&the_once, __initialize);
San Mehatc0dfca72009-10-27 11:52:55 -0700193
194 if (__sys_supports_schedgroups) {
195 char grpBuf[32];
196 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
197 return -1;
198 if (grpBuf[0] == '\0') {
199 *policy = SP_FOREGROUND;
200 } else if (!strcmp(grpBuf, "bg_non_interactive")) {
201 *policy = SP_BACKGROUND;
202 } else {
203 errno = ERANGE;
204 return -1;
205 }
206 } else {
207 int rc = sched_getscheduler(tid);
208 if (rc < 0)
209 return -1;
210 else if (rc == SCHED_NORMAL)
211 *policy = SP_FOREGROUND;
212 else if (rc == SCHED_BATCH)
213 *policy = SP_BACKGROUND;
214 else {
215 errno = ERANGE;
216 return -1;
217 }
218 }
219 return 0;
220}
221
222int set_sched_policy(int tid, SchedPolicy policy)
223{
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700224 pthread_once(&the_once, __initialize);
San Mehat493dad92009-09-12 10:06:57 -0700225
San Mehatd2e4e462009-10-29 11:48:00 -0700226#if POLICY_DEBUG
227 char statfile[64];
228 char statline[1024];
229 char thread_name[255];
230 int fd;
231
232 sprintf(statfile, "/proc/%d/stat", tid);
233 memset(thread_name, 0, sizeof(thread_name));
234
235 fd = open(statfile, O_RDONLY);
236 if (fd >= 0) {
237 int rc = read(fd, statline, 1023);
238 close(fd);
239 statline[rc] = 0;
240 char *p = statline;
241 char *q;
242
243 for (p = statline; *p != '('; p++);
244 p++;
245 for (q = p; *q != ')'; q++);
246
247 strncpy(thread_name, p, (q-p));
248 }
249 if (policy == SP_BACKGROUND) {
San Mehat7e8529a2010-03-25 09:31:42 -0700250 SLOGD("vvv tid %d (%s)", tid, thread_name);
San Mehatd2e4e462009-10-29 11:48:00 -0700251 } else if (policy == SP_FOREGROUND) {
San Mehat7e8529a2010-03-25 09:31:42 -0700252 SLOGD("^^^ tid %d (%s)", tid, thread_name);
San Mehatd2e4e462009-10-29 11:48:00 -0700253 } else {
San Mehat7e8529a2010-03-25 09:31:42 -0700254 SLOGD("??? tid %d (%s)", tid, thread_name);
San Mehatd2e4e462009-10-29 11:48:00 -0700255 }
256#endif
257
San Mehat493dad92009-09-12 10:06:57 -0700258 if (__sys_supports_schedgroups) {
Brad Fitzpatricke43c2482010-05-07 12:06:05 -0700259 if (add_tid_to_cgroup(tid, policy)) {
San Mehat493dad92009-09-12 10:06:57 -0700260 if (errno != ESRCH && errno != ENOENT)
261 return -errno;
262 }
263 } else {
264 struct sched_param param;
265
266 param.sched_priority = 0;
267 sched_setscheduler(tid,
268 (policy == SP_BACKGROUND) ?
269 SCHED_BATCH : SCHED_NORMAL,
270 &param);
271 }
272
273 return 0;
274}
Raphael0384a982009-09-15 17:10:17 -0700275
Glenn Kasten86c7cc82012-03-05 16:14:39 -0800276const char *get_sched_policy_name(SchedPolicy policy)
277{
278 static const char * const strings[SP_CNT] = {
279 [SP_BACKGROUND] = "bg",
280 [SP_FOREGROUND] = "fg",
281 };
282 if ((policy < SP_CNT) && (strings[policy] != NULL))
283 return strings[policy];
284 else
285 return "error";
286}
287
Brad Fitzpatrick86b12152010-05-08 11:51:13 -0700288#endif /* HAVE_PTHREADS */
Raphael0384a982009-09-15 17:10:17 -0700289#endif /* HAVE_SCHED_H */