Search in sources :

Example 26 with NetworkStats

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

the class BatteryStatsImpl method getNetworkStatsDeltaLocked.

/**
     * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
     * as a buffer of NetworkStats objects to cycle through when computing deltas.
     */
private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces, NetworkStats[] networkStatsBuffer) throws IOException {
    if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED, false)) {
        return null;
    }
    final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
    networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats, networkStatsBuffer[NETWORK_STATS_LAST], null, null, networkStatsBuffer[NETWORK_STATS_DELTA]);
    networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
    networkStatsBuffer[NETWORK_STATS_LAST] = stats;
    return networkStatsBuffer[NETWORK_STATS_DELTA];
}
Also used : NetworkStats(android.net.NetworkStats)

Example 27 with NetworkStats

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

the class NetworkStatsFactory method readNetworkStatsDetailInternal.

private NetworkStats readNetworkStatsDetailInternal(int limitUid, String[] limitIfaces, int limitTag, NetworkStats lastStats) throws IOException {
    if (USE_NATIVE_PARSING) {
        final NetworkStats stats;
        if (lastStats != null) {
            stats = lastStats;
            stats.setElapsedRealtime(SystemClock.elapsedRealtime());
        } else {
            stats = new NetworkStats(SystemClock.elapsedRealtime(), -1);
        }
        if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid, limitIfaces, limitTag) != 0) {
            throw new IOException("Failed to parse network stats");
        }
        if (SANITY_CHECK_NATIVE) {
            final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag);
            assertEquals(javaStats, stats);
        }
        return stats;
    } else {
        return javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag);
    }
}
Also used : NetworkStats(android.net.NetworkStats) IOException(java.io.IOException)

Example 28 with NetworkStats

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

the class NetworkStatsCollection method getSummary.

/**
     * Summarize all {@link NetworkStatsHistory} in this collection which match
     * the requested parameters.
     */
public NetworkStats getSummary(NetworkTemplate template, long start, long end, @NetworkStatsAccess.Level int accessLevel, int callerUid) {
    final long now = System.currentTimeMillis();
    final NetworkStats stats = new NetworkStats(end - start, 24);
    // shortcut when we know stats will be empty
    if (start == end)
        return stats;
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    NetworkStatsHistory.Entry historyEntry = null;
    for (int i = 0; i < mStats.size(); i++) {
        final Key key = mStats.keyAt(i);
        if (templateMatches(template, key.ident) && NetworkStatsAccess.isAccessibleToUser(key.uid, callerUid, accessLevel) && key.set < NetworkStats.SET_DEBUG_START) {
            final NetworkStatsHistory value = mStats.valueAt(i);
            historyEntry = value.getValues(start, end, now, historyEntry);
            entry.iface = IFACE_ALL;
            entry.uid = key.uid;
            entry.set = key.set;
            entry.tag = key.tag;
            entry.metered = key.ident.isAnyMemberMetered() ? METERED_YES : METERED_NO;
            entry.roaming = key.ident.isAnyMemberRoaming() ? ROAMING_YES : ROAMING_NO;
            entry.rxBytes = historyEntry.rxBytes;
            entry.rxPackets = historyEntry.rxPackets;
            entry.txBytes = historyEntry.txBytes;
            entry.txPackets = historyEntry.txPackets;
            entry.operations = historyEntry.operations;
            if (!entry.isEmpty()) {
                stats.combineValues(entry);
            }
        }
    }
    return stats;
}
Also used : NetworkStats(android.net.NetworkStats) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 29 with NetworkStats

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

the class NetworkStatsService method createSession.

