Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3f8d28d authored by Isaac Chen's avatar Isaac Chen Committed by Gerrit Code Review
Browse files

Merge "Removed cpusets/schedboost build time dependency."

parents 4a534ebc 795267d4
Loading
Loading
Loading
Loading
+1 −9
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ cc_library {
        "native_handle.c",
        "open_memstream.c",
        "record_stream.c",
        "sched_policy.c",
        "sched_policy.cpp",
        "sockets.cpp",
        "strdup16to8.c",
        "strdup8to16.c",
@@ -141,14 +141,6 @@ cc_library {
    header_libs: ["libcutils_headers"],
    export_header_lib_headers: ["libcutils_headers"],

    product_variables: {
        cpusets: {
            cflags: ["-DUSE_CPUSETS"],
        },
        schedboost: {
            cflags: ["-DUSE_SCHEDBOOST"],
        },
    },
    cflags: [
        "-Werror",
        "-Wall",
+17 −0
Original line number Diff line number Diff line
@@ -17,10 +17,27 @@
#ifndef __CUTILS_SCHED_POLICY_H
#define __CUTILS_SCHED_POLICY_H

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#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 */
typedef enum {
    SP_DEFAULT    = -1,
+108 −65
Original line number Diff line number Diff line
@@ -58,13 +58,11 @@ static int __sys_supports_timerslack = -1;
static int bg_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
static int system_bg_cpuset_fd = -1;
static int bg_cpuset_fd = -1;
static int fg_cpuset_fd = -1;
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
static int bg_schedboost_fd = -1;
@@ -106,8 +104,53 @@ static int add_tid_to_cgroup(int tid, int fd)
    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)) {
        __sys_supports_schedgroups = 1;

@@ -126,7 +169,7 @@ static void __initialize(void) {
        __sys_supports_schedgroups = 0;
    }

#ifdef USE_CPUSETS
    if (cpusets_enabled()) {
        if (!access("/dev/cpuset/tasks", W_OK)) {

            filename = "/dev/cpuset/foreground/tasks";
@@ -138,16 +181,16 @@ static void __initialize(void) {
            filename = "/dev/cpuset/top-app/tasks";
            ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);

#ifdef USE_SCHEDBOOST
            if (schedboost_enabled()) {
                filename = "/dev/stune/top-app/tasks";
                ta_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC);
                filename = "/dev/stune/foreground/tasks";
                fg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC);
                filename = "/dev/stune/background/tasks";
                bg_schedboost_fd = open(filename, O_WRONLY | O_CLOEXEC);
#endif
            }
#endif
        }
    }

    char buf[64];
    snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", getpid());
@@ -235,7 +278,8 @@ int get_sched_policy(int tid, SchedPolicy *policy)

    if (__sys_supports_schedgroups) {
        char grpBuf[32];
#ifdef USE_CPUSETS

        if (cpusets_enabled()) {
            if (getCGroupSubsys(tid, "cpuset", grpBuf, sizeof(grpBuf)) < 0)
                return -1;
            if (grpBuf[0] == '\0') {
@@ -250,7 +294,7 @@ int get_sched_policy(int tid, SchedPolicy *policy)
                errno = ERANGE;
                return -1;
            }
#else
        } else {
            if (getCGroupSubsys(tid, "cpu", grpBuf, sizeof(grpBuf)) < 0)
                return -1;
            if (grpBuf[0] == '\0') {
@@ -261,7 +305,7 @@ int get_sched_policy(int tid, SchedPolicy *policy)
                errno = ERANGE;
                return -1;
            }
#endif
        }
    } else {
        int rc = sched_getscheduler(tid);
        if (rc < 0)
@@ -281,9 +325,10 @@ int get_sched_policy(int tid, SchedPolicy *policy)
int set_cpuset_policy(int tid, SchedPolicy policy)
{
    // in the absence of cpusets, use the old sched policy
#ifndef USE_CPUSETS
    if (!cpusets_enabled()) {
        return set_sched_policy(tid, policy);
#else
    }

    if (tid == 0) {
        tid = gettid();
    }
@@ -320,15 +365,14 @@ int set_cpuset_policy(int tid, SchedPolicy policy)
            return -errno;
    }

#ifdef USE_SCHEDBOOST
    if (schedboost_enabled()) {
        if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) {
            if (errno != ESRCH && errno != ENOENT)
                return -errno;
        }
#endif
    }

    return 0;
#endif
}

static void set_timerslack_ns(int tid, unsigned long long slack) {
@@ -420,18 +464,17 @@ int set_sched_policy(int tid, SchedPolicy policy)
            break;
        }


        if (add_tid_to_cgroup(tid, fd) != 0) {
            if (errno != ESRCH && errno != ENOENT)
                return -errno;
        }

#ifdef USE_SCHEDBOOST
        if (schedboost_enabled()) {
            if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) {
                if (errno != ESRCH && errno != ENOENT)
                    return -errno;
            }
#endif
        }
    } else {
        struct sched_param param;