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

Commit a3549830 authored by Garfield Tan's avatar Garfield Tan
Browse files

Implement launch bounds logic in Android (1/3)

Abstract threading logic that can be shared among persisters into a
common class. Later we'll introduce a new persister that stores mapping
from components to launch params (such as launch bounds and display
unique ID). That persister could reuse this class for threading which
could avoid creating another thread for persistence.

Also added some unit tests for the threading logic.

Bug: 113252871
Test: Manually test that recent tasks can still be restored across
reboots. atest PersisterThreadingTests. TaskPersisterTest is broken in
ToT.

Change-Id: I2b48593f38efcc205c2a213dbd93607c9588b12c
parent 56e55986
Loading
Loading
Loading
Loading
+277 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.am;

import android.os.Process;
import android.os.SystemClock;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.function.Predicate;

/**
 * The common threading logic for persisters to use so that they can run in the same threads.
 * Methods in this class are synchronized on its instance, so caller could also synchronize on
 * its instance to perform modifications in items.
 */
class PersisterQueue {
    private static final String TAG = "PersisterQueue";
    private static final boolean DEBUG = false;

    /** When not flushing don't write out files faster than this */
    private static final long INTER_WRITE_DELAY_MS = 500;

    /**
     * When not flushing delay this long before writing the first file out. This gives the next task
     * being launched a chance to load its resources without this occupying IO bandwidth.
     */
    private static final long PRE_TASK_DELAY_MS = 3000;

    /** The maximum number of entries to keep in the queue before draining it automatically. */
    private static final int MAX_WRITE_QUEUE_LENGTH = 6;

    /** Special value for mWriteTime to mean don't wait, just write */
    private static final long FLUSH_QUEUE = -1;

    /** An {@link WriteQueueItem} that doesn't do anything. Used to trigger {@link
     * Listener#onPreProcessItem}. */
    static final WriteQueueItem EMPTY_ITEM = () -> { };

    private final long mInterWriteDelayMs;
    private final long mPreTaskDelayMs;
    private final LazyTaskWriterThread mLazyTaskWriterThread;
    private final ArrayList<WriteQueueItem> mWriteQueue = new ArrayList<>();

    private final ArrayList<Listener> mListeners = new ArrayList<>();

    /**
     * Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
     * until the image queue is drained and all tasks needing persisting are written to disk. There
     * is no delay between writes. == 0 We are Idle. Next writes will be delayed by
     * #PRE_TASK_DELAY_MS. > 0 We are Actively writing. Next write will be at this time. Subsequent
     * writes will be delayed by #INTER_WRITE_DELAY_MS.
     */
    private long mNextWriteTime = 0;

    PersisterQueue() {
        this(INTER_WRITE_DELAY_MS, PRE_TASK_DELAY_MS);
    }

    /** Used for tests to reduce waiting time. */
    @VisibleForTesting
    PersisterQueue(long interWriteDelayMs, long preTaskDelayMs) {
        if (interWriteDelayMs < 0 || preTaskDelayMs < 0) {
            throw new IllegalArgumentException("Both inter-write delay and pre-task delay need to"
                    + "be non-negative. inter-write delay: " + interWriteDelayMs
                    + "ms pre-task delay: " + preTaskDelayMs);
        }
        mInterWriteDelayMs = interWriteDelayMs;
        mPreTaskDelayMs = preTaskDelayMs;
        mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
    }

    synchronized void startPersisting() {
        if (!mLazyTaskWriterThread.isAlive()) {
            mLazyTaskWriterThread.start();
        }
    }

    /** Stops persisting thread. Should only be used in tests. */
    @VisibleForTesting
    void stopPersisting() throws InterruptedException {
        if (!mLazyTaskWriterThread.isAlive()) {
            return;
        }

        synchronized (this) {
            mLazyTaskWriterThread.interrupt();
        }
        mLazyTaskWriterThread.join();
    }

