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

Commit 28de9664 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge "Clear out battery stats for removed user more quickly"

parents 51a18cab f10585a6
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -79,7 +79,12 @@ public class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStat
    private static final long MAX_WIFI_STATS_SAMPLE_ERROR_MILLIS = 750;

    // Delay for clearing out battery stats for UIDs corresponding to a removed user
    public static final int UID_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS = 10_000;
    public static final int UID_QUICK_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS = 2_000;

    // Delay for the _final_ clean-up of battery stats after a user removal - just in case
    // some UIDs took longer than UID_QUICK_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS to
    // stop running.
    public static final int UID_FINAL_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS = 10_000;

    private final ScheduledExecutorService mExecutorService =
            Executors.newSingleThreadScheduledExecutor(
@@ -336,11 +341,20 @@ public class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStat
    @Override
    public Future<?> scheduleCleanupDueToRemovedUser(int userId) {
        synchronized (BatteryExternalStatsWorker.this) {
            // Initial quick clean-up after a user removal
            mExecutorService.schedule(() -> {
                synchronized (mStats) {
                    mStats.clearRemovedUserUidsLocked(userId);
                }
            }, UID_QUICK_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS, TimeUnit.MILLISECONDS);

            // Final clean-up after a user removal, to take care of UIDs that were running longer
            // than expected
            return mExecutorService.schedule(() -> {
                synchronized (mStats) {
                    mStats.clearRemovedUserUidsLocked(userId);
                }
            }, UID_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS, TimeUnit.MILLISECONDS);
            }, UID_FINAL_REMOVAL_AFTER_USER_REMOVAL_DELAY_MILLIS, TimeUnit.MILLISECONDS);
        }
    }

+7 −5
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import androidx.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

@@ -51,8 +50,8 @@ public class BatteryStatsUserLifecycleTests {

    private static final long POLL_INTERVAL_MS = 500;
    private static final long USER_REMOVE_TIMEOUT_MS = 5_000;
    private static final long STOP_USER_TIMEOUT_MS = 10_000;
    private static final long USER_UIDS_REMOVE_TIMEOUT_MS = 15_000;
    private static final long STOP_USER_TIMEOUT_MS = 20_000;
    private static final long USER_UIDS_REMOVE_TIMEOUT_MS = 20_000;
    private static final long BATTERYSTATS_POLLING_TIMEOUT_MS = 5_000;

    private static final String CPU_DATA_TAG = "cpu";
@@ -79,26 +78,29 @@ public class BatteryStatsUserLifecycleTests {
        batteryOnScreenOff();
    }

    @Ignore("b/244349060")
    @Test
    public void testNoCpuDataForRemovedUser() throws Exception {
        mIam.startUserInBackground(mTestUserId);
        waitUntilTrue("No uids for started user " + mTestUserId,
                () -> getNumberOfUidsInBatteryStats() > 0, BATTERYSTATS_POLLING_TIMEOUT_MS);

        final boolean[] userStopped = new boolean[1];
        CountDownLatch stopUserLatch = new CountDownLatch(1);
        mIam.stopUser(mTestUserId, true, new IStopUserCallback.Stub() {
            @Override
            public void userStopped(int userId) throws RemoteException {
                userStopped[0] = true;
                stopUserLatch.countDown();
            }

            @Override
            public void userStopAborted(int userId) throws RemoteException {
                stopUserLatch.countDown();
            }
        });
        assertTrue("User " + mTestUserId + " could not be stopped",
        assertTrue("User " + mTestUserId + " could not be stopped in " + STOP_USER_TIMEOUT_MS,
                stopUserLatch.await(STOP_USER_TIMEOUT_MS, TimeUnit.MILLISECONDS));
        assertTrue("User " + mTestUserId + " could not be stopped", userStopped[0]);

        mUm.removeUser(mTestUserId);
        waitUntilTrue("Unable to remove user " + mTestUserId, () -> {