diff options
author | 2025-03-17 14:51:30 -0700 | |
---|---|---|
committer | 2025-03-17 14:51:30 -0700 | |
commit | 1e81b31a61d5d08f4e2327e49bdca4829371f353 (patch) | |
tree | a42f1fbca88f1fba20af08b483d30bc722649e12 | |
parent | 3704dfb15fa94a72baf069fd1c76ffbf66b7195f (diff) | |
parent | 51395dd8dff3839d0791e6d89817f294392d766e (diff) |
Merge "Use Background thread in LastMileLogger" into main
3 files changed, 59 insertions, 16 deletions
diff --git a/service/java/com/android/server/wifi/LastMileLogger.java b/service/java/com/android/server/wifi/LastMileLogger.java index 0c9c0addcf..49c506ed62 100644 --- a/service/java/com/android/server/wifi/LastMileLogger.java +++ b/service/java/com/android/server/wifi/LastMileLogger.java @@ -17,6 +17,7 @@ package com.android.server.wifi; +import android.os.Handler; import android.util.ArrayMap; import com.android.internal.annotations.VisibleForTesting; @@ -34,7 +35,9 @@ import java.util.Map; * Provides a facility for capturing kernel trace events related to Wifi control and data paths. */ public class LastMileLogger { - public LastMileLogger(WifiInjector injector) { + private final Handler mBackgroundHandler; + public LastMileLogger(WifiInjector injector, Handler handler) { + mBackgroundHandler = handler; File tracefsEnablePath = new File(WIFI_EVENT_ENABLE_PATH); if (tracefsEnablePath.exists()) { initLastMileLogger(injector, WIFI_EVENT_BUFFER_PATH, WIFI_EVENT_ENABLE_PATH, @@ -47,7 +50,8 @@ public class LastMileLogger { @VisibleForTesting public LastMileLogger(WifiInjector injector, String bufferPath, String enablePath, - String releasePath) { + String releasePath, Handler handler) { + mBackgroundHandler = handler; initLastMileLogger(injector, bufferPath, enablePath, releasePath); } @@ -62,16 +66,17 @@ public class LastMileLogger { boolean shouldTracingBeEnabled = anyConnectionInProgress(); - if (!wasTracingEnabled && shouldTracingBeEnabled) { - enableTracing(); - } else if (wasTracingEnabled && !shouldTracingBeEnabled) { - disableTracing(); - } - - if (event == WifiDiagnostics.CONNECTION_EVENT_FAILED - || event == WifiDiagnostics.CONNECTION_EVENT_TIMEOUT) { - mLastMileLogForLastFailure = readTrace(); - } + mBackgroundHandler.post(() -> { + if (!wasTracingEnabled && shouldTracingBeEnabled) { + enableTracing(); + } else if (wasTracingEnabled && !shouldTracingBeEnabled) { + disableTracing(); + } + if (event == WifiDiagnostics.CONNECTION_EVENT_FAILED + || event == WifiDiagnostics.CONNECTION_EVENT_TIMEOUT) { + mLastMileLogForLastFailure = readTrace(); + } + }); } private boolean anyConnectionInProgress() { diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 300f850d51..dcf4388095 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -418,7 +418,8 @@ public class WifiInjector { : maxLinesHighRam); mWifiDiagnostics = new WifiDiagnostics( mContext, this, mWifiNative, mBuildProperties, - new LastMileLogger(this), mClock, mWifiDiagnosticsHandlerThread.getLooper()); + new LastMileLogger(this, BackgroundThread.getHandler()), mClock, + mWifiDiagnosticsHandlerThread.getLooper()); mWifiLastResortWatchdog = new WifiLastResortWatchdog(this, mContext, mClock, mWifiMetrics, mWifiDiagnostics, wifiLooper, mDeviceConfigFacade, mWifiThreadRunner, mWifiMonitor); diff --git a/service/tests/wifitests/src/com/android/server/wifi/LastMileLoggerTest.java b/service/tests/wifitests/src/com/android/server/wifi/LastMileLoggerTest.java index 158e35bb9c..ab181f693a 100644 --- a/service/tests/wifitests/src/com/android/server/wifi/LastMileLoggerTest.java +++ b/service/tests/wifitests/src/com/android/server/wifi/LastMileLoggerTest.java @@ -24,6 +24,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; +import android.os.Handler; +import android.os.test.TestLooper; + import androidx.test.filters.SmallTest; import com.android.server.wifi.util.FileUtils; @@ -52,6 +55,8 @@ public class LastMileLoggerTest extends WifiBaseTest { @Mock WifiInjector mWifiInjector; @Spy FakeWifiLog mLog; + private TestLooper mLooper; + @Before public void setUp() throws Exception { @@ -64,8 +69,10 @@ public class LastMileLoggerTest extends WifiBaseTest { mTraceEnableFile.deleteOnExit(); mTraceReleaseFile.deleteOnExit(); FileUtils.stringToFile(mTraceEnableFile.getPath(), "0"); + mLooper = new TestLooper(); + Handler backgroundHandler = new Handler(mLooper.getLooper()); mLastMileLogger = new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(), - mTraceEnableFile.getPath(), mTraceReleaseFile.getPath()); + mTraceEnableFile.getPath(), mTraceReleaseFile.getPath(), backgroundHandler); } private static String readFileAsString(File file) throws IOException { @@ -75,13 +82,15 @@ public class LastMileLoggerTest extends WifiBaseTest { @Test public void ctorDoesNotCrash() throws Exception { new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(), mTraceEnableFile.getPath(), - mTraceReleaseFile.getPath()); + mTraceReleaseFile.getPath(), new Handler(mLooper.getLooper())); + mLooper.dispatchAll(); verifyNoMoreInteractions(mLog); } @Test public void connectionEventStartedEnablesTracing() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); assertEquals("1", readFileAsString(mTraceEnableFile)); } @@ -89,6 +98,7 @@ public class LastMileLoggerTest extends WifiBaseTest { public void connectionEventStartedDoesNotCrashIfReleaseFileIsMissing() throws Exception { mTraceReleaseFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); verify(mLog).warn(contains("Failed to open free_buffer")); } @@ -97,13 +107,14 @@ public class LastMileLoggerTest extends WifiBaseTest { throws Exception { mTraceReleaseFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); assertEquals("0", readFileAsString(mTraceEnableFile)); } @Test public void connectionEventStartedDoesNotAttemptToReopenReleaseFile() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); - + mLooper.dispatchAll(); // This is a rather round-about way of verifying that we don't attempt to re-open // the file. Namely: if we delete the |release| file, and CONNECTION_EVENT_STARTED // _did_ re-open the file, then we'd log an error message. Since the test is deleting the @@ -114,6 +125,7 @@ public class LastMileLoggerTest extends WifiBaseTest { // FileInputStream. mTraceReleaseFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); verifyNoMoreInteractions(mLog); } @@ -121,36 +133,42 @@ public class LastMileLoggerTest extends WifiBaseTest { public void connectionEventStartedDoesNotCrashIfEnableFileIsMissing() throws Exception { mTraceEnableFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); } @Test public void connectionEventStartedDoesNotCrashOnRepeatedCalls() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); } @Test public void connectionEventSucceededDisablesTracing() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_SUCCEEDED); assertEquals("0", readFileAsString(mTraceEnableFile)); + mLooper.dispatchAll(); } @Test public void connectionEventSucceededDoesNotCrashIfEnableFileIsMissing() throws Exception { mTraceEnableFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_SUCCEEDED); + mLooper.dispatchAll(); } @Test public void connectionEventSucceededDoesNotCrashOnRepeatedCalls() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_SUCCEEDED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_SUCCEEDED); + mLooper.dispatchAll(); } @Test public void connectionEventFailedDisablesTracingWhenPendingFails() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); + mLooper.dispatchAll(); assertEquals("0", readFileAsString(mTraceEnableFile)); } @@ -158,25 +176,30 @@ public class LastMileLoggerTest extends WifiBaseTest { public void connectionEventTimeoutDisablesTracing() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_TIMEOUT); + mLooper.dispatchAll(); assertEquals("0", readFileAsString(mTraceEnableFile)); } @Test public void multipleIfaces() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); assertEquals("1", readFileAsString(mTraceEnableFile)); mLastMileLogger.reportConnectionEvent(WLAN1, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); assertEquals("1", readFileAsString(mTraceEnableFile)); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect"); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_TIMEOUT); + mLooper.dispatchAll(); assertEquals("1", readFileAsString(mTraceEnableFile)); String dumpString = getDumpString(); assertTrue(dumpString.contains("--- Last failed")); assertTrue(dumpString.contains("rdev_connect")); mLastMileLogger.reportConnectionEvent(WLAN1, WifiDiagnostics.CONNECTION_EVENT_SUCCEEDED); + mLooper.dispatchAll(); assertEquals("0", readFileAsString(mTraceEnableFile)); } @@ -184,25 +207,30 @@ public class LastMileLoggerTest extends WifiBaseTest { public void connectionEventFailedDoesNotCrashIfEnableFileIsMissing() throws Exception { mTraceEnableFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); + mLooper.dispatchAll(); } @Test public void connectionEventFailedDoesNotCrashIfDataFileIsMissing() throws Exception { mTraceDataFile.delete(); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); + mLooper.dispatchAll(); } @Test public void connectionEventFailedDoesNotCrashOnRepeatedCalls() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); + mLooper.dispatchAll(); } @Test public void dumpShowsFailureTrace() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect"); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); + mLooper.dispatchAll(); assertTrue(getDumpString().contains("--- Last failed")); assertTrue(getDumpString().contains("rdev_connect")); } @@ -211,6 +239,7 @@ public class LastMileLoggerTest extends WifiBaseTest { @Test public void dumpShowsPendingConnectionTrace() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect"); assertTrue(getDumpString().contains("No last mile log for \"Last failed")); assertTrue(getDumpString().contains("--- Latest")); @@ -220,9 +249,11 @@ public class LastMileLoggerTest extends WifiBaseTest { @Test public void dumpShowsLastFailureTraceAndPendingConnectionTrace() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #1"); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #2"); String dumpString = getDumpString(); @@ -233,11 +264,14 @@ public class LastMileLoggerTest extends WifiBaseTest { @Test public void dumpShowsLastFailureTraceAndCurrentConnectionTrace() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #1"); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #2"); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_SUCCEEDED); + mLooper.dispatchAll(); String dumpString = getDumpString(); assertTrue(dumpString.contains("rdev_connect try #1")); @@ -247,8 +281,10 @@ public class LastMileLoggerTest extends WifiBaseTest { @Test public void dumpDoesNotClearLastFailureData() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect"); mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_FAILED); + mLooper.dispatchAll(); getDumpString(); String dumpString = getDumpString(); @@ -258,6 +294,7 @@ public class LastMileLoggerTest extends WifiBaseTest { @Test public void dumpDoesNotClearPendingConnectionTrace() throws Exception { mLastMileLogger.reportConnectionEvent(WLAN0, WifiDiagnostics.CONNECTION_EVENT_STARTED); + mLooper.dispatchAll(); FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect"); getDumpString(); |