private INetworkStatsSession createSession(final String callingPackage, boolean pollOnCreate) {
    assertBandwidthControlEnabled();
    if (pollOnCreate) {
        final long ident = Binder.clearCallingIdentity();
        try {
            performPoll(FLAG_PERSIST_ALL);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }
    return new INetworkStatsSession.Stub() {

        private NetworkStatsCollection mUidComplete;

        private NetworkStatsCollection mUidTagComplete;

        private String mCallingPackage = callingPackage;

        private NetworkStatsCollection getUidComplete() {
            synchronized (mStatsLock) {
                if (mUidComplete == null) {
                    mUidComplete = mUidRecorder.getOrLoadCompleteLocked();
                }
                return mUidComplete;
            }
        }

        private NetworkStatsCollection getUidTagComplete() {
            synchronized (mStatsLock) {
                if (mUidTagComplete == null) {
                    mUidTagComplete = mUidTagRecorder.getOrLoadCompleteLocked();
                }
                return mUidTagComplete;
            }
        }

        @Override
        public int[] getRelevantUids() {
            return getUidComplete().getRelevantUids(checkAccessLevel(mCallingPackage));
        }

        @Override
        public NetworkStats getDeviceSummaryForNetwork(NetworkTemplate template, long start, long end) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            if (accessLevel < NetworkStatsAccess.Level.DEVICESUMMARY) {
                throw new SecurityException("Calling package " + mCallingPackage + " cannot access device summary network stats");
            }
            NetworkStats result = new NetworkStats(end - start, 1);
            final long ident = Binder.clearCallingIdentity();
            try {
                // Using access level higher than the one we checked for above.
                // Reason is that we are combining usage data in a way that is not PII
                // anymore.
                result.combineAllValues(internalGetSummaryForNetwork(template, start, end, NetworkStatsAccess.Level.DEVICE));
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
            return result;
        }

        @Override
        public NetworkStats getSummaryForNetwork(NetworkTemplate template, long start, long end) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            return internalGetSummaryForNetwork(template, start, end, accessLevel);
        }

        @Override
        public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            return internalGetHistoryForNetwork(template, fields, accessLevel);
        }

        @Override
        public NetworkStats getSummaryForAllUid(NetworkTemplate template, long start, long end, boolean includeTags) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            final NetworkStats stats = getUidComplete().getSummary(template, start, end, accessLevel);
            if (includeTags) {
                final NetworkStats tagStats = getUidTagComplete().getSummary(template, start, end, accessLevel);
                stats.combineAllValues(tagStats);
            }
            return stats;
        }

        @Override
        public NetworkStatsHistory getHistoryForUid(NetworkTemplate template, int uid, int set, int tag, int fields) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            if (tag == TAG_NONE) {
                return getUidComplete().getHistory(template, uid, set, tag, fields, accessLevel);
            } else {
                return getUidTagComplete().getHistory(template, uid, set, tag, fields, accessLevel);
            }
        }

        @Override
        public NetworkStatsHistory getHistoryIntervalForUid(NetworkTemplate template, int uid, int set, int tag, int fields, long start, long end) {
            @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(mCallingPackage);
            if (tag == TAG_NONE) {
                return getUidComplete().getHistory(template, uid, set, tag, fields, start, end, accessLevel);
            } else if (uid == Binder.getCallingUid()) {
                return getUidTagComplete().getHistory(template, uid, set, tag, fields, start, end, accessLevel);
            } else {
                throw new SecurityException("Calling package " + mCallingPackage + " cannot access tag information from a different uid");
            }
        }

        @Override
        public void close() {
            mUidComplete = null;
            mUidTagComplete = null;
        }
    };
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkStats(android.net.NetworkStats)

Example 30 with NetworkStats

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

the class NetworkStatsServiceTest method testForegroundBackground.

@Test
public void testForegroundBackground() throws Exception {
    // pretend that network comes online
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectBandwidthControlCheck();
    mService.forceUpdateIfaces();
    // create some initial traffic
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
    mService.incrementOperationCount(UID_RED, 0xF00D, 1);
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
    // now switch to foreground
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
    mService.setUidForeground(UID_RED, true);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
    forcePollAndWaitForIdle();
    // test that we combined correctly
    assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
    // verify entire history present
    final NetworkStats stats = mSession.getSummaryForAllUid(sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
    assertEquals(4, stats.size());
    assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 2L, 128L, 2L, 1);
    assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, 1L, 64L, 1L, 1);
    assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 32L, 2L, 32L, 2L, 1);
    assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, METERED_NO, ROAMING_NO, 1L, 1L, 1L, 1L, 1);
}
Also used : NetworkStats(android.net.NetworkStats) Test(org.junit.Test)

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