From 165c521e698a350eb9bdebeeae03afe4d12819d1 Mon Sep 17 00:00:00 2001 From: Ryan Zuklie Date: Thu, 15 Jun 2023 17:46:17 -0700 Subject: [PATCH] Trace events while not recording history. This makes the ATrace events be recorded even when the history events are not. History recording may be disabled when the device is on a charger and the batter is nearly fully charged. BatteryStats will reset history around this time so it may be fine to stop history, but the same is not true for tracing. Note: this introduces two new fields to record the state since when history is not recorded, mHistoryLastWritten isn't updated. Test: atest FrameworksServicesTests:BatteryStatsHistoryTest Bug: 286818006 (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:e1b5dc72c4eb910c3c70b7eda697a6ad6511010b) Merged-In: I52fc02cd8336adcb6f12305c584206242c40f507 Change-Id: I52fc02cd8336adcb6f12305c584206242c40f507 --- .../internal/os/BatteryStatsHistory.java | 21 +++++++++++-------- .../power/stats/BatteryStatsHistoryTest.java | 8 ------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/core/java/com/android/internal/os/BatteryStatsHistory.java b/core/java/com/android/internal/os/BatteryStatsHistory.java index fdcb87ff5e3fc..bbaaa472cbbba 100644 --- a/core/java/com/android/internal/os/BatteryStatsHistory.java +++ b/core/java/com/android/internal/os/BatteryStatsHistory.java @@ -259,6 +259,8 @@ public class BatteryStatsHistory { } private TraceDelegate mTracer; + private int mTraceLastState = 0; + private int mTraceLastState2 = 0; /** * Constructor @@ -1241,7 +1243,6 @@ public class BatteryStatsHistory { */ private void recordTraceEvents(int code, HistoryTag tag) { if (code == HistoryItem.EVENT_NONE) return; - if (!mTracer.tracingEnabled()) return; final int idx = code & HistoryItem.EVENT_TYPE_MASK; final String prefix = (code & HistoryItem.EVENT_FLAG_START) != 0 ? "+" : @@ -1270,8 +1271,6 @@ public class BatteryStatsHistory { * Writes changes to a HistoryItem state bitmap to Atrace. */ private void recordTraceCounters(int oldval, int newval, BitDescription[] descriptions) { - if (!mTracer.tracingEnabled()) return; - int diff = oldval ^ newval; if (diff == 0) return; @@ -1324,6 +1323,16 @@ public class BatteryStatsHistory { } private void writeHistoryItem(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) { + if (mTracer != null && mTracer.tracingEnabled()) { + recordTraceEvents(cur.eventCode, cur.eventTag); + recordTraceCounters(mTraceLastState, cur.states, + BatteryStats.HISTORY_STATE_DESCRIPTIONS); + recordTraceCounters(mTraceLastState2, cur.states2, + BatteryStats.HISTORY_STATE2_DESCRIPTIONS); + mTraceLastState = cur.states; + mTraceLastState2 = cur.states2; + } + if (!mHaveBatteryLevel || !mRecordingHistory) { return; } @@ -1345,12 +1354,6 @@ public class BatteryStatsHistory { + Integer.toHexString(lastDiffStates2)); } - recordTraceEvents(cur.eventCode, cur.eventTag); - recordTraceCounters(mHistoryLastWritten.states, - cur.states, BatteryStats.HISTORY_STATE_DESCRIPTIONS); - recordTraceCounters(mHistoryLastWritten.states2, - cur.states2, BatteryStats.HISTORY_STATE2_DESCRIPTIONS); - if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE && timeDiffMs < 1000 && (diffStates & lastDiffStates) == 0 && (diffStates2 & lastDiffStates2) == 0 diff --git a/services/tests/servicestests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java b/services/tests/servicestests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java index e65229f188fcc..1ba14623f04e3 100644 --- a/services/tests/servicestests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java +++ b/services/tests/servicestests/src/com/android/server/power/stats/BatteryStatsHistoryTest.java @@ -93,8 +93,6 @@ public class BatteryStatsHistoryTest { @Test public void testAtraceBinaryState1() { - mHistory.forceRecordAllHistory(); - InOrder inOrder = Mockito.inOrder(mTracer); Mockito.when(mTracer.tracingEnabled()).thenReturn(true); @@ -112,8 +110,6 @@ public class BatteryStatsHistoryTest { @Test public void testAtraceBinaryState2() { - mHistory.forceRecordAllHistory(); - InOrder inOrder = Mockito.inOrder(mTracer); Mockito.when(mTracer.tracingEnabled()).thenReturn(true); @@ -131,8 +127,6 @@ public class BatteryStatsHistoryTest { @Test public void testAtraceNumericalState() { - mHistory.forceRecordAllHistory(); - InOrder inOrder = Mockito.inOrder(mTracer); Mockito.when(mTracer.tracingEnabled()).thenReturn(true); @@ -150,8 +144,6 @@ public class BatteryStatsHistoryTest { @Test public void testAtraceInstantEvent() { - mHistory.forceRecordAllHistory(); - InOrder inOrder = Mockito.inOrder(mTracer); Mockito.when(mTracer.tracingEnabled()).thenReturn(true);