Loading libcutils/Android.bp +1 −9 Original line number Original line Diff line number Diff line Loading @@ -60,7 +60,7 @@ cc_library { "native_handle.c", "native_handle.c", "open_memstream.c", "open_memstream.c", "record_stream.c", "record_stream.c", "sched_policy.c", "sched_policy.cpp", "sockets.cpp", "sockets.cpp", "strdup16to8.c", "strdup16to8.c", "strdup8to16.c", "strdup8to16.c", Loading Loading @@ -141,14 +141,6 @@ cc_library { header_libs: ["libcutils_headers"], header_libs: ["libcutils_headers"], export_header_lib_headers: ["libcutils_headers"], export_header_lib_headers: ["libcutils_headers"], product_variables: { cpusets: { cflags: ["-DUSE_CPUSETS"], }, schedboost: { cflags: ["-DUSE_SCHEDBOOST"], }, }, cflags: [ cflags: [ "-Werror", "-Werror", "-Wall", "-Wall", Loading libcutils/include/cutils/sched_policy.h +17 −0 Original line number Original line Diff line number Diff line Loading @@ -17,10 +17,27 @@ #ifndef __CUTILS_SCHED_POLICY_H #ifndef __CUTILS_SCHED_POLICY_H #define __CUTILS_SCHED_POLICY_H #define __CUTILS_SCHED_POLICY_H #include <stdbool.h> #ifdef __cplusplus #ifdef __cplusplus extern "C" { extern "C" { #endif #endif /* * Check if Linux kernel enables CPUSETS feature. * * Return value: 1 if Linux kernel CONFIG_CPUSETS=y; 0 otherwise. */ extern bool cpusets_enabled(); /* * Check if Linux kernel enables SCHEDTUNE feature (only available in Android * common kernel or Linaro LSK, not in mainline Linux as of v4.9) * * Return value: 1 if Linux kernel CONFIG_SCHEDTUNE=y; 0 otherwise. */ extern bool schedboost_enabled(); /* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */ /* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */ typedef enum { typedef enum { SP_DEFAULT = -1, SP_DEFAULT = -1, Loading libcutils/sched_policy.c→libcutils/sched_policy.cpp +108 −65 Original line number Original line Diff line number Diff line Loading @@ -58,13 +58,11 @@ static int __sys_supports_timerslack = -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; #ifdef USE_CPUSETS // File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error // File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error static int system_bg_cpuset_fd = -1; static int system_bg_cpuset_fd = -1; static int bg_cpuset_fd = -1; static int bg_cpuset_fd = -1; static int fg_cpuset_fd = -1; static int fg_cpuset_fd = -1; static int ta_cpuset_fd = -1; // special cpuset for top app static int ta_cpuset_fd = -1; // special cpuset for top app #endif // File descriptors open to /dev/stune/../tasks, setup by initialize, or -1 on error // File descriptors open to /dev/stune/../tasks, setup by initialize, or -1 on error static int bg_schedboost_fd = -1; static int bg_schedboost_fd = -1; Loading Loading @@ -106,8 +104,53 @@ static int add_tid_to_cgroup(int tid, int fd) return 0; return 0; } } static void __initialize(void) { /* char* filename; If CONFIG_CPUSETS for Linux kernel is set, "tasks" can be found under /dev/cpuset mounted in init.rc; otherwise, that file does not exist even though the directory, /dev/cpuset, is still created (by init.rc). A couple of other candidates (under cpuset mount directory): notify_on_release release_agent Yet another way to decide if cpuset is enabled is to parse /proc/self/status and search for lines begin with "Mems_allowed". If CONFIG_PROC_PID_CPUSET is set, the existence "/proc/self/cpuset" can be used to decide if CONFIG_CPUSETS is set, so we don't have a dependency on where init.rc mounts cpuset. That's why we'd better require this configuration be set if CONFIG_CPUSETS is set. With runtime check using the following function, build time variables like ENABLE_CPUSETS (used in Android.mk) or cpusets (used in Android.bp) are not needed. */ bool cpusets_enabled() { static bool enabled = (access("/dev/cpuset/tasks", F_OK) == 0); return enabled; } /* Similar to CONFIG_CPUSETS above, but with a different configuration CONFIG_SCHEDTUNE that's in Android common Linux kernel and Linaro Stable Kernel (LSK), but not in mainline Linux as of v4.9. With runtime check using the following function, build time variables like ENABLE_SCHEDBOOST (used in Android.mk) or schedboost (used in Android.bp) are not needed. */ bool schedboost_enabled() { static bool enabled = (access("/dev/stune/tasks", F_OK) == 0); return enabled; } static void __initialize() { const char* filename; if (!access("/dev/cpuctl/tasks", W_OK)) { if (!access("/dev/cpuctl/tasks", W_OK)) { __sys_supports_schedgroups = 1; __sys_supports_schedgroups = 1; Loading @@ -126,7 +169,7 @@ static void __initialize(void) { __sys_supports_schedgroups = 0; __sys_supports_schedgroups = 0; } } #ifdef USE_CPUSETS if (cpusets_enabled()) { if (!access("/dev/cpuset/tasks", W_OK)) { if (!access("/dev/cpuset/tasks", W_OK)) { filename = "/dev/cpuset/foreground/tasks"; filename = "/dev/cpuset/foreground/tasks"; Loading @@ -138,16 +181,16 @@ static void __initialize(void) { filename = "/dev/cpuset/top-app/tasks"; filename = "/dev/cpuset/top-app/tasks"; ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); #ifdef USE_SCHEDBOOST if (schedboost_enabled()) { filename = "/dev/stune/top-app/tasks"; filename = "/dev/stune/top-app/tasks"; ta_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); ta_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); filename = "/dev/stune/foreground/tasks"; filename = "/dev/stune/foreground/tasks"; fg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); fg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); filename = "/dev/stune/background/tasks"; filename = "/dev/stune/background/tasks"; bg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); bg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); #endif } } #endif } } char buf[64]; char buf[64]; snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", getpid()); snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", getpid()); Loading Loading @@ -235,7 +278,8 @@ int get_sched_policy(int tid, SchedPolicy *policy) if (__sys_supports_schedgroups) { if (__sys_supports_schedgroups) { char grpBuf[32]; char grpBuf[32]; #ifdef USE_CPUSETS if (cpusets_enabled()) { if (getCGroupSubsys(tid, "cpuset", grpBuf, sizeof(grpBuf)) < 0) if (getCGroupSubsys(tid, "cpuset", grpBuf, sizeof(grpBuf)) < 0) return -1; return -1; if (grpBuf[0] == '\0') { if (grpBuf[0] == '\0') { Loading @@ -250,7 +294,7 @@ int get_sched_policy(int tid, SchedPolicy *policy) errno = ERANGE; errno = ERANGE; return -1; return -1; } } #else } else { if (getCGroupSubsys(tid, "cpu", grpBuf, sizeof(grpBuf)) < 0) if (getCGroupSubsys(tid, "cpu", grpBuf, sizeof(grpBuf)) < 0) return -1; return -1; if (grpBuf[0] == '\0') { if (grpBuf[0] == '\0') { Loading @@ -261,7 +305,7 @@ int get_sched_policy(int tid, SchedPolicy *policy) errno = ERANGE; errno = ERANGE; return -1; return -1; } } #endif } } else { } else { int rc = sched_getscheduler(tid); int rc = sched_getscheduler(tid); if (rc < 0) if (rc < 0) Loading @@ -281,9 +325,10 @@ int get_sched_policy(int tid, SchedPolicy *policy) int set_cpuset_policy(int tid, SchedPolicy policy) int set_cpuset_policy(int tid, SchedPolicy policy) { { // in the absence of cpusets, use the old sched policy // in the absence of cpusets, use the old sched policy #ifndef USE_CPUSETS if (!cpusets_enabled()) { return set_sched_policy(tid, policy); return set_sched_policy(tid, policy); #else } if (tid == 0) { if (tid == 0) { tid = gettid(); tid = gettid(); } } Loading Loading @@ -320,15 +365,14 @@ int set_cpuset_policy(int tid, SchedPolicy policy) return -errno; return -errno; } } #ifdef USE_SCHEDBOOST if (schedboost_enabled()) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (errno != ESRCH && errno != ENOENT) if (errno != ESRCH && errno != ENOENT) return -errno; return -errno; } } #endif } return 0; return 0; #endif } } static void set_timerslack_ns(int tid, unsigned long long slack) { static void set_timerslack_ns(int tid, unsigned long long slack) { Loading Loading @@ -420,18 +464,17 @@ int set_sched_policy(int tid, SchedPolicy policy) break; break; } } if (add_tid_to_cgroup(tid, fd) != 0) { if (add_tid_to_cgroup(tid, fd) != 0) { if (errno != ESRCH && errno != ENOENT) if (errno != ESRCH && errno != ENOENT) return -errno; return -errno; } } #ifdef USE_SCHEDBOOST if (schedboost_enabled()) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (errno != ESRCH && errno != ENOENT) if (errno != ESRCH && errno != ENOENT) return -errno; return -errno; } } #endif } } else { } else { struct sched_param param; struct sched_param param; Loading Loading
libcutils/Android.bp +1 −9 Original line number Original line Diff line number Diff line Loading @@ -60,7 +60,7 @@ cc_library { "native_handle.c", "native_handle.c", "open_memstream.c", "open_memstream.c", "record_stream.c", "record_stream.c", "sched_policy.c", "sched_policy.cpp", "sockets.cpp", "sockets.cpp", "strdup16to8.c", "strdup16to8.c", "strdup8to16.c", "strdup8to16.c", Loading Loading @@ -141,14 +141,6 @@ cc_library { header_libs: ["libcutils_headers"], header_libs: ["libcutils_headers"], export_header_lib_headers: ["libcutils_headers"], export_header_lib_headers: ["libcutils_headers"], product_variables: { cpusets: { cflags: ["-DUSE_CPUSETS"], }, schedboost: { cflags: ["-DUSE_SCHEDBOOST"], }, }, cflags: [ cflags: [ "-Werror", "-Werror", "-Wall", "-Wall", Loading
libcutils/include/cutils/sched_policy.h +17 −0 Original line number Original line Diff line number Diff line Loading @@ -17,10 +17,27 @@ #ifndef __CUTILS_SCHED_POLICY_H #ifndef __CUTILS_SCHED_POLICY_H #define __CUTILS_SCHED_POLICY_H #define __CUTILS_SCHED_POLICY_H #include <stdbool.h> #ifdef __cplusplus #ifdef __cplusplus extern "C" { extern "C" { #endif #endif /* * Check if Linux kernel enables CPUSETS feature. * * Return value: 1 if Linux kernel CONFIG_CPUSETS=y; 0 otherwise. */ extern bool cpusets_enabled(); /* * Check if Linux kernel enables SCHEDTUNE feature (only available in Android * common kernel or Linaro LSK, not in mainline Linux as of v4.9) * * Return value: 1 if Linux kernel CONFIG_SCHEDTUNE=y; 0 otherwise. */ extern bool schedboost_enabled(); /* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */ /* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */ typedef enum { typedef enum { SP_DEFAULT = -1, SP_DEFAULT = -1, Loading
libcutils/sched_policy.c→libcutils/sched_policy.cpp +108 −65 Original line number Original line Diff line number Diff line Loading @@ -58,13 +58,11 @@ static int __sys_supports_timerslack = -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; #ifdef USE_CPUSETS // File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error // File descriptors open to /dev/cpuset/../tasks, setup by initialize, or -1 on error static int system_bg_cpuset_fd = -1; static int system_bg_cpuset_fd = -1; static int bg_cpuset_fd = -1; static int bg_cpuset_fd = -1; static int fg_cpuset_fd = -1; static int fg_cpuset_fd = -1; static int ta_cpuset_fd = -1; // special cpuset for top app static int ta_cpuset_fd = -1; // special cpuset for top app #endif // File descriptors open to /dev/stune/../tasks, setup by initialize, or -1 on error // File descriptors open to /dev/stune/../tasks, setup by initialize, or -1 on error static int bg_schedboost_fd = -1; static int bg_schedboost_fd = -1; Loading Loading @@ -106,8 +104,53 @@ static int add_tid_to_cgroup(int tid, int fd) return 0; return 0; } } static void __initialize(void) { /* char* filename; If CONFIG_CPUSETS for Linux kernel is set, "tasks" can be found under /dev/cpuset mounted in init.rc; otherwise, that file does not exist even though the directory, /dev/cpuset, is still created (by init.rc). A couple of other candidates (under cpuset mount directory): notify_on_release release_agent Yet another way to decide if cpuset is enabled is to parse /proc/self/status and search for lines begin with "Mems_allowed". If CONFIG_PROC_PID_CPUSET is set, the existence "/proc/self/cpuset" can be used to decide if CONFIG_CPUSETS is set, so we don't have a dependency on where init.rc mounts cpuset. That's why we'd better require this configuration be set if CONFIG_CPUSETS is set. With runtime check using the following function, build time variables like ENABLE_CPUSETS (used in Android.mk) or cpusets (used in Android.bp) are not needed. */ bool cpusets_enabled() { static bool enabled = (access("/dev/cpuset/tasks", F_OK) == 0); return enabled; } /* Similar to CONFIG_CPUSETS above, but with a different configuration CONFIG_SCHEDTUNE that's in Android common Linux kernel and Linaro Stable Kernel (LSK), but not in mainline Linux as of v4.9. With runtime check using the following function, build time variables like ENABLE_SCHEDBOOST (used in Android.mk) or schedboost (used in Android.bp) are not needed. */ bool schedboost_enabled() { static bool enabled = (access("/dev/stune/tasks", F_OK) == 0); return enabled; } static void __initialize() { const char* filename; if (!access("/dev/cpuctl/tasks", W_OK)) { if (!access("/dev/cpuctl/tasks", W_OK)) { __sys_supports_schedgroups = 1; __sys_supports_schedgroups = 1; Loading @@ -126,7 +169,7 @@ static void __initialize(void) { __sys_supports_schedgroups = 0; __sys_supports_schedgroups = 0; } } #ifdef USE_CPUSETS if (cpusets_enabled()) { if (!access("/dev/cpuset/tasks", W_OK)) { if (!access("/dev/cpuset/tasks", W_OK)) { filename = "/dev/cpuset/foreground/tasks"; filename = "/dev/cpuset/foreground/tasks"; Loading @@ -138,16 +181,16 @@ static void __initialize(void) { filename = "/dev/cpuset/top-app/tasks"; filename = "/dev/cpuset/top-app/tasks"; ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); #ifdef USE_SCHEDBOOST if (schedboost_enabled()) { filename = "/dev/stune/top-app/tasks"; filename = "/dev/stune/top-app/tasks"; ta_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); ta_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); filename = "/dev/stune/foreground/tasks"; filename = "/dev/stune/foreground/tasks"; fg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); fg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); filename = "/dev/stune/background/tasks"; filename = "/dev/stune/background/tasks"; bg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); bg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC); #endif } } #endif } } char buf[64]; char buf[64]; snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", getpid()); snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", getpid()); Loading Loading @@ -235,7 +278,8 @@ int get_sched_policy(int tid, SchedPolicy *policy) if (__sys_supports_schedgroups) { if (__sys_supports_schedgroups) { char grpBuf[32]; char grpBuf[32]; #ifdef USE_CPUSETS if (cpusets_enabled()) { if (getCGroupSubsys(tid, "cpuset", grpBuf, sizeof(grpBuf)) < 0) if (getCGroupSubsys(tid, "cpuset", grpBuf, sizeof(grpBuf)) < 0) return -1; return -1; if (grpBuf[0] == '\0') { if (grpBuf[0] == '\0') { Loading @@ -250,7 +294,7 @@ int get_sched_policy(int tid, SchedPolicy *policy) errno = ERANGE; errno = ERANGE; return -1; return -1; } } #else } else { if (getCGroupSubsys(tid, "cpu", grpBuf, sizeof(grpBuf)) < 0) if (getCGroupSubsys(tid, "cpu", grpBuf, sizeof(grpBuf)) < 0) return -1; return -1; if (grpBuf[0] == '\0') { if (grpBuf[0] == '\0') { Loading @@ -261,7 +305,7 @@ int get_sched_policy(int tid, SchedPolicy *policy) errno = ERANGE; errno = ERANGE; return -1; return -1; } } #endif } } else { } else { int rc = sched_getscheduler(tid); int rc = sched_getscheduler(tid); if (rc < 0) if (rc < 0) Loading @@ -281,9 +325,10 @@ int get_sched_policy(int tid, SchedPolicy *policy) int set_cpuset_policy(int tid, SchedPolicy policy) int set_cpuset_policy(int tid, SchedPolicy policy) { { // in the absence of cpusets, use the old sched policy // in the absence of cpusets, use the old sched policy #ifndef USE_CPUSETS if (!cpusets_enabled()) { return set_sched_policy(tid, policy); return set_sched_policy(tid, policy); #else } if (tid == 0) { if (tid == 0) { tid = gettid(); tid = gettid(); } } Loading Loading @@ -320,15 +365,14 @@ int set_cpuset_policy(int tid, SchedPolicy policy) return -errno; return -errno; } } #ifdef USE_SCHEDBOOST if (schedboost_enabled()) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (errno != ESRCH && errno != ENOENT) if (errno != ESRCH && errno != ENOENT) return -errno; return -errno; } } #endif } return 0; return 0; #endif } } static void set_timerslack_ns(int tid, unsigned long long slack) { static void set_timerslack_ns(int tid, unsigned long long slack) { Loading Loading @@ -420,18 +464,17 @@ int set_sched_policy(int tid, SchedPolicy policy) break; break; } } if (add_tid_to_cgroup(tid, fd) != 0) { if (add_tid_to_cgroup(tid, fd) != 0) { if (errno != ESRCH && errno != ENOENT) if (errno != ESRCH && errno != ENOENT) return -errno; return -errno; } } #ifdef USE_SCHEDBOOST if (schedboost_enabled()) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (errno != ESRCH && errno != ENOENT) if (errno != ESRCH && errno != ENOENT) return -errno; return -errno; } } #endif } } else { } else { struct sched_param param; struct sched_param param; Loading