    synchronized void addItem(WriteQueueItem item, boolean flush) {
        mWriteQueue.add(item);

        if (flush || mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
            mNextWriteTime = FLUSH_QUEUE;
        } else if (mNextWriteTime == 0) {
            mNextWriteTime = SystemClock.uptimeMillis() + mPreTaskDelayMs;
        }
        notify();
    }

    synchronized <T extends WriteQueueItem> T findLastItem(Predicate<T> predicate, Class<T> clazz) {
        for (int i = mWriteQueue.size() - 1; i >= 0; --i) {
            WriteQueueItem writeQueueItem = mWriteQueue.get(i);
            if (clazz.isInstance(writeQueueItem)) {
                T item = clazz.cast(writeQueueItem);
                if (predicate.test(item)) {
                    return item;
                }
            }
        }

        return null;
    }

    synchronized <T extends WriteQueueItem> void removeItems(Predicate<T> predicate,
            Class<T> clazz) {
        for (int i = mWriteQueue.size() - 1; i >= 0; --i) {
            WriteQueueItem writeQueueItem = mWriteQueue.get(i);
            if (clazz.isInstance(writeQueueItem)) {
                T item = clazz.cast(writeQueueItem);
                if (predicate.test(item)) {
                    if (DEBUG) Slog.d(TAG, "Removing " + item + " from write queue.");
                    mWriteQueue.remove(i);
                }
            }
        }
    }

    synchronized void flush() {
        mNextWriteTime = FLUSH_QUEUE;
        notifyAll();
        do {
            try {
                wait();
            } catch (InterruptedException e) {
            }
        } while (mNextWriteTime == FLUSH_QUEUE);
    }

    void yieldIfQueueTooDeep() {
        boolean stall = false;
        synchronized (this) {
            if (mNextWriteTime == FLUSH_QUEUE) {
                stall = true;
            }
        }
        if (stall) {
            Thread.yield();
        }
    }

    void addListener(Listener listener) {
        mListeners.add(listener);
    }

    private void processNextItem() throws InterruptedException {
        // This part is extracted into a method so that the GC can clearly see the end of the
        // scope of the variable 'item'.  If this part was in the loop in LazyTaskWriterThread, the
        // last item it processed would always "leak".
        // See https://b.corp.google.com/issues/64438652#comment7

        // If mNextWriteTime, then don't delay between each call to saveToXml().
        final WriteQueueItem item;
        synchronized (this) {
            if (mNextWriteTime != FLUSH_QUEUE) {
                // The next write we don't have to wait so long.
                mNextWriteTime = SystemClock.uptimeMillis() + mInterWriteDelayMs;
                if (DEBUG) {
                    Slog.d(TAG, "Next write time may be in " + mInterWriteDelayMs
                            + " msec. (" + mNextWriteTime + ")");
                }
            }

            while (mWriteQueue.isEmpty()) {
                if (mNextWriteTime != 0) {
                    mNextWriteTime = 0; // idle.
                    notify(); // May need to wake up flush().
                }
                // Make sure we exit this thread correctly when interrupted before going to
                // indefinite wait.
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }
                if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting indefinitely.");
                wait();
                // Invariant: mNextWriteTime is either FLUSH_QUEUE or PRE_WRITE_DELAY_MS
                // from now.
            }
            item = mWriteQueue.remove(0);

            long now = SystemClock.uptimeMillis();
            if (DEBUG) {
                Slog.d(TAG, "LazyTaskWriter: now=" + now + " mNextWriteTime=" + mNextWriteTime
                        + " mWriteQueue.size=" + mWriteQueue.size());
            }
            while (now < mNextWriteTime) {
                if (DEBUG) {
                    Slog.d(TAG, "LazyTaskWriter: waiting " + (mNextWriteTime - now));
                }
                wait(mNextWriteTime - now);
                now = SystemClock.uptimeMillis();
            }

            // Got something to do.
        }

