Search in sources :

Example 46 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by DirtyUnicorns.

the class DataIdleTest method fetchStats.

/**
     * Helper method that fetches all the network stats available and reports it
     * to instrumentation out.
     * @param template {@link NetworkTemplate} to match.
     */
private void fetchStats(NetworkTemplate template) {
    INetworkStatsSession session = null;
    try {
        mStatsService.forceUpdate();
        session = mStatsService.openSession();
        final NetworkStats stats = session.getSummaryForAllUid(template, Long.MIN_VALUE, Long.MAX_VALUE, false);
        reportStats(stats);
    } catch (RemoteException e) {
        Log.w(LOG_TAG, "Failed to fetch network stats.");
    } finally {
        TrafficStats.closeQuietly(session);
    }
}
Also used : INetworkStatsSession(android.net.INetworkStatsSession) NetworkStats(android.net.NetworkStats) RemoteException(android.os.RemoteException)

Example 47 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method testStatsRebootPersist.

public void testStatsRebootPersist() throws Exception {
    assertStatsFilesExist(false);
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    expectBandwidthControlCheck();
    replay();
    mService.forceUpdateIfaces();
    // verify service has empty history for wifi
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    verifyAndReset();
    // modify some number on wifi, and trigger poll event
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
    expectNetworkStatsPoll();
    mService.setUidForeground(UID_RED, false);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
    mService.setUidForeground(UID_RED, true);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
    assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L, 6);
    assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
    verifyAndReset();
    // graceful shutdown system, which should trigger persist of stats, and
    // clear any values in memory.
    expectCurrentTime();
    expectDefaultSettings();
    replay();
    mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
    verifyAndReset();
    assertStatsFilesExist(true);
    // boot through serviceReady() again
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectSystemReady();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    replay();
    mService.systemReady();
    mNetworkObserver = networkObserver.getValue();
    // after systemReady(), we should have historical stats loaded again
    assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
    assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L, 6);
    assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Capture(org.easymock.Capture)

Example 48 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method testRoaming.

public void testRoaming() throws Exception {
    // pretend that network comes online
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildMobile3gState(IMSI_1, true));
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    expectBandwidthControlCheck();
    replay();
    mService.forceUpdateIfaces();
    verifyAndReset();
    // Create some traffic
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    // Note that all traffic from NetworkManagementService is tagged as ROAMING_NO, because
    // roaming isn't tracked at that layer. We layer it on top by inspecting the iface
    // properties.
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 128L, 2L, 128L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 64L, 1L, 64L, 1L, 0L));
    expectNetworkStatsPoll();
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
    // verify entire history present
    final NetworkStats stats = mSession.getSummaryForAllUid(sTemplateImsi1, Long.MIN_VALUE, Long.MAX_VALUE, true);
    assertEquals(2, stats.size());
    assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_YES, 128L, 2L, 128L, 2L, 0);
    assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_YES, 64L, 1L, 64L, 1L, 0);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats)

Example 49 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method testTethering.

public void testTethering() throws Exception {
    // pretend first mobile network comes online
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildMobile3gState(IMSI_1));
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    expectBandwidthControlCheck();
    replay();
    mService.forceUpdateIfaces();
    verifyAndReset();
    // create some tethering traffic
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
    final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
    final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
    final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
    expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
    expectNetworkStatsPoll();
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
    assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
    assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats)

Example 50 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method assertUidTotal.

private void assertUidTotal(NetworkTemplate template, int uid, int set, int roaming, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
    // verify history API
    final NetworkStatsHistory history = mSession.getHistoryForUid(template, uid, set, TAG_NONE, FIELD_ALL);
    assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes, txPackets, operations);
    // verify summary API
    final NetworkStats stats = mSession.getSummaryForAllUid(template, Long.MIN_VALUE, Long.MAX_VALUE, false);
    assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, roaming, rxBytes, rxPackets, txBytes, txPackets, operations);
}
Also used : NetworkStats(android.net.NetworkStats) NetworkStatsHistory(android.net.NetworkStatsHistory)

Aggregations

NetworkStats (android.net.NetworkStats)271 File (java.io.File)49 DataUsageRequest (android.net.DataUsageRequest)32 Intent (android.content.Intent)29 NetworkIdentity (android.net.NetworkIdentity)28 NetworkStatsHistory (android.net.NetworkStatsHistory)21 Bundle (android.os.Bundle)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 FileInputStream (java.io.FileInputStream)18 ProtocolException (java.net.ProtocolException)18 PendingIntent (android.app.PendingIntent)17 IOException (java.io.IOException)17 NetworkPolicy (android.net.NetworkPolicy)15 NetworkState (android.net.NetworkState)15 Test (org.junit.Test)14 Suppress (android.test.suitebuilder.annotation.Suppress)13 VpnInfo (com.android.internal.net.VpnInfo)10 RemoteException (android.os.RemoteException)8 NetworkTemplate (android.net.NetworkTemplate)6