From 99ab9530d321bebfec09695efc878b844f18eea8 Mon Sep 17 00:00:00 2001 From: Olivier Gaillard Date: Mon, 4 Feb 2019 10:47:48 +0000 Subject: [PATCH] Bug fix: Do not track the looper exception if the session is not sampled Test: atest LooperStatsTest Change-Id: I1811ad12bdf9830699c615fff02f5aceea74c175 --- .../com/android/internal/os/LooperStats.java | 8 +++++--- .../android/internal/os/LooperStatsTest.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/core/java/com/android/internal/os/LooperStats.java b/core/java/com/android/internal/os/LooperStats.java index 0f0eeddf9a472..b3d9ca7670b51 100644 --- a/core/java/com/android/internal/os/LooperStats.java +++ b/core/java/com/android/internal/os/LooperStats.java @@ -126,9 +126,11 @@ public class LooperStats implements Looper.Observer { } DispatchSession session = (DispatchSession) token; - Entry entry = findEntry(msg, /* allowCreateNew= */true); - synchronized (entry) { - entry.exceptionCount++; + Entry entry = findEntry(msg, /* allowCreateNew= */session != DispatchSession.NOT_SAMPLED); + if (entry != null) { + synchronized (entry) { + entry.exceptionCount++; + } } recycleSession(session); 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 2c597b14c5573..79b8477543115 100644 --- a/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java +++ b/core/tests/coretests/src/com/android/internal/os/LooperStatsTest.java @@ -137,6 +137,26 @@ public final class LooperStatsTest { assertThat(entry.maxCpuUsageMicros).isEqualTo(0); } + @Test + public void testThrewException_notSampled() { + TestableLooperStats looperStats = new TestableLooperStats(2, 100); + + Object token = looperStats.messageDispatchStarting(); + looperStats.tickRealtime(10); + looperStats.tickThreadTime(10); + looperStats.messageDispatched(token, mHandlerFirst.obtainMessage(0)); + assertThat(looperStats.getEntries()).hasSize(1); + + // Will not be sampled so does not contribute to any entries. + Object token2 = looperStats.messageDispatchStarting(); + looperStats.tickRealtime(100); + looperStats.tickThreadTime(10); + looperStats.dispatchingThrewException( + token2, mHandlerSecond.obtainMessage(7), new ArithmeticException()); + assertThat(looperStats.getEntries()).hasSize(1); + assertThat(looperStats.getEntries().get(0).messageCount).isEqualTo(1); + } + @Test public void testMultipleMessagesDispatched() { TestableLooperStats looperStats = new TestableLooperStats(2, 100);