diff options
10 files changed, 329 insertions, 279 deletions
diff --git a/core/java/android/net/NetworkStats.java b/core/java/android/net/NetworkStats.java index a4c66e4..fbff7d8 100644 --- a/core/java/android/net/NetworkStats.java +++ b/core/java/android/net/NetworkStats.java @@ -43,31 +43,19 @@ public class NetworkStats implements Parcelable { /** {@link #tag} value for without tag. */ public static final int TAG_NONE = 0; - // TODO: move public fields to Entry accessors, then undeprecate - // TODO: refactor rx/tx to rxBytes/txBytes - /** * {@link SystemClock#elapsedRealtime()} timestamp when this data was * generated. */ - @Deprecated - public final long elapsedRealtime; - @Deprecated - public int size; - @Deprecated - public String[] iface; - @Deprecated - public int[] uid; - @Deprecated - public int[] tag; - @Deprecated - public long[] rx; - @Deprecated - public long[] rxPackets; - @Deprecated - public long[] tx; - @Deprecated - public long[] txPackets; + private final long elapsedRealtime; + private int size; + private String[] iface; + private int[] uid; + private int[] tag; + private long[] rxBytes; + private long[] rxPackets; + private long[] txBytes; + private long[] txPackets; public static class Entry { public String iface; @@ -77,6 +65,20 @@ public class NetworkStats implements Parcelable { public long rxPackets; public long txBytes; public long txPackets; + + public Entry() { + } + + public Entry(String iface, int uid, int tag, long rxBytes, long rxPackets, long txBytes, + long txPackets) { + this.iface = iface; + this.uid = uid; + this.tag = tag; + this.rxBytes = rxBytes; + this.rxPackets = rxPackets; + this.txBytes = txBytes; + this.txPackets = txPackets; + } } public NetworkStats(long elapsedRealtime, int initialSize) { @@ -85,9 +87,9 @@ public class NetworkStats implements Parcelable { this.iface = new String[initialSize]; this.uid = new int[initialSize]; this.tag = new int[initialSize]; - this.rx = new long[initialSize]; + this.rxBytes = new long[initialSize]; this.rxPackets = new long[initialSize]; - this.tx = new long[initialSize]; + this.txBytes = new long[initialSize]; this.txPackets = new long[initialSize]; } @@ -97,23 +99,15 @@ public class NetworkStats implements Parcelable { iface = parcel.createStringArray(); uid = parcel.createIntArray(); tag = parcel.createIntArray(); - rx = parcel.createLongArray(); + rxBytes = parcel.createLongArray(); rxPackets = parcel.createLongArray(); - tx = parcel.createLongArray(); + txBytes = parcel.createLongArray(); txPackets = parcel.createLongArray(); } - /** - * Add new stats entry with given values. - */ - public NetworkStats addEntry(String iface, int uid, int tag, long rx, long tx) { - final Entry entry = new Entry(); - entry.iface = iface; - entry.uid = uid; - entry.tag = tag; - entry.rxBytes = rx; - entry.txBytes = tx; - return addValues(entry); + public NetworkStats addValues(String iface, int uid, int tag, long rxBytes, long rxPackets, + long txBytes, long txPackets) { + return addValues(new Entry(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets)); } /** @@ -126,18 +120,18 @@ public class NetworkStats implements Parcelable { iface = Arrays.copyOf(iface, newLength); uid = Arrays.copyOf(uid, newLength); tag = Arrays.copyOf(tag, newLength); - rx = Arrays.copyOf(rx, newLength); + rxBytes = Arrays.copyOf(rxBytes, newLength); rxPackets = Arrays.copyOf(rxPackets, newLength); - tx = Arrays.copyOf(tx, newLength); + txBytes = Arrays.copyOf(txBytes, newLength); txPackets = Arrays.copyOf(txPackets, newLength); } iface[size] = entry.iface; uid[size] = entry.uid; tag[size] = entry.tag; - rx[size] = entry.rxBytes; + rxBytes[size] = entry.rxBytes; rxPackets[size] = entry.rxPackets; - tx[size] = entry.txBytes; + txBytes[size] = entry.txBytes; txPackets[size] = entry.txPackets; size++; @@ -152,9 +146,9 @@ public class NetworkStats implements Parcelable { entry.iface = iface[i]; entry.uid = uid[i]; entry.tag = tag[i]; - entry.rxBytes = rx[i]; + entry.rxBytes = rxBytes[i]; entry.rxPackets = rxPackets[i]; - entry.txBytes = tx[i]; + entry.txBytes = txBytes[i]; entry.txPackets = txPackets[i]; return entry; } @@ -167,20 +161,31 @@ public class NetworkStats implements Parcelable { return size; } + // @VisibleForTesting + public int internalSize() { + return iface.length; + } + + public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets, + long txBytes, long txPackets) { + return combineValues(new Entry(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets)); + } + /** * Combine given values with an existing row, or create a new row if * {@link #findIndex(String, int, int)} is unable to find match. Can also be * used to subtract values from existing rows. */ - public NetworkStats combineEntry(String iface, int uid, int tag, long rx, long tx) { - // TODO: extent to accept rxPackets/txPackets - final int i = findIndex(iface, uid, tag); + public NetworkStats combineValues(Entry entry) { + final int i = findIndex(entry.iface, entry.uid, entry.tag); if (i == -1) { // only create new entry when positive contribution - addEntry(iface, uid, tag, rx, tx); + addValues(entry); } else { - this.rx[i] += rx; - this.tx[i] += tx; + rxBytes[i] += entry.rxBytes; + rxPackets[i] += entry.rxPackets; + txBytes[i] += entry.txBytes; + txPackets[i] += entry.txPackets; } return this; } @@ -280,15 +285,15 @@ public class NetworkStats implements Parcelable { final int j = value.findIndex(entry.iface, entry.uid, entry.tag); if (j == -1) { // newly appearing row, return entire value - entry.rxBytes = rx[i]; + entry.rxBytes = rxBytes[i]; entry.rxPackets = rxPackets[i]; - entry.txBytes = tx[i]; + entry.txBytes = txBytes[i]; entry.txPackets = txPackets[i]; } else { // existing row, subtract remote value - entry.rxBytes = rx[i] - value.rx[j]; + entry.rxBytes = rxBytes[i] - value.rxBytes[j]; entry.rxPackets = rxPackets[i] - value.rxPackets[j]; - entry.txBytes = tx[i] - value.tx[j]; + entry.txBytes = txBytes[i] - value.txBytes[j]; entry.txPackets = txPackets[i] - value.txPackets[j]; if (enforceMonotonic && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0 @@ -321,9 +326,9 @@ public class NetworkStats implements Parcelable { pw.print(" iface="); pw.print(iface[i]); pw.print(" uid="); pw.print(uid[i]); pw.print(" tag="); pw.print(tag[i]); - pw.print(" rxBytes="); pw.print(rx[i]); + pw.print(" rxBytes="); pw.print(rxBytes[i]); pw.print(" rxPackets="); pw.print(rxPackets[i]); - pw.print(" txBytes="); pw.print(tx[i]); + pw.print(" txBytes="); pw.print(txBytes[i]); pw.print(" txPackets="); pw.println(txPackets[i]); } } @@ -347,9 +352,9 @@ public class NetworkStats implements Parcelable { dest.writeStringArray(iface); dest.writeIntArray(uid); dest.writeIntArray(tag); - dest.writeLongArray(rx); + dest.writeLongArray(rxBytes); dest.writeLongArray(rxPackets); - dest.writeLongArray(tx); + dest.writeLongArray(txBytes); dest.writeLongArray(txPackets); } diff --git a/core/java/android/net/NetworkStatsHistory.java b/core/java/android/net/NetworkStatsHistory.java index dd2945c..b0930b2 100644 --- a/core/java/android/net/NetworkStatsHistory.java +++ b/core/java/android/net/NetworkStatsHistory.java @@ -43,13 +43,20 @@ public class NetworkStatsHistory implements Parcelable { private static final int VERSION_INIT = 1; // TODO: teach about varint encoding to use less disk space - - public final long bucketDuration; - - public int bucketCount; - public long[] bucketStart; - public long[] rx; - public long[] tx; + // TODO: extend to record rxPackets/txPackets + + private final long bucketDuration; + private int bucketCount; + private long[] bucketStart; + private long[] rxBytes; + private long[] txBytes; + + public static class Entry { + public long bucketStart; + public long bucketDuration; + public long rxBytes; + public long txBytes; + } public NetworkStatsHistory(long bucketDuration) { this(bucketDuration, 10); @@ -58,16 +65,16 @@ public class NetworkStatsHistory implements Parcelable { public NetworkStatsHistory(long bucketDuration, int initialSize) { this.bucketDuration = bucketDuration; bucketStart = new long[initialSize]; - rx = new long[initialSize]; - tx = new long[initialSize]; + rxBytes = new long[initialSize]; + txBytes = new long[initialSize]; bucketCount = 0; } public NetworkStatsHistory(Parcel in) { bucketDuration = in.readLong(); bucketStart = readLongArray(in); - rx = in.createLongArray(); - tx = in.createLongArray(); + rxBytes = in.createLongArray(); + txBytes = in.createLongArray(); bucketCount = bucketStart.length; } @@ -75,8 +82,8 @@ public class NetworkStatsHistory implements Parcelable { public void writeToParcel(Parcel out, int flags) { out.writeLong(bucketDuration); writeLongArray(out, bucketStart, bucketCount); - writeLongArray(out, rx, bucketCount); - writeLongArray(out, tx, bucketCount); + writeLongArray(out, rxBytes, bucketCount); + writeLongArray(out, txBytes, bucketCount); } public NetworkStatsHistory(DataInputStream in) throws IOException { @@ -85,8 +92,8 @@ public class NetworkStatsHistory implements Parcelable { case VERSION_INIT: { bucketDuration = in.readLong(); bucketStart = readLongArray(in); - rx = readLongArray(in); - tx = readLongArray(in); + rxBytes = readLongArray(in); + txBytes = readLongArray(in); bucketCount = bucketStart.length; break; } @@ -100,8 +107,8 @@ public class NetworkStatsHistory implements Parcelable { out.writeInt(VERSION_INIT); out.writeLong(bucketDuration); writeLongArray(out, bucketStart, bucketCount); - writeLongArray(out, rx, bucketCount); - writeLongArray(out, tx, bucketCount); + writeLongArray(out, rxBytes, bucketCount); + writeLongArray(out, txBytes, bucketCount); } /** {@inheritDoc} */ @@ -109,6 +116,26 @@ public class NetworkStatsHistory implements Parcelable { return 0; } + public int size() { + return bucketCount; + } + + public long getBucketDuration() { + return bucketDuration; + } + + /** + * Return specific stats entry. + */ + public Entry getValues(int i, Entry recycle) { + final Entry entry = recycle != null ? recycle : new Entry(); + entry.bucketStart = bucketStart[i]; + entry.bucketDuration = bucketDuration; + entry.rxBytes = rxBytes[i]; + entry.txBytes = txBytes[i]; + return entry; + } + /** * Record that data traffic occurred in the given time range. Will * distribute across internal buckets, creating new buckets as needed. @@ -135,8 +162,8 @@ public class NetworkStatsHistory implements Parcelable { final long overlap = Math.min(curEnd, end) - Math.max(curStart, start); if (overlap > 0) { - this.rx[i] += rx * overlap / duration; - this.tx[i] += tx * overlap / duration; + this.rxBytes[i] += rx * overlap / duration; + this.txBytes[i] += tx * overlap / duration; } } } @@ -149,7 +176,7 @@ public class NetworkStatsHistory implements Parcelable { for (int i = 0; i < input.bucketCount; i++) { final long start = input.bucketStart[i]; final long end = start + input.bucketDuration; - recordData(start, end, input.rx[i], input.tx[i]); + recordData(start, end, input.rxBytes[i], input.txBytes[i]); } } @@ -179,8 +206,8 @@ public class NetworkStatsHistory implements Parcelable { if (bucketCount >= bucketStart.length) { final int newLength = Math.max(bucketStart.length, 10) * 3 / 2; bucketStart = Arrays.copyOf(bucketStart, newLength); - rx = Arrays.copyOf(rx, newLength); - tx = Arrays.copyOf(tx, newLength); + rxBytes = Arrays.copyOf(rxBytes, newLength); + txBytes = Arrays.copyOf(txBytes, newLength); } // create gap when inserting bucket in middle @@ -189,13 +216,13 @@ public class NetworkStatsHistory implements Parcelable { final int length = bucketCount - index; System.arraycopy(bucketStart, index, bucketStart, dstPos, length); - System.arraycopy(rx, index, rx, dstPos, length); - System.arraycopy(tx, index, tx, dstPos, length); + System.arraycopy(rxBytes, index, rxBytes, dstPos, length); + System.arraycopy(txBytes, index, txBytes, dstPos, length); } bucketStart[index] = start; - rx[index] = 0; - tx[index] = 0; + rxBytes[index] = 0; + txBytes[index] = 0; bucketCount++; } @@ -216,8 +243,8 @@ public class NetworkStatsHistory implements Parcelable { if (i > 0) { final int length = bucketStart.length; bucketStart = Arrays.copyOfRange(bucketStart, i, length); - rx = Arrays.copyOfRange(rx, i, length); - tx = Arrays.copyOfRange(tx, i, length); + rxBytes = Arrays.copyOfRange(rxBytes, i, length); + txBytes = Arrays.copyOfRange(txBytes, i, length); bucketCount -= i; } } @@ -241,8 +268,8 @@ public class NetworkStatsHistory implements Parcelable { final long overlap = Math.min(curEnd, end) - Math.max(curStart, start); if (overlap > 0) { - rx += this.rx[i] * overlap / bucketDuration; - tx += this.tx[i] * overlap / bucketDuration; + rx += this.rxBytes[i] * overlap / bucketDuration; + tx += this.txBytes[i] * overlap / bucketDuration; } } @@ -292,8 +319,8 @@ public class NetworkStatsHistory implements Parcelable { for (int i = start; i < bucketCount; i++) { pw.print(prefix); pw.print(" bucketStart="); pw.print(bucketStart[i]); - pw.print(" rx="); pw.print(rx[i]); - pw.print(" tx="); pw.println(tx[i]); + pw.print(" rxBytes="); pw.print(rxBytes[i]); + pw.print(" txBytes="); pw.println(txBytes[i]); } } diff --git a/core/tests/coretests/src/android/net/NetworkStatsHistoryTest.java b/core/tests/coretests/src/android/net/NetworkStatsHistoryTest.java index 0b72c3c..16bb000 100644 --- a/core/tests/coretests/src/android/net/NetworkStatsHistoryTest.java +++ b/core/tests/coretests/src/android/net/NetworkStatsHistoryTest.java @@ -54,8 +54,8 @@ public class NetworkStatsHistoryTest extends TestCase { // record data into narrow window to get single bucket stats.recordData(TEST_START, TEST_START + SECOND_IN_MILLIS, 1024L, 2048L); - assertEquals(1, stats.bucketCount); - assertBucket(stats, 0, 1024L, 2048L); + assertEquals(1, stats.size()); + assertEntry(stats, 0, 1024L, 2048L); } public void testRecordEqualBuckets() throws Exception { @@ -66,9 +66,9 @@ public class NetworkStatsHistoryTest extends TestCase { final long recordStart = TEST_START + (bucketDuration / 2); stats.recordData(recordStart, recordStart + bucketDuration, 1024L, 128L); - assertEquals(2, stats.bucketCount); - assertBucket(stats, 0, 512L, 64L); - assertBucket(stats, 1, 512L, 64L); + assertEquals(2, stats.size()); + assertEntry(stats, 0, 512L, 64L); + assertEntry(stats, 1, 512L, 64L); } public void testRecordTouchingBuckets() throws Exception { @@ -81,13 +81,13 @@ public class NetworkStatsHistoryTest extends TestCase { final long recordEnd = (TEST_START + (BUCKET_SIZE * 2)) + (MINUTE_IN_MILLIS * 4); stats.recordData(recordStart, recordEnd, 1000L, 5000L); - assertEquals(3, stats.bucketCount); + assertEquals(3, stats.size()); // first bucket should have (1/20 of value) - assertBucket(stats, 0, 50L, 250L); + assertEntry(stats, 0, 50L, 250L); // second bucket should have (15/20 of value) - assertBucket(stats, 1, 750L, 3750L); + assertEntry(stats, 1, 750L, 3750L); // final bucket should have (4/20 of value) - assertBucket(stats, 2, 200L, 1000L); + assertEntry(stats, 2, 200L, 1000L); } public void testRecordGapBuckets() throws Exception { @@ -101,9 +101,9 @@ public class NetworkStatsHistoryTest extends TestCase { stats.recordData(lastStart, lastStart + SECOND_IN_MILLIS, 64L, 512L); // we should have two buckets, far apart from each other - assertEquals(2, stats.bucketCount); - assertBucket(stats, 0, 128L, 256L); - assertBucket(stats, 1, 64L, 512L); + assertEquals(2, stats.size()); + assertEntry(stats, 0, 128L, 256L); + assertEntry(stats, 1, 64L, 512L); // now record something in middle, spread across two buckets final long middleStart = TEST_START + DAY_IN_MILLIS; @@ -111,11 +111,11 @@ public class NetworkStatsHistoryTest extends TestCase { stats.recordData(middleStart, middleEnd, 2048L, 2048L); // now should have four buckets, with new record in middle two buckets - assertEquals(4, stats.bucketCount); - assertBucket(stats, 0, 128L, 256L); - assertBucket(stats, 1, 1024L, 1024L); - assertBucket(stats, 2, 1024L, 1024L); - assertBucket(stats, 3, 64L, 512L); + assertEquals(4, stats.size()); + assertEntry(stats, 0, 128L, 256L); + assertEntry(stats, 1, 1024L, 1024L); + assertEntry(stats, 2, 1024L, 1024L); + assertEntry(stats, 3, 64L, 512L); } public void testRecordOverlapBuckets() throws Exception { @@ -128,9 +128,9 @@ public class NetworkStatsHistoryTest extends TestCase { stats.recordData(midStart, midStart + HOUR_IN_MILLIS, 1024L, 1024L); // should have two buckets, with some data mixed together - assertEquals(2, stats.bucketCount); - assertBucket(stats, 0, 768L, 768L); - assertBucket(stats, 1, 512L, 512L); + assertEquals(2, stats.size()); + assertEntry(stats, 0, 768L, 768L); + assertEntry(stats, 1, 512L, 512L); } public void testRecordEntireGapIdentical() throws Exception { @@ -154,10 +154,10 @@ public class NetworkStatsHistoryTest extends TestCase { assertTotalEquals(total, 3000L, 1500L); // now inspect internal buckets - assertBucket(stats, 0, 1000L, 500L); - assertBucket(stats, 1, 1000L, 500L); - assertBucket(stats, 2, 500L, 250L); - assertBucket(stats, 3, 500L, 250L); + assertEntry(stats, 0, 1000L, 500L); + assertEntry(stats, 1, 1000L, 500L); + assertEntry(stats, 2, 500L, 250L); + assertEntry(stats, 3, 500L, 250L); } public void testRecordEntireOverlapVaryingBuckets() throws Exception { @@ -181,13 +181,13 @@ public class NetworkStatsHistoryTest extends TestCase { assertTotalEquals(total, 650L, 650L); // now inspect internal buckets - assertBucket(stats, 0, 10L, 10L); - assertBucket(stats, 1, 20L, 20L); - assertBucket(stats, 2, 20L, 20L); - assertBucket(stats, 3, 20L, 20L); - assertBucket(stats, 4, 20L, 20L); - assertBucket(stats, 5, 20L, 20L); - assertBucket(stats, 6, 10L, 10L); + assertEntry(stats, 0, 10L, 10L); + assertEntry(stats, 1, 20L, 20L); + assertEntry(stats, 2, 20L, 20L); + assertEntry(stats, 3, 20L, 20L); + assertEntry(stats, 4, 20L, 20L); + assertEntry(stats, 5, 20L, 20L); + assertEntry(stats, 6, 10L, 10L); // now combine using 15min buckets stats = new NetworkStatsHistory(HOUR_IN_MILLIS / 4); @@ -199,10 +199,10 @@ public class NetworkStatsHistoryTest extends TestCase { assertTotalEquals(total, 650L, 650L); // and inspect buckets - assertBucket(stats, 0, 200L, 200L); - assertBucket(stats, 1, 150L, 150L); - assertBucket(stats, 2, 150L, 150L); - assertBucket(stats, 3, 150L, 150L); + assertEntry(stats, 0, 200L, 200L); + assertEntry(stats, 1, 150L, 150L); + assertEntry(stats, 2, 150L, 150L); + assertEntry(stats, 3, 150L, 150L); } public void testRemove() throws Exception { @@ -210,28 +210,28 @@ public class NetworkStatsHistoryTest extends TestCase { // record some data across 24 buckets stats.recordData(TEST_START, TEST_START + DAY_IN_MILLIS, 24L, 24L); - assertEquals(24, stats.bucketCount); + assertEquals(24, stats.size()); // try removing far before buckets; should be no change stats.removeBucketsBefore(TEST_START - YEAR_IN_MILLIS); - assertEquals(24, stats.bucketCount); + assertEquals(24, stats.size()); // try removing just moments into first bucket; should be no change // since that bucket contains data beyond the cutoff stats.removeBucketsBefore(TEST_START + SECOND_IN_MILLIS); - assertEquals(24, stats.bucketCount); + assertEquals(24, stats.size()); // try removing single bucket stats.removeBucketsBefore(TEST_START + HOUR_IN_MILLIS); - assertEquals(23, stats.bucketCount); + assertEquals(23, stats.size()); // try removing multiple buckets stats.removeBucketsBefore(TEST_START + (4 * HOUR_IN_MILLIS)); - assertEquals(20, stats.bucketCount); + assertEquals(20, stats.size()); // try removing all buckets stats.removeBucketsBefore(TEST_START + YEAR_IN_MILLIS); - assertEquals(0, stats.bucketCount); + assertEquals(0, stats.size()); } public void testTotalData() throws Exception { @@ -293,19 +293,25 @@ public class NetworkStatsHistoryTest extends TestCase { private static void assertConsistent(NetworkStatsHistory stats) { // verify timestamps are monotonic - for (int i = 1; i < stats.bucketCount; i++) { - assertTrue(stats.bucketStart[i - 1] < stats.bucketStart[i]); + long lastStart = Long.MIN_VALUE; + NetworkStatsHistory.Entry entry = null; + for (int i = 0; i < stats.size(); i++) { + entry = stats.getValues(i, entry); + assertTrue(lastStart < entry.bucketStart); + lastStart = entry.bucketStart; } } - private static void assertTotalEquals(long[] total, long rx, long tx) { - assertEquals("unexpected rx", rx, total[0]); - assertEquals("unexpected tx", tx, total[1]); + private static void assertTotalEquals(long[] total, long rxBytes, long txBytes) { + assertEquals("unexpected rxBytes", rxBytes, total[0]); + assertEquals("unexpected txBytes", txBytes, total[1]); } - private static void assertBucket(NetworkStatsHistory stats, int index, long rx, long tx) { - assertEquals("unexpected rx", rx, stats.rx[index]); - assertEquals("unexpected tx", tx, stats.tx[index]); + private static void assertEntry( + NetworkStatsHistory stats, int index, long rxBytes, long txBytes) { + final NetworkStatsHistory.Entry entry = stats.getValues(index, null); + assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); + assertEquals("unexpected txBytes", txBytes, entry.txBytes); } } diff --git a/core/tests/coretests/src/android/net/NetworkStatsTest.java b/core/tests/coretests/src/android/net/NetworkStatsTest.java index 82345e2..2434e9f 100644 --- a/core/tests/coretests/src/android/net/NetworkStatsTest.java +++ b/core/tests/coretests/src/android/net/NetworkStatsTest.java @@ -31,9 +31,9 @@ public class NetworkStatsTest extends TestCase { public void testFindIndex() throws Exception { final NetworkStats stats = new NetworkStats(TEST_START, 3) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L) - .addEntry(TEST_IFACE, 102, TAG_NONE, 1024L, 1024L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L) + .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L) + .addValues(TEST_IFACE, 102, TAG_NONE, 1024L, 8L, 1024L, 8L); assertEquals(2, stats.findIndex(TEST_IFACE, 102, TAG_NONE)); assertEquals(2, stats.findIndex(TEST_IFACE, 102, TAG_NONE)); @@ -45,109 +45,105 @@ public class NetworkStatsTest extends TestCase { final NetworkStats stats = new NetworkStats(TEST_START, 2); assertEquals(0, stats.size()); - assertEquals(2, stats.iface.length); + assertEquals(2, stats.internalSize()); - stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 1L, 2L); - stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 2L, 2L); + stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 1L, 1L, 2L, 2L); + stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 2L, 2L, 2L, 2L); assertEquals(2, stats.size()); - assertEquals(2, stats.iface.length); + assertEquals(2, stats.internalSize()); - stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 3L, 4L); - stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 4L, 4L); - stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 5L, 5L); + stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 3L, 30L, 4L, 40L); + stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 4L, 40L, 4L, 40L); + stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 5L, 50L, 5L, 50L); assertEquals(5, stats.size()); - assertTrue(stats.iface.length >= 5); + assertTrue(stats.internalSize() >= 5); - assertEquals(1L, stats.rx[0]); - assertEquals(2L, stats.rx[1]); - assertEquals(3L, stats.rx[2]); - assertEquals(4L, stats.rx[3]); - assertEquals(5L, stats.rx[4]); + assertEntry(stats, 0, TEST_IFACE, TEST_UID, TAG_NONE, 1L, 1L, 2L, 2L); + assertEntry(stats, 1, TEST_IFACE, TEST_UID, TAG_NONE, 2L, 2L, 2L, 2L); + assertEntry(stats, 2, TEST_IFACE, TEST_UID, TAG_NONE, 3L, 30L, 4L, 40L); + assertEntry(stats, 3, TEST_IFACE, TEST_UID, TAG_NONE, 4L, 40L, 4L, 40L); + assertEntry(stats, 4, TEST_IFACE, TEST_UID, TAG_NONE, 5L, 50L, 5L, 50L); } public void testCombineExisting() throws Exception { final NetworkStats stats = new NetworkStats(TEST_START, 10); - stats.addEntry(TEST_IFACE, 1001, TAG_NONE, 512L, 256L); - stats.addEntry(TEST_IFACE, 1001, 0xff, 128L, 128L); - stats.combineEntry(TEST_IFACE, 1001, TAG_NONE, -128L, -128L); + stats.addValues(TEST_IFACE, 1001, TAG_NONE, 512L, 4L, 256L, 2L); + stats.addValues(TEST_IFACE, 1001, 0xff, 128L, 1L, 128L, 1L); + stats.combineValues(TEST_IFACE, 1001, TAG_NONE, -128L, -1L, -128L, -1L); - assertStatsEntry(stats, 0, TEST_IFACE, 1001, TAG_NONE, 384L, 128L); - assertStatsEntry(stats, 1, TEST_IFACE, 1001, 0xff, 128L, 128L); + assertEntry(stats, 0, TEST_IFACE, 1001, TAG_NONE, 384L, 3L, 128L, 1L); + assertEntry(stats, 1, TEST_IFACE, 1001, 0xff, 128L, 1L, 128L, 1L); // now try combining that should create row - stats.combineEntry(TEST_IFACE, 5005, TAG_NONE, 128L, 128L); - assertStatsEntry(stats, 2, TEST_IFACE, 5005, TAG_NONE, 128L, 128L); - stats.combineEntry(TEST_IFACE, 5005, TAG_NONE, 128L, 128L); - assertStatsEntry(stats, 2, TEST_IFACE, 5005, TAG_NONE, 256L, 256L); + stats.combineValues(TEST_IFACE, 5005, TAG_NONE, 128L, 1L, 128L, 1L); + assertEntry(stats, 2, TEST_IFACE, 5005, TAG_NONE, 128L, 1L, 128L, 1L); + stats.combineValues(TEST_IFACE, 5005, TAG_NONE, 128L, 1L, 128L, 1L); + assertEntry(stats, 2, TEST_IFACE, 5005, TAG_NONE, 256L, 2L, 256L, 2L); } public void testSubtractIdenticalData() throws Exception { final NetworkStats before = new NetworkStats(TEST_START, 2) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L) + .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L); final NetworkStats after = new NetworkStats(TEST_START, 2) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L) + .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L); final NetworkStats result = after.subtract(before); // identical data should result in zero delta - assertEquals(0, result.rx[0]); - assertEquals(0, result.tx[0]); - assertEquals(0, result.rx[1]); - assertEquals(0, result.tx[1]); + assertEntry(result, 0, TEST_IFACE, 100, TAG_NONE, 0L, 0L, 0L, 0L); + assertEntry(result, 1, TEST_IFACE, 101, TAG_NONE, 0L, 0L, 0L, 0L); } public void testSubtractIdenticalRows() throws Exception { final NetworkStats before = new NetworkStats(TEST_START, 2) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L) + .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L); final NetworkStats after = new NetworkStats(TEST_START, 2) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1025L, 2L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 3L, 1028L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1025L, 9L, 2L, 1L) + .addValues(TEST_IFACE, 101, TAG_NONE, 3L, 1L, 1028L, 9L); final NetworkStats result = after.subtract(before); // expect delta between measurements - assertEquals(1, result.rx[0]); - assertEquals(2, result.tx[0]); - assertEquals(3, result.rx[1]); - assertEquals(4, result.tx[1]); + assertEntry(result, 0, TEST_IFACE, 100, TAG_NONE, 1L, 1L, 2L, 1L); + assertEntry(result, 1, TEST_IFACE, 101, TAG_NONE, 3L, 1L, 4L, 1L); } public void testSubtractNewRows() throws Exception { final NetworkStats before = new NetworkStats(TEST_START, 2) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L) + .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L); final NetworkStats after = new NetworkStats(TEST_START, 3) - .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L) - .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L) - .addEntry(TEST_IFACE, 102, TAG_NONE, 1024L, 1024L); + .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L) + .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L) + .addValues(TEST_IFACE, 102, TAG_NONE, 1024L, 8L, 1024L, 8L); final NetworkStats result = after.subtract(before); // its okay to have new rows - assertEquals(0, result.rx[0]); - assertEquals(0, result.tx[0]); - assertEquals(0, result.rx[1]); - assertEquals(0, result.tx[1]); - assertEquals(1024, result.rx[2]); - assertEquals(1024, result.tx[2]); + assertEntry(result, 0, TEST_IFACE, 100, TAG_NONE, 0L, 0L, 0L, 0L); + assertEntry(result, 1, TEST_IFACE, 101, TAG_NONE, 0L, 0L, 0L, 0L); + assertEntry(result, 2, TEST_IFACE, 102, TAG_NONE, 1024L, 8L, 1024L, 8L); } - private static void assertStatsEntry( - NetworkStats stats, int i, String iface, int uid, int tag, long rx, long tx) { - assertEquals(iface, stats.iface[i]); - assertEquals(uid, stats.uid[i]); - assertEquals(tag, stats.tag[i]); - assertEquals(rx, stats.rx[i]); - assertEquals(tx, stats.tx[i]); + private static void assertEntry(NetworkStats stats, int index, String iface, int uid, int tag, + long rxBytes, long rxPackets, long txBytes, long txPackets) { + final NetworkStats.Entry entry = stats.getValues(index, null); + assertEquals(iface, entry.iface); + assertEquals(uid, entry.uid); + assertEquals(tag, entry.tag); + assertEquals(rxBytes, entry.rxBytes); + assertEquals(rxPackets, entry.rxPackets); + assertEquals(txBytes, entry.txBytes); + assertEquals(txPackets, entry.txPackets); } } diff --git a/services/java/com/android/server/ThrottleService.java b/services/java/com/android/server/ThrottleService.java index 24d4dd3..b8890aa 100644 --- a/services/java/com/android/server/ThrottleService.java +++ b/services/java/com/android/server/ThrottleService.java @@ -515,8 +515,9 @@ public class ThrottleService extends IThrottleManager.Stub { mIface, NetworkStats.UID_ALL, NetworkStats.TAG_NONE); if (index != -1) { - incRead = stats.rx[index] - mLastRead; - incWrite = stats.tx[index] - mLastWrite; + final NetworkStats.Entry entry = stats.getValues(index, null); + incRead = entry.rxBytes - mLastRead; + incWrite = entry.txBytes - mLastWrite; } else { // missing iface, assume stats are 0 Slog.w(TAG, "unable to find stats for iface " + mIface); diff --git a/services/java/com/android/server/net/NetworkPolicyManagerService.java b/services/java/com/android/server/net/NetworkPolicyManagerService.java index d23d0f4..d30b66b 100644 --- a/services/java/com/android/server/net/NetworkPolicyManagerService.java +++ b/services/java/com/android/server/net/NetworkPolicyManagerService.java @@ -405,7 +405,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { try { final NetworkStats stats = mNetworkStats.getSummaryForNetwork( policy.template, start, end); - total = stats.rx[0] + stats.tx[0]; + final NetworkStats.Entry entry = stats.getValues(0, null); + total = entry.rxBytes + entry.txBytes; } catch (RemoteException e) { Slog.w(TAG, "problem reading summary for template " + policy.template); continue; @@ -605,7 +606,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { final long total; try { stats = mNetworkStats.getSummaryForNetwork(policy.template, start, end); - total = stats.rx[0] + stats.tx[0]; + final NetworkStats.Entry entry = stats.getValues(0, null); + total = entry.rxBytes + entry.txBytes; } catch (RemoteException e) { Slog.w(TAG, "problem reading summary for template " + policy.template); continue; diff --git a/services/java/com/android/server/net/NetworkStatsService.java b/services/java/com/android/server/net/NetworkStatsService.java index b6834f6..872438c 100644 --- a/services/java/com/android/server/net/NetworkStatsService.java +++ b/services/java/com/android/server/net/NetworkStatsService.java @@ -313,22 +313,26 @@ public class NetworkStatsService extends INetworkStatsService.Stub { mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); synchronized (mStatsLock) { - long rx = 0; - long tx = 0; - long[] networkTotal = new long[2]; + final NetworkStats stats = new NetworkStats(end - start, 1); + final NetworkStats.Entry entry = new NetworkStats.Entry(); + long[] total = new long[2]; // combine total from all interfaces that match template for (NetworkIdentitySet ident : mNetworkStats.keySet()) { if (templateMatches(template, ident)) { final NetworkStatsHistory history = mNetworkStats.get(ident); - networkTotal = history.getTotalData(start, end, networkTotal); - rx += networkTotal[0]; - tx += networkTotal[1]; + total = history.getTotalData(start, end, total); + + entry.iface = IFACE_ALL; + entry.uid = UID_ALL; + entry.tag = TAG_NONE; + entry.rxBytes = total[0]; + entry.txBytes = total[1]; + + stats.combineValues(entry); } } - final NetworkStats stats = new NetworkStats(end - start, 1); - stats.addEntry(IFACE_ALL, UID_ALL, TAG_NONE, rx, tx); return stats; } } @@ -342,6 +346,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { ensureUidStatsLoadedLocked(); final NetworkStats stats = new NetworkStats(end - start, 24); + final NetworkStats.Entry entry = new NetworkStats.Entry(); long[] total = new long[2]; for (NetworkIdentitySet ident : mUidStats.keySet()) { @@ -357,10 +362,15 @@ public class NetworkStatsService extends INetworkStatsService.Stub { if (tag == TAG_NONE || includeTags) { final NetworkStatsHistory history = uidStats.valueAt(i); total = history.getTotalData(start, end, total); - final long rx = total[0]; - final long tx = total[1]; - if (rx > 0 || tx > 0) { - stats.combineEntry(IFACE_ALL, uid, tag, rx, tx); + + entry.iface = IFACE_ALL; + entry.uid = uid; + entry.tag = tag; + entry.rxBytes = total[0]; + entry.txBytes = total[1]; + + if (entry.rxBytes > 0 || entry.txBytes > 0) { + stats.combineValues(entry); } } } @@ -512,10 +522,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final NetworkStats persistDelta = computeStatsDelta( mLastPersistNetworkSnapshot, networkSnapshot); final long persistThreshold = mSettings.getPersistThreshold(); + + NetworkStats.Entry entry = null; for (String iface : persistDelta.getUniqueIfaces()) { final int index = persistDelta.findIndex(iface, UID_ALL, TAG_NONE); - if (forcePersist || persistDelta.rx[index] > persistThreshold - || persistDelta.tx[index] > persistThreshold) { + entry = persistDelta.getValues(index, entry); + if (forcePersist || entry.rxBytes > persistThreshold + || entry.txBytes > persistThreshold) { writeNetworkStatsLocked(); if (mUidStatsLoaded) { writeUidStatsLocked(); @@ -538,20 +551,19 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final HashSet<String> unknownIface = Sets.newHashSet(); final NetworkStats delta = computeStatsDelta(mLastNetworkSnapshot, networkSnapshot); - final long timeStart = currentTime - delta.elapsedRealtime; - for (int i = 0; i < delta.size; i++) { - final String iface = delta.iface[i]; - final NetworkIdentitySet ident = mActiveIfaces.get(iface); + final long timeStart = currentTime - delta.getElapsedRealtime(); + + NetworkStats.Entry entry = null; + for (int i = 0; i < delta.size(); i++) { + entry = delta.getValues(i, entry); + final NetworkIdentitySet ident = mActiveIfaces.get(entry.iface); if (ident == null) { - unknownIface.add(iface); + unknownIface.add(entry.iface); continue; } - final long rx = delta.rx[i]; - final long tx = delta.tx[i]; - final NetworkStatsHistory history = findOrCreateNetworkStatsLocked(ident); - history.recordData(timeStart, currentTime, rx, tx); + history.recordData(timeStart, currentTime, entry.rxBytes, entry.txBytes); } // trim any history beyond max @@ -574,22 +586,19 @@ public class NetworkStatsService extends INetworkStatsService.Stub { ensureUidStatsLoadedLocked(); final NetworkStats delta = computeStatsDelta(mLastUidSnapshot, uidSnapshot); - final long timeStart = currentTime - delta.elapsedRealtime; + final long timeStart = currentTime - delta.getElapsedRealtime(); - for (int i = 0; i < delta.size; i++) { - final String iface = delta.iface[i]; - final NetworkIdentitySet ident = mActiveIfaces.get(iface); + NetworkStats.Entry entry = null; + for (int i = 0; i < delta.size(); i++) { + entry = delta.getValues(i, entry); + final NetworkIdentitySet ident = mActiveIfaces.get(entry.iface); if (ident == null) { continue; } - final int uid = delta.uid[i]; - final int tag = delta.tag[i]; - final long rx = delta.rx[i]; - final long tx = delta.tx[i]; - - final NetworkStatsHistory history = findOrCreateUidStatsLocked(ident, uid, tag); - history.recordData(timeStart, currentTime, rx, tx); + final NetworkStatsHistory history = findOrCreateUidStatsLocked( + ident, entry.uid, entry.tag); + history.recordData(timeStart, currentTime, entry.rxBytes, entry.txBytes); } // trim any history beyond max @@ -651,7 +660,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { NetworkStatsHistory updated = null; if (existing == null) { updated = new NetworkStatsHistory(bucketDuration, 10); - } else if (existing.bucketDuration != bucketDuration) { + } else if (existing.getBucketDuration() != bucketDuration) { updated = new NetworkStatsHistory( bucketDuration, estimateResizeBuckets(existing, bucketDuration)); updated.recordEntireHistory(existing); @@ -683,7 +692,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { NetworkStatsHistory updated = null; if (existing == null) { updated = new NetworkStatsHistory(bucketDuration, 10); - } else if (existing.bucketDuration != bucketDuration) { + } else if (existing.getBucketDuration() != bucketDuration) { updated = new NetworkStatsHistory( bucketDuration, estimateResizeBuckets(existing, bucketDuration)); updated.recordEntireHistory(existing); @@ -1003,7 +1012,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } private static int estimateResizeBuckets(NetworkStatsHistory existing, long newBucketDuration) { - return (int) (existing.bucketCount * existing.bucketDuration / newBucketDuration); + return (int) (existing.size() * existing.getBucketDuration() / newBucketDuration); } // @VisibleForTesting diff --git a/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java index b4ac987..33fd355 100644 --- a/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java @@ -447,7 +447,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase { // pretend that 512 bytes total have happened stats = new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 256L, 256L); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 256L, 2L, 256L, 2L); expect(mStatsService.getSummaryForNetwork(sTemplateWifi, TIME_FEB_15, TIME_MAR_10)) .andReturn(stats).atLeastOnce(); diff --git a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java index f2c28bb..36b3b82 100644 --- a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java @@ -168,7 +168,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectDefaultSettings(); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 1L, 2048L, 2L)); expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); replay(); @@ -184,7 +184,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectDefaultSettings(); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 8192L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 4L, 8192L, 8L)); expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); replay(); @@ -219,10 +219,10 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectDefaultSettings(); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 8L, 2048L, 16L)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 2) - .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 512L, 256L) - .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 128L)); + .addValues(TEST_IFACE, UID_RED, TAG_NONE, 512L, 4L, 256L, 2L) + .addValues(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 1L, 128L, 1L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -284,7 +284,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 512L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 4L, 512L, 4L)); expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); replay(); @@ -295,8 +295,8 @@ public class NetworkStatsServiceTest extends AndroidTestCase { total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); assertEquals(512L, total[0]); assertEquals(512L, total[1]); - assertEquals(HOUR_IN_MILLIS, history.bucketDuration); - assertEquals(2, history.bucketCount); + assertEquals(HOUR_IN_MILLIS, history.getBucketDuration()); + assertEquals(2, history.size()); verifyAndReset(); // now change bucket duration setting and trigger another poll with @@ -314,8 +314,8 @@ public class NetworkStatsServiceTest extends AndroidTestCase { total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); assertEquals(512L, total[0]); assertEquals(512L, total[1]); - assertEquals(30 * MINUTE_IN_MILLIS, history.bucketDuration); - assertEquals(4, history.bucketCount); + assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration()); + assertEquals(4, history.size()); verifyAndReset(); } @@ -338,11 +338,11 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectDefaultSettings(); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 2048L, 512L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 2048L, 16L, 512L, 4L)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 3) - .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 1536L, 512L) - .addEntry(TEST_IFACE, UID_RED, 0xF00D, 512L, 512L) - .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 512L, 0L)); + .addValues(TEST_IFACE, UID_RED, TAG_NONE, 1536L, 12L, 512L, 4L) + .addValues(TEST_IFACE, UID_RED, 0xF00D, 512L, 4L, 512L, 4L) + .addValues(TEST_IFACE, UID_BLUE, TAG_NONE, 512L, 4L, 0L, 0L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -373,9 +373,9 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectDefaultSettings(); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 128L, 1024L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 128L, 1L, 1024L, 8L)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 1024L)); + .addValues(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 1L, 1024L, 8L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -412,11 +412,11 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectTime(TEST_START + elapsedRealtime); expectDefaultSettings(); expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4128L, 544L)); + .addValues(TEST_IFACE, UID_ALL, TAG_NONE, 4128L, 258L, 544L, 34L)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 16L, 16L) - .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 4096L, 512L) - .addEntry(TEST_IFACE, UID_GREEN, TAG_NONE, 16L, 16L)); + .addValues(TEST_IFACE, UID_RED, TAG_NONE, 16L, 1L, 16L, 1L) + .addValues(TEST_IFACE, UID_BLUE, TAG_NONE, 4096L, 258L, 512L, 32L) + .addValues(TEST_IFACE, UID_GREEN, TAG_NONE, 16L, 1L, 16L, 1L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -468,8 +468,8 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectDefaultSettings(); expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 1024L, 1024L) - .addEntry(TEST_IFACE, UID_RED, 0xF00D, 512L, 512L)); + .addValues(TEST_IFACE, UID_RED, TAG_NONE, 1024L, 8L, 1024L, 8L) + .addValues(TEST_IFACE, UID_RED, 0xF00D, 512L, 4L, 512L, 4L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -497,7 +497,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectDefaultSettings(); expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 512L, 256L)); + .addValues(TEST_IFACE, UID_RED, TAG_NONE, 512L, 4L, 256L, 2L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -548,9 +548,9 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectDefaultSettings(); expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 50L, 50L) - .addEntry(TEST_IFACE, UID_RED, 0xF00D, 10L, 10L) - .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 1024L, 512L)); + .addValues(TEST_IFACE, UID_RED, TAG_NONE, 50L, 5L, 50L, 5L) + .addValues(TEST_IFACE, UID_RED, 0xF00D, 10L, 1L, 10L, 1L) + .addValues(TEST_IFACE, UID_BLUE, TAG_NONE, 1024L, 8L, 512L, 4L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -566,7 +566,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { expectDefaultSettings(); expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) - .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 2048L, 1024L)); + .addValues(TEST_IFACE, UID_BLUE, TAG_NONE, 2048L, 16L, 1024L, 8L)); replay(); mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); @@ -574,17 +574,17 @@ public class NetworkStatsServiceTest extends AndroidTestCase { // first verify entire history present NetworkStats stats = mService.getSummaryForAllUid( sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); - assertEquals(3, stats.size); - assertStatsEntry(stats, 0, IFACE_ALL, UID_RED, TAG_NONE, 50L, 50L); - assertStatsEntry(stats, 1, IFACE_ALL, UID_RED, 0xF00D, 10L, 10L); - assertStatsEntry(stats, 2, IFACE_ALL, UID_BLUE, TAG_NONE, 2048L, 1024L); + assertEquals(3, stats.size()); + assertEntry(stats, 0, IFACE_ALL, UID_RED, TAG_NONE, 50L, 5L, 50L, 5L); + assertEntry(stats, 1, IFACE_ALL, UID_RED, 0xF00D, 10L, 1L, 10L, 1L); + assertEntry(stats, 2, IFACE_ALL, UID_BLUE, TAG_NONE, 2048L, 16L, 1024L, 8L); // now verify that recent history only contains one uid final long currentTime = TEST_START + elapsedRealtime; stats = mService.getSummaryForAllUid( sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true); - assertEquals(1, stats.size); - assertStatsEntry(stats, 0, IFACE_ALL, UID_BLUE, TAG_NONE, 1024L, 512L); + assertEquals(1, stats.size()); + assertEntry(stats, 0, IFACE_ALL, UID_BLUE, TAG_NONE, 1024L, 8L, 512L, 4L); verifyAndReset(); } @@ -660,13 +660,17 @@ public class NetworkStatsServiceTest extends AndroidTestCase { } } - private static void assertStatsEntry( - NetworkStats stats, int i, String iface, int uid, int tag, long rx, long tx) { - assertEquals(iface, stats.iface[i]); - assertEquals(uid, stats.uid[i]); - assertEquals(tag, stats.tag[i]); - assertEquals(rx, stats.rx[i]); - assertEquals(tx, stats.tx[i]); + private static void assertEntry(NetworkStats stats, int i, String iface, int uid, int tag, + long rxBytes, long rxPackets, long txBytes, long txPackets) { + final NetworkStats.Entry entry = stats.getValues(i, null); + assertEquals(iface, entry.iface); + assertEquals(uid, entry.uid); + assertEquals(tag, entry.tag); + assertEquals(rxBytes, entry.rxBytes); + // TODO: enable testing packet counts once stored in history +// assertEquals(rxPackets, entry.rxPackets); + assertEquals(txBytes, entry.txBytes); +// assertEquals(txPackets, entry.txPackets); } private static NetworkState buildWifiState() { diff --git a/services/tests/servicestests/src/com/android/server/ThrottleServiceTest.java b/services/tests/servicestests/src/com/android/server/ThrottleServiceTest.java index 2f275c3..50c18f0 100644 --- a/services/tests/servicestests/src/com/android/server/ThrottleServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/ThrottleServiceTest.java @@ -289,7 +289,7 @@ public class ThrottleServiceTest extends AndroidTestCase { public void expectGetInterfaceCounter(long rx, long tx) throws Exception { // TODO: provide elapsedRealtime mock to match TimeAuthority final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1); - stats.addEntry(TEST_IFACE, NetworkStats.UID_ALL, NetworkStats.TAG_NONE, rx, tx); + stats.addValues(TEST_IFACE, NetworkStats.UID_ALL, NetworkStats.TAG_NONE, rx, 0L, tx, 0L); expect(mMockNMService.getNetworkStatsSummary()).andReturn(stats).atLeastOnce(); } |