1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* libs/cutils/sched_policy.c
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#define LOG_TAG "SchedPolicy"
#include "cutils/log.h"
#ifdef HAVE_SCHED_H
#include <sched.h>
#include <cutils/sched_policy.h>
#ifndef SCHED_NORMAL
#define SCHED_NORMAL 0
#endif
#ifndef SCHED_BATCH
#define SCHED_BATCH 3
#endif
#define POLICY_DEBUG 0
static int __sys_supports_schedgroups = -1;
static int add_tid_to_cgroup(int tid, const char *grp_name)
{
int fd;
char path[255];
char text[64];
sprintf(path, "/dev/cpuctl/%s/tasks", grp_name);
if ((fd = open(path, O_WRONLY)) < 0) {
LOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", path,
strerror(errno));
return -1;
}
sprintf(text, "%d", tid);
if (write(fd, text, strlen(text)) < 0) {
close(fd);
/*
* If the thread is in the process of exiting,
* don't flag an error
*/
if (errno == ESRCH)
return 0;
LOGW("add_tid_to_cgroup failed to write '%s' (%s)\n", path,
strerror(errno));
return -1;
}
close(fd);
return 0;
}
static inline void initialize()
{
if (__sys_supports_schedgroups < 0) {
if (!access("/dev/cpuctl/tasks", F_OK)) {
__sys_supports_schedgroups = 1;
} else {
__sys_supports_schedgroups = 0;
}
}
}
/*
* Try to get the scheduler group.
*
* The data from /proc/<pid>/cgroup looks like:
* 2:cpu:/bg_non_interactive
*
* We return the part after the "/", which will be an empty string for
* the default cgroup. If the string is longer than "bufLen", the string
* will be truncated.
*/
static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
{
#ifdef HAVE_ANDROID_OS
char pathBuf[32];
char readBuf[256];
ssize_t count;
int fd;
snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
if ((fd = open(pathBuf, O_RDONLY)) < 0) {
return -1;
}
count = read(fd, readBuf, sizeof(readBuf));
if (count <= 0) {
close(fd);
errno = ENODATA;
return -1;
}
close(fd);
readBuf[--count] = '\0'; /* remove the '\n', now count==strlen */
char* cp = strchr(readBuf, '/');
if (cp == NULL) {
readBuf[sizeof(readBuf)-1] = '\0';
errno = ENODATA;
return -1;
}
memcpy(buf, cp+1, count); /* count-1 for cp+1, count+1 for NUL */
return 0;
#else
errno = ENOSYS;
return -1;
#endif
}
int get_sched_policy(int tid, SchedPolicy *policy)
{
initialize();
if (__sys_supports_schedgroups) {
char grpBuf[32];
if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
return -1;
if (grpBuf[0] == '\0') {
*policy = SP_FOREGROUND;
} else if (!strcmp(grpBuf, "bg_non_interactive")) {
*policy = SP_BACKGROUND;
} else {
errno = ERANGE;
return -1;
}
} else {
int rc = sched_getscheduler(tid);
if (rc < 0)
return -1;
else if (rc == SCHED_NORMAL)
*policy = SP_FOREGROUND;
else if (rc == SCHED_BATCH)
*policy = SP_BACKGROUND;
else {
errno = ERANGE;
return -1;
}
}
return 0;
}
int set_sched_policy(int tid, SchedPolicy policy)
{
initialize();
#if POLICY_DEBUG
char statfile[64];
char statline[1024];
char thread_name[255];
int fd;
sprintf(statfile, "/proc/%d/stat", tid);
memset(thread_name, 0, sizeof(thread_name));
fd = open(statfile, O_RDONLY);
if (fd >= 0) {
int rc = read(fd, statline, 1023);
close(fd);
statline[rc] = 0;
char *p = statline;
char *q;
for (p = statline; *p != '('; p++);
p++;
for (q = p; *q != ')'; q++);
strncpy(thread_name, p, (q-p));
}
if (policy == SP_BACKGROUND) {
LOGD("vvv tid %d (%s)", tid, thread_name);
} else if (policy == SP_FOREGROUND) {
LOGD("^^^ tid %d (%s)", tid, thread_name);
} else {
LOGD("??? tid %d (%s)", tid, thread_name);
}
#endif
if (__sys_supports_schedgroups) {
const char *grp = "";
if (policy == SP_BACKGROUND) {
grp = "bg_non_interactive";
}
if (add_tid_to_cgroup(tid, grp)) {
if (errno != ESRCH && errno != ENOENT)
return -errno;
}
} else {
struct sched_param param;
param.sched_priority = 0;
sched_setscheduler(tid,
(policy == SP_BACKGROUND) ?
SCHED_BATCH : SCHED_NORMAL,
¶m);
}
return 0;
}
#endif /* HAVE_SCHED_H */
|