Search in sources :

Example 11 with NetworkStats

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

the class NetworkStatsService method internalGetSummaryForNetwork.

/**
     * Return network summary, splicing between {@link #mDevStatsCached}
     * and {@link #mXtStatsCached} when appropriate.
     */
private NetworkStats internalGetSummaryForNetwork(NetworkTemplate template, long start, long end) {
    if (!mSettings.getReportXtOverDev()) {
        // shortcut when XT reporting disabled
        return mDevStatsCached.getSummary(template, start, end);
    }
    // splice stats between DEV and XT, switching over from DEV to XT at
    // first atomic bucket.
    final long firstAtomicBucket = mXtStatsCached.getFirstAtomicBucketMillis();
    final NetworkStats dev = mDevStatsCached.getSummary(template, Math.min(start, firstAtomicBucket), Math.min(end, firstAtomicBucket));
    final NetworkStats xt = mXtStatsCached.getSummary(template, Math.max(start, firstAtomicBucket), Math.max(end, firstAtomicBucket));
    xt.combineAllValues(dev);
    return xt;
}
Also used : NetworkStats(android.net.NetworkStats)

Example 12 with NetworkStats

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

the class NetworkStatsService method bootstrapStatsLocked.

/**
     * Bootstrap initial stats snapshot, usually during {@link #systemReady()}
     * so we have baseline values without double-counting.
     */
private void bootstrapStatsLocked() {
    final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis();
    try {
        // snapshot and record current counters; read UID stats first to
        // avoid overcounting dev stats.
        final NetworkStats uidSnapshot = getNetworkStatsUidDetail();
        final NetworkStats xtSnapshot = mNetworkManager.getNetworkStatsSummaryXt();
        final NetworkStats devSnapshot = mNetworkManager.getNetworkStatsSummaryDev();
        mDevRecorder.recordSnapshotLocked(devSnapshot, mActiveIfaces, currentTime);
        mXtRecorder.recordSnapshotLocked(xtSnapshot, mActiveIfaces, currentTime);
        mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveIfaces, currentTime);
        mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveIfaces, currentTime);
    } catch (IllegalStateException e) {
        Slog.w(TAG, "problem reading network stats: " + e);
    } catch (RemoteException e) {
    // ignored; service lives in system_server
    }
}
Also used : NetworkStats(android.net.NetworkStats) RemoteException(android.os.RemoteException)

Example 13 with NetworkStats

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

the class NetworkPolicyManagerServiceTest method testMeteredNetworkWithoutLimit.

public void testMeteredNetworkWithoutLimit() throws Exception {
    NetworkState[] state = null;
    NetworkStats stats = null;
    Future<Void> future;
    Future<String> tagFuture;
    final long TIME_FEB_15 = 1171497600000L;
    final long TIME_MAR_10 = 1173484800000L;
    final int CYCLE_DAY = 15;
    setCurrentTimeMillis(TIME_MAR_10);
    // bring up wifi network with metered policy
    state = new NetworkState[] { buildWifi() };
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
    {
        expectCurrentTime();
        expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
        expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())).andReturn(stats.getTotalBytes()).atLeastOnce();
        expectPolicyDataEnable(TYPE_WIFI, true);
        expectRemoveInterfaceQuota(TEST_IFACE);
        expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
        expectClearNotifications();
        expectAdvisePersistThreshold();
        future = expectMeteredIfacesChanged(TEST_IFACE);
        replay();
        setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, true));
        future.get();
        verifyAndReset();
    }
}
Also used : NetworkPolicy(android.net.NetworkPolicy) NetworkStats(android.net.NetworkStats) NetworkState(android.net.NetworkState)

Example 14 with NetworkStats

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

the class NetworkPolicyManagerServiceTest method testNetworkPolicyAppliedCycleLastMonth.

public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
    NetworkState[] state = null;
    NetworkStats stats = null;
    Future<Void> future;
    final long TIME_FEB_15 = 1171497600000L;
    final long TIME_MAR_10 = 1173484800000L;
    final int CYCLE_DAY = 15;
    setCurrentTimeMillis(TIME_MAR_10);
    // first, pretend that wifi network comes online. no policy active,
    // which means we shouldn't push limit to interface.
    state = new NetworkState[] { buildWifi() };
    expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
    expectCurrentTime();
    expectClearNotifications();
    expectAdvisePersistThreshold();
    future = expectMeteredIfacesChanged();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    future.get();
    verifyAndReset();
    // now change cycle to be on 15th, and test in early march, to verify we
    // pick cycle day in previous month.
    expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
    expectCurrentTime();
    // pretend that 512 bytes total have happened
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L);
    expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10)).andReturn(stats.getTotalBytes()).atLeastOnce();
    expectPolicyDataEnable(TYPE_WIFI, true);
    // TODO: consider making strongly ordered mock
    expectRemoveInterfaceQuota(TEST_IFACE);
    expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
    expectClearNotifications();
    expectAdvisePersistThreshold();
    future = expectMeteredIfacesChanged(TEST_IFACE);
    replay();
    setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
    future.get();
    verifyAndReset();
}
Also used : NetworkPolicy(android.net.NetworkPolicy) NetworkStats(android.net.NetworkStats) Intent(android.content.Intent) NetworkState(android.net.NetworkState)

Example 15 with NetworkStats

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

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();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    // 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();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // 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, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 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();
    expectNetworkStatsSummary(buildEmptyStats());
    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, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 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)

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