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

Commit e21661db authored by Rick Yiu's avatar Rick Yiu Committed by Android (Google) Code Review
Browse files

Merge "Use async approach to change sched policy"

parents a4b4b7b5 98e23164
Loading
Loading
Loading
Loading
+47 −5
Original line number Diff line number Diff line
@@ -19,16 +19,18 @@

// To make sure cpu_set_t is included from sched.h
#define _GNU_SOURCE 1
#include <utils/Log.h>
#include <android-base/properties.h>
#include <android-base/unique_fd.h>
#include <binder/ActivityManager.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <utils/String8.h>
#include <utils/Vector.h>
#include <meminfo/procmeminfo.h>
#include <meminfo/sysmeminfo.h>
#include <processgroup/processgroup.h>
#include <processgroup/sched_policy.h>
#include <android-base/unique_fd.h>
#include <utils/Log.h>
#include <utils/String8.h>
#include <utils/Vector.h>

#include <algorithm>
#include <array>
@@ -84,6 +86,8 @@ Mutex gKeyCreateMutex;
static pthread_key_t gBgKey = -1;
#endif

static bool boot_completed = false;

// For both of these, err should be in the errno range (positive), not a status_t (negative)
static void signalExceptionForError(JNIEnv* env, int err, int tid) {
    switch (err) {
@@ -596,7 +600,20 @@ void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
    }
#endif

    int rc = androidSetThreadPriority(pid, pri);
    SchedPolicy policy;
    bool policy_changed = false;
    int rc = 0, curr_pri = getpriority(PRIO_PROCESS, pid);

    if (pri == curr_pri) {
        return;
    }

    if (!boot_completed) {
        boot_completed = android::base::GetBoolProperty("sys.boot_completed", false);
    }

    // Do not change sched policy cgroup after boot complete.
    rc = androidSetThreadPriority(pid, pri, !boot_completed);
    if (rc != 0) {
        if (rc == INVALID_OPERATION) {
            signalExceptionForPriorityError(env, errno, pid);
@@ -605,6 +622,31 @@ void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz,
        }
    }

    // Only use async approach after boot complete.
    if (!boot_completed) {
        return;
    }

    // Change to background sched policy for the thread if setting to low priority.
    if (pri >= ANDROID_PRIORITY_BACKGROUND) {
        policy = SP_BACKGROUND;
        policy_changed = true;
        // Change to sched policy of the process if thread priority is raising from low priority.
    } else if (curr_pri >= ANDROID_PRIORITY_BACKGROUND) {
        // If we cannot get sched policy of the process, use SP_FOREGROUND as default.
        policy = SP_FOREGROUND;
        get_sched_policy(getpid(), &policy);
        policy_changed = true;
    }

    // Sched policy will only change in above 2 cases.
    if (policy_changed) {
        ActivityManager am;
        if (!am.setSchedPolicyCgroup(pid, policy)) {
            ALOGE("am.setThreadPriority failed: tid=%d priority=%d policy=%d", pid, pri, policy);
        }
    }

    //ALOGI("Setting priority of %" PRId32 ": %" PRId32 ", getpriority returns %d\n",
    //     pid, pri, getpriority(PRIO_PROCESS, pid));
}