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

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

Move some tracking starts out of constructor.

Shift the tracking initiation of some controllers out of the constructor
and do them asynchronously to reduce the amount of work done inside
JobScheduler's constructor.

Bug: 309292340
Test: atest CtsJobSchedulerTestCases:JobSchedulingTest
Test: atest frameworks/base/services/tests/mockingservicestests/src/com/android/server/job
Test: atest frameworks/base/services/tests/servicestests/src/com/android/server/job
Change-Id: I7812419d7434ecf3fcb917f8c8bdd270d00bdd75
parent 418c3175
Loading
Loading
Loading
Loading
+29 −2
Original line number Diff line number Diff line
@@ -268,6 +268,7 @@ public class JobSchedulerService extends com.android.server.SystemService
    /** Master list of jobs. */
    final JobStore mJobs;
    private final CountDownLatch mJobStoreLoadedLatch;
    private final CountDownLatch mStartControllerTrackingLatch;
    /** Tracking the standby bucket state of each app */
    final StandbyTracker mStandbyTracker;
    /** Tracking amount of time each package runs for. */
@@ -2521,6 +2522,7 @@ public class JobSchedulerService extends com.android.server.SystemService
        mBatteryStateTracker.startTracking();

        // Create the controllers.
        mStartControllerTrackingLatch = new CountDownLatch(1);
        mControllers = new ArrayList<StateController>();
        mPrefetchController = new PrefetchController(this);
        mControllers.add(mPrefetchController);
@@ -2552,6 +2554,8 @@ public class JobSchedulerService extends com.android.server.SystemService
                new TareController(this, backgroundJobsController, mConnectivityController);
        mControllers.add(mTareController);

        startControllerTrackingAsync();

        mRestrictiveControllers = new ArrayList<>();
        mRestrictiveControllers.add(batteryController);
        mRestrictiveControllers.add(mConnectivityController);
@@ -2623,7 +2627,13 @@ public class JobSchedulerService extends com.android.server.SystemService
    public void onBootPhase(int phase) {
        if (PHASE_LOCK_SETTINGS_READY == phase) {
            // This is the last phase before PHASE_SYSTEM_SERVICES_READY. We need to ensure that
            // controllers have started tracking and that
            // persisted jobs are loaded before we can proceed to PHASE_SYSTEM_SERVICES_READY.
            try {
                mStartControllerTrackingLatch.await();
            } catch (InterruptedException e) {
                Slog.e(TAG, "Couldn't wait on controller tracking start latch");
            }
            try {
                mJobStoreLoadedLatch.await();
            } catch (InterruptedException e) {
@@ -2631,8 +2641,8 @@ public class JobSchedulerService extends com.android.server.SystemService
            }
        } else if (PHASE_SYSTEM_SERVICES_READY == phase) {
            mConstantsObserver.start();
            for (StateController controller : mControllers) {
                controller.onSystemServicesReady();
            for (int i = mControllers.size() - 1; i >= 0; --i) {
                mControllers.get(i).onSystemServicesReady();
            }

            mAppStateTracker = (AppStateTrackerImpl) Objects.requireNonNull(
@@ -2695,6 +2705,17 @@ public class JobSchedulerService extends com.android.server.SystemService
        }
    }

    private void startControllerTrackingAsync() {
        mHandler.post(() -> {
            synchronized (mLock) {
                for (int i = mControllers.size() - 1; i >= 0; --i) {
                    mControllers.get(i).startTrackingLocked();
                }
            }
            mStartControllerTrackingLatch.countDown();
        });
    }

    /**
     * Called when we have a job status object that we need to insert in our
     * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
@@ -5175,6 +5196,12 @@ public class JobSchedulerService extends com.android.server.SystemService
        return mTareController;
    }

    @VisibleForTesting
    protected void waitOnAsyncLoadingForTesting() throws Exception {
        mStartControllerTrackingLatch.await();
        // Ignore the job store loading for testing.
    }

    // Shell command infrastructure
    int getJobState(PrintWriter pw, String pkgName, int userId, @Nullable String namespace,
            int jobId) {
+4 −0
Original line number Diff line number Diff line
@@ -73,6 +73,10 @@ public final class BackgroundJobsController extends StateController {
                LocalServices.getService(ActivityManagerInternal.class));
        mAppStateTracker = (AppStateTrackerImpl) Objects.requireNonNull(
                LocalServices.getService(AppStateTracker.class));
    }

    @Override
    public void startTrackingLocked() {
        mAppStateTracker.addListener(mForceAppStandbyListener);
    }

+5 −1
Original line number Diff line number Diff line
@@ -78,10 +78,14 @@ public final class BatteryController extends RestrictingController {
            FlexibilityController flexibilityController) {
        super(service);
        mPowerTracker = new PowerTracker();
        mPowerTracker.startTracking();
        mFlexibilityController = flexibilityController;
    }

    @Override
    public void startTrackingLocked() {
        mPowerTracker.startTracking();
    }

    @Override
    public void maybeStartTrackingJobLocked(JobStatus taskStatus, JobStatus lastJob) {
        if (taskStatus.hasPowerConstraint()) {
+3 −0
Original line number Diff line number Diff line
@@ -106,7 +106,10 @@ public class ComponentController extends StateController {

    public ComponentController(JobSchedulerService service) {
        super(service);
    }

    @Override
    public void startTrackingLocked() {
        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
+4 −0
Original line number Diff line number Diff line
@@ -201,6 +201,10 @@ public final class FlexibilityController extends StateController {
        mPercentToDropConstraints =
                mFcConfig.DEFAULT_PERCENT_TO_DROP_FLEXIBLE_CONSTRAINTS;
        mPrefetchController = prefetchController;
    }

    @Override
    public void startTrackingLocked() {
        if (mFlexibilityEnabled) {
            mPrefetchController.registerPrefetchChangedListener(mPrefetchChangedListener);
        }
Loading