diff options
author | Jeff Sharkey <jsharkey@android.com> | 2011-07-12 20:20:56 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2011-07-12 20:20:56 -0700 |
commit | 434962e44ea93b1c4d216c55f636a435bf54aa54 (patch) | |
tree | 582235f79936fc76b59fb17bae16dbc29f2ff5fd /services | |
parent | f0c3b1b6b7fd2b851851c96d98d2f9a77415bf43 (diff) | |
download | frameworks_base-434962e44ea93b1c4d216c55f636a435bf54aa54.zip frameworks_base-434962e44ea93b1c4d216c55f636a435bf54aa54.tar.gz frameworks_base-434962e44ea93b1c4d216c55f636a435bf54aa54.tar.bz2 |
Include full network history around current time.
When requesting historical values with time ranges, always include
full values for buckets that span current time. (It doesn't make
sense to interpolate partial data.) Move getTotalData() to return
full Entry objects to prepare for packet counts.
Bug: 4691901
Change-Id: I717bd721be9f1d4a47c4121e46e07a56cb15bbf1
Diffstat (limited to 'services')
-rw-r--r-- | services/java/com/android/server/net/NetworkStatsService.java | 23 | ||||
-rw-r--r-- | services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java | 38 |
2 files changed, 33 insertions, 28 deletions
diff --git a/services/java/com/android/server/net/NetworkStatsService.java b/services/java/com/android/server/net/NetworkStatsService.java index 872438c..54e94db 100644 --- a/services/java/com/android/server/net/NetworkStatsService.java +++ b/services/java/com/android/server/net/NetworkStatsService.java @@ -313,21 +313,24 @@ public class NetworkStatsService extends INetworkStatsService.Stub { mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); synchronized (mStatsLock) { + // use system clock to be externally consistent + final long now = System.currentTimeMillis(); + final NetworkStats stats = new NetworkStats(end - start, 1); final NetworkStats.Entry entry = new NetworkStats.Entry(); - long[] total = new long[2]; + NetworkStatsHistory.Entry historyEntry = null; // combine total from all interfaces that match template for (NetworkIdentitySet ident : mNetworkStats.keySet()) { if (templateMatches(template, ident)) { final NetworkStatsHistory history = mNetworkStats.get(ident); - total = history.getTotalData(start, end, total); + historyEntry = history.getValues(start, end, now, historyEntry); entry.iface = IFACE_ALL; entry.uid = UID_ALL; entry.tag = TAG_NONE; - entry.rxBytes = total[0]; - entry.txBytes = total[1]; + entry.rxBytes = historyEntry.rxBytes; + entry.txBytes = historyEntry.txBytes; stats.combineValues(entry); } @@ -345,9 +348,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub { synchronized (mStatsLock) { ensureUidStatsLoadedLocked(); + // use system clock to be externally consistent + final long now = System.currentTimeMillis(); + final NetworkStats stats = new NetworkStats(end - start, 24); final NetworkStats.Entry entry = new NetworkStats.Entry(); - long[] total = new long[2]; + NetworkStatsHistory.Entry historyEntry = null; for (NetworkIdentitySet ident : mUidStats.keySet()) { if (templateMatches(template, ident)) { @@ -361,13 +367,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // other tags when requested. if (tag == TAG_NONE || includeTags) { final NetworkStatsHistory history = uidStats.valueAt(i); - total = history.getTotalData(start, end, total); + historyEntry = history.getValues(start, end, now, historyEntry); entry.iface = IFACE_ALL; entry.uid = uid; entry.tag = tag; - entry.rxBytes = total[0]; - entry.txBytes = total[1]; + entry.rxBytes = historyEntry.rxBytes; + entry.txBytes = historyEntry.txBytes; if (entry.rxBytes > 0 || entry.txBytes > 0) { stats.combineValues(entry); @@ -425,6 +431,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // broadcast. final int uid = intent.getIntExtra(EXTRA_UID, 0); synchronized (mStatsLock) { + // TODO: perform one last stats poll for UID removeUidLocked(uid); } } diff --git a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java index 36b3b82..ac74063 100644 --- a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java @@ -264,7 +264,6 @@ public class NetworkStatsServiceTest extends AndroidTestCase { public void testStatsBucketResize() throws Exception { long elapsedRealtime = 0; NetworkStatsHistory history = null; - long[] total = null; assertStatsFilesExist(false); @@ -292,9 +291,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { // verify service recorded history history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); - total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); - assertEquals(512L, total[0]); - assertEquals(512L, total[1]); + assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 512L); assertEquals(HOUR_IN_MILLIS, history.getBucketDuration()); assertEquals(2, history.size()); verifyAndReset(); @@ -311,9 +308,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { // verify identical stats, but spread across 4 buckets now history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); - total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); - assertEquals(512L, total[0]); - assertEquals(512L, total[1]); + assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 512L); assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration()); assertEquals(4, history.size()); verifyAndReset(); @@ -575,32 +570,28 @@ public class NetworkStatsServiceTest extends AndroidTestCase { NetworkStats stats = mService.getSummaryForAllUid( sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); 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); + assertValues(stats, 0, IFACE_ALL, UID_RED, TAG_NONE, 50L, 5L, 50L, 5L); + assertValues(stats, 1, IFACE_ALL, UID_RED, 0xF00D, 10L, 1L, 10L, 1L); + assertValues(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()); - assertEntry(stats, 0, IFACE_ALL, UID_BLUE, TAG_NONE, 1024L, 8L, 512L, 4L); + assertValues(stats, 0, IFACE_ALL, UID_BLUE, TAG_NONE, 1024L, 8L, 512L, 4L); verifyAndReset(); } - private void assertNetworkTotal(NetworkTemplate template, long rx, long tx) { + private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long txBytes) { final NetworkStatsHistory history = mService.getHistoryForNetwork(template); - final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); - assertEquals(rx, total[0]); - assertEquals(tx, total[1]); + assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, txBytes); } - private void assertUidTotal(NetworkTemplate template, int uid, long rx, long tx) { + private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long txBytes) { final NetworkStatsHistory history = mService.getHistoryForUid(template, uid, TAG_NONE); - final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); - assertEquals(rx, total[0]); - assertEquals(tx, total[1]); + assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, txBytes); } private void expectSystemReady() throws Exception { @@ -660,7 +651,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { } } - private static void assertEntry(NetworkStats stats, int i, String iface, int uid, int tag, + private static void assertValues(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); @@ -673,6 +664,13 @@ public class NetworkStatsServiceTest extends AndroidTestCase { // assertEquals(txPackets, entry.txPackets); } + private static void assertValues( + NetworkStatsHistory stats, long start, long end, long rxBytes, long txBytes) { + final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); + assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); + assertEquals("unexpected txBytes", txBytes, entry.txBytes); + } + private static NetworkState buildWifiState() { final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); info.setDetailedState(DetailedState.CONNECTED, null, null); |