diff options
| author | 2019-10-02 14:41:55 +0800 | |
|---|---|---|
| committer | 2019-10-05 11:32:57 +0800 | |
| commit | c9faeea98c80f6e170c9c5a7fcd87656263f744c (patch) | |
| tree | 24961ae3edc3b54598415ee195dab1bfec9c6567 | |
| parent | c7eb6aa0bf39116bc301a97ca604d21aefc0b782 (diff) | |
Replace set_cpuset/sched_policy with SetTaskProfiles
To replace set_cpuset/sched_policy with SetTaskProfiles, and use
aggregate profiles if applicable. Also skipped set_sched_policy if
set_cpuset_policy is called, because set_cpuset_policy already
contains the behavior of set_sched_policy.
Bug: 139521784
Test: thread cgroups are set/changing as expected
Change-Id: Ib7ad78886b6f410af1e734dff998265a2f28300f
| -rw-r--r-- | core/jni/android_util_Process.cpp | 67 |
1 files changed, 36 insertions, 31 deletions
diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index 3cefeea68406..0fada1bae8ed 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -35,6 +35,7 @@ #include <limits> #include <memory> #include <string> +#include <unordered_map> #include <vector> #include "core_jni_helpers.h" @@ -81,6 +82,25 @@ Mutex gKeyCreateMutex; static pthread_key_t gBgKey = -1; #endif +/* + * cpuset/sched aggregate profile mappings + */ +static const std::unordered_map<int, std::string> kCpusetProfileMap = { + {SP_DEFAULT, "CPUSET_SP_DEFAULT"}, {SP_BACKGROUND, "CPUSET_SP_BACKGROUND"}, + {SP_FOREGROUND, "CPUSET_SP_FOREGROUND"},{SP_SYSTEM, "CPUSET_SP_SYSTEM"}, + {SP_AUDIO_APP, "CPUSET_SP_FOREGROUND"}, {SP_AUDIO_SYS, "CPUSET_SP_FOREGROUND"}, + {SP_TOP_APP, "CPUSET_SP_TOP_APP"}, {SP_RT_APP, "CPUSET_SP_DEFAULT"}, + {SP_RESTRICTED, "CPUSET_SP_RESTRICTED"} +}; + +static const std::unordered_map<int, std::string> kSchedProfileMap = { + {SP_DEFAULT, "SCHED_SP_DEFAULT"}, {SP_BACKGROUND, "SCHED_SP_BACKGROUND"}, + {SP_FOREGROUND, "SCHED_SP_FOREGROUND"}, {SP_SYSTEM, "SCHED_SP_DEFAULT"}, + {SP_AUDIO_APP, "SCHED_SP_FOREGROUND"}, {SP_AUDIO_SYS, "SCHED_SP_FOREGROUND"}, + {SP_TOP_APP, "SCHED_SP_TOP_APP"}, {SP_RT_APP, "SCHED_SP_RT_APP"}, + {SP_RESTRICTED, "SCHED_SP_DEFAULT"} +}; + // 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) { @@ -206,8 +226,9 @@ void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int tid, jint if (!verifyGroup(env, grp)) { return; } - SchedPolicy sp = (SchedPolicy) grp; - int res = set_sched_policy(tid, sp); + + int res = SetTaskProfiles(tid, {kSchedProfileMap.at(grp)}, true) ? 0 : -1; + if (res != NO_ERROR) { signalExceptionForGroupError(env, -res, tid); } @@ -219,14 +240,9 @@ void android_os_Process_setThreadGroupAndCpuset(JNIEnv* env, jobject clazz, int if (!verifyGroup(env, grp)) { return; } - SchedPolicy sp = (SchedPolicy) grp; - int res = set_sched_policy(tid, sp); - if (res != NO_ERROR) { - signalExceptionForGroupError(env, -res, tid); - } + int res = SetTaskProfiles(tid, {kCpusetProfileMap.at(grp)}, true) ? 0 : -1; - res = set_cpuset_policy(tid, sp); if (res != NO_ERROR) { signalExceptionForGroupError(env, -res, tid); } @@ -239,7 +255,11 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin char proc_path[255]; struct dirent *de; - if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) { + if (!verifyGroup(env, grp)) { + return; + } + + if (grp == SP_FOREGROUND) { signalExceptionForGroupError(env, EINVAL, pid); return; } @@ -249,10 +269,6 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin grp = SP_FOREGROUND; isDefault = true; } - if (!verifyGroup(env, grp)) { - return; - } - SchedPolicy sp = (SchedPolicy) grp; if (kDebugPolicy) { char cmdline[32]; @@ -268,7 +284,7 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin close(fd); } - if (sp == SP_BACKGROUND) { + if (grp == SP_BACKGROUND) { ALOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline); } else { ALOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline); @@ -286,6 +302,7 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin while ((de = readdir(d))) { int t_pid; int t_pri; + int err; if (de->d_name[0] == '.') continue; @@ -311,28 +328,16 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin if (t_pri >= ANDROID_PRIORITY_BACKGROUND) { // This task wants to stay at background // update its cpuset so it doesn't only run on bg core(s) - if (cpusets_enabled()) { - int err = set_cpuset_policy(t_pid, sp); - if (err != NO_ERROR) { - signalExceptionForGroupError(env, -err, t_pid); - break; - } + err = SetTaskProfiles(t_pid, {kCpusetProfileMap.at(grp)}, true) ? 0 : -1; + if (err != NO_ERROR) { + signalExceptionForGroupError(env, -err, t_pid); + break; } continue; } } - int err; - - if (cpusets_enabled()) { - // set both cpuset and cgroup for general threads - err = set_cpuset_policy(t_pid, sp); - if (err != NO_ERROR) { - signalExceptionForGroupError(env, -err, t_pid); - break; - } - } - err = set_sched_policy(t_pid, sp); + err = SetTaskProfiles(t_pid, {kCpusetProfileMap.at(grp)}, true) ? 0 : -1; if (err != NO_ERROR) { signalExceptionForGroupError(env, -err, t_pid); break; |