Search in sources :

Example 16 with NetworkStats

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

the class NetworkStatsServiceTest method assertNetworkTotal.

private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
    // verify history API
    final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
    assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
    // verify summary API
    final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
    assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, operations);
}
Also used : NetworkStats(android.net.NetworkStats) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 17 with NetworkStats

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

the class NetworkStatsServiceTest method testUidStatsAcrossNetworks.

public void testUidStatsAcrossNetworks() throws Exception {
    // pretend first mobile network comes online
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildMobile3gState(IMSI_1));
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    verifyAndReset();
    // create some traffic on first network
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
    expectNetworkStatsPoll();
    mService.incrementOperationCount(UID_RED, 0xF00D, 10);
    replay();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // verify service recorded history
    assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
    assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
    verifyAndReset();
    // now switch networks; this also tests that we're okay with interfaces
    // disappearing, to verify we don't count backwards.
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildMobile3gState(IMSI_2));
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    verifyAndReset();
    // create traffic on second network
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
    expectNetworkStatsPoll();
    mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
    replay();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // verify original history still intact
    assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
    assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
    assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
    // and verify new history also recorded under different template, which
    // verifies that we didn't cross the streams.
    assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 18 with NetworkStats

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

the class NetworkStatsServiceTest method testUidRemovedIsMoved.

public void testUidRemovedIsMoved() throws Exception {
    // pretend that network comes online
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    verifyAndReset();
    // create some traffic
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L).addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
    expectNetworkStatsPoll();
    mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
    replay();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
    assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
    assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
    verifyAndReset();
    // now pretend two UIDs are uninstalled, which should migrate stats to
    // special "removed" bucket.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L).addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
    expectNetworkStatsPoll();
    replay();
    final Intent intent = new Intent(ACTION_UID_REMOVED);
    intent.putExtra(EXTRA_UID, UID_BLUE);
    mServiceContext.sendBroadcast(intent);
    intent.putExtra(EXTRA_UID, UID_RED);
    mServiceContext.sendBroadcast(intent);
    // existing uid and total should remain unchanged; but removed UID
    // should be gone completely.
    assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
    assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
    assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
    assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 19 with NetworkStats

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

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 20 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class BandwidthTest method fetchDataFromProc.

/**
     * Fetch network data from /proc/uid_stat/uid
     *
     * @return populated {@link NetworkStats}
     */
public NetworkStats fetchDataFromProc(int uid) {
    String root_filepath = "/proc/uid_stat/" + uid + "/";
    File rcv_stat = new File(root_filepath + "tcp_rcv");
    int rx = BandwidthTestUtil.parseIntValueFromFile(rcv_stat);
    File snd_stat = new File(root_filepath + "tcp_snd");
    int tx = BandwidthTestUtil.parseIntValueFromFile(snd_stat);
    NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
    stats.addValues(NetworkStats.IFACE_ALL, uid, NetworkStats.SET_DEFAULT, NetworkStats.TAG_NONE, rx, 0, tx, 0, 0);
    return stats;
}
Also used : NetworkStats(android.net.NetworkStats) File(java.io.File)

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