2014-04-30 00:49:14 +02:00
|
|
|
/*
|
2009-09-12 19:06:57 +02:00
|
|
|
** 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.
|
|
|
|
*/
|
|
|
|
|
2012-05-09 00:05:42 +02:00
|
|
|
#define LOG_TAG "SchedPolicy"
|
|
|
|
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2009-09-12 19:06:57 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-05-09 00:05:42 +02:00
|
|
|
#include <cutils/sched_policy.h>
|
2014-04-30 00:49:14 +02:00
|
|
|
#include <log/log.h>
|
|
|
|
|
|
|
|
#define UNUSED __attribute__((__unused__))
|
2009-09-16 02:10:17 +02:00
|
|
|
|
2012-05-09 00:05:42 +02:00
|
|
|
/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
|
|
|
|
* Call this any place a SchedPolicy is used as an input parameter.
|
|
|
|
* Returns the possibly re-mapped policy.
|
|
|
|
*/
|
|
|
|
static inline SchedPolicy _policy(SchedPolicy p)
|
|
|
|
{
|
|
|
|
return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
|
|
|
|
}
|
2009-10-29 19:48:00 +01:00
|
|
|
|
2012-05-09 00:05:42 +02:00
|
|
|
#if defined(HAVE_ANDROID_OS) && defined(HAVE_SCHED_H) && defined(HAVE_PTHREADS)
|
2009-09-16 02:10:17 +02:00
|
|
|
|
2010-05-08 20:51:13 +02:00
|
|
|
#include <pthread.h>
|
2014-07-12 05:57:03 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/prctl.h>
|
2009-09-15 01:05:24 +02:00
|
|
|
|
2009-10-29 21:56:26 +01:00
|
|
|
#define POLICY_DEBUG 0
|
2009-10-29 19:48:00 +01:00
|
|
|
|
2012-04-20 00:25:58 +02:00
|
|
|
#define CAN_SET_SP_SYSTEM 0 // non-zero means to implement set_sched_policy(tid, SP_SYSTEM)
|
|
|
|
|
2014-07-18 02:15:14 +02:00
|
|
|
// This prctl is only available in Android kernels.
|
|
|
|
#define PR_SET_TIMERSLACK_PID 41
|
|
|
|
|
2014-04-23 03:55:08 +02:00
|
|
|
// timer slack value in nS enforced when the thread moves to background
|
|
|
|
#define TIMER_SLACK_BG 40000000
|
|
|
|
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
static pthread_once_t the_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
2009-10-27 19:52:55 +01:00
|
|
|
static int __sys_supports_schedgroups = -1;
|
|
|
|
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
|
|
|
|
static int bg_cgroup_fd = -1;
|
2012-04-20 00:25:58 +02:00
|
|
|
static int fg_cgroup_fd = -1;
|
|
|
|
#if CAN_SET_SP_SYSTEM
|
|
|
|
static int system_cgroup_fd = -1;
|
|
|
|
#endif
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
|
|
|
|
/* Add tid to the scheduling group defined by the policy */
|
|
|
|
static int add_tid_to_cgroup(int tid, SchedPolicy policy)
|
2009-09-12 19:06:57 +02:00
|
|
|
{
|
|
|
|
int fd;
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
|
2012-04-20 00:25:58 +02:00
|
|
|
switch (policy) {
|
|
|
|
case SP_BACKGROUND:
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
fd = bg_cgroup_fd;
|
2012-04-20 00:25:58 +02:00
|
|
|
break;
|
|
|
|
case SP_FOREGROUND:
|
2012-06-04 22:14:36 +02:00
|
|
|
case SP_AUDIO_APP:
|
|
|
|
case SP_AUDIO_SYS:
|
2012-04-20 00:25:58 +02:00
|
|
|
fd = fg_cgroup_fd;
|
|
|
|
break;
|
|
|
|
#if CAN_SET_SP_SYSTEM
|
|
|
|
case SP_SYSTEM:
|
|
|
|
fd = system_cgroup_fd;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
fd = -1;
|
|
|
|
break;
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fd < 0) {
|
2012-04-20 00:25:58 +02:00
|
|
|
SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
|
2009-09-12 19:06:57 +02:00
|
|
|
return -1;
|
2009-10-29 21:56:26 +01:00
|
|
|
}
|
2009-09-12 19:06:57 +02:00
|
|
|
|
2010-05-06 19:00:37 +02:00
|
|
|
// specialized itoa -- works for tid > 0
|
|
|
|
char text[22];
|
|
|
|
char *end = text + sizeof(text) - 1;
|
|
|
|
char *ptr = end;
|
|
|
|
*ptr = '\0';
|
|
|
|
while (tid > 0) {
|
|
|
|
*--ptr = '0' + (tid % 10);
|
|
|
|
tid = tid / 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, ptr, end - ptr) < 0) {
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
/*
|
|
|
|
* If the thread is in the process of exiting,
|
|
|
|
* don't flag an error
|
|
|
|
*/
|
|
|
|
if (errno == ESRCH)
|
|
|
|
return 0;
|
2012-04-20 00:25:58 +02:00
|
|
|
SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
|
|
|
|
ptr, strerror(errno), policy);
|
2009-09-12 19:06:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
static void __initialize(void) {
|
|
|
|
char* filename;
|
|
|
|
if (!access("/dev/cpuctl/tasks", F_OK)) {
|
|
|
|
__sys_supports_schedgroups = 1;
|
|
|
|
|
2012-04-20 00:25:58 +02:00
|
|
|
#if CAN_SET_SP_SYSTEM
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
filename = "/dev/cpuctl/tasks";
|
2012-05-09 00:07:58 +02:00
|
|
|
system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
|
2012-04-20 00:25:58 +02:00
|
|
|
if (system_cgroup_fd < 0) {
|
|
|
|
SLOGV("open of %s failed: %s\n", filename, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-04 19:42:38 +02:00
|
|
|
filename = "/dev/cpuctl/apps/tasks";
|
2012-05-09 00:07:58 +02:00
|
|
|
fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
|
2012-04-20 00:25:58 +02:00
|
|
|
if (fg_cgroup_fd < 0) {
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
SLOGE("open of %s failed: %s\n", filename, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:42:38 +02:00
|
|
|
filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
|
2012-05-09 00:07:58 +02:00
|
|
|
bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
if (bg_cgroup_fd < 0) {
|
|
|
|
SLOGE("open of %s failed: %s\n", filename, strerror(errno));
|
2009-09-12 19:06:57 +02:00
|
|
|
}
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
} else {
|
|
|
|
__sys_supports_schedgroups = 0;
|
2009-09-12 19:06:57 +02:00
|
|
|
}
|
2009-10-27 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to get the scheduler group.
|
|
|
|
*
|
2010-03-03 02:09:56 +01:00
|
|
|
* The data from /proc/<pid>/cgroup looks (something) like:
|
2009-10-27 19:52:55 +01:00
|
|
|
* 2:cpu:/bg_non_interactive
|
2010-03-03 02:09:56 +01:00
|
|
|
* 1:cpuacct:/
|
2009-10-27 19:52:55 +01:00
|
|
|
*
|
|
|
|
* 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];
|
2010-03-03 02:09:56 +01:00
|
|
|
char lineBuf[256];
|
|
|
|
FILE *fp;
|
2009-10-27 19:52:55 +01:00
|
|
|
|
|
|
|
snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
|
2010-03-03 02:09:56 +01:00
|
|
|
if (!(fp = fopen(pathBuf, "r"))) {
|
2009-10-27 19:52:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-03 02:09:56 +01:00
|
|
|
while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
|
|
|
|
char *next = lineBuf;
|
|
|
|
char *subsys;
|
|
|
|
char *grp;
|
|
|
|
size_t len;
|
2009-10-27 19:52:55 +01:00
|
|
|
|
2010-03-03 02:09:56 +01:00
|
|
|
/* Junk the first field */
|
|
|
|
if (!strsep(&next, ":")) {
|
|
|
|
goto out_bad_data;
|
|
|
|
}
|
2009-10-27 19:52:55 +01:00
|
|
|
|
2010-03-03 02:09:56 +01:00
|
|
|
if (!(subsys = strsep(&next, ":"))) {
|
|
|
|
goto out_bad_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(subsys, "cpu")) {
|
|
|
|
/* Not the subsys we're looking for */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(grp = strsep(&next, ":"))) {
|
|
|
|
goto out_bad_data;
|
|
|
|
}
|
|
|
|
grp++; /* Drop the leading '/' */
|
|
|
|
len = strlen(grp);
|
|
|
|
grp[len-1] = '\0'; /* Drop the trailing '\n' */
|
|
|
|
|
|
|
|
if (bufLen <= len) {
|
|
|
|
len = bufLen - 1;
|
|
|
|
}
|
|
|
|
strncpy(buf, grp, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2009-10-27 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
2010-03-25 17:31:42 +01:00
|
|
|
SLOGE("Failed to find cpu subsys");
|
2010-03-03 02:09:56 +01:00
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
out_bad_data:
|
2010-03-25 17:31:42 +01:00
|
|
|
SLOGE("Bad cgroup data {%s}", lineBuf);
|
2010-03-03 02:09:56 +01:00
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
2009-10-27 19:52:55 +01:00
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_sched_policy(int tid, SchedPolicy *policy)
|
|
|
|
{
|
2012-03-16 17:43:19 +01:00
|
|
|
if (tid == 0) {
|
|
|
|
tid = gettid();
|
|
|
|
}
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
pthread_once(&the_once, __initialize);
|
2009-10-27 19:52:55 +01:00
|
|
|
|
|
|
|
if (__sys_supports_schedgroups) {
|
|
|
|
char grpBuf[32];
|
|
|
|
if (getSchedulerGroup(tid, grpBuf, sizeof(grpBuf)) < 0)
|
|
|
|
return -1;
|
|
|
|
if (grpBuf[0] == '\0') {
|
2012-04-20 00:25:58 +02:00
|
|
|
*policy = SP_SYSTEM;
|
2012-06-04 19:42:38 +02:00
|
|
|
} else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
|
2009-10-27 19:52:55 +01:00
|
|
|
*policy = SP_BACKGROUND;
|
2012-06-04 19:42:38 +02:00
|
|
|
} else if (!strcmp(grpBuf, "apps")) {
|
2012-04-20 00:25:58 +02:00
|
|
|
*policy = SP_FOREGROUND;
|
2009-10-27 19:52:55 +01:00
|
|
|
} 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)
|
|
|
|
{
|
2012-03-16 17:43:19 +01:00
|
|
|
if (tid == 0) {
|
|
|
|
tid = gettid();
|
|
|
|
}
|
|
|
|
policy = _policy(policy);
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
pthread_once(&the_once, __initialize);
|
2009-09-12 19:06:57 +02:00
|
|
|
|
2009-10-29 19:48:00 +01:00
|
|
|
#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));
|
|
|
|
}
|
2012-04-20 00:25:58 +02:00
|
|
|
switch (policy) {
|
|
|
|
case SP_BACKGROUND:
|
2010-03-25 17:31:42 +01:00
|
|
|
SLOGD("vvv tid %d (%s)", tid, thread_name);
|
2012-04-20 00:25:58 +02:00
|
|
|
break;
|
|
|
|
case SP_FOREGROUND:
|
2012-06-04 22:14:36 +02:00
|
|
|
case SP_AUDIO_APP:
|
|
|
|
case SP_AUDIO_SYS:
|
2010-03-25 17:31:42 +01:00
|
|
|
SLOGD("^^^ tid %d (%s)", tid, thread_name);
|
2012-04-20 00:25:58 +02:00
|
|
|
break;
|
|
|
|
case SP_SYSTEM:
|
|
|
|
SLOGD("/// tid %d (%s)", tid, thread_name);
|
|
|
|
break;
|
|
|
|
default:
|
2010-03-25 17:31:42 +01:00
|
|
|
SLOGD("??? tid %d (%s)", tid, thread_name);
|
2012-04-20 00:25:58 +02:00
|
|
|
break;
|
2009-10-29 19:48:00 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-12 19:06:57 +02:00
|
|
|
if (__sys_supports_schedgroups) {
|
More fixes to Binder perf regression from Eclair
Re-use the file descriptor to change the cgroups. There's a fast path
in the kernel for the case where the value's already uncahnged. Also,
this is okay because the kernel ignores the write offset. This is
essentially a ioctl-ish/datagram interface, despite being a fd we're
writing to.
This reduces Dalvik->Dalvik void/void Binder calls on Sapphire from
~1.2 to ~0.8 ms. This is a bit slower than the ~0.7 ms we get
avoiding the write altogether.
strace confirms:
[pid 1557] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1556] <... ioctl resumed> , 0x45399cd0) = 0
[pid 1556] write(22, "1556", 4) = 4
[pid 1556] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1555] <... ioctl resumed> , 0x45299cd0) = 0
[pid 1555] write(22, "1555", 4) = 4
[pid 1555] ioctl(21, 0xc0186201 <unfinished ...>
[pid 1554] <... ioctl resumed> , 0x45199cd0) = 0
[pid 1554] write(22, "1554", 4) = 4
BUG=2660235
Change-Id: Ia9f9e1d5b792eaebc9560f89931faf6df0cf9c0d
2010-05-07 21:06:05 +02:00
|
|
|
if (add_tid_to_cgroup(tid, policy)) {
|
2009-09-12 19:06:57 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-04-23 03:55:08 +02:00
|
|
|
prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : 0, tid);
|
|
|
|
|
2009-09-12 19:06:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-16 02:10:17 +02:00
|
|
|
|
2012-05-09 00:05:42 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* Stubs for non-Android targets. */
|
|
|
|
|
2014-04-30 00:49:14 +02:00
|
|
|
int set_sched_policy(int tid UNUSED, SchedPolicy policy UNUSED)
|
2012-05-09 00:05:42 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-30 00:49:14 +02:00
|
|
|
int get_sched_policy(int tid UNUSED, SchedPolicy *policy)
|
2012-05-09 00:05:42 +02:00
|
|
|
{
|
|
|
|
*policy = SP_SYSTEM_DEFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-03-06 01:14:39 +01:00
|
|
|
const char *get_sched_policy_name(SchedPolicy policy)
|
|
|
|
{
|
2012-03-16 17:43:19 +01:00
|
|
|
policy = _policy(policy);
|
2012-03-06 01:14:39 +01:00
|
|
|
static const char * const strings[SP_CNT] = {
|
|
|
|
[SP_BACKGROUND] = "bg",
|
|
|
|
[SP_FOREGROUND] = "fg",
|
2012-04-20 00:25:58 +02:00
|
|
|
[SP_SYSTEM] = " ",
|
|
|
|
[SP_AUDIO_APP] = "aa",
|
|
|
|
[SP_AUDIO_SYS] = "as",
|
2012-03-06 01:14:39 +01:00
|
|
|
};
|
|
|
|
if ((policy < SP_CNT) && (strings[policy] != NULL))
|
|
|
|
return strings[policy];
|
|
|
|
else
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
|