Search in sources :

Example 66 with NetworkStatsHistory

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

the class NetworkStatsCollection method findOrCreateHistory.

private NetworkStatsHistory findOrCreateHistory(NetworkIdentitySet ident, int uid, int set, int tag) {
    final Key key = new Key(ident, uid, set, tag);
    final NetworkStatsHistory existing = mStats.get(key);
    // update when no existing, or when bucket duration changed
    NetworkStatsHistory updated = null;
    if (existing == null) {
        updated = new NetworkStatsHistory(mBucketDuration, 10);
    } else if (existing.getBucketDuration() != mBucketDuration) {
        updated = new NetworkStatsHistory(existing, mBucketDuration);
    }
    if (updated != null) {
        mStats.put(key, updated);
        return updated;
    } else {
        return existing;
    }
}
Also used : NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 67 with NetworkStatsHistory

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

the class NetworkStatsCollection method write.

public void write(DataOutputStream out) throws IOException {
    // cluster key lists grouped by ident
    final HashMap<NetworkIdentitySet, ArrayList<Key>> keysByIdent = Maps.newHashMap();
    for (Key key : mStats.keySet()) {
        ArrayList<Key> keys = keysByIdent.get(key.ident);
        if (keys == null) {
            keys = Lists.newArrayList();
            keysByIdent.put(key.ident, keys);
        }
        keys.add(key);
    }
    out.writeInt(FILE_MAGIC);
    out.writeInt(VERSION_UNIFIED_INIT);
    out.writeInt(keysByIdent.size());
    for (NetworkIdentitySet ident : keysByIdent.keySet()) {
        final ArrayList<Key> keys = keysByIdent.get(ident);
        ident.writeToStream(out);
        out.writeInt(keys.size());
        for (Key key : keys) {
            final NetworkStatsHistory history = mStats.get(key);
            out.writeInt(key.uid);
            out.writeInt(key.set);
            out.writeInt(key.tag);
            history.writeToStream(out);
        }
    }
    out.flush();
}
Also used : ArrayList(java.util.ArrayList) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 68 with NetworkStatsHistory

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

the class NetworkStatsCollection method dumpCheckin.

/**
     * Dump all contained stats that match requested parameters, but group
     * together all matching {@link NetworkTemplate} under a single prefix.
     */
private void dumpCheckin(PrintWriter pw, long start, long end, NetworkTemplate groupTemplate, String groupPrefix) {
    final ArrayMap<Key, NetworkStatsHistory> grouped = new ArrayMap<>();
    // Walk through all history, grouping by matching network templates
    for (int i = 0; i < mStats.size(); i++) {
        final Key key = mStats.keyAt(i);
        final NetworkStatsHistory value = mStats.valueAt(i);
        if (!templateMatches(groupTemplate, key.ident))
            continue;
        if (key.set >= NetworkStats.SET_DEBUG_START)
            continue;
        final Key groupKey = new Key(null, key.uid, key.set, key.tag);
        NetworkStatsHistory groupHistory = grouped.get(groupKey);
        if (groupHistory == null) {
            groupHistory = new NetworkStatsHistory(value.getBucketDuration());
            grouped.put(groupKey, groupHistory);
        }
        groupHistory.recordHistory(value, start, end);
    }
    for (int i = 0; i < grouped.size(); i++) {
        final Key key = grouped.keyAt(i);
        final NetworkStatsHistory value = grouped.valueAt(i);
        if (value.size() == 0)
            continue;
        pw.print("c,");
        pw.print(groupPrefix);
        pw.print(',');
        pw.print(key.uid);
        pw.print(',');
        pw.print(NetworkStats.setToCheckinString(key.set));
        pw.print(',');
        pw.print(key.tag);
        pw.println();
        value.dumpCheckin(pw);
    }
}
Also used : ArrayMap(android.util.ArrayMap) NetworkStatsHistory(android.net.NetworkStatsHistory)

Example 69 with NetworkStatsHistory

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

the class NetworkStatsServiceTest method testStatsBucketResize.

// TODO: simulate reboot to test bucket resize
@Test
@Ignore
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());
    expectBandwidthControlCheck();
    mService.forceUpdateIfaces();
    // 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());
    forcePollAndWaitForIdle();
    // 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());
    // 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());
    forcePollAndWaitForIdle();
    // 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());
}
Also used : NetworkStats(android.net.NetworkStats) NetworkStatsHistory(android.net.NetworkStatsHistory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 70 with NetworkStatsHistory

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

the class NetworkStatsServiceTest method assertUidTotal.

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