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

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

Merge "Keep the sched policy for a thread of low priority"

parents b669efd8 e38dcaa3
Loading
Loading
Loading
Loading
+40 −18
Original line number Diff line number Diff line
@@ -247,10 +247,8 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin
        return;
    }

    bool isDefault = false;
    if (grp < 0) {
        grp = SP_FOREGROUND;
        isDefault = true;
    }

    if (kDebugPolicy) {
@@ -285,7 +283,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;
        std::string taskprofile;

        if (de->d_name[0] == '.')
            continue;
@@ -307,25 +305,49 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin
            }
        }

        if (isDefault) {
            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)
                err = SetTaskProfiles(t_pid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}, true) ? 0 : -1;
                if (err != NO_ERROR) {
                    signalExceptionForGroupError(env, -err, t_pid);
        errno = 0;
        // grp == SP_BACKGROUND. Set background cpuset policy profile for all threads.
        if (grp == SP_BACKGROUND) {
            if (!SetTaskProfiles(t_pid, {"CPUSET_SP_BACKGROUND"}, true)) {
                signalExceptionForGroupError(env, errno ? errno : EPERM, t_pid);
                break;
            }
            continue;
        }
        }

        err = SetTaskProfiles(t_pid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}, true) ? 0 : -1;
        if (err != NO_ERROR) {
            signalExceptionForGroupError(env, -err, t_pid);
        // grp != SP_BACKGROUND. Only change the cpuset cgroup for low priority thread, so it could
        // preserve it sched policy profile setting.
        if (t_pri >= ANDROID_PRIORITY_BACKGROUND) {
            switch (grp) {
                case SP_SYSTEM:
                    taskprofile = "ServiceCapacityLow";
                    break;
                case SP_RESTRICTED:
                    taskprofile = "ServiceCapacityRestricted";
                    break;
                case SP_FOREGROUND:
                case SP_AUDIO_APP:
                case SP_AUDIO_SYS:
                    taskprofile = "ProcessCapacityHigh";
                    break;
                case SP_TOP_APP:
                    taskprofile = "ProcessCapacityMax";
                    break;
                default:
                    taskprofile = "ProcessCapacityNormal";
                    break;
            }

            if (!SetTaskProfiles(t_pid, {taskprofile}, true)) {
                signalExceptionForGroupError(env, errno ? errno : EPERM, t_pid);
                break;
            }
        // Change the cpuset policy profile for non-low priority thread according to the grp
        } else {
            if (!SetTaskProfiles(t_pid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}, true)) {
                signalExceptionForGroupError(env, errno ? errno : EPERM, t_pid);
                break;
            }
        }
    }
    closedir(d);
}