Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0401f6e4 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge "Limit the number of iterations in BatteryStatsHistoryIterator" into main

parents 4fb09d57 460c76cf
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -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.
         */
@@ -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.
+9 −0
Original line number Diff line number Diff line
@@ -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) {
@@ -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
@@ -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;
@@ -109,6 +117,7 @@ public class BatteryStatsHistoryIterator implements Iterator<BatteryStats.Histor
                break;
            }
            if (mHistoryItem.time >= mStartTimeMs) {
                mItemIndex++;
                mNextItemReady = true;
                return;
            }
+1 −0
Original line number Diff line number Diff line
@@ -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;
    }
+3 −1
Original line number Diff line number Diff line
@@ -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;
        }
    }

+9 −3
Original line number Diff line number Diff line
@@ -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;
@@ -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);