Stop using IIpConnectivityMetrics in ConnectivityService.

Currently, ConnectivityService calls the IpConnectivityMetrics
service class directly to log default network events. This is
incompatible with ConnectivityService being in a mainline module.
Replace direct access to IIpConnectivityMetrics with public
methods in IpConnectivityLog, which is @SystemApi class.

The new methods are not yet @SystemApi, but they can be made so
if desired. Alternatively, these metrics could be deleted.

Also remove the IpConectivityMetrics service from the
service-connectivity JAR, and go back to starting it from
SystemServer.java, which is what was happening a few hours ago
before aosp/1542626 was merged.

Test: builds, boots
Test: atest FrameworksNetTests
Test: "dumpsys connmetrics" shows events, including default network events
Change-Id: I9d6147d93590363a2f8f83f39f05c03d001b4851
This commit is contained in:
Lorenzo Colitti
2021-01-07 19:28:32 +09:00
parent ad7708353d
commit 1c8119e81a
3 changed files with 44 additions and 22 deletions

View File

@@ -87,7 +87,6 @@ import android.net.ICaptivePortal;
import android.net.IConnectivityDiagnosticsCallback; import android.net.IConnectivityDiagnosticsCallback;
import android.net.IConnectivityManager; import android.net.IConnectivityManager;
import android.net.IDnsResolver; import android.net.IDnsResolver;
import android.net.IIpConnectivityMetrics;
import android.net.INetd; import android.net.INetd;
import android.net.INetworkManagementEventObserver; import android.net.INetworkManagementEventObserver;
import android.net.INetworkMonitor; import android.net.INetworkMonitor;
@@ -154,7 +153,6 @@ import android.os.PersistableBundle;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process; import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceSpecificException; import android.os.ServiceSpecificException;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.SystemProperties; import android.os.SystemProperties;
@@ -927,14 +925,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
"no IpConnectivityMetrics service"); "no IpConnectivityMetrics service");
} }
/**
* @see IpConnectivityMetrics
*/
public IIpConnectivityMetrics getIpConnectivityMetrics() {
return IIpConnectivityMetrics.Stub.asInterface(
ServiceManager.getService(IpConnectivityLog.SERVICE_NAME));
}
public IBatteryStats getBatteryStatsService() { public IBatteryStats getBatteryStatsService() {
return BatteryStatsService.getService(); return BatteryStatsService.getService();
} }
@@ -2983,9 +2973,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
if (valid != nai.lastValidated) { if (valid != nai.lastValidated) {
if (wasDefault) { if (wasDefault) {
mDeps.getMetricsLogger() mMetricsLog.logDefaultNetworkValidity(valid);
.defaultNetworkMetrics().logDefaultNetworkValidity(
SystemClock.elapsedRealtime(), valid);
} }
final int oldScore = nai.getCurrentScore(); final int oldScore = nai.getCurrentScore();
nai.lastValidated = valid; nai.lastValidated = valid;
@@ -3413,7 +3401,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
// if there is a fallback. Taken together, the two form a X -> 0, 0 -> Y sequence // if there is a fallback. Taken together, the two form a X -> 0, 0 -> Y sequence
// whose timestamps tell how long it takes to recover a default network. // whose timestamps tell how long it takes to recover a default network.
long now = SystemClock.elapsedRealtime(); long now = SystemClock.elapsedRealtime();
mDeps.getMetricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent(now, null, nai); mMetricsLog.logDefaultNetworkEvent(null, 0, false,
null /* lp */, null /* nc */, nai.network, nai.getCurrentScore(),
nai.linkProperties, nai.networkCapabilities);
} }
notifyIfacesChangedForNetworkStats(); notifyIfacesChangedForNetworkStats();
// TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied // TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied
@@ -7167,9 +7157,28 @@ public class ConnectivityService extends IConnectivityManager.Stub
updateDataActivityTracking(newDefaultNetwork, oldDefaultNetwork); updateDataActivityTracking(newDefaultNetwork, oldDefaultNetwork);
// Notify system services of the new default. // Notify system services of the new default.
makeDefault(newDefaultNetwork); makeDefault(newDefaultNetwork);
// Log 0 -> X and Y -> X default network transitions, where X is the new default. // Log 0 -> X and Y -> X default network transitions, where X is the new default.
mDeps.getMetricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent( final Network network = (newDefaultNetwork != null) ? newDefaultNetwork.network : null;
now, newDefaultNetwork, oldDefaultNetwork); final int score = (newDefaultNetwork != null) ? newDefaultNetwork.getCurrentScore() : 0;
final boolean validated = newDefaultNetwork != null && newDefaultNetwork.lastValidated;
final LinkProperties lp = (newDefaultNetwork != null)
? newDefaultNetwork.linkProperties : null;
final NetworkCapabilities nc = (newDefaultNetwork != null)
? newDefaultNetwork.networkCapabilities : null;
final Network prevNetwork = (oldDefaultNetwork != null)
? oldDefaultNetwork.network : null;
final int prevScore = (oldDefaultNetwork != null)
? oldDefaultNetwork.getCurrentScore() : 0;
final LinkProperties prevLp = (oldDefaultNetwork != null)
? oldDefaultNetwork.linkProperties : null;
final NetworkCapabilities prevNc = (oldDefaultNetwork != null)
? oldDefaultNetwork.networkCapabilities : null;
mMetricsLog.logDefaultNetworkEvent(network, score, validated, lp, nc,
prevNetwork, prevScore, prevLp, prevNc);
// Have a new default network, release the transition wakelock in // Have a new default network, release the transition wakelock in
scheduleReleaseNetworkTransitionWakelock(); scheduleReleaseNetworkTransitionWakelock();
} }

