From 45489787d7f84a5530c1e88bebc1eb6aa614bd74 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Thu, 15 Dec 2016 23:45:17 -0800 Subject: [PATCH] Reset BatteryStats when too large for clients to handle When BatteryStats history is too large, the Settings app crashes. Reset the battery stats by marking an *OVERFLOW* event at the beginning of the newly reset history. This seems to happen in Retail mode when the devices are on charger for an extended period of time and accumulate a sufficiently large history. Resetting makes sense because the phone is most likely charged and will reset anyways when unplugged. Bug: 32540341 Test: Manual Change-Id: I8c2892458b5d9ce64b1c44aa2e2a000340e71e63 --- .../com/android/internal/os/BatteryStatsImpl.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 3b3344e7d934..3c0976c14b79 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -2877,8 +2877,22 @@ public class BatteryStatsImpl extends BatteryStats { mHistoryLastWritten.setTo(mHistoryLastLastWritten); } + boolean recordResetDueToOverflow = false; final int dataSize = mHistoryBuffer.dataSize(); - if (dataSize >= MAX_HISTORY_BUFFER) { + if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) { + // Clients can't deal with history buffers this large. This only + // really happens when the device is on charger and interacted with + // for long periods of time, like in retail mode. Since the device is + // most likely charged, when unplugged, stats would have reset anyways. + // Reset the stats and mark that we overflowed. + // b/32540341 + resetAllStatsLocked(); + + // Mark that we want to set *OVERFLOW* event and the RESET:START + // events. + recordResetDueToOverflow = true; + + } else if (dataSize >= MAX_HISTORY_BUFFER) { if (!mHistoryOverflow) { mHistoryOverflow = true; addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur); @@ -2924,9 +2938,12 @@ public class BatteryStatsImpl extends BatteryStats { return; } - if (dataSize == 0) { + if (dataSize == 0 || recordResetDueToOverflow) { // The history is currently empty; we need it to start with a time stamp. cur.currentTime = System.currentTimeMillis(); + if (recordResetDueToOverflow) { + addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur); + } addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur); } addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur); -- 2.11.0