diff options
author | Jeff Sharkey <jsharkey@android.com> | 2011-10-04 16:54:49 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2011-10-09 13:49:08 -0700 |
commit | 1059c3c30ad96a15695c1a92ae8896e078a6309f (patch) | |
tree | 3f73fd2918711f25b2ad4dd643db73b6f5483856 /core/tests | |
parent | 650c53aad5a49c793bbcddad9bfce63a3b3d0360 (diff) | |
download | frameworks_base-1059c3c30ad96a15695c1a92ae8896e078a6309f.zip frameworks_base-1059c3c30ad96a15695c1a92ae8896e078a6309f.tar.gz frameworks_base-1059c3c30ad96a15695c1a92ae8896e078a6309f.tar.bz2 |
Move battery stats to xt_qtaguid for data stats.
Replace TrafficStats calls by reading values from xt_qtaguid kernel
module. To keep BatteryStatsImpl changes lightweight, cache recently
parsed stats. Tracks mobile ifaces from ConnectivityService.
Refactor xt_qtaguid parsing into factory outside of NMS. Add stats
grouping based on UID, and total based on limiting filters like iface
prefix and UID.
Bug: 4902271
Change-Id: I533f116c434b77f93355bf95b839e7478528505b
Diffstat (limited to 'core/tests')
7 files changed, 313 insertions, 0 deletions
diff --git a/core/tests/coretests/res/raw/net_dev_typical b/core/tests/coretests/res/raw/net_dev_typical new file mode 100644 index 0000000..290bf03 --- /dev/null +++ b/core/tests/coretests/res/raw/net_dev_typical @@ -0,0 +1,8 @@ +Inter-| Receive | Transmit + face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed + lo: 8308 116 0 0 0 0 0 0 8308 116 0 0 0 0 0 0 +rmnet0: 1507570 2205 0 0 0 0 0 0 489339 2237 0 0 0 0 0 0 + ifb0: 52454 151 0 151 0 0 0 0 0 0 0 0 0 0 0 0 + ifb1: 52454 151 0 151 0 0 0 0 0 0 0 0 0 0 0 0 + sit0: 0 0 0 0 0 0 0 0 0 0 148 0 0 0 0 0 +ip6tnl0: 0 0 0 0 0 0 0 0 0 0 151 151 0 0 0 0 diff --git a/core/tests/coretests/res/raw/xt_qtaguid_extended b/core/tests/coretests/res/raw/xt_qtaguid_extended new file mode 100644 index 0000000..2f3b4ec --- /dev/null +++ b/core/tests/coretests/res/raw/xt_qtaguid_extended @@ -0,0 +1,3 @@ +acct_tag_hex uid_tag_int iface rx_bytes rx_packets tx_bytes tx_packets teleported_goats idx +0x0 1000 test0 1024 10 2048 20 2716057 2 +0x0000F00D00000000 1000 test0 512 5 512 5 3370318 3 diff --git a/core/tests/coretests/res/raw/xt_qtaguid_iface_typical b/core/tests/coretests/res/raw/xt_qtaguid_iface_typical new file mode 100644 index 0000000..610723a --- /dev/null +++ b/core/tests/coretests/res/raw/xt_qtaguid_iface_typical @@ -0,0 +1,6 @@ +rmnet3 1 0 0 0 0 20822 501 1149991 815 +rmnet2 1 0 0 0 0 1594 15 1313 15 +rmnet1 1 0 0 0 0 207398 458 166918 565 +rmnet0 1 0 0 0 0 2112 24 700 10 +test1 1 1 2 3 4 5 6 7 8 +test2 0 1 2 3 4 5 6 7 8 diff --git a/core/tests/coretests/res/raw/xt_qtaguid_typical b/core/tests/coretests/res/raw/xt_qtaguid_typical new file mode 100644 index 0000000..8df4b1b --- /dev/null +++ b/core/tests/coretests/res/raw/xt_qtaguid_typical @@ -0,0 +1,32 @@ +idx iface acct_tag_hex uid_tag_int rx_bytes tx_bytes +2 wlan0 0x0 0 14615 4270 +3 wlan0 0x0 1000 5175 915 +4 wlan0 0x0 1021 3381 903 +5 wlan0 0x0 10004 333821 53558 +6 wlan0 0x0 10010 4888 37363 +7 wlan0 0x0 10013 52 104 +8 wlan0 0x74182ada00000000 10004 18725 1066 +9 rmnet0 0x0 0 301274 30244 +10 rmnet0 0x0 1000 304 441 +11 rmnet0 0x0 1013 2880 2272 +12 rmnet0 0x0 1021 31407 8430 +13 rmnet0 0x0 10003 32665 3814 +14 rmnet0 0x0 10004 2373141 420112 +15 rmnet0 0x0 10010 870370 1111727 +16 rmnet0 0x0 10013 240 240 +17 rmnet0 0x0 10016 16703 13512 +18 rmnet0 0x0 10017 3990 3269 +19 rmnet0 0x0 10018 474504 14516062 +20 rmnet0 0x0 10019 782804 71077 +21 rmnet0 0x0 10022 70671 49684 +22 rmnet0 0x0 10029 5785354 397159 +23 rmnet0 0x0 10033 2102 1686 +24 rmnet0 0x0 10034 15495464 227694 +25 rmnet0 0x0 10037 31184994 684122 +26 rmnet0 0x0 10051 298687 113485 +27 rmnet0 0x0 10056 29504 20669 +28 rmnet0 0x0 10069 683 596 +29 rmnet0 0x0 10072 34051 12453 +30 rmnet0 0x0 10077 7025393 213866 +31 rmnet0 0x0 10081 354 1178 +32 rmnet0 0x74182ada00000000 10037 28507378 437004 diff --git a/core/tests/coretests/res/raw/xt_qtaguid_typical_with_set b/core/tests/coretests/res/raw/xt_qtaguid_typical_with_set new file mode 100644 index 0000000..b302bb7 --- /dev/null +++ b/core/tests/coretests/res/raw/xt_qtaguid_typical_with_set @@ -0,0 +1,13 @@ +idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packets rx_tcp_packets rx_tcp_bytes rx_udp_packets rx_udp_bytes rx_other_packets rx_other_bytes tx_tcp_packets tx_tcp_bytes tx_udp_packets tx_udp_bytes tx_other_packets tx_other_bytes +2 rmnet0 0x0 0 0 14855 82 2804 47 2000 45 12799 35 56 2 676 13 2128 34 0 0 +3 rmnet0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +4 rmnet0 0x0 1000 0 278102 253 10487 182 277342 243 760 10 0 0 9727 172 760 10 0 0 +5 rmnet0 0x0 1000 1 26033 30 1401 26 25881 28 152 2 0 0 1249 24 152 2 0 0 +6 rmnet0 0x0 10012 0 40524 272 134138 293 40524 272 0 0 0 0 134138 293 0 0 0 0 +7 rmnet0 0x0 10012 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +8 rmnet0 0x0 10034 0 15791 59 9905 69 15791 59 0 0 0 0 9905 69 0 0 0 0 +9 rmnet0 0x0 10034 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +10 rmnet0 0x0 10055 0 3602 29 7739 59 3602 29 0 0 0 0 7739 59 0 0 0 0 +11 rmnet0 0x0 10055 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +12 rmnet0 0x7fff000300000000 1000 0 483 4 1931 6 483 4 0 0 0 0 1931 6 0 0 0 0 +13 rmnet0 0x7fff000300000000 1000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/core/tests/coretests/src/android/net/NetworkStatsTest.java b/core/tests/coretests/src/android/net/NetworkStatsTest.java index d78d2ef..7082deb 100644 --- a/core/tests/coretests/src/android/net/NetworkStatsTest.java +++ b/core/tests/coretests/src/android/net/NetworkStatsTest.java @@ -25,8 +25,12 @@ import static android.net.NetworkStats.UID_ALL; import android.test.suitebuilder.annotation.SmallTest; +import com.google.android.collect.Sets; + import junit.framework.TestCase; +import java.util.HashSet; + @SmallTest public class NetworkStatsTest extends TestCase { @@ -233,13 +237,43 @@ public class NetworkStatsTest extends TestCase { assertValues(first, 2, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L); } + public void testGetTotal() { + final NetworkStats stats = new NetworkStats(TEST_START, 3) + .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L) + .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L) + .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 64L, 4L, 0L, 0L, 0L) + .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 512L, 32L, 0L, 0L, 0L) + .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L) + .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L); + + assertValues(stats.getTotal(null), 1280L, 80L, 0L, 2L, 20L); + assertValues(stats.getTotal(null, 100), 1152L, 72L, 0L, 2L, 20L); + assertValues(stats.getTotal(null, 101), 128L, 8L, 0L, 0L, 0L); + + final HashSet<String> ifaces = Sets.newHashSet(); + assertValues(stats.getTotal(null, ifaces), 0L, 0L, 0L, 0L, 0L); + + ifaces.add(TEST_IFACE2); + assertValues(stats.getTotal(null, ifaces), 1024L, 64L, 0L, 0L, 0L); + } + private static void assertValues(NetworkStats stats, int index, String iface, int uid, int set, int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { final NetworkStats.Entry entry = stats.getValues(index, null); + assertValues(entry, iface, uid, set, tag); + assertValues(entry, rxBytes, rxPackets, txBytes, txPackets, operations); + } + + private static void assertValues( + NetworkStats.Entry entry, String iface, int uid, int set, int tag) { assertEquals(iface, entry.iface); assertEquals(uid, entry.uid); assertEquals(set, entry.set); assertEquals(tag, entry.tag); + } + + private static void assertValues(NetworkStats.Entry entry, long rxBytes, long rxPackets, + long txBytes, long txPackets, long operations) { assertEquals(rxBytes, entry.rxBytes); assertEquals(rxPackets, entry.rxPackets); assertEquals(txBytes, entry.txBytes); diff --git a/core/tests/coretests/src/com/android/internal/net/NetworkStatsFactoryTest.java b/core/tests/coretests/src/com/android/internal/net/NetworkStatsFactoryTest.java new file mode 100644 index 0000000..8a64f2b --- /dev/null +++ b/core/tests/coretests/src/com/android/internal/net/NetworkStatsFactoryTest.java @@ -0,0 +1,217 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.net; + +import static android.net.NetworkStats.SET_DEFAULT; +import static android.net.NetworkStats.SET_FOREGROUND; +import static android.net.NetworkStats.TAG_NONE; +import static android.net.NetworkStats.UID_ALL; +import static com.android.server.NetworkManagementSocketTagger.kernelToTag; + +import android.content.res.Resources; +import android.net.NetworkStats; +import android.test.AndroidTestCase; + +import com.android.frameworks.coretests.R; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.InputStream; +import java.io.OutputStream; + +import libcore.io.IoUtils; +import libcore.io.Streams; + +/** + * Tests for {@link NetworkStatsFactory}. + */ +public class NetworkStatsFactoryTest extends AndroidTestCase { + private File mTestProc; + private NetworkStatsFactory mFactory; + + @Override + public void setUp() throws Exception { + super.setUp(); + + mTestProc = new File(getContext().getFilesDir(), "proc"); + if (mTestProc.exists()) { + IoUtils.deleteContents(mTestProc); + } + + mFactory = new NetworkStatsFactory(mTestProc); + } + + @Override + public void tearDown() throws Exception { + mFactory = null; + + if (mTestProc.exists()) { + IoUtils.deleteContents(mTestProc); + } + + super.tearDown(); + } + + public void testNetworkStatsDetail() throws Exception { + stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); + + final NetworkStats stats = mFactory.readNetworkStatsDetail(); + assertEquals(31, stats.size()); + assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0, 14615L, 4270L); + assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 0, 333821L, 53558L); + assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 1947740890, 18725L, 1066L); + assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 0, 31184994L, 684122L); + assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 1947740890, 28507378L, 437004L); + } + + public void testNetworkStatsDetailExtended() throws Exception { + stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats")); + + final NetworkStats stats = mFactory.readNetworkStatsDetail(); + assertEquals(2, stats.size()); + assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0, 1024L, 2048L); + assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0xF00D, 512L, 512L); + } + + public void testNetworkStatsSummary() throws Exception { + stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); + + final NetworkStats stats = mFactory.readNetworkStatsSummary(); + assertEquals(6, stats.size()); + assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L); + assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L); + assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L); + assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L); + assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L); + assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L); + } + + public void testNetworkStatsSummaryDown() throws Exception { + stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); + stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active")); + stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes")); + stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets")); + stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes")); + stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets")); + + final NetworkStats stats = mFactory.readNetworkStatsSummary(); + assertEquals(7, stats.size()); + assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L); + assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L); + } + + public void testNetworkStatsCombined() throws Exception { + stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); + stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); + stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); + stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); + stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); + stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); + + final NetworkStats stats = mFactory.readNetworkStatsSummary(); + assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L, + 2205L + 20L, 489339L + 30L, 2237L + 40L); + } + + public void testNetworkStatsCombinedInactive() throws Exception { + stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); + stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); + stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); + stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); + stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); + stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); + + final NetworkStats stats = mFactory.readNetworkStatsSummary(); + assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L); + } + + public void testKernelTags() throws Exception { + assertEquals(0, kernelToTag("0x0000000000000000")); + assertEquals(0x32, kernelToTag("0x0000003200000000")); + assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); + assertEquals(0, kernelToTag("0x0000000000000000")); + assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); + } + + public void testNetworkStatsWithSet() throws Exception { + stageFile(R.raw.xt_qtaguid_typical_with_set, new File(mTestProc, "net/xt_qtaguid/stats")); + + final NetworkStats stats = mFactory.readNetworkStatsDetail(); + assertEquals(12, stats.size()); + assertStatsEntry(stats, "rmnet0", 1000, SET_DEFAULT, 0, 278102L, 253L, 10487L, 182L); + assertStatsEntry(stats, "rmnet0", 1000, SET_FOREGROUND, 0, 26033L, 30L, 1401L, 26L); + } + + public void testNetworkStatsSingle() throws Exception { + stageFile(R.raw.xt_qtaguid_iface_typical, new File(mTestProc, "net/xt_qtaguid/iface_stat_all")); + + final NetworkStats stats = mFactory.readNetworkStatsSummary(); + assertEquals(6, stats.size()); + assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 2112L, 24L, 700L, 10L); + assertStatsEntry(stats, "test1", UID_ALL, SET_DEFAULT, TAG_NONE, 6L, 8L, 10L, 12L); + assertStatsEntry(stats, "test2", UID_ALL, SET_DEFAULT, TAG_NONE, 1L, 2L, 3L, 4L); + } + + /** + * Copy a {@link Resources#openRawResource(int)} into {@link File} for + * testing purposes. + */ + private void stageFile(int rawId, File file) throws Exception { + new File(file.getParent()).mkdirs(); + InputStream in = null; + OutputStream out = null; + try { + in = getContext().getResources().openRawResource(rawId); + out = new FileOutputStream(file); + Streams.copy(in, out); + } finally { + IoUtils.closeQuietly(in); + IoUtils.closeQuietly(out); + } + } + + private void stageLong(long value, File file) throws Exception { + new File(file.getParent()).mkdirs(); + FileWriter out = null; + try { + out = new FileWriter(file); + out.write(Long.toString(value)); + } finally { + IoUtils.closeQuietly(out); + } + } + + private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, + int tag, long rxBytes, long txBytes) { + final int i = stats.findIndex(iface, uid, set, tag); + final NetworkStats.Entry entry = stats.getValues(i, null); + assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); + assertEquals("unexpected txBytes", txBytes, entry.txBytes); + } + + private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, + int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { + final int i = stats.findIndex(iface, uid, set, tag); + final NetworkStats.Entry entry = stats.getValues(i, null); + assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); + assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); + assertEquals("unexpected txBytes", txBytes, entry.txBytes); + assertEquals("unexpected txPackets", txPackets, entry.txPackets); + } + +} |