Search in sources :

Example 61 with NetworkStatsHistory

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

the class NetworkStatsCollection method removeUids.

/**
     * Remove any {@link NetworkStatsHistory} attributed to the requested UID,
     * moving any {@link NetworkStats#TAG_NONE} series to
     * {@link TrafficStats#UID_REMOVED}.
     */
public void removeUids(int[] uids) {
    final ArrayList<Key> knownKeys = Lists.newArrayList();
    knownKeys.addAll(mStats.keySet());
    // migrate all UID stats into special "removed" bucket
    for (Key key : knownKeys) {
        if (ArrayUtils.contains(uids, key.uid)) {
            // only migrate combined TAG_NONE history
            if (key.tag == TAG_NONE) {
                final NetworkStatsHistory uidHistory = mStats.get(key);
                final NetworkStatsHistory removedHistory = findOrCreateHistory(key.ident, UID_REMOVED, SET_DEFAULT, TAG_NONE);
                removedHistory.recordEntireHistory(uidHistory);
            }
            mStats.remove(key);
            mDirty = true;
        }
    }
}
Also used : NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 62 with NetworkStatsHistory

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

the class NetworkStatsServiceTest method assertUidTotal.

private void assertUidTotal(NetworkTemplate template, int uid, int set, 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, rxBytes, rxPackets, txBytes, txPackets, operations);
}
Also used : NetworkStats(android.net.NetworkStats) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 63 with NetworkStatsHistory

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

the class NetworkStatsServiceTest method testStatsBucketResize.

// TODO: simulate reboot to test bucket resize
@Suppress
public void testStatsBucketResize() throws Exception {
    NetworkStatsHistory history = null;
    assertStatsFilesExist(false);
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    verifyAndReset();
    // modify some number on wifi, and trigger poll event
    incrementCurrentTime(2 * HOUR_IN_MILLIS);
    expectCurrentTime();
    expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // verify service recorded history
    history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
    assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
    assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
    assertEquals(2, history.size());
    verifyAndReset();
    // now change bucket duration setting and trigger another poll with
    // exact same values, which should resize existing buckets.
    expectCurrentTime();
    expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // verify identical stats, but spread across 4 buckets now
    history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
    assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
    assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
    assertEquals(4, history.size());
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) NetworkStatsHistory(android.net.NetworkStatsHistory) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 64 with NetworkStatsHistory

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

the class ChartDataLoader method loadInBackground.

private ChartData loadInBackground(NetworkTemplate template, AppItem app, int fields) throws RemoteException {
    final ChartData data = new ChartData();
    data.network = mSession.getHistoryForNetwork(template, fields);
    if (app != null) {
        // load stats for current uid and template
        final int size = app.uids.size();
        for (int i = 0; i < size; i++) {
            final int uid = app.uids.keyAt(i);
            data.detailDefault = collectHistoryForUid(template, uid, SET_DEFAULT, data.detailDefault);
            data.detailForeground = collectHistoryForUid(template, uid, SET_FOREGROUND, data.detailForeground);
        }
        if (size > 0) {
            data.detail = new NetworkStatsHistory(data.detailForeground.getBucketDuration());
            data.detail.recordEntireHistory(data.detailDefault);
            data.detail.recordEntireHistory(data.detailForeground);
        } else {
            data.detailDefault = new NetworkStatsHistory(HOUR_IN_MILLIS);
            data.detailForeground = new NetworkStatsHistory(HOUR_IN_MILLIS);
            data.detail = new NetworkStatsHistory(HOUR_IN_MILLIS);
        }
    }
    return data;
}
Also used : NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 65 with NetworkStatsHistory

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

the class NetworkStatsCollection method getHistory.

/**
     * Combine all {@link NetworkStatsHistory} in this collection which match
     * the requested parameters.
     */
public NetworkStatsHistory getHistory(NetworkTemplate template, int uid, int set, int tag, int fields, long start, long end, @NetworkStatsAccess.Level int accessLevel, int callerUid) {
    if (!NetworkStatsAccess.isAccessibleToUser(uid, callerUid, accessLevel)) {
        throw new SecurityException("Network stats history of uid " + uid + " is forbidden for caller " + callerUid);
    }
    final NetworkStatsHistory combined = new NetworkStatsHistory(mBucketDuration, start == end ? 1 : estimateBuckets(), fields);
    // shortcut when we know stats will be empty
    if (start == end)
        return combined;
    for (int i = 0; i < mStats.size(); i++) {
        final Key key = mStats.keyAt(i);
        if (key.uid == uid && NetworkStats.setMatches(set, key.set) && key.tag == tag && templateMatches(template, key.ident)) {
            final NetworkStatsHistory value = mStats.valueAt(i);
            combined.recordHistory(value, start, end);
        }
    }
    return combined;
}
Also used : NetworkStatsHistory(android.net.NetworkStatsHistory)

Aggregations

NetworkStatsHistory (android.net.NetworkStatsHistory)109 NetworkStats (android.net.NetworkStats)21 ProtocolException (java.net.ProtocolException)18 AtomicFile (android.util.AtomicFile)12 BufferedInputStream (java.io.BufferedInputStream)12 DataInputStream (java.io.DataInputStream)12 FileNotFoundException (java.io.FileNotFoundException)12 RemoteException (android.os.RemoteException)10 ArrayList (java.util.ArrayList)6 INetworkStatsSession (android.net.INetworkStatsSession)5 NetworkPolicy (android.net.NetworkPolicy)5 Time (android.text.format.Time)5 ArrayMap (android.util.ArrayMap)5 IntArray (android.util.IntArray)5 Date (java.util.Date)5 Suppress (android.test.suitebuilder.annotation.Suppress)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1