Loading core/java/com/android/internal/os/BatteryStatsHistory.java +21 −0 Original line number Diff line number Diff line Loading @@ -180,6 +180,11 @@ public class BatteryStatsHistory { * Persistent storage for battery history fragments */ public interface BatteryHistoryStore { /** * Returns the maximum amount of storage that can be occupied by the battery history story. */ int getMaxHistorySize(); /** * Returns the table of contents, in the chronological order. */ Loading Loading @@ -515,6 +520,22 @@ public class BatteryStatsHistory { mMaxHistoryBufferSize = maxHistoryBufferSize; } /** * Returns a high estimate of how many items are currently included in the battery history. */ public int getEstimatedItemCount() { int estimatedBytes = mHistoryBuffer.dataSize(); if (mStore != null) { estimatedBytes += mStore.getMaxHistorySize() * 10; // account for the compression ratio } if (mHistoryParcels != null) { for (int i = mHistoryParcels.size() - 1; i >= 0; i--) { estimatedBytes += mHistoryParcels.get(i).dataSize(); } } return estimatedBytes / 4; // Minimum size of a history item is 4 bytes } /** * Creates a read-only copy of the battery history. Does not copy the files stored * in the system directory, so it is not safe while actively writing history. Loading core/java/com/android/internal/os/BatteryStatsHistoryIterator.java +9 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,8 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor private boolean mClosed; private long mBaseMonotonicTime; private long mBaseTimeUtc; private int mItemIndex = 0; private int mMaxHistoryItems; public BatteryStatsHistoryIterator(@NonNull BatteryStatsHistory history, long startTimeMs, long endTimeMs) { Loading @@ -54,6 +56,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor mStartTimeMs = startTimeMs; mEndTimeMs = (endTimeMs != MonotonicClock.UNDEFINED) ? endTimeMs : Long.MAX_VALUE; mHistoryItem.clear(); mMaxHistoryItems = history.getEstimatedItemCount(); } @Override Loading @@ -80,6 +83,11 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor private void advance() { while (true) { if (mItemIndex > mMaxHistoryItems) { Slog.wtfStack(TAG, "Number of battery history items is too large: " + mItemIndex); break; } Parcel p = mBatteryStatsHistory.getNextParcel(mStartTimeMs, mEndTimeMs); if (p == null) { break; Loading Loading @@ -109,6 +117,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor break; } if (mHistoryItem.time >= mStartTimeMs) { mItemIndex++; mNextItemReady = true; return; } Loading services/core/java/com/android/server/power/stats/BatteryHistoryDirectory.java +1 −0 Original line number Diff line number Diff line Loading @@ -143,6 +143,7 @@ public class BatteryHistoryDirectory implements BatteryStatsHistory.BatteryHisto /** * Returns the maximum storage size allocated to battery history. */ @Override public int getMaxHistorySize() { return mMaxHistorySize; } Loading services/core/java/com/android/server/power/stats/processor/AggregatedPowerStats.java +3 −1 Original line number Diff line number Diff line Loading @@ -120,16 +120,18 @@ class AggregatedPowerStats { * {@link com.android.internal.os.MonotonicClock} * @param currentTime current time in milliseconds, see {@link System#currentTimeMillis()} */ void addClockUpdate(long monotonicTime, @CurrentTimeMillisLong long currentTime) { boolean addClockUpdate(long monotonicTime, @CurrentTimeMillisLong long currentTime) { ClockUpdate clockUpdate = new ClockUpdate(); clockUpdate.monotonicTime = monotonicTime; clockUpdate.currentTime = currentTime; if (mClockUpdates.size() < MAX_CLOCK_UPDATES) { mClockUpdates.add(clockUpdate); return true; } else { Slog.i(TAG, "Too many clock updates. Replacing the previous update with " + DateFormat.format("yyyy-MM-dd-HH-mm-ss", currentTime)); mClockUpdates.set(mClockUpdates.size() - 1, clockUpdate); return false; } } Loading services/core/java/com/android/server/power/stats/processor/PowerStatsAggregator.java +9 −3 Original line number Diff line number Diff line Loading @@ -98,14 +98,18 @@ public class PowerStatsAggregator { if (!startedSession) { mStats.start(item.time); mStats.addClockUpdate(item.time, item.currentTime); if (!mStats.addClockUpdate(item.time, item.currentTime)) { break; } if (baseTime == UNINITIALIZED) { baseTime = item.time; } startedSession = true; } else if (item.cmd == BatteryStats.HistoryItem.CMD_CURRENT_TIME || item.cmd == BatteryStats.HistoryItem.CMD_RESET) { mStats.addClockUpdate(item.time, item.currentTime); if (!mStats.addClockUpdate(item.time, item.currentTime)) { break; } } lastTime = item.time; Loading Loading @@ -164,7 +168,9 @@ public class PowerStatsAggregator { consumer.accept(mStats); } mStats.reset(); mStats.addClockUpdate(item.time, item.currentTime); if (!mStats.addClockUpdate(item.time, item.currentTime)) { break; } baseTime = lastTime = item.time; } mStats.addPowerStats(item.powerStats, item.time); Loading Loading
core/java/com/android/internal/os/BatteryStatsHistory.java +21 −0 Original line number Diff line number Diff line Loading @@ -180,6 +180,11 @@ public class BatteryStatsHistory { * Persistent storage for battery history fragments */ public interface BatteryHistoryStore { /** * Returns the maximum amount of storage that can be occupied by the battery history story. */ int getMaxHistorySize(); /** * Returns the table of contents, in the chronological order. */ Loading Loading @@ -515,6 +520,22 @@ public class BatteryStatsHistory { mMaxHistoryBufferSize = maxHistoryBufferSize; } /** * Returns a high estimate of how many items are currently included in the battery history. */ public int getEstimatedItemCount() { int estimatedBytes = mHistoryBuffer.dataSize(); if (mStore != null) { estimatedBytes += mStore.getMaxHistorySize() * 10; // account for the compression ratio } if (mHistoryParcels != null) { for (int i = mHistoryParcels.size() - 1; i >= 0; i--) { estimatedBytes += mHistoryParcels.get(i).dataSize(); } } return estimatedBytes / 4; // Minimum size of a history item is 4 bytes } /** * Creates a read-only copy of the battery history. Does not copy the files stored * in the system directory, so it is not safe while actively writing history. Loading
core/java/com/android/internal/os/BatteryStatsHistoryIterator.java +9 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,8 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor private boolean mClosed; private long mBaseMonotonicTime; private long mBaseTimeUtc; private int mItemIndex = 0; private int mMaxHistoryItems; public BatteryStatsHistoryIterator(@NonNull BatteryStatsHistory history, long startTimeMs, long endTimeMs) { Loading @@ -54,6 +56,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor mStartTimeMs = startTimeMs; mEndTimeMs = (endTimeMs != MonotonicClock.UNDEFINED) ? endTimeMs : Long.MAX_VALUE; mHistoryItem.clear(); mMaxHistoryItems = history.getEstimatedItemCount(); } @Override Loading @@ -80,6 +83,11 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor private void advance() { while (true) { if (mItemIndex > mMaxHistoryItems) { Slog.wtfStack(TAG, "Number of battery history items is too large: " + mItemIndex); break; } Parcel p = mBatteryStatsHistory.getNextParcel(mStartTimeMs, mEndTimeMs); if (p == null) { break; Loading Loading @@ -109,6 +117,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor break; } if (mHistoryItem.time >= mStartTimeMs) { mItemIndex++; mNextItemReady = true; return; } Loading
services/core/java/com/android/server/power/stats/BatteryHistoryDirectory.java +1 −0 Original line number Diff line number Diff line Loading @@ -143,6 +143,7 @@ public class BatteryHistoryDirectory implements BatteryStatsHistory.BatteryHisto /** * Returns the maximum storage size allocated to battery history. */ @Override public int getMaxHistorySize() { return mMaxHistorySize; } Loading
services/core/java/com/android/server/power/stats/processor/AggregatedPowerStats.java +3 −1 Original line number Diff line number Diff line Loading @@ -120,16 +120,18 @@ class AggregatedPowerStats { * {@link com.android.internal.os.MonotonicClock} * @param currentTime current time in milliseconds, see {@link System#currentTimeMillis()} */ void addClockUpdate(long monotonicTime, @CurrentTimeMillisLong long currentTime) { boolean addClockUpdate(long monotonicTime, @CurrentTimeMillisLong long currentTime) { ClockUpdate clockUpdate = new ClockUpdate(); clockUpdate.monotonicTime = monotonicTime; clockUpdate.currentTime = currentTime; if (mClockUpdates.size() < MAX_CLOCK_UPDATES) { mClockUpdates.add(clockUpdate); return true; } else { Slog.i(TAG, "Too many clock updates. Replacing the previous update with " + DateFormat.format("yyyy-MM-dd-HH-mm-ss", currentTime)); mClockUpdates.set(mClockUpdates.size() - 1, clockUpdate); return false; } } Loading
services/core/java/com/android/server/power/stats/processor/PowerStatsAggregator.java +9 −3 Original line number Diff line number Diff line Loading @@ -98,14 +98,18 @@ public class PowerStatsAggregator { if (!startedSession) { mStats.start(item.time); mStats.addClockUpdate(item.time, item.currentTime); if (!mStats.addClockUpdate(item.time, item.currentTime)) { break; } if (baseTime == UNINITIALIZED) { baseTime = item.time; } startedSession = true; } else if (item.cmd == BatteryStats.HistoryItem.CMD_CURRENT_TIME || item.cmd == BatteryStats.HistoryItem.CMD_RESET) { mStats.addClockUpdate(item.time, item.currentTime); if (!mStats.addClockUpdate(item.time, item.currentTime)) { break; } } lastTime = item.time; Loading Loading @@ -164,7 +168,9 @@ public class PowerStatsAggregator { consumer.accept(mStats); } mStats.reset(); mStats.addClockUpdate(item.time, item.currentTime); if (!mStats.addClockUpdate(item.time, item.currentTime)) { break; } baseTime = lastTime = item.time; } mStats.addPowerStats(item.powerStats, item.time); Loading