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

Commit e7c539f8 authored by Kweku Adams's avatar Kweku Adams
Browse files

Update maybe-batching logic.

1. Don't re-evaluate state for already running jobs. If the state
   changed, JobSchedulerService would be notified about it.
2. Only send a MSG_STOP_JOB message for jobs that are actually running.
   If the job isn't running, then just remove it from the pending list
   (if applicable).

Bug: 138469672
Bug: 141645789
Test: atest CtsJobSchedulerTestCases
Change-Id: Ia0d9f8845c81fe927193e63a0957f6a3ae0e202a
parent 7efcc4df
Loading
Loading
Loading
Loading
+23 −7
Original line number Diff line number Diff line
@@ -1712,6 +1712,12 @@ public class JobSchedulerService extends com.android.server.SystemService
        return false;
    }

    /** Return {@code true} if the specified job is currently executing. */
    @GuardedBy("mLock")
    public boolean isCurrentlyRunningLocked(JobStatus job) {
        return mConcurrencyManager.isJobRunningLocked(job);
    }

    void noteJobsPending(List<JobStatus> jobs) {
        for (int i = jobs.size() - 1; i >= 0; i--) {
            JobStatus job = jobs.get(i);
@@ -1719,10 +1725,13 @@ public class JobSchedulerService extends com.android.server.SystemService
        }
    }

    private void noteJobNonPending(JobStatus job) {
        mJobPackageTracker.noteNonpending(job);
    }

    void noteJobsNonpending(List<JobStatus> jobs) {
        for (int i = jobs.size() - 1; i >= 0; i--) {
            JobStatus job = jobs.get(i);
            mJobPackageTracker.noteNonpending(job);
            noteJobNonPending(jobs.get(i));
        }
    }

@@ -2217,14 +2226,19 @@ public class JobSchedulerService extends com.android.server.SystemService
        // Functor method invoked for each job via JobStore.forEachJob()
        @Override
        public void accept(JobStatus job) {
            if (isReadyToBeExecutedLocked(job)) {
            final boolean isRunning = isCurrentlyRunningLocked(job);
            if (isReadyToBeExecutedLocked(job, false)) {
                if (mActivityManagerInternal.isAppStartModeDisabled(job.getUid(),
                        job.getJob().getService().getPackageName())) {
                    Slog.w(TAG, "Aborting job " + job.getUid() + ":"
                            + job.getJob().toString() + " -- package not allowed to start");
                    if (isRunning) {
                        mHandler.obtainMessage(MSG_STOP_JOB,
                                JobParameters.STOP_REASON_BACKGROUND_RESTRICTION, 0, job)
                                .sendToTarget();
                    } else if (mPendingJobs.remove(job)) {
                        noteJobNonPending(job);
                    }
                    return;
                }

@@ -2257,7 +2271,9 @@ public class JobSchedulerService extends com.android.server.SystemService
                } else {
                    unbatchedCount++;
                }
                if (!isRunning) {
                    runnableJobs.add(job);
                }
            } else {
                evaluateControllerStatesLocked(job);
            }
+2 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

@@ -705,7 +706,7 @@ public class JobSchedulerServiceTest {
        spyOn(mService);
        doNothing().when(mService).evaluateControllerStatesLocked(any());
        doNothing().when(mService).noteJobsPending(any());
        doReturn(true).when(mService).isReadyToBeExecutedLocked(any());
        doReturn(true).when(mService).isReadyToBeExecutedLocked(any(), anyBoolean());
        advanceElapsedClock(24 * HOUR_IN_MILLIS);

        JobSchedulerService.MaybeReadyJobQueueFunctor maybeQueueFunctor =