Merge "add cpuset support to libcutils" into mnc-dev
This commit is contained in:
commit
e5b0c83785
4 changed files with 116 additions and 31 deletions
|
|
@ -34,6 +34,8 @@ typedef enum {
|
||||||
SP_SYSTEM_DEFAULT = SP_FOREGROUND,
|
SP_SYSTEM_DEFAULT = SP_FOREGROUND,
|
||||||
} SchedPolicy;
|
} SchedPolicy;
|
||||||
|
|
||||||
|
extern int set_cpuset_policy(int tid, SchedPolicy policy);
|
||||||
|
|
||||||
/* Assign thread tid to the cgroup associated with the specified policy.
|
/* Assign thread tid to the cgroup associated with the specified policy.
|
||||||
* If the thread is a thread group leader, that is it's gettid() == getpid(),
|
* If the thread is a thread group leader, that is it's gettid() == getpid(),
|
||||||
* then the other threads in the same thread group are _not_ affected.
|
* then the other threads in the same thread group are _not_ affected.
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,9 @@ LOCAL_SRC_FILES_x86_64 += \
|
||||||
|
|
||||||
LOCAL_C_INCLUDES := $(libcutils_c_includes)
|
LOCAL_C_INCLUDES := $(libcutils_c_includes)
|
||||||
LOCAL_STATIC_LIBRARIES := liblog
|
LOCAL_STATIC_LIBRARIES := liblog
|
||||||
|
ifneq ($(ENABLE_CPUSETS),)
|
||||||
|
LOCAL_CFLAGS += -DUSE_CPUSETS
|
||||||
|
endif
|
||||||
LOCAL_CFLAGS += -Werror -std=gnu90
|
LOCAL_CFLAGS += -Werror -std=gnu90
|
||||||
include $(BUILD_STATIC_LIBRARY)
|
include $(BUILD_STATIC_LIBRARY)
|
||||||
|
|
||||||
|
|
@ -134,6 +137,9 @@ LOCAL_MODULE := libcutils
|
||||||
# liblog symbols present in libcutils.
|
# liblog symbols present in libcutils.
|
||||||
LOCAL_WHOLE_STATIC_LIBRARIES := libcutils liblog
|
LOCAL_WHOLE_STATIC_LIBRARIES := libcutils liblog
|
||||||
LOCAL_SHARED_LIBRARIES := liblog
|
LOCAL_SHARED_LIBRARIES := liblog
|
||||||
|
ifneq ($(ENABLE_CPUSETS),)
|
||||||
|
LOCAL_CFLAGS += -DUSE_CPUSETS
|
||||||
|
endif
|
||||||
LOCAL_CFLAGS += -Werror
|
LOCAL_CFLAGS += -Werror
|
||||||
LOCAL_C_INCLUDES := $(libcutils_c_includes)
|
LOCAL_C_INCLUDES := $(libcutils_c_includes)
|
||||||
include $(BUILD_SHARED_LIBRARY)
|
include $(BUILD_SHARED_LIBRARY)
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
/*
|
/*
|
||||||
** Copyright 2007, The Android Open Source Project
|
** Copyright 2007, The Android Open Source Project
|
||||||
**
|
**
|
||||||
** Licensed under the Apache License, Version 2.0 (the "License");
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
** you may not use this file except in compliance with the License.
|
** you may not use this file except in compliance with the License.
|
||||||
** You may obtain a copy of the License at
|
** You may obtain a copy of the License at
|
||||||
**
|
**
|
||||||
** http://www.apache.org/licenses/LICENSE-2.0
|
** http://www.apache.org/licenses/LICENSE-2.0
|
||||||
**
|
**
|
||||||
** Unless required by applicable law or agreed to in writing, software
|
** Unless required by applicable law or agreed to in writing, software
|
||||||
** distributed under the License is distributed on an "AS IS" BASIS,
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
** See the License for the specific language governing permissions and
|
** See the License for the specific language governing permissions and
|
||||||
** limitations under the License.
|
** limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -59,27 +59,16 @@ static int __sys_supports_schedgroups = -1;
|
||||||
static int bg_cgroup_fd = -1;
|
static int bg_cgroup_fd = -1;
|
||||||
static int fg_cgroup_fd = -1;
|
static int fg_cgroup_fd = -1;
|
||||||
|
|
||||||
|
// File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error
|
||||||
|
static int bg_cpuset_fd = -1;
|
||||||
|
static int fg_cpuset_fd = -1;
|
||||||
|
|
||||||
/* Add tid to the scheduling group defined by the policy */
|
/* Add tid to the scheduling group defined by the policy */
|
||||||
static int add_tid_to_cgroup(int tid, SchedPolicy policy)
|
static int add_tid_to_cgroup(int tid, int fd)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
|
|
||||||
switch (policy) {
|
|
||||||
case SP_BACKGROUND:
|
|
||||||
fd = bg_cgroup_fd;
|
|
||||||
break;
|
|
||||||
case SP_FOREGROUND:
|
|
||||||
case SP_AUDIO_APP:
|
|
||||||
case SP_AUDIO_SYS:
|
|
||||||
fd = fg_cgroup_fd;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
fd = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy);
|
SLOGE("add_tid_to_cgroup failed; fd=%d\n", fd);
|
||||||
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,8 +89,9 @@ static int add_tid_to_cgroup(int tid, SchedPolicy policy)
|
||||||
*/
|
*/
|
||||||
if (errno == ESRCH)
|
if (errno == ESRCH)
|
||||||
return 0;
|
return 0;
|
||||||
SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n",
|
SLOGW("add_tid_to_cgroup failed to write '%s' (%s); fd=%d\n",
|
||||||
ptr, strerror(errno), policy);
|
ptr, strerror(errno), fd);
|
||||||
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,6 +117,17 @@ static void __initialize(void) {
|
||||||
} else {
|
} else {
|
||||||
__sys_supports_schedgroups = 0;
|
__sys_supports_schedgroups = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_CPUSETS
|
||||||
|
if (!access("/dev/cpuset/tasks", F_OK)) {
|
||||||
|
|
||||||
|
filename = "/dev/cpuset/foreground/tasks";
|
||||||
|
fg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
|
||||||
|
filename = "/dev/cpuset/background/tasks";
|
||||||
|
bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -236,6 +237,45 @@ int get_sched_policy(int tid, SchedPolicy *policy)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int set_cpuset_policy(int tid, SchedPolicy policy)
|
||||||
|
{
|
||||||
|
// in the absence of cpusets, use the old sched policy
|
||||||
|
#ifndef USE_CPUSETS
|
||||||
|
return set_sched_policy(tid, policy);
|
||||||
|
#else
|
||||||
|
if (tid == 0) {
|
||||||
|
tid = gettid();
|
||||||
|
}
|
||||||
|
policy = _policy(policy);
|
||||||
|
pthread_once(&the_once, __initialize);
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
switch (policy) {
|
||||||
|
case SP_BACKGROUND:
|
||||||
|
fd = bg_cpuset_fd;
|
||||||
|
break;
|
||||||
|
case SP_FOREGROUND:
|
||||||
|
case SP_AUDIO_APP:
|
||||||
|
case SP_AUDIO_SYS:
|
||||||
|
fd = fg_cpuset_fd;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fd = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (add_tid_to_cgroup(tid, fd) != 0) {
|
||||||
|
if (errno != ESRCH && errno != ENOENT)
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
// we do both setting of cpuset and setting of cgroup
|
||||||
|
// ensures that backgrounded apps are actually deprioritized
|
||||||
|
// including on core 0
|
||||||
|
return set_sched_policy(tid, policy);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
int set_sched_policy(int tid, SchedPolicy policy)
|
int set_sched_policy(int tid, SchedPolicy policy)
|
||||||
{
|
{
|
||||||
if (tid == 0) {
|
if (tid == 0) {
|
||||||
|
|
@ -286,7 +326,23 @@ int set_sched_policy(int tid, SchedPolicy policy)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (__sys_supports_schedgroups) {
|
if (__sys_supports_schedgroups) {
|
||||||
if (add_tid_to_cgroup(tid, policy)) {
|
int fd;
|
||||||
|
switch (policy) {
|
||||||
|
case SP_BACKGROUND:
|
||||||
|
fd = bg_cgroup_fd;
|
||||||
|
break;
|
||||||
|
case SP_FOREGROUND:
|
||||||
|
case SP_AUDIO_APP:
|
||||||
|
case SP_AUDIO_SYS:
|
||||||
|
fd = fg_cgroup_fd;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fd = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (add_tid_to_cgroup(tid, fd) != 0) {
|
||||||
if (errno != ESRCH && errno != ENOENT)
|
if (errno != ESRCH && errno != ENOENT)
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
@ -296,7 +352,7 @@ int set_sched_policy(int tid, SchedPolicy policy)
|
||||||
param.sched_priority = 0;
|
param.sched_priority = 0;
|
||||||
sched_setscheduler(tid,
|
sched_setscheduler(tid,
|
||||||
(policy == SP_BACKGROUND) ?
|
(policy == SP_BACKGROUND) ?
|
||||||
SCHED_BATCH : SCHED_NORMAL,
|
SCHED_BATCH : SCHED_NORMAL,
|
||||||
¶m);
|
¶m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -337,4 +393,3 @@ const char *get_sched_policy_name(SchedPolicy policy)
|
||||||
else
|
else
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,28 @@ on init
|
||||||
write /dev/cpuctl/bg_non_interactive/cpu.rt_runtime_us 700000
|
write /dev/cpuctl/bg_non_interactive/cpu.rt_runtime_us 700000
|
||||||
write /dev/cpuctl/bg_non_interactive/cpu.rt_period_us 1000000
|
write /dev/cpuctl/bg_non_interactive/cpu.rt_period_us 1000000
|
||||||
|
|
||||||
|
# sets up initial cpusets for ActivityManager
|
||||||
|
mkdir /dev/cpuset
|
||||||
|
mount cpuset none /dev/cpuset
|
||||||
|
mkdir /dev/cpuset/foreground
|
||||||
|
mkdir /dev/cpuset/background
|
||||||
|
# this ensures that the cpusets are present and usable, but the device's
|
||||||
|
# init.rc must actually set the correct cpus
|
||||||
|
write /dev/cpuset/foreground/cpus 0
|
||||||
|
write /dev/cpuset/background/cpus 0
|
||||||
|
write /dev/cpuset/foreground/mems 0
|
||||||
|
write /dev/cpuset/background/mems 0
|
||||||
|
chown system system /dev/cpuset
|
||||||
|
chown system system /dev/cpuset/foreground
|
||||||
|
chown system system /dev/cpuset/background
|
||||||
|
chown system system /dev/cpuset/tasks
|
||||||
|
chown system system /dev/cpuset/foreground/tasks
|
||||||
|
chown system system /dev/cpuset/background/tasks
|
||||||
|
chmod 0644 /dev/cpuset/foreground/tasks
|
||||||
|
chmod 0644 /dev/cpuset/background/tasks
|
||||||
|
chmod 0644 /dev/cpuset/tasks
|
||||||
|
|
||||||
|
|
||||||
# qtaguid will limit access to specific data based on group memberships.
|
# qtaguid will limit access to specific data based on group memberships.
|
||||||
# net_bw_acct grants impersonation of socket owners.
|
# net_bw_acct grants impersonation of socket owners.
|
||||||
# net_bw_stats grants access to other apps' detailed tagged-socket stats.
|
# net_bw_stats grants access to other apps' detailed tagged-socket stats.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue