Loading include/cutils/sched_policy.h +2 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,8 @@ typedef enum { SP_SYSTEM_DEFAULT = SP_FOREGROUND, } SchedPolicy; extern int set_cpuset_policy(int tid, SchedPolicy 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(), * then the other threads in the same thread group are _not_ affected. Loading libcutils/Android.mk +6 −0 Original line number Diff line number Diff line Loading @@ -122,6 +122,9 @@ LOCAL_SRC_FILES_x86_64 += \ LOCAL_C_INCLUDES := $(libcutils_c_includes) LOCAL_STATIC_LIBRARIES := liblog ifneq ($(ENABLE_CPUSETS),) LOCAL_CFLAGS += -DUSE_CPUSETS endif LOCAL_CFLAGS += -Werror -std=gnu90 include $(BUILD_STATIC_LIBRARY) Loading @@ -131,6 +134,9 @@ LOCAL_MODULE := libcutils # liblog symbols present in libcutils. LOCAL_WHOLE_STATIC_LIBRARIES := libcutils liblog LOCAL_SHARED_LIBRARIES := liblog ifneq ($(ENABLE_CPUSETS),) LOCAL_CFLAGS += -DUSE_CPUSETS endif LOCAL_CFLAGS += -Werror LOCAL_C_INCLUDES := $(libcutils_c_includes) include $(BUILD_SHARED_LIBRARY) Loading libcutils/sched_policy.c +86 −31 Original line number Diff line number Diff line Loading @@ -59,27 +59,16 @@ static int __sys_supports_schedgroups = -1; static int bg_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 */ 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) { SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy); SLOGE("add_tid_to_cgroup failed; fd=%d\n", fd); errno = EINVAL; return -1; } Loading @@ -100,8 +89,9 @@ static int add_tid_to_cgroup(int tid, SchedPolicy policy) */ if (errno == ESRCH) return 0; SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n", ptr, strerror(errno), policy); SLOGW("add_tid_to_cgroup failed to write '%s' (%s); fd=%d\n", ptr, strerror(errno), fd); errno = EINVAL; return -1; } Loading @@ -127,6 +117,17 @@ static void __initialize(void) { } else { __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 } /* Loading Loading @@ -236,6 +237,45 @@ int get_sched_policy(int tid, SchedPolicy *policy) 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) { if (tid == 0) { Loading Loading @@ -286,7 +326,23 @@ int set_sched_policy(int tid, SchedPolicy policy) #endif 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) return -errno; } Loading Loading @@ -337,4 +393,3 @@ const char *get_sched_policy_name(SchedPolicy policy) else return "error"; } rootdir/init.rc +22 −0 Original line number Diff line number Diff line Loading @@ -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_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. # net_bw_acct grants impersonation of socket owners. # net_bw_stats grants access to other apps' detailed tagged-socket stats. Loading Loading
include/cutils/sched_policy.h +2 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,8 @@ typedef enum { SP_SYSTEM_DEFAULT = SP_FOREGROUND, } SchedPolicy; extern int set_cpuset_policy(int tid, SchedPolicy 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(), * then the other threads in the same thread group are _not_ affected. Loading
libcutils/Android.mk +6 −0 Original line number Diff line number Diff line Loading @@ -122,6 +122,9 @@ LOCAL_SRC_FILES_x86_64 += \ LOCAL_C_INCLUDES := $(libcutils_c_includes) LOCAL_STATIC_LIBRARIES := liblog ifneq ($(ENABLE_CPUSETS),) LOCAL_CFLAGS += -DUSE_CPUSETS endif LOCAL_CFLAGS += -Werror -std=gnu90 include $(BUILD_STATIC_LIBRARY) Loading @@ -131,6 +134,9 @@ LOCAL_MODULE := libcutils # liblog symbols present in libcutils. LOCAL_WHOLE_STATIC_LIBRARIES := libcutils liblog LOCAL_SHARED_LIBRARIES := liblog ifneq ($(ENABLE_CPUSETS),) LOCAL_CFLAGS += -DUSE_CPUSETS endif LOCAL_CFLAGS += -Werror LOCAL_C_INCLUDES := $(libcutils_c_includes) include $(BUILD_SHARED_LIBRARY) Loading
libcutils/sched_policy.c +86 −31 Original line number Diff line number Diff line Loading @@ -59,27 +59,16 @@ static int __sys_supports_schedgroups = -1; static int bg_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 */ 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) { SLOGE("add_tid_to_cgroup failed; policy=%d\n", policy); SLOGE("add_tid_to_cgroup failed; fd=%d\n", fd); errno = EINVAL; return -1; } Loading @@ -100,8 +89,9 @@ static int add_tid_to_cgroup(int tid, SchedPolicy policy) */ if (errno == ESRCH) return 0; SLOGW("add_tid_to_cgroup failed to write '%s' (%s); policy=%d\n", ptr, strerror(errno), policy); SLOGW("add_tid_to_cgroup failed to write '%s' (%s); fd=%d\n", ptr, strerror(errno), fd); errno = EINVAL; return -1; } Loading @@ -127,6 +117,17 @@ static void __initialize(void) { } else { __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 } /* Loading Loading @@ -236,6 +237,45 @@ int get_sched_policy(int tid, SchedPolicy *policy) 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) { if (tid == 0) { Loading Loading @@ -286,7 +326,23 @@ int set_sched_policy(int tid, SchedPolicy policy) #endif 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) return -errno; } Loading Loading @@ -337,4 +393,3 @@ const char *get_sched_policy_name(SchedPolicy policy) else return "error"; }
rootdir/init.rc +22 −0 Original line number Diff line number Diff line Loading @@ -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_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. # net_bw_acct grants impersonation of socket owners. # net_bw_stats grants access to other apps' detailed tagged-socket stats. Loading