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

This commit is contained in:
Dmitri Plotnikov
2023-02-06 17:49:02 +00:00
committed by Android (Google) Code Review
2 changed files with 23 additions and 7 deletions

View File

@@ -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);
}
}

View File

@@ -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, () -> {