From 5b4edd60380f68d5ecd054c21fe3dae9fde84369 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Thu, 13 Jun 2019 10:42:48 -0700 Subject: [PATCH] Fix the missing longwake finish event problem. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When starting a new batteystats history file, before inserting a new CMD_CURRENT_TIME HistoryItem at beginning of the history buffer, make a copy of the current HistoryItem. Otherwise the current HistoryItem could be overriden by the CMD_CURRENT_TIME and we lose a HistoryItem. In b/130083227, the longwake start event happends before opening the new history file, after CMD_CURRENT_TIME event, the longwake finish event is missing. The fix is to make a copy of current HistoryItem before adding CMD_CURRENT_TIME event. Change-Id: I5048f98b6b47f54336fb01e354fb5a6284c84b43 Fix: 130083227 Test: b/130083227 can be reproduced by createFakeHistoryEvents() call, missing longwake finish event can be seen after "TIME:" record. With the fix, no more missing longwake finish event after "TIME:" record. “adb shell dumpsys batterystats --history-create-events 50000” to create enough events to start new history files, “adb shell dumpsys batterystats --history” to observe all history records. --- core/java/com/android/internal/os/BatteryStatsImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index c2e37d5adca86..3a7caa4c2fc02 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -3609,8 +3609,8 @@ public class BatteryStatsImpl extends BatteryStats { public void createFakeHistoryEvents(long numEvents) { for(long i = 0; i < numEvents; i++) { - noteWifiOnLocked(); - noteWifiOffLocked(); + noteLongPartialWakelockStart("name1", "historyName1", 1000); + noteLongPartialWakelockFinish("name1", "historyName1", 1000); } } @@ -3693,9 +3693,10 @@ public class BatteryStatsImpl extends BatteryStats { mHistoryBufferLastPos = -1; final long elapsedRealtime = mClocks.elapsedRealtime(); final long uptime = mClocks.uptimeMillis(); + HistoryItem newItem = new HistoryItem(); + newItem.setTo(cur); startRecordingHistory(elapsedRealtime, uptime, false); - - addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE, cur); + addHistoryBufferLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE, newItem); return; }