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

Commit d4464c1e authored by Wei Sheng Shih's avatar Wei Sheng Shih Committed by Android (Google) Code Review
Browse files

Merge "Tuning snapshot persist queue for shell transition." into main

parents b6d46793 ac7028c2
Loading
Loading
Loading
Loading
+40 −17
Original line number Diff line number Diff line
@@ -237,22 +237,8 @@ class ActivitySnapshotController extends AbsAppSnapshotController<ActivityRecord
            }
            if (containsFile(code, userId)) {
                synchronized (mSnapshotPersistQueue.getLock()) {
                    mSnapshotPersistQueue.sendToQueueLocked(
                            new SnapshotPersistQueue.WriteQueueItem(mPersistInfoProvider) {
                                @Override
                                void write() {
                                    Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
                                            "load_activity_snapshot");
                                    final TaskSnapshot snapshot = mSnapshotLoader.loadTask(code,
                                            userId, false /* loadLowResolutionBitmap */);
                                    synchronized (mService.getWindowManagerLock()) {
                                        if (snapshot != null && !ar.finishing) {
                                            mCache.putSnapshot(ar, snapshot);
                                        }
                                    }
                                    Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
                                }
                            });
                    mSnapshotPersistQueue.insertQueueAtFirstLocked(
                            new LoadActivitySnapshotItem(ar, code, userId, mPersistInfoProvider));
                }
            }
        }
@@ -273,6 +259,42 @@ class ActivitySnapshotController extends AbsAppSnapshotController<ActivityRecord
        resetTmpFields();
    }

    class LoadActivitySnapshotItem extends SnapshotPersistQueue.WriteQueueItem {
        private final int mCode;
        private final int mUserId;
        private final ActivityRecord mActivityRecord;

        LoadActivitySnapshotItem(@NonNull ActivityRecord ar, int code, int userId,
                @NonNull PersistInfoProvider persistInfoProvider) {
            super(persistInfoProvider);
            mActivityRecord = ar;
            mCode = code;
            mUserId = userId;
        }

        @Override
        void write() {
            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
                    "load_activity_snapshot");
            final TaskSnapshot snapshot = mSnapshotLoader.loadTask(mCode,
                    mUserId, false /* loadLowResolutionBitmap */);
            synchronized (mService.getWindowManagerLock()) {
                if (snapshot != null && !mActivityRecord.finishing) {
                    mCache.putSnapshot(mActivityRecord, snapshot);
                }
            }
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        }

        @Override
        public boolean equals(Object o) {
            if (o == null || getClass() != o.getClass()) return false;
            final LoadActivitySnapshotItem other = (LoadActivitySnapshotItem) o;
            return mCode == other.mCode && mUserId == other.mUserId
                    && mPersistInfoProvider == other.mPersistInfoProvider;
        }
    }

    void recordSnapshot(ActivityRecord activity) {
        if (shouldDisableSnapshots()) {
            return;
@@ -571,7 +593,8 @@ class ActivitySnapshotController extends AbsAppSnapshotController<ActivityRecord
            for (int i = savedFileCount - 1; i > removeTillIndex; --i) {
                final UserSavedFile usf = mSavedFilesInOrder.remove(i);
                if (usf != null) {
                    mUserSavedFiles.remove(usf.mFileId);
                    final SparseArray<UserSavedFile> records = getUserFiles(usf.mUserId);
                    records.remove(usf.mFileId);
                    usfs.add(usf);
                }
            }
+36 −5
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;

import android.annotation.NonNull;
import android.annotation.TestApi;
import android.graphics.Bitmap;
import android.os.Process;
import android.os.SystemClock;
@@ -33,6 +32,7 @@ import android.util.Slog;
import android.window.TaskSnapshot;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.LocalServices;
import com.android.server.pm.UserManagerInternal;
import com.android.server.wm.BaseAppSnapshotPersister.PersistInfoProvider;
@@ -100,7 +100,7 @@ class SnapshotPersistQueue {
        }
    }

    @TestApi
    @VisibleForTesting
    void waitForQueueEmpty() {
        while (true) {
            synchronized (mLock) {
@@ -112,9 +112,20 @@ class SnapshotPersistQueue {
        }
    }

    @GuardedBy("mLock")
    void sendToQueueLocked(WriteQueueItem item) {
        mWriteQueue.offer(item);
    @VisibleForTesting
    int peekQueueSize() {
        synchronized (mLock) {
            return mWriteQueue.size();
        }
    }

    private void addToQueueInternal(WriteQueueItem item, boolean insertToFront) {
        mWriteQueue.removeFirstOccurrence(item);
        if (insertToFront) {
            mWriteQueue.addFirst(item);
        } else {
            mWriteQueue.addLast(item);
        }
        item.onQueuedLocked();
        ensureStoreQueueDepthLocked();
        if (!mPaused) {
@@ -122,6 +133,16 @@ class SnapshotPersistQueue {
        }
    }

    @GuardedBy("mLock")
    void sendToQueueLocked(WriteQueueItem item) {
        addToQueueInternal(item, false /* insertToFront */);
    }

    @GuardedBy("mLock")
    void insertQueueAtFirstLocked(WriteQueueItem item) {
        addToQueueInternal(item, true /* insertToFront */);
    }

    @GuardedBy("mLock")
    private void ensureStoreQueueDepthLocked() {
        while (mStoreQueueItems.size() > MAX_STORE_QUEUE_DEPTH) {
@@ -235,6 +256,8 @@ class SnapshotPersistQueue {
        @GuardedBy("mLock")
        @Override
        void onQueuedLocked() {
            // Remove duplicate request.
            mStoreQueueItems.remove(this);
            mStoreQueueItems.offer(this);
        }

@@ -358,6 +381,14 @@ class SnapshotPersistQueue {

            return true;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null || getClass() != o.getClass()) return false;
            final StoreWriteQueueItem other = (StoreWriteQueueItem) o;
            return mId == other.mId && mUserId == other.mUserId
                    && mPersistInfoProvider == other.mPersistInfoProvider;
        }
    }

    DeleteWriteQueueItem createDeleteWriteQueueItem(int id, int userId,
+2 −1
Original line number Diff line number Diff line
@@ -1408,12 +1408,13 @@ class Transition implements BLASTSyncEngine.TransactionReadyListener {
                    false /* forceRelayout */);
        }
        cleanUpInternal();
        mController.updateAnimatingState();

        // Handle back animation if it's already started.
        mController.mAtm.mBackNavigationController.onTransitionFinish(mTargets, this);
        mController.mFinishingTransition = null;
        mController.mSnapshotController.onTransitionFinish(mType, mTargets);
        // Resume snapshot persist thread after snapshot controller analysis this transition.
        mController.updateAnimatingState();
    }

    @Nullable
+4 −0
Original line number Diff line number Diff line
@@ -132,6 +132,10 @@ public class TaskSnapshotPersisterLoaderTest extends TaskSnapshotPersisterTestBa
        mPersister.persistSnapshot(2, mTestUserId, createSnapshot());
        mPersister.persistSnapshot(3, mTestUserId, createSnapshot());
        mPersister.persistSnapshot(4, mTestUserId, createSnapshot());
        // Verify there should only keep the latest request when received a duplicated id.
        mPersister.persistSnapshot(4, mTestUserId, createSnapshot());
        // Expected 3: One remove obsolete request, two persist request.
        assertEquals(3, mSnapshotPersistQueue.peekQueueSize());
        mSnapshotPersistQueue.setPaused(false);
        mSnapshotPersistQueue.waitForQueueEmpty();