View File

@@ -161,7 +161,6 @@ import android.net.DataStallReportParcelable;
import android.net.EthernetManager; import android.net.EthernetManager;
import android.net.IConnectivityDiagnosticsCallback; import android.net.IConnectivityDiagnosticsCallback;
import android.net.IDnsResolver; import android.net.IDnsResolver;
import android.net.IIpConnectivityMetrics;
import android.net.INetd; import android.net.INetd;
import android.net.INetworkMonitor; import android.net.INetworkMonitor;
import android.net.INetworkMonitorCallbacks; import android.net.INetworkMonitorCallbacks;
@@ -359,7 +358,6 @@ public class ConnectivityServiceTest {
private HandlerThread mAlarmManagerThread; private HandlerThread mAlarmManagerThread;
private TestNetIdManager mNetIdManager; private TestNetIdManager mNetIdManager;
@Mock IIpConnectivityMetrics mIpConnectivityMetrics;
@Mock IpConnectivityMetrics.Logger mMetricsService; @Mock IpConnectivityMetrics.Logger mMetricsService;
@Mock DefaultNetworkMetrics mDefaultNetworkMetrics; @Mock DefaultNetworkMetrics mDefaultNetworkMetrics;
@Mock DeviceIdleInternal mDeviceIdleInternal; @Mock DeviceIdleInternal mDeviceIdleInternal;
@@ -1373,7 +1371,6 @@ public class ConnectivityServiceTest {
doReturn(mock(ProxyTracker.class)).when(deps).makeProxyTracker(any(), any()); doReturn(mock(ProxyTracker.class)).when(deps).makeProxyTracker(any(), any());
doReturn(mMetricsService).when(deps).getMetricsLogger(); doReturn(mMetricsService).when(deps).getMetricsLogger();
doReturn(true).when(deps).queryUserAccess(anyInt(), anyInt()); doReturn(true).when(deps).queryUserAccess(anyInt(), anyInt());
doReturn(mIpConnectivityMetrics).when(deps).getIpConnectivityMetrics();
doReturn(mBatteryStatsService).when(deps).getBatteryStatsService(); doReturn(mBatteryStatsService).when(deps).getBatteryStatsService();
doAnswer(inv -> { doAnswer(inv -> {
mPolicyTracker = new WrappedMultinetworkPolicyTracker( mPolicyTracker = new WrappedMultinetworkPolicyTracker(

View File

@@ -124,6 +124,22 @@ public class IpConnectivityMetricsTest {
assertEquals("", output2); assertEquals("", output2);
} }
private void logDefaultNetworkEvent(long timeMs, NetworkAgentInfo nai,
NetworkAgentInfo oldNai) {
final Network network = (nai != null) ? nai.network() : null;
final int score = (nai != null) ? nai.getCurrentScore() : 0;
final boolean validated = (nai != null) ? nai.lastValidated : false;
final LinkProperties lp = (nai != null) ? nai.linkProperties : null;
final NetworkCapabilities nc = (nai != null) ? nai.networkCapabilities : null;
final Network prevNetwork = (oldNai != null) ? oldNai.network() : null;
final int prevScore = (oldNai != null) ? oldNai.getCurrentScore() : 0;
final LinkProperties prevLp = (oldNai != null) ? oldNai.linkProperties : null;
final NetworkCapabilities prevNc = (oldNai != null) ? oldNai.networkCapabilities : null;
mService.mDefaultNetworkMetrics.logDefaultNetworkEvent(timeMs, network, score, validated,
lp, nc, prevNetwork, prevScore, prevLp, prevNc);
}
@Test @Test
public void testDefaultNetworkEvents() throws Exception { public void testDefaultNetworkEvents() throws Exception {
final long cell = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_CELLULAR}); final long cell = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_CELLULAR});
@@ -147,7 +163,7 @@ public class IpConnectivityMetricsTest {
for (NetworkAgentInfo[] pair : defaultNetworks) { for (NetworkAgentInfo[] pair : defaultNetworks) {
timeMs += durationMs; timeMs += durationMs;
durationMs += durationMs; durationMs += durationMs;
mService.mDefaultNetworkMetrics.logDefaultNetworkEvent(timeMs, pair[1], pair[0]); logDefaultNetworkEvent(timeMs, pair[1], pair[0]);
} }
String want = String.join("\n", String want = String.join("\n",
@@ -331,8 +347,8 @@ public class IpConnectivityMetricsTest {
final long wifi = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_WIFI}); final long wifi = BitUtils.packBits(new int[]{NetworkCapabilities.TRANSPORT_WIFI});
NetworkAgentInfo cellNai = makeNai(100, 50, false, true, cell); NetworkAgentInfo cellNai = makeNai(100, 50, false, true, cell);
NetworkAgentInfo wifiNai = makeNai(101, 60, true, false, wifi); NetworkAgentInfo wifiNai = makeNai(101, 60, true, false, wifi);
mService.mDefaultNetworkMetrics.logDefaultNetworkEvent(timeMs + 200, cellNai, null); logDefaultNetworkEvent(timeMs + 200L, cellNai, null);
mService.mDefaultNetworkMetrics.logDefaultNetworkEvent(timeMs + 300, wifiNai, cellNai); logDefaultNetworkEvent(timeMs + 300L, wifiNai, cellNai);
String want = String.join("\n", String want = String.join("\n",
"dropped_events: 0", "dropped_events: 0",