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

Commit 3e6494eb authored by Marcin Oczeretko's avatar Marcin Oczeretko
Browse files

Prepare LooperStats to be collected as a Westworld gauge matric

- call LooperStats.reset() from StatsCompanionService
- add screen_interactive to the LooperStats atom
- temporarily set uid = 1000 to avoid statsd warnings that the uid is invalid

Bug: 113651685
Test: Manual and UTs
Change-Id: Iacf45fcb746c6bb9837456a2a0c74f5a073ff822
parent 6c109c76
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -2261,6 +2261,14 @@ message BinderCallsExceptions {
    optional int64 exception_count = 2;
}

/**
 * Pulls the statistics of message dispatching on HandlerThreads.
 *
 * Looper stats will be reset every time the data is pulled. It means it can only be pulled by one
 * config on the device.
 *
 * Next tag: 11
 */
message LooperStats {
    // Currently not collected and always set to 0.
    optional int32 uid = 1 [(is_uid) = true];
@@ -2304,8 +2312,11 @@ message LooperStats {
    // Total CPU usage of all processed message.
    // Average can be computed using recorded_total_cpu_micros /
    // recorded_message_count. Total can be computed using
    // recorded_total_cpu_micros / recorded_message_count * call_count.
    // recorded_total_cpu_micros / recorded_message_count * message_count.
    optional int64 recorded_total_cpu_micros = 9;

    // True if the screen was interactive PowerManager#isInteractive at the end of the call.
    optional bool screen_interactive = 10;
}

/**
+1 −1
Original line number Diff line number Diff line
@@ -184,7 +184,7 @@ const std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
        // looper_stats
        {android::util::LOOPER_STATS,
         {{5, 6, 7, 8, 9},
          {2, 3, 4},
          {2, 3, 4, 10},
          1 * NS_PER_SEC,
          new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
        // Disk Stats
+13 −12
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ public class LooperStats implements Looper.Observer {
        synchronized (entry) {
            entry.exceptionCount++;
        }

        recycleSession(session);
    }

@@ -116,29 +117,29 @@ public class LooperStats implements Looper.Observer {

    /** Returns an array of {@link ExportedEntry entries} with the aggregated statistics. */
    public List<ExportedEntry> getEntries() {
        final ArrayList<ExportedEntry> entries;
        final ArrayList<ExportedEntry> exportedEntries;
        synchronized (mLock) {
            final int size = mEntries.size();
            entries = new ArrayList<>(size);
            exportedEntries = new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                Entry entry = mEntries.valueAt(i);
                synchronized (entry) {
                    entries.add(new ExportedEntry(entry));
                    exportedEntries.add(new ExportedEntry(entry));
                }
            }
        }
        // Add the overflow and collision entries only if they have any data.
        if (mOverflowEntry.messageCount > 0 || mOverflowEntry.exceptionCount > 0) {
            synchronized (mOverflowEntry) {
                entries.add(new ExportedEntry(mOverflowEntry));
            }
        maybeAddSpecialEntry(exportedEntries, mOverflowEntry);
        maybeAddSpecialEntry(exportedEntries, mHashCollisionEntry);
        return exportedEntries;
    }
        if (mHashCollisionEntry.messageCount > 0 || mHashCollisionEntry.exceptionCount > 0) {
            synchronized (mHashCollisionEntry) {
                entries.add(new ExportedEntry(mHashCollisionEntry));

    private void maybeAddSpecialEntry(List<ExportedEntry> exportedEntries, Entry specialEntry) {
        synchronized (specialEntry) {
            if (specialEntry.messageCount > 0 || specialEntry.exceptionCount > 0) {
                exportedEntries.add(new ExportedEntry(specialEntry));
            }
        }
        return entries;
    }

    /** Removes all collected data. */
+14 −0
Original line number Diff line number Diff line
@@ -342,6 +342,20 @@ public final class LooperStatsTest {
        assertThat(looperStats.getEntries().get(0).messageCount).isEqualTo(2);
    }

    @Test
    public void testReset() {
        TestableLooperStats looperStats = new TestableLooperStats(1, 1);

        Object token1 = looperStats.messageDispatchStarting();
        looperStats.messageDispatched(token1, mHandlerFirst.obtainMessage(1000));
        Object token2 = looperStats.messageDispatchStarting();
        looperStats.messageDispatched(token2, mHandlerFirst.obtainMessage(2000));
        looperStats.reset();

        List<LooperStats.ExportedEntry> entries = looperStats.getEntries();
        assertThat(entries).hasSize(0);
    }

    private static void assertThrows(Class<? extends Exception> exceptionClass, Runnable r) {
        try {
            r.run();
+4 −2
Original line number Diff line number Diff line
@@ -976,10 +976,11 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }

        List<LooperStats.ExportedEntry> entries = looperStats.getEntries();
        looperStats.reset();
        long elapsedNanos = SystemClock.elapsedRealtimeNanos();
        for (LooperStats.ExportedEntry entry : entries) {
            StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 9 /* fields */);
            e.writeLong(0); // uid collection not implemented yet
            StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 10 /* fields */);
            e.writeInt(1000); // uid collection not implemented yet
            e.writeString(entry.handlerClassName);
            e.writeString(entry.threadName);
            e.writeString(entry.messageName);
@@ -988,6 +989,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            e.writeLong(entry.recordedMessageCount);
            e.writeLong(entry.totalLatencyMicros);
            e.writeLong(entry.cpuUsageMicros);
            e.writeBoolean(entry.isInteractive);
            pulledData.add(e);
        }
    }