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

Commit a5c336c3 authored by Wei Wang's avatar Wei Wang
Browse files

Move ProcessCpuTracker.init into CpuTracker thread

ProcessCpuTracker.init is slow to start, moving it out of main thread
will accelerate the boot up. ProcessCpuTracker.init is protected with
synchronized keyword, so once the CpuTracker thread is up and running,
we should not see any racing condition caused by this move.

This changes saves ~140ms starting time on marlin

Test: run test frameworks/base/tests/ActivityTests
Bug: 33681304
Change-Id: I260a57ca721999be3a84c0e43837d684e47e3941
parent 220c4155
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -257,6 +257,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.CountDownLatch;
import dalvik.system.VMRuntime;
@@ -1465,6 +1466,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            MONITOR_THREAD_CPU_USAGE);
    final AtomicLong mLastCpuTime = new AtomicLong(0);
    final AtomicBoolean mProcessCpuMutexFree = new AtomicBoolean(true);
    final CountDownLatch mProcessCpuInitLatch = new CountDownLatch(1);
    long mLastWriteTime = 0;
@@ -2647,13 +2649,9 @@ public class ActivityManagerService extends IActivityManager.Stub
        }
        mTrackingAssociations = "1".equals(SystemProperties.get("debug.track-associations"));
        mTempConfig.setToDefaults();
        mTempConfig.setLocales(LocaleList.getDefault());
        mConfigurationSeq = mTempConfig.seq = 1;
        mProcessCpuTracker.init();
        mStackSupervisor = new ActivityStackSupervisor(this);
        mStackSupervisor.onConfigurationChanged(mTempConfig);
        mKeyguardController = mStackSupervisor.mKeyguardController;
@@ -2667,6 +2665,10 @@ public class ActivityManagerService extends IActivityManager.Stub
        mProcessCpuThread = new Thread("CpuTracker") {
            @Override
            public void run() {
                synchronized (mProcessCpuTracker) {
                    mProcessCpuInitLatch.countDown();
                    mProcessCpuTracker.init();
                }
                while (true) {
                    try {
                        try {
@@ -2714,6 +2716,16 @@ public class ActivityManagerService extends IActivityManager.Stub
        mAppOpsService.publish(mContext);
        Slog.d("AppOps", "AppOpsService published");
        LocalServices.addService(ActivityManagerInternal.class, new LocalService());
        // Wait for the synchronized block started in mProcessCpuThread,
        // so that any other acccess to mProcessCpuTracker from main thread
        // will be blocked during mProcessCpuTracker initialization.
        try {
            mProcessCpuInitLatch.await();
        } catch (InterruptedException e) {
            Slog.wtf(TAG, "Interrupted wait during start", e);
            Thread.currentThread().interrupt();
            throw new IllegalStateException("Interrupted wait during start");
        }
    }
    void onUserStoppedLocked(int userId) {