        item.process();
    }

    interface WriteQueueItem {
        void process();
    }

    interface Listener {
        /**
         * Called before {@link PersisterQueue} tries to process next item.
         *
         * Note if the queue is empty, this callback will be called before the indefinite wait. This
         * will be called once when {@link PersisterQueue} starts the internal thread before the
         * indefinite wait.
         *
         * This callback is called w/o locking the instance of {@link PersisterQueue}.
         *
         * @param queueEmpty {@code true} if the queue is empty, which indicates {@link
         * PersisterQueue} is likely to enter indefinite wait; or {@code false} if there is still
         * item to process.
         */
        void onPreProcessItem(boolean queueEmpty);
    }

    private class LazyTaskWriterThread extends Thread {

        private LazyTaskWriterThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            try {
                while (true) {
                    final boolean probablyDone;
                    synchronized (PersisterQueue.this) {
                        probablyDone = mWriteQueue.isEmpty();
                    }

                    for (int i = mListeners.size() - 1; i >= 0; --i) {
                        mListeners.get(i).onPreProcessItem(probablyDone);
                    }

                    processNextItem();
                }
            } catch (InterruptedException e) {
                Slog.e(TAG, "Persister thread is exiting. Should never happen in prod, but"
                        + "it's OK in tests.");
            }
        }
    }
}
+1 −3
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

