From 235268ea8bff5fd012632720baa38abae98c38fa Mon Sep 17 00:00:00 2001 From: Rick Yiu Date: Fri, 28 Aug 2020 05:10:29 +0000 Subject: [PATCH] Revert "Use async approach to change sched policy" This reverts commit 98e231640833cf88331e6c91e1db760f8cb24ff5. Reason for revert: may have problem under race condition Change-Id: I2ced8b1a78e107f107b63468c5d8e4d62faa143c --- core/jni/android_util_Process.cpp | 52 +++---------------------------- 1 file changed, 5 insertions(+), 47 deletions(-) diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index cbcbe7f113903..891d6352a4a9a 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -19,18 +19,16 @@ // To make sure cpu_set_t is included from sched.h #define _GNU_SOURCE 1 -#include -#include -#include +#include #include #include +#include +#include #include #include #include #include -#include -#include -#include +#include #include #include @@ -86,8 +84,6 @@ 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) { @@ -600,20 +596,7 @@ void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, } #endif - 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 = androidSetThreadPriorityAndPolicy(pid, pri, !boot_completed); + int rc = androidSetThreadPriority(pid, pri); if (rc != 0) { if (rc == INVALID_OPERATION) { signalExceptionForPriorityError(env, errno, pid); @@ -622,31 +605,6 @@ 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)); }