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

Commit 44cb0c4e authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Fix recording of battery history tags

Bug: 230552845
Test: atest FrameworksCoreTests:BatteryStatsTests

Change-Id: I018cf3f85e8ef11167e780653863b234822be27a
parent abeb3828
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private static final int MAGIC = 0xBA757475; // 'BATSTATS'
    // Current on-disk Parcel version
    static final int VERSION = 207;
    static final int VERSION = 208;
    // The maximum number of names wakelocks we will keep track of
    // per uid; once the limit is reached, we batch the remaining wakelocks
@@ -3981,8 +3981,7 @@ public class BatteryStatsImpl extends BatteryStats {
        if (idxObj != null) {
            idx = idxObj;
            if ((idx & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
                idx &= ~TAG_FIRST_OCCURRENCE_FLAG;
                mHistoryTagPool.put(tag, idx);
                mHistoryTagPool.put(tag, idx & ~TAG_FIRST_OCCURRENCE_FLAG);
            }
            return idx;
        } else if (mNextHistoryTagIdx < HISTORY_TAG_INDEX_LIMIT) {
+19 −2
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import java.io.File;

@RunWith(AndroidJUnit4.class)
@SmallTest
@SuppressWarnings("GuardedBy")
public class BatteryStatsHistoryIteratorTest {
    private static final int APP_UID = Process.FIRST_APPLICATION_UID + 42;

@@ -124,7 +125,10 @@ public class BatteryStatsHistoryIteratorTest {
        // More than 32k strings
        final int eventCount = 0x7FFF + 100;
        for (int i = 0; i < eventCount; i++) {
            mBatteryStats.noteAlarmStartLocked("a" + i, null, APP_UID, 3_000_000, 2_000_000);
            // Names repeat in order to verify de-duping of identical history tags.
            String name = "a" + (i % 10);
            mBatteryStats.noteAlarmStartLocked(name, null, APP_UID, 3_000_000, 2_000_000);
            mBatteryStats.noteAlarmFinishLocked(name, null, APP_UID, 3_500_000, 2_500_000);
        }

        final BatteryStatsHistoryIterator iterator =
@@ -149,10 +153,23 @@ public class BatteryStatsHistoryIteratorTest {
        assertThat(item.time).isEqualTo(2_000_000);

        for (int i = 0; i < eventCount; i++) {
            String name = "a" + (i % 10);
            assertThat(iterator.next(item)).isTrue();
            // Skip a blank event inserted at the start of every buffer
            if (item.eventCode == BatteryStats.HistoryItem.EVENT_NONE) {
                assertThat(iterator.next(item)).isTrue();
            }
            assertThat(item.eventCode).isEqualTo(BatteryStats.HistoryItem.EVENT_ALARM
                    | BatteryStats.HistoryItem.EVENT_FLAG_START);
            assertThat(item.eventTag.string).isEqualTo("a" + i);
            assertThat(item.eventTag.string).isEqualTo(name);

            assertThat(iterator.next(item)).isTrue();
            if (item.eventCode == BatteryStats.HistoryItem.EVENT_NONE) {
                assertThat(iterator.next(item)).isTrue();
            }
            assertThat(item.eventCode).isEqualTo(BatteryStats.HistoryItem.EVENT_ALARM
                    | BatteryStats.HistoryItem.EVENT_FLAG_FINISH);
            assertThat(item.eventTag.string).isEqualTo(name);
        }

        assertThat(iterator.next(item)).isFalse();