import static android.os.Process.SYSTEM_UID;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS_TRIM_TASKS;
@@ -55,7 +54,6 @@ import android.content.pm.ParceledListSlice;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
@@ -431,7 +429,7 @@ class RecentTasks {
    void onSystemReadyLocked() {
        loadRecentsComponent(mService.mContext.getResources());
        mTasks.clear();
        mTaskPersister.startPersisting();
        mTaskPersister.onSystemReady();
    }

    Bitmap getTaskDescriptionIcon(String path) {
+166 −298

File changed.

Preview size limit exceeded, changes collapsed.

+300 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.server.am;

import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertSame;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.os.SystemClock;
import android.platform.test.annotations.Presubmit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;

import androidx.test.filters.FlakyTest;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;

/**
 * atest PersisterQueueTests
 */
@RunWith(AndroidJUnit4.class)
@MediumTest
@Presubmit
@FlakyTest(detail = "Confirm stable in post-submit before removing")
public class PersisterQueueTests implements PersisterQueue.Listener {
    private static final long INTER_WRITE_DELAY_MS = 50;
    private static final long PRE_TASK_DELAY_MS = 300;
    // We allow at most 1s more than the expected timeout.
    private static final long TIMEOUT_ALLOWANCE = 100;

    private static final Predicate<MatchingTestItem> TEST_ITEM_PREDICATE = item -> item.mMatching;

    private AtomicInteger mItemCount;
    private CountDownLatch mSetUpLatch;
    private volatile CountDownLatch mLatch;
    private List<Boolean> mProbablyDoneResults;

    private PersisterQueue mTarget;

    @Before
    public void setUp() throws Exception {
        mItemCount = new AtomicInteger(0);
        mProbablyDoneResults = new ArrayList<>();
        mSetUpLatch = new CountDownLatch(1);

        mTarget = new PersisterQueue(INTER_WRITE_DELAY_MS, PRE_TASK_DELAY_MS);
        mTarget.addListener(this);
        mTarget.startPersisting();

        assertTrue("Target didn't call callback on start up.",
                mSetUpLatch.await(TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
    }

    @After
    public void tearDown() throws Exception {
        mTarget.stopPersisting();
    }

    @Test
    public void testCallCallbackOnStartUp() throws Exception {
        // The onPreProcessItem() must be called on start up.
        assertEquals(1, mProbablyDoneResults.size());
        // The last one must be called with probably done being true.
        assertTrue("The last probablyDone must be true.", mProbablyDoneResults.get(0));
    }

    @Test
    public void testProcessOneItem() throws Exception {
        mLatch = new CountDownLatch(1);

        final long dispatchTime = SystemClock.uptimeMillis();
        mTarget.addItem(new TestItem(), false);
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process item.", 1, mItemCount.get());
        final long processDuration = SystemClock.uptimeMillis() - dispatchTime;
        assertTrue("Target didn't wait enough time before processing item. duration: "
                        + processDuration + "ms pretask delay: " + PRE_TASK_DELAY_MS + "ms",
                processDuration >= PRE_TASK_DELAY_MS);

        // Once before processing this item, once after that.
        assertEquals(2, mProbablyDoneResults.size());
        // The last one must be called with probably done being true.
        assertTrue("The last probablyDone must be true.", mProbablyDoneResults.get(1));
    }

    @Test
    public void testProcessOneItem_Flush() throws Exception {
        mLatch = new CountDownLatch(1);

        final long dispatchTime = SystemClock.uptimeMillis();
        mTarget.addItem(new TestItem(), true);
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process item.", 1, mItemCount.get());
        final long processDuration = SystemClock.uptimeMillis() - dispatchTime;
        assertTrue("Target didn't process item immediately when flushing. duration: "
                        + processDuration + "ms pretask delay: "
                        + PRE_TASK_DELAY_MS + "ms",
                processDuration < PRE_TASK_DELAY_MS);

        // Once before processing this item, once after that.
        assertEquals(2, mProbablyDoneResults.size());
        // The last one must be called with probably done being true.
        assertTrue("The last probablyDone must be true.", mProbablyDoneResults.get(1));
    }

    @Test
    public void testProcessTwoItems() throws Exception {
        mLatch = new CountDownLatch(2);

        final long dispatchTime = SystemClock.uptimeMillis();
        mTarget.addItem(new TestItem(), false);
        mTarget.addItem(new TestItem(), false);
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + INTER_WRITE_DELAY_MS + TIMEOUT_ALLOWANCE,
                        TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process all items.", 2, mItemCount.get());
        final long processDuration = SystemClock.uptimeMillis() - dispatchTime;
        assertTrue("Target didn't wait enough time before processing item. duration: "
                        + processDuration + "ms pretask delay: " + PRE_TASK_DELAY_MS
                        + "ms inter write delay: " + INTER_WRITE_DELAY_MS + "ms",
                processDuration >= PRE_TASK_DELAY_MS + INTER_WRITE_DELAY_MS);

        // Once before processing this item, once after that.
        assertEquals(3, mProbablyDoneResults.size());
        // The first one must be called with probably done being false.
        assertFalse("The first probablyDone must be false.", mProbablyDoneResults.get(1));
        // The last one must be called with probably done being true.
        assertTrue("The last probablyDone must be true.", mProbablyDoneResults.get(2));
    }

    @Test
    public void testProcessTwoItems_OneAfterAnother() throws Exception {
        // First item
        mLatch = new CountDownLatch(1);
        long dispatchTime = SystemClock.uptimeMillis();
        mTarget.addItem(new TestItem(), false);
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        long processDuration = SystemClock.uptimeMillis() - dispatchTime;
        assertTrue("Target didn't wait enough time before processing item."
                        + processDuration + "ms pretask delay: "
                        + PRE_TASK_DELAY_MS + "ms",
                processDuration >= PRE_TASK_DELAY_MS);
        assertEquals("Target didn't process item.", 1, mItemCount.get());

        // Second item
        mLatch = new CountDownLatch(1);
        dispatchTime = SystemClock.uptimeMillis();
        // Synchronize on the instance to make sure we schedule the item after it starts to wait for
        // task indefinitely.
        synchronized (mTarget) {
            mTarget.addItem(new TestItem(), false);
        }
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process all items.", 2, mItemCount.get());
        processDuration = SystemClock.uptimeMillis() - dispatchTime;
        assertTrue("Target didn't wait enough time before processing item."
                        + processDuration + "ms pre task delay: "
                        + PRE_TASK_DELAY_MS + "ms",
                processDuration >= PRE_TASK_DELAY_MS);

        // Once before processing this item, once after that.
        assertEquals(3, mProbablyDoneResults.size());
        // The last one must be called with probably done being true.
        assertTrue("The last probablyDone must be true.", mProbablyDoneResults.get(2));
    }

    @Test
    public void testFindLastItemNotReturnDifferentType() throws Exception {
        synchronized (mTarget) {
            mTarget.addItem(new TestItem(), false);
            assertNull(mTarget.findLastItem(TEST_ITEM_PREDICATE, MatchingTestItem.class));
        }
    }

    @Test
    public void testFindLastItemNotReturnMismatchItem() throws Exception {
        synchronized (mTarget) {
            mTarget.addItem(new MatchingTestItem(false), false);
            assertNull(mTarget.findLastItem(TEST_ITEM_PREDICATE, MatchingTestItem.class));
        }
    }

    @Test
    public void testFindLastItemReturnMatchedItem() throws Exception {
        synchronized (mTarget) {
            final MatchingTestItem item = new MatchingTestItem(true);
            mTarget.addItem(item, false);
            assertSame(item, mTarget.findLastItem(TEST_ITEM_PREDICATE, MatchingTestItem.class));
        }
    }

    @Test
    public void testRemoveItemsNotRemoveDifferentType() throws Exception {
        mLatch = new CountDownLatch(1);
        synchronized (mTarget) {
            mTarget.addItem(new TestItem(), false);
            mTarget.removeItems(TEST_ITEM_PREDICATE, MatchingTestItem.class);
        }
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process item.", 1, mItemCount.get());
    }

    @Test
    public void testRemoveItemsNotRemoveMismatchedItem() throws Exception {
        mLatch = new CountDownLatch(1);
        synchronized (mTarget) {
            mTarget.addItem(new MatchingTestItem(false), false);
            mTarget.removeItems(TEST_ITEM_PREDICATE, MatchingTestItem.class);
        }
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process item.", 1, mItemCount.get());
    }

    @Test
    public void testRemoveItemsRemoveMatchedItem() throws Exception {
        mLatch = new CountDownLatch(1);
        synchronized (mTarget) {
            mTarget.addItem(new TestItem(), false);
            mTarget.addItem(new MatchingTestItem(true), false);
            mTarget.removeItems(TEST_ITEM_PREDICATE, MatchingTestItem.class);
        }
        assertTrue("Target didn't call callback enough times.",
                mLatch.await(PRE_TASK_DELAY_MS + TIMEOUT_ALLOWANCE, TimeUnit.MILLISECONDS));
        assertEquals("Target didn't process item.", 1, mItemCount.get());
    }

    @Test
    public void testFlushWaitSynchronously() {
        final long dispatchTime = SystemClock.uptimeMillis();
        mTarget.addItem(new TestItem(), false);
        mTarget.addItem(new TestItem(), false);
        mTarget.flush();
        assertEquals("Flush should wait until all items are processed before return.",
                2, mItemCount.get());
        final long processTime = SystemClock.uptimeMillis() - dispatchTime;
        assertTrue("Flush should trigger immediate flush without delays. processTime: "
                + processTime, processTime < TIMEOUT_ALLOWANCE);
    }

    @Override
    public void onPreProcessItem(boolean queueEmpty) {
        mProbablyDoneResults.add(queueEmpty);

        final CountDownLatch latch = mLatch;
        if (latch != null) {
            latch.countDown();
        }

        mSetUpLatch.countDown();
    }

    private class TestItem implements PersisterQueue.WriteQueueItem {
        @Override
        public void process() {
            mItemCount.getAndIncrement();
        }
    }

    private class MatchingTestItem extends TestItem {
        private boolean mMatching;

        private MatchingTestItem(boolean matching) {
            mMatching = matching;
        }
    }
}