diff options
Diffstat (limited to 'core/java/com')
-rw-r--r-- | core/java/com/android/internal/net/NetworkStatsFactory.java | 33 | ||||
-rw-r--r-- | core/java/com/android/internal/os/BatteryStatsImpl.java | 122 |
2 files changed, 116 insertions, 39 deletions
diff --git a/core/java/com/android/internal/net/NetworkStatsFactory.java b/core/java/com/android/internal/net/NetworkStatsFactory.java index 8282d23..e2a2b1e 100644 --- a/core/java/com/android/internal/net/NetworkStatsFactory.java +++ b/core/java/com/android/internal/net/NetworkStatsFactory.java @@ -17,6 +17,7 @@ package com.android.internal.net; import static android.net.NetworkStats.SET_ALL; +import static android.net.NetworkStats.TAG_ALL; import static android.net.NetworkStats.TAG_NONE; import static android.net.NetworkStats.UID_ALL; import static com.android.server.NetworkManagementSocketTagger.kernelToTag; @@ -26,6 +27,7 @@ import android.os.StrictMode; import android.os.SystemClock; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.util.ArrayUtils; import com.android.internal.util.ProcFileReader; import java.io.File; @@ -165,22 +167,32 @@ public class NetworkStatsFactory { } public NetworkStats readNetworkStatsDetail() throws IOException { - return readNetworkStatsDetail(UID_ALL); + return readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null); } - public NetworkStats readNetworkStatsDetail(int limitUid) throws IOException { + public NetworkStats readNetworkStatsDetail(int limitUid, String[] limitIfaces, int limitTag, + NetworkStats lastStats) + throws IOException { if (USE_NATIVE_PARSING) { - final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 0); - if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid) != 0) { + final NetworkStats stats; + if (lastStats != null) { + stats = lastStats; + stats.setElapsedRealtime(SystemClock.elapsedRealtime()); + } else { + stats = new NetworkStats(SystemClock.elapsedRealtime(), -1); + } + if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid, + limitIfaces, limitTag) != 0) { throw new IOException("Failed to parse network stats"); } if (SANITY_CHECK_NATIVE) { - final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid); + final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid, + limitIfaces, limitTag); assertEquals(javaStats, stats); } return stats; } else { - return javaReadNetworkStatsDetail(mStatsXtUid, limitUid); + return javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag); } } @@ -189,7 +201,8 @@ public class NetworkStatsFactory { * expected to monotonically increase since device boot. */ @VisibleForTesting - public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid) + public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid, + String[] limitIfaces, int limitTag) throws IOException { final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); @@ -222,7 +235,9 @@ public class NetworkStatsFactory { entry.txBytes = reader.nextLong(); entry.txPackets = reader.nextLong(); - if (limitUid == UID_ALL || limitUid == entry.uid) { + if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface)) + && (limitUid == UID_ALL || limitUid == entry.uid) + && (limitTag == TAG_ALL || limitTag == entry.tag)) { stats.addValues(entry); } @@ -264,5 +279,5 @@ public class NetworkStatsFactory { */ @VisibleForTesting public static native int nativeReadNetworkStatsDetail( - NetworkStats stats, String path, int limitUid); + NetworkStats stats, String path, int limitUid, String[] limitIfaces, int limitTag); } diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 274e267..c3e9862 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -16,6 +16,7 @@ package com.android.internal.os; +import static android.net.NetworkStats.UID_ALL; import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED; import android.bluetooth.BluetoothDevice; @@ -52,6 +53,7 @@ import android.util.TimeUtils; import com.android.internal.annotations.GuardedBy; import com.android.internal.net.NetworkStatsFactory; +import com.android.internal.util.ArrayUtils; import com.android.internal.util.FastPrintWriter; import com.android.internal.util.JournaledFile; import com.google.android.collect.Sets; @@ -370,12 +372,15 @@ public final class BatteryStatsImpl extends BatteryStats { new HashMap<String, KernelWakelockStats>(); private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory(); - private NetworkStats mLastSnapshot; + private NetworkStats mLastMobileSnapshot; + private NetworkStats mLastWifiSnapshot; + private NetworkStats mTmpNetworkStats; + private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry(); @GuardedBy("this") - private HashSet<String> mMobileIfaces = Sets.newHashSet(); + private String[] mMobileIfaces = new String[0]; @GuardedBy("this") - private HashSet<String> mWifiIfaces = Sets.newHashSet(); + private String[] mWifiIfaces = new String[0]; // For debugging public BatteryStatsImpl() { @@ -2954,16 +2959,45 @@ public final class BatteryStatsImpl extends BatteryStats { } } + private static String[] includeInStringArray(String[] array, String str) { + if (ArrayUtils.indexOf(array, str) >= 0) { + return array; + } + String[] newArray = new String[array.length+1]; + System.arraycopy(array, 0, newArray, 0, array.length); + newArray[array.length] = str; + return newArray; + } + + private static String[] excludeFromStringArray(String[] array, String str) { + int index = ArrayUtils.indexOf(array, str); + if (index >= 0) { + String[] newArray = new String[array.length-1]; + if (index > 0) { + System.arraycopy(array, 0, newArray, 0, index); + } + if (index < array.length-1) { + System.arraycopy(array, index+1, newArray, index, array.length-index-1); + } + return newArray; + } + return array; + } + public void noteNetworkInterfaceTypeLocked(String iface, int networkType) { if (ConnectivityManager.isNetworkTypeMobile(networkType)) { - mMobileIfaces.add(iface); + mMobileIfaces = includeInStringArray(mMobileIfaces, iface); + if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces); } else { - mMobileIfaces.remove(iface); + mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface); + if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces); } if (ConnectivityManager.isNetworkTypeWifi(networkType)) { - mWifiIfaces.add(iface); + mWifiIfaces = includeInStringArray(mWifiIfaces, iface); + if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces); } else { - mWifiIfaces.remove(iface); + mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface); + if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces); } } @@ -5572,33 +5606,33 @@ public final class BatteryStatsImpl extends BatteryStats { private void updateNetworkActivityLocked() { if (!SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) return; - final NetworkStats snapshot; - try { - snapshot = mNetworkStatsFactory.readNetworkStatsDetail(); - } catch (IOException e) { - Log.wtf(TAG, "Failed to read network stats", e); - return; - } - - if (mLastSnapshot == null) { - mLastSnapshot = snapshot; - return; - } + if (mMobileIfaces.length > 0) { + final NetworkStats snapshot; + try { + snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL, + mMobileIfaces, NetworkStats.TAG_NONE, mLastMobileSnapshot); + } catch (IOException e) { + Log.wtf(TAG, "Failed to read mobile network stats", e); + return; + } - final NetworkStats delta = snapshot.subtract(mLastSnapshot); - mLastSnapshot = snapshot; + if (mLastMobileSnapshot == null) { + mLastMobileSnapshot = snapshot; + return; + } - NetworkStats.Entry entry = null; - final int size = delta.size(); - for (int i = 0; i < size; i++) { - entry = delta.getValues(i, entry); + final NetworkStats delta = NetworkStats.subtract(snapshot, mLastMobileSnapshot, + null, null, mTmpNetworkStats); + mTmpNetworkStats = delta; + mLastMobileSnapshot = snapshot; - if (entry.rxBytes == 0 || entry.txBytes == 0) continue; - if (entry.tag != NetworkStats.TAG_NONE) continue; + final int size = delta.size(); + for (int i = 0; i < size; i++) { + final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry); - final Uid u = getUidStatsLocked(entry.uid); + if (entry.rxBytes == 0 || entry.txBytes == 0) continue; - if (mMobileIfaces.contains(entry.iface)) { + final Uid u = getUidStatsLocked(entry.uid); u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets); u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, @@ -5610,8 +5644,36 @@ public final class BatteryStatsImpl extends BatteryStats { entry.rxPackets); mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked( entry.txPackets); + } + } + + if (mWifiIfaces.length > 0) { + final NetworkStats snapshot; + try { + snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL, + mWifiIfaces, NetworkStats.TAG_NONE, mLastWifiSnapshot); + } catch (IOException e) { + Log.wtf(TAG, "Failed to read wifi network stats", e); + return; + } + + if (mLastWifiSnapshot == null) { + mLastWifiSnapshot = snapshot; + return; + } + + final NetworkStats delta = NetworkStats.subtract(snapshot, mLastWifiSnapshot, + null, null, mTmpNetworkStats); + mTmpNetworkStats = delta; + mLastWifiSnapshot = snapshot; + + final int size = delta.size(); + for (int i = 0; i < size; i++) { + final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry); + + if (entry.rxBytes == 0 || entry.txBytes == 0) continue; - } else if (mWifiIfaces.contains(entry.iface)) { + final Uid u = getUidStatsLocked(entry.uid); u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes, entry.rxPackets); u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes, entry.txPackets); |