From 57affbf91d016eda1395106c4d5f0350471dca85 Mon Sep 17 00:00:00 2001 From: Rick Yiu Date: Tue, 11 Feb 2020 14:59:37 +0800 Subject: [PATCH] Add choice for changing sched policy when setting thread priority If the policy has been changed already, we do not need to change it again. Bug: 139521784 Test: functionality verified Change-Id: I251db1d3f874896ba9be68df87209e7e514b80f9 --- libutils/Threads.cpp | 25 +++++++++++++------------ libutils/include/utils/AndroidThreads.h | 4 +++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp index 540dcf49d..147db542d 100644 --- a/libutils/Threads.cpp +++ b/libutils/Threads.cpp @@ -302,8 +302,7 @@ void androidSetCreateThreadFunc(android_create_thread_fn func) } #if defined(__ANDROID__) -int androidSetThreadPriority(pid_t tid, int pri) -{ +int androidSetThreadPriority(pid_t tid, int pri, bool change_policy) { int rc = 0; int lasterr = 0; int curr_pri = getpriority(PRIO_PROCESS, tid); @@ -312,17 +311,19 @@ int androidSetThreadPriority(pid_t tid, int pri) return rc; } - if (pri >= ANDROID_PRIORITY_BACKGROUND) { - rc = SetTaskProfiles(tid, {"SCHED_SP_BACKGROUND"}, true) ? 0 : -1; - } else if (curr_pri >= ANDROID_PRIORITY_BACKGROUND) { - SchedPolicy policy = SP_FOREGROUND; - // Change to the sched policy group of the process. - get_sched_policy(getpid(), &policy); - rc = SetTaskProfiles(tid, {get_sched_policy_profile_name(policy)}, true) ? 0 : -1; - } + if (change_policy) { + if (pri >= ANDROID_PRIORITY_BACKGROUND) { + rc = SetTaskProfiles(tid, {"SCHED_SP_BACKGROUND"}, true) ? 0 : -1; + } else if (curr_pri >= ANDROID_PRIORITY_BACKGROUND) { + SchedPolicy policy = SP_FOREGROUND; + // Change to the sched policy group of the process. + get_sched_policy(getpid(), &policy); + rc = SetTaskProfiles(tid, {get_sched_policy_profile_name(policy)}, true) ? 0 : -1; + } - if (rc) { - lasterr = errno; + if (rc) { + lasterr = errno; + } } if (setpriority(PRIO_PROCESS, tid, pri) < 0) { diff --git a/libutils/include/utils/AndroidThreads.h b/libutils/include/utils/AndroidThreads.h index a8d785175..3c30a2a85 100644 --- a/libutils/include/utils/AndroidThreads.h +++ b/libutils/include/utils/AndroidThreads.h @@ -78,7 +78,9 @@ extern void androidSetCreateThreadFunc(android_create_thread_fn func); // should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION // if the priority set failed, else another value if just the group set failed; // in either case errno is set. Thread ID zero means current thread. -extern int androidSetThreadPriority(pid_t tid, int prio); +// Parameter "change_policy" indicates if sched policy should be changed. It needs +// not be checked again if the change is done elsewhere like activity manager. +extern int androidSetThreadPriority(pid_t tid, int prio, bool change_policy = true); // Get the current priority of a particular thread. Returns one of the // ANDROID_PRIORITY constants or a negative result in case of error.