From a480ba57f52127924b5dc207b4ca0fb28aa525ef Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Mon, 3 Sep 2018 08:19:02 +0900 Subject: [PATCH] ConnectivityService: Sort requests and networks in dumpsys. This patch adds two utility functions for sorting requests and networks tracked by ConnectivityService by request id and network id respectively. These utility functions are then used to improve the output of adb shell dumpsys connectivity so that networks and requests are printed in a more stable fashion. Bug: none Test: Compiled, flashed, booted, checked output of adb shell dumpsys connectivity. Change-Id: I3cb9b2ceab64145611a416dcb8c5d512838a2626 --- .../android/server/ConnectivityService.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 28ac784937..0c9c5285ff 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -180,6 +180,7 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -1945,7 +1946,7 @@ public class ConnectivityService extends IConnectivityManager.Stub private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { final List netDiags = new ArrayList(); final long DIAG_TIME_MS = 5000; - for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { + for (NetworkAgentInfo nai : networksSortedById()) { // Start gathering diagnostic information. netDiags.add(new NetworkDiagnostics( nai.network, @@ -1991,7 +1992,7 @@ public class ConnectivityService extends IConnectivityManager.Stub pw.println("Current Networks:"); pw.increaseIndent(); - for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { + for (NetworkAgentInfo nai : networksSortedById()) { pw.println(nai.toString()); pw.increaseIndent(); pw.println(String.format( @@ -2016,7 +2017,7 @@ public class ConnectivityService extends IConnectivityManager.Stub pw.println("Network Requests:"); pw.increaseIndent(); - for (NetworkRequestInfo nri : mNetworkRequests.values()) { + for (NetworkRequestInfo nri : requestsSortedById()) { pw.println(nri.toString()); } pw.println(); @@ -2073,6 +2074,26 @@ public class ConnectivityService extends IConnectivityManager.Stub } } + /** + * Return an array of all current NetworkAgentInfos sorted by network id. + */ + private NetworkAgentInfo[] networksSortedById() { + NetworkAgentInfo[] networks = new NetworkAgentInfo[0]; + networks = mNetworkAgentInfos.values().toArray(networks); + Arrays.sort(networks, Comparator.comparingInt(nai -> nai.network.netId)); + return networks; + } + + /** + * Return an array of all current NetworkRequest sorted by request id. + */ + private NetworkRequestInfo[] requestsSortedById() { + NetworkRequestInfo[] requests = new NetworkRequestInfo[0]; + requests = mNetworkRequests.values().toArray(requests); + Arrays.sort(requests, Comparator.comparingInt(nri -> nri.request.requestId)); + return requests; + } + private boolean isLiveNetworkAgent(NetworkAgentInfo nai, int what) { if (nai.network == null) return false; final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); @@ -2864,7 +2885,7 @@ public class ConnectivityService extends IConnectivityManager.Stub pw.println("User setting: " + description); pw.println("Network overrides:"); pw.increaseIndent(); - for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { + for (NetworkAgentInfo nai : networksSortedById()) { if (nai.avoidUnvalidated) { pw.println(nai.name()); } @@ -5803,4 +5824,4 @@ public class ConnectivityService extends IConnectivityManager.Stub pw.println(" Get airplane mode."); } } -} \ No newline at end of file +}