diff options
| author | 2025-03-13 16:59:00 -0700 | |
|---|---|---|
| committer | 2025-03-13 16:59:00 -0700 | |
| commit | 3a41e2240f00d6bdc65cb68657223075e9d45c02 (patch) | |
| tree | 0b991c624d03e87f4c2cf654ed3e455f04b7b848 | |
| parent | 922f6b52cb7476aee4c57a9d96e3cb2982c154b8 (diff) | |
| parent | 2fb20058ffeed3962d47c2ee6cc260513aadebc6 (diff) | |
Merge "Avoid iterating over the current history buffer twice" into main
| -rw-r--r-- | core/java/com/android/internal/os/BatteryStatsHistory.java | 15 | ||||
| -rw-r--r-- | services/tests/powerstatstests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java | 28 |
2 files changed, 31 insertions, 12 deletions
diff --git a/core/java/com/android/internal/os/BatteryStatsHistory.java b/core/java/com/android/internal/os/BatteryStatsHistory.java index 8151429f9139..f1c47a7a023b 100644 --- a/core/java/com/android/internal/os/BatteryStatsHistory.java +++ b/core/java/com/android/internal/os/BatteryStatsHistory.java @@ -760,9 +760,20 @@ public class BatteryStatsHistory { break; } - if (fragment.monotonicTimeMs >= startTimeMs && fragment != mActiveFragment) { - containers.add(new BatteryHistoryParcelContainer(fragment)); + if (fragment.monotonicTimeMs >= mHistoryBufferStartTime) { + // Do not include the backup of the current buffer, which is explicitly + // included later + continue; } + + if (i < fragments.size() - 1 + && fragments.get(i + 1).monotonicTimeMs < startTimeMs) { + // Since fragments are ordered, an early start of next fragment implies an + // early end for this one. + continue; + } + + containers.add(new BatteryHistoryParcelContainer(fragment)); } } diff --git a/services/tests/powerstatstests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java b/services/tests/powerstatstests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java index fc864dd230d9..3ed4a52bed23 100644 --- a/services/tests/powerstatstests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java +++ b/services/tests/powerstatstests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java @@ -343,12 +343,13 @@ public class BatteryStatsHistoryTest { assertThat(mReadFiles).containsExactly("123.bh", "1000.bh"); } else if (item.eventCode == HistoryItem.EVENT_ALARM) { eventsRead++; - assertThat(mReadFiles).containsExactly("123.bh", "1000.bh", "2000.bh"); + // This event is in the current buffer, so 2000.bh shouldn't be read from disk + assertThat(mReadFiles).containsExactly("123.bh", "1000.bh"); } } assertThat(eventsRead).isEqualTo(3); - assertThat(mReadFiles).containsExactly("123.bh", "1000.bh", "2000.bh", "3000.bh"); + assertThat(mReadFiles).containsExactly("123.bh", "1000.bh"); } @Test @@ -366,34 +367,41 @@ public class BatteryStatsHistoryTest { return invocation.callRealMethod(); }).when(mHistory).readFragmentToParcel(any(), any()); - BatteryStatsHistoryIterator iterator = mHistory.iterate(1000, 3000); + int eventsRead = 0; + BatteryStatsHistoryIterator iterator = mHistory.iterate(1001, 3000); while (iterator.hasNext()) { HistoryItem item = iterator.next(); if (item.eventCode == HistoryItem.EVENT_JOB_START) { fail("Event outside the range"); } else if (item.eventCode == HistoryItem.EVENT_JOB_FINISH) { + eventsRead++; assertThat(mReadFiles).containsExactly("1000.bh"); } else if (item.eventCode == HistoryItem.EVENT_ALARM) { fail("Event outside the range"); } } - assertThat(mReadFiles).containsExactly("1000.bh", "2000.bh"); + assertThat(eventsRead).isEqualTo(1); + assertThat(mReadFiles).containsExactly("1000.bh"); } private void prepareMultiFileHistory() { - mClock.realtime = 1000; - mClock.uptime = 1000; + mClock.realtime = 500; + mClock.uptime = 500; mHistory.recordEvent(mClock.realtime, mClock.uptime, BatteryStats.HistoryItem.EVENT_JOB_START, "job", 42); + mClock.realtime = 1000; + mClock.uptime = 1000; mHistory.startNextFragment(mClock.realtime); // 1000.bh - mClock.realtime = 2000; - mClock.uptime = 2000; + mClock.realtime = 1500; + mClock.uptime = 1500; mHistory.recordEvent(mClock.realtime, mClock.uptime, BatteryStats.HistoryItem.EVENT_JOB_FINISH, "job", 42); + mClock.realtime = 2000; + mClock.uptime = 2000; mHistory.startNextFragment(mClock.realtime); // 2000.bh mClock.realtime = 3000; @@ -401,8 +409,8 @@ public class BatteryStatsHistoryTest { mHistory.recordEvent(mClock.realtime, mClock.uptime, HistoryItem.EVENT_ALARM, "alarm", 42); - // Flush accumulated history to disk - mHistory.startNextFragment(mClock.realtime); + // Back up accumulated history to disk + mHistory.writeHistory(); } private void verifyActiveFile(BatteryStatsHistory history, String file) { |