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

Commit 7aae3874 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Fix flaky BstatsCpuTimesValidationTest test

The change only affects the explicit reset flow, which
is performed at the start of each test method in this test.

Bug: 405208819
Test: atest --rerun-until-failure 100 PowerStatsTests
Flag: EXEMPT bugfix
Change-Id: I16997f969460dba8541a3d3ae75745473deda1af
parent 46bca737
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -14424,6 +14424,7 @@ public class BatteryStatsImpl extends BatteryStats {
     * @param updatedUids If not null, then the uids found in the snapshot will be added to this.
     */
    @VisibleForTesting
    @SuppressWarnings("GuardedBy")  // errorprone false positive on readDelta
    public void readKernelUidCpuTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers,
            @Nullable SparseLongArray updatedUids, boolean onBattery) {
        mTempTotalCpuUserTimeUs = mTempTotalCpuSystemTimeUs = 0;
@@ -14431,7 +14432,13 @@ public class BatteryStatsImpl extends BatteryStats {
        final long startTimeMs = mClock.uptimeMillis();
        final long elapsedRealtimeMs = mClock.elapsedRealtime();
        mCpuUidUserSysTimeReader.readDelta(false, (uid, timesUs) -> {
        // When mIgnoreNextExternalStats is set, we are supposed to establish a CPU stats baseline
        // by force reading the delta. In this case we ignore the delta itself, because it
        // represents data collected before the current battery session started
        mCpuUidUserSysTimeReader.readDelta(mIgnoreNextExternalStats /* force */, (uid, timesUs) -> {
            if (mIgnoreNextExternalStats) {
                return;
            }
            long userTimeUs = timesUs[0], systemTimeUs = timesUs[1];
            uid = mapUid(uid);
@@ -14489,7 +14496,7 @@ public class BatteryStatsImpl extends BatteryStats {
            Slog.d(TAG, "Reading cpu stats took " + elapsedTimeMs + "ms");
        }
        if (numWakelocks > 0) {
        if (numWakelocks > 0 && !mIgnoreNextExternalStats) {
            // Distribute a portion of the total cpu time to wakelock holders.
            mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
            mTempTotalCpuSystemTimeUs =
@@ -14537,6 +14544,7 @@ public class BatteryStatsImpl extends BatteryStats {
     * @param powerAccumulator object to accumulate the estimated cluster charge consumption.
     */
    @VisibleForTesting
    @SuppressWarnings("GuardedBy")    // errorprone false positive on readDelta
    public void readKernelUidCpuFreqTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers,
            boolean onBattery, boolean onBatteryScreenOff,
            @Nullable CpuDeltaPowerAccumulator powerAccumulator) {
@@ -14549,8 +14557,12 @@ public class BatteryStatsImpl extends BatteryStats {
        final long startTimeMs = mClock.uptimeMillis();
        final long elapsedRealtimeMs = mClock.elapsedRealtime();
        // If power is being accumulated for attribution, data needs to be read immediately.
        final boolean forceRead = powerAccumulator != null;
        final boolean forceRead = powerAccumulator != null || mIgnoreNextExternalStats;
        mCpuUidFreqTimeReader.readDelta(forceRead, (uid, cpuFreqTimeMs) -> {
            if (mIgnoreNextExternalStats) {
                return;
            }
            uid = mapUid(uid);
            if (Process.isIsolated(uid)) {
                if (DEBUG) Slog.d(TAG, "Got freq readings for an isolated uid: " + uid);
+10 −31
Original line number Diff line number Diff line
@@ -209,9 +209,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOn();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        doSomeWork();
        forceStop();

@@ -237,9 +234,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOff();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        doSomeWork();
        forceStop();

@@ -271,9 +265,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOn();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        doSomeWorkInIsolatedProcess();
        forceStop();

@@ -299,9 +290,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOn();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_TOP));

@@ -332,9 +320,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOn();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_TOP));

@@ -373,9 +358,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOff();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_TOP_SLEEPING));

@@ -406,9 +388,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOff();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_FOREGROUND_SERVICE));

@@ -439,9 +418,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOn();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_FOREGROUND));

@@ -472,9 +448,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOff();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_BACKGROUND));

@@ -505,9 +478,6 @@ public class BstatsCpuTimesValidationTest {
        batteryOnScreenOn();
        forceStop();
        resetBatteryStats();
        final long[] initialSnapshot = getAllCpuFreqTimes(sTestPkgUid);
        assertNull("Initial snapshot should be null, initial=" + Arrays.toString(initialSnapshot),
                initialSnapshot);
        assertNull("Initial top state snapshot should be null",
                getAllCpuFreqTimes(sTestPkgUid, PROCESS_STATE_CACHED));

@@ -802,7 +772,16 @@ public class BstatsCpuTimesValidationTest {
    }

    private void resetBatteryStats() throws Exception {
        assertDelayedCondition("", () -> {
            executeCmd("dumpsys batterystats --reset");
            // Wait for all residual CPU cycles to taper off
            long[] cpuFreqTimes = getAllCpuFreqTimes(sTestPkgUid);
            if (cpuFreqTimes == null) {
                return null;
            }
            return "Unexpected CPU cycles for UID = " + sTestPkgUid + ": "
                    + Arrays.toString(cpuFreqTimes);
        });
    }

    private void batteryOnScreenOn() throws Exception {