blob: 8134aa13aff79ee975ea21cef1c66bba9959221e [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
30
San Mehat493dad92009-09-12 10:06:57 -070031#include <sched.h>
San Mehat493dad92009-09-12 10:06:57 -070032
33#include <cutils/sched_policy.h>
34
San Mehat3cd5b662009-09-14 16:05:24 -070035#ifndef SCHED_NORMAL
36 #define SCHED_NORMAL 0
37#endif
38
39#ifndef SCHED_BATCH
40 #define SCHED_BATCH 3
41#endif
42
San Mehat805d67a2009-10-29 13:56:26 -070043#define POLICY_DEBUG 0
San Mehatd2e4e462009-10-29 11:48:00 -070044
San Mehatc0dfca72009-10-27 11:52:55 -070045static int __sys_supports_schedgroups = -1;
46
San Mehat493dad92009-09-12 10:06:57 -070047static int add_tid_to_cgroup(int tid, const char *grp_name)
48{
49 int fd;
50 char path[255];
51 char text[64];
52
53 sprintf(path, "/dev/cpuctl/%s/tasks", grp_name);
54
San Mehat805d67a2009-10-29 13:56:26 -070055 if ((fd = open(path, O_WRONLY)) < 0) {
San Mehatc1c38dd2009-12-03 12:19:12 -080056 LOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", path,
57 strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -070058 return -1;
San Mehat805d67a2009-10-29 13:56:26 -070059 }
San Mehat493dad92009-09-12 10:06:57 -070060
61 sprintf(text, "%d", tid);
62 if (write(fd, text, strlen(text)) < 0) {
63 close(fd);
San Mehatc1c38dd2009-12-03 12:19:12 -080064 /*
65 * If the thread is in the process of exiting,
66 * don't flag an error
67 */
68 if (errno == ESRCH)
69 return 0;
70 LOGW("add_tid_to_cgroup failed to write '%s' (%s)\n", path,
71 strerror(errno));
San Mehat493dad92009-09-12 10:06:57 -070072 return -1;
73 }
74
75 close(fd);
76 return 0;
77}
78
San Mehatc0dfca72009-10-27 11:52:55 -070079static inline void initialize()
San Mehat493dad92009-09-12 10:06:57 -070080{
San Mehat493dad92009-09-12 10:06:57 -070081 if (__sys_supports_schedgroups < 0) {
82 if (!access("/dev/cpuctl/tasks", F_OK)) {
83 __sys_supports_schedgroups = 1;
84 } else {
85 __sys_supports_schedgroups = 0;
86 }
87 }
San Mehatc0dfca72009-10-27 11:52:55 -070088}
89
90/*
91 * Try to get the scheduler group.
92 *
93 * The data from /proc/<pid>/cgroup looks like:
94 * 2:cpu:/bg_non_interactive
95 *
96 * We return the part after the "/", which will be an empty string for
97 * the default cgroup. If the string is longer than "bufLen", the string
98 * will be truncated.
99 */
100static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
101{
102#ifdef HAVE_ANDROID_OS
103 char pathBuf[32];
104 char readBuf[256];
105 ssize_t count;
106 int fd;
107
108 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
109 if ((fd = open(pathBuf, O_RDONLY)) < 0) {
110 return -1;
111 }
112
113 count = read(fd, readBuf, sizeof(readBuf));
114 if (count <= 0) {
115 close(fd);
116 errno = ENODATA;
117 return -1;
118 }
119 close(fd);
120
121 readBuf[--count] = '\0'; /* remove the '\n', now count==strlen */
122
123 char* cp = strchr(readBuf, '/');
124 if (cp == NULL) {
125 readBuf[sizeof(readBuf)-1] = '\0';
126 errno = ENODATA;
127 return -1;
128 }
129
130 memcpy(buf, cp+1, count); /* count-1 for cp+1, count+1 for NUL */
131 return 0;
132#else
133 errno = ENOSYS;
134 return -1;
135#endif
136}
137
138int get_sched_policy(int tid, SchedPolicy *policy)
139{
140 initialize();
141
142 if (__sys_supports_schedgroups) {
143 char grpBuf[32];
144 if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
145 return -1;
146 if (grpBuf[0] == '\0') {
147 *policy = SP_FOREGROUND;
148 } else if (!strcmp(grpBuf, "bg_non_interactive")) {
149 *policy = SP_BACKGROUND;
150 } else {
151 errno = ERANGE;
152 return -1;
153 }
154 } else {
155 int rc = sched_getscheduler(tid);
156 if (rc < 0)
157 return -1;
158 else if (rc == SCHED_NORMAL)
159 *policy = SP_FOREGROUND;
160 else if (rc == SCHED_BATCH)
161 *policy = SP_BACKGROUND;
162 else {
163 errno = ERANGE;
164 return -1;
165 }
166 }
167 return 0;
168}
169
170int set_sched_policy(int tid, SchedPolicy policy)
171{
172 initialize();
San Mehat493dad92009-09-12 10:06:57 -0700173
San Mehatd2e4e462009-10-29 11:48:00 -0700174#if POLICY_DEBUG
175 char statfile[64];
176 char statline[1024];
177 char thread_name[255];
178 int fd;
179
180 sprintf(statfile, "/proc/%d/stat", tid);
181 memset(thread_name, 0, sizeof(thread_name));
182
183 fd = open(statfile, O_RDONLY);
184 if (fd >= 0) {
185 int rc = read(fd, statline, 1023);
186 close(fd);
187 statline[rc] = 0;
188 char *p = statline;
189 char *q;
190
191 for (p = statline; *p != '('; p++);
192 p++;
193 for (q = p; *q != ')'; q++);
194
195 strncpy(thread_name, p, (q-p));
196 }
197 if (policy == SP_BACKGROUND) {
198 LOGD("vvv tid %d (%s)", tid, thread_name);
199 } else if (policy == SP_FOREGROUND) {
200 LOGD("^^^ tid %d (%s)", tid, thread_name);
201 } else {
202 LOGD("??? tid %d (%s)", tid, thread_name);
203 }
204#endif
205
San Mehat493dad92009-09-12 10:06:57 -0700206 if (__sys_supports_schedgroups) {
San Mehat805d67a2009-10-29 13:56:26 -0700207 const char *grp = "";
San Mehat493dad92009-09-12 10:06:57 -0700208
209 if (policy == SP_BACKGROUND) {
210 grp = "bg_non_interactive";
211 }
212
213 if (add_tid_to_cgroup(tid, grp)) {
214 if (errno != ESRCH && errno != ENOENT)
215 return -errno;
216 }
217 } else {
218 struct sched_param param;
219
220 param.sched_priority = 0;
221 sched_setscheduler(tid,
222 (policy == SP_BACKGROUND) ?
223 SCHED_BATCH : SCHED_NORMAL,
224 &param);
225 }
226
227 return 0;
228}
Raphael0384a982009-09-15 17:10:17 -0700229
230#endif /* HAVE_SCHED_H */