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

Commit ebb62de7 authored by Sanath Kumar's avatar Sanath Kumar
Browse files

Refactor ThermalStatusRestriction

Encapsulate checking of Job Bias(Procstate) in ThermalStatusRestriction.
This improves extensibility of the code.

Test: Unit test ThermalStatusRestrictionTest and JobSchedulerServiceTest
passes
Bug: 315157163

Change-Id: Ide560967e8a0448f02de53502d414f703a34618a
parent 11871ce2
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -1375,8 +1375,10 @@ class JobConcurrencyManager {
            final JobServiceContext jsc = mActiveServices.get(i);
            final JobStatus jobStatus = jsc.getRunningJobLocked();

            if (jobStatus != null && !jsc.isWithinExecutionGuaranteeTime()
                    && restriction.isJobRestricted(jobStatus)) {
            if (jobStatus != null
                    && !jsc.isWithinExecutionGuaranteeTime()
                    && restriction.isJobRestricted(
                            jobStatus, mService.evaluateJobBiasLocked(jobStatus))) {
                jsc.cancelExecutingJobLocked(restriction.getStopReason(),
                        restriction.getInternalReason(),
                        JobParameters.getInternalReasonCodeDescription(
+6 −10
Original line number Diff line number Diff line
@@ -310,7 +310,8 @@ public class JobSchedulerService extends com.android.server.SystemService
     * Note: do not add to or remove from this list at runtime except in the constructor, because we
     * do not synchronize access to this list.
     */
    private final List<JobRestriction> mJobRestrictions;
    @VisibleForTesting
    final List<JobRestriction> mJobRestrictions;

    @GuardedBy("mLock")
    @VisibleForTesting
@@ -3498,8 +3499,6 @@ public class JobSchedulerService extends com.android.server.SystemService

    /**
     * Check if a job is restricted by any of the declared {@link JobRestriction JobRestrictions}.
     * Note, that the jobs with {@link JobInfo#BIAS_FOREGROUND_SERVICE} bias or higher may not
     * be restricted, thus we won't even perform the check, but simply return null early.
     *
     * @param job to be checked
     * @return the first {@link JobRestriction} restricting the given job that has been found; null
@@ -3508,13 +3507,9 @@ public class JobSchedulerService extends com.android.server.SystemService
     */
    @GuardedBy("mLock")
    JobRestriction checkIfRestricted(JobStatus job) {
        if (evaluateJobBiasLocked(job) >= JobInfo.BIAS_FOREGROUND_SERVICE) {
            // Jobs with BIAS_FOREGROUND_SERVICE or higher should not be restricted
            return null;
        }
        for (int i = mJobRestrictions.size() - 1; i >= 0; i--) {
            final JobRestriction restriction = mJobRestrictions.get(i);
            if (restriction.isJobRestricted(job)) {
            if (restriction.isJobRestricted(job, evaluateJobBiasLocked(job))) {
                return restriction;
            }
        }
@@ -4221,6 +4216,7 @@ public class JobSchedulerService extends com.android.server.SystemService
        return curBias;
    }

    /** Gets and returns the adjusted Job Bias **/
    int evaluateJobBiasLocked(JobStatus job) {
        int bias = job.getBias();
        if (bias >= JobInfo.BIAS_BOUND_FOREGROUND_SERVICE) {
@@ -5907,7 +5903,7 @@ public class JobSchedulerService extends com.android.server.SystemService
                    if (isRestricted) {
                        for (int i = mJobRestrictions.size() - 1; i >= 0; i--) {
                            final JobRestriction restriction = mJobRestrictions.get(i);
                            if (restriction.isJobRestricted(job)) {
                            if (restriction.isJobRestricted(job, evaluateJobBiasLocked(job))) {
                                final int reason = restriction.getInternalReason();
                                pw.print(" ");
                                pw.print(JobParameters.getInternalReasonCodeDescription(reason));
@@ -6240,7 +6236,7 @@ public class JobSchedulerService extends com.android.server.SystemService
                        proto.write(JobSchedulerServiceDumpProto.JobRestriction.REASON,
                                restriction.getInternalReason());
                        proto.write(JobSchedulerServiceDumpProto.JobRestriction.IS_RESTRICTING,
                                restriction.isJobRestricted(job));
                                restriction.isJobRestricted(job, evaluateJobBiasLocked(job)));
                        proto.end(restrictionsToken);
                    }

+2 −1
Original line number Diff line number Diff line
@@ -62,10 +62,11 @@ public abstract class JobRestriction {
     * fine with it).
     *
     * @param job to be checked
     * @param bias job bias to be checked
     * @return false if the {@link JobSchedulerService} should not schedule this job at the moment,
     * true - otherwise
     */
    public abstract boolean isJobRestricted(JobStatus job);
    public abstract boolean isJobRestricted(JobStatus job, int bias);

    /** Dump any internal constants the Restriction may have. */
    public abstract void dumpConstants(IndentingPrintWriter pw);
+5 −1
Original line number Diff line number Diff line
@@ -85,7 +85,11 @@ public class ThermalStatusRestriction extends JobRestriction {
    }

    @Override
    public boolean isJobRestricted(JobStatus job) {
    public boolean isJobRestricted(JobStatus job, int bias) {
        if (bias >= JobInfo.BIAS_FOREGROUND_SERVICE) {
            // Jobs with BIAS_FOREGROUND_SERVICE or higher should not be restricted
            return false;
        }
        if (mThermalStatus >= UPPER_THRESHOLD) {
            return true;
        }
+92 −0
Original line number Diff line number Diff line
@@ -85,6 +85,8 @@ import com.android.server.SystemServiceManager;
import com.android.server.job.controllers.ConnectivityController;
import com.android.server.job.controllers.JobStatus;
import com.android.server.job.controllers.QuotaController;
import com.android.server.job.restrictions.JobRestriction;
import com.android.server.job.restrictions.ThermalStatusRestriction;
import com.android.server.pm.UserManagerInternal;
import com.android.server.usage.AppStandbyInternal;

@@ -2385,6 +2387,96 @@ public class JobSchedulerServiceTest {
        assertEquals(JobScheduler.PENDING_JOB_REASON_USER, mService.getPendingJobReason(job2b));
    }

    /**
     * Unit tests {@link JobSchedulerService#checkIfRestricted(JobStatus)} with single {@link
     * JobRestriction} registered.
     */
    @Test
    public void testCheckIfRestrictedSingleRestriction() {
        int bias = JobInfo.BIAS_BOUND_FOREGROUND_SERVICE;
        JobStatus fgsJob =
                createJobStatus(
                        "testCheckIfRestrictedSingleRestriction", createJobInfo(1).setBias(bias));
        ThermalStatusRestriction mockThermalStatusRestriction =
                mock(ThermalStatusRestriction.class);
        mService.mJobRestrictions.clear();
        mService.mJobRestrictions.add(mockThermalStatusRestriction);
        when(mockThermalStatusRestriction.isJobRestricted(fgsJob, bias)).thenReturn(true);

        synchronized (mService.mLock) {
            assertEquals(mService.checkIfRestricted(fgsJob), mockThermalStatusRestriction);
        }

        when(mockThermalStatusRestriction.isJobRestricted(fgsJob, bias)).thenReturn(false);
        synchronized (mService.mLock) {
            assertNull(mService.checkIfRestricted(fgsJob));
        }
    }

    /**
     * Unit tests {@link JobSchedulerService#checkIfRestricted(JobStatus)} with multiple {@link
     * JobRestriction} registered.
     */
    @Test
    public void testCheckIfRestrictedMultipleRestrictions() {
        int bias = JobInfo.BIAS_BOUND_FOREGROUND_SERVICE;
        JobStatus fgsJob =
                createJobStatus(
                        "testGetMinJobExecutionGuaranteeMs", createJobInfo(1).setBias(bias));
        JobRestriction mock1JobRestriction = mock(JobRestriction.class);
        JobRestriction mock2JobRestriction = mock(JobRestriction.class);
        mService.mJobRestrictions.clear();
        mService.mJobRestrictions.add(mock1JobRestriction);
        mService.mJobRestrictions.add(mock2JobRestriction);

        // Jobs will be restricted if any one of the registered {@link JobRestriction}
        // reports true.
        when(mock1JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(true);
        when(mock2JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(false);
        synchronized (mService.mLock) {
            assertEquals(mService.checkIfRestricted(fgsJob), mock1JobRestriction);
        }

        when(mock1JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(false);
        when(mock2JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(true);
        synchronized (mService.mLock) {
            assertEquals(mService.checkIfRestricted(fgsJob), mock2JobRestriction);
        }

        when(mock1JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(false);
        when(mock2JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(false);
        synchronized (mService.mLock) {
            assertNull(mService.checkIfRestricted(fgsJob));
        }

        when(mock1JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(true);
        when(mock2JobRestriction.isJobRestricted(fgsJob, bias)).thenReturn(true);
        synchronized (mService.mLock) {
            assertNotEquals(mService.checkIfRestricted(fgsJob), mock1JobRestriction);
        }
    }

    /**
     * Jobs with foreground service and top app biases must not be restricted. Modify the test
     * accordingly, if this assumption changes in the future.
     */
    @Test
    public void testCheckIfRestricted_highJobBias() {
        JobStatus fgsJob =
                createJobStatus(
                        "testCheckIfRestrictedJobBiasFgs",
                        createJobInfo(1).setBias(JobInfo.BIAS_FOREGROUND_SERVICE));
        JobStatus topAppJob =
                createJobStatus(
                        "testCheckIfRestrictedJobBiasTopApp",
                        createJobInfo(1).setBias(JobInfo.BIAS_TOP_APP));

        synchronized (mService.mLock) {
            assertNull(mService.checkIfRestricted(fgsJob));
            assertNull(mService.checkIfRestricted(topAppJob));
        }
    }

    private void setBatteryLevel(int level) {
        doReturn(level).when(mBatteryManagerInternal).getBatteryLevel();
        mService.mBatteryStateTracker
Loading