From 3e6494eb3e59ba4f7d7e26b5c1c5b3378efdf9e6 Mon Sep 17 00:00:00 2001 From: Marcin Oczeretko Date: Mon, 10 Sep 2018 18:06:52 +0100 Subject: [PATCH] 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 --- cmds/statsd/src/atoms.proto | 13 +++++++++- .../src/external/StatsPullerManager.cpp | 2 +- .../com/android/internal/os/LooperStats.java | 25 ++++++++++--------- .../android/internal/os/LooperStatsTest.java | 14 +++++++++++ .../server/stats/StatsCompanionService.java | 6 +++-- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto index 6ab4dd9151786..ab12035074f14 100644 --- a/cmds/statsd/src/atoms.proto +++ b/cmds/statsd/src/atoms.proto @@ -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; } /** diff --git a/cmds/statsd/src/external/StatsPullerManager.cpp b/cmds/statsd/src/external/StatsPullerManager.cpp index f6ba0b6017be2..7f4a7836a8705 100644 --- a/cmds/statsd/src/external/StatsPullerManager.cpp +++ b/cmds/statsd/src/external/StatsPullerManager.cpp @@ -184,7 +184,7 @@ const std::map 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 diff --git a/core/java/com/android/internal/os/LooperStats.java b/core/java/com/android/internal/os/LooperStats.java index 02a8b224e3c2e..0650d0af7caf6 100644 --- a/core/java/com/android/internal/os/LooperStats.java +++ b/core/java/com/android/internal/os/LooperStats.java @@ -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 getEntries() { - final ArrayList entries; + final ArrayList 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; + } + + private void maybeAddSpecialEntry(List exportedEntries, Entry specialEntry) { + synchronized (specialEntry) { + if (specialEntry.messageCount > 0 || specialEntry.exceptionCount > 0) { + exportedEntries.add(new ExportedEntry(specialEntry)); } } - if (mHashCollisionEntry.messageCount > 0 || mHashCollisionEntry.exceptionCount > 0) { - synchronized (mHashCollisionEntry) { - entries.add(new ExportedEntry(mHashCollisionEntry)); - } - } - return entries; } /** Removes all collected data. */ diff --git a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java index 0eb3d06e79de5..565a3ecd0411e 100644 --- a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java +++ b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java @@ -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 entries = looperStats.getEntries(); + assertThat(entries).hasSize(0); + } + private static void assertThrows(Class exceptionClass, Runnable r) { try { r.run(); diff --git a/services/core/java/com/android/server/stats/StatsCompanionService.java b/services/core/java/com/android/server/stats/StatsCompanionService.java index 20918995a8ee6..a4d42a127cd64 100644 --- a/services/core/java/com/android/server/stats/StatsCompanionService.java +++ b/services/core/java/com/android/server/stats/StatsCompanionService.java @@ -976,10 +976,11 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { } List 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); } }