use of android.net.NetworkStats in project android_frameworks_base by ResurrectionRemix.
the class BatteryStatsImpl method getNetworkStatsDeltaLocked.
/**
* Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
* as a buffer of NetworkStats objects to cycle through when computing deltas.
*/
private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces, NetworkStats[] networkStatsBuffer) throws IOException {
if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED, false)) {
return null;
}
final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats, networkStatsBuffer[NETWORK_STATS_LAST], null, null, networkStatsBuffer[NETWORK_STATS_DELTA]);
networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
networkStatsBuffer[NETWORK_STATS_LAST] = stats;
return networkStatsBuffer[NETWORK_STATS_DELTA];
}
use of android.net.NetworkStats in project android_frameworks_base by ResurrectionRemix.
the class NetworkStatsFactory method readNetworkStatsSummaryDev.
/**
* Parse and return interface-level summary {@link NetworkStats} measured
* using {@code /proc/net/dev} style hooks, which may include non IP layer
* traffic. Values monotonically increase since device boot, and may include
* details about inactive interfaces.
*
* @throws IllegalStateException when problem parsing stats.
*/
public NetworkStats readNetworkStatsSummaryDev() throws IOException {
final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
final NetworkStats.Entry entry = new NetworkStats.Entry();
ProcFileReader reader = null;
try {
reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll));
while (reader.hasMoreData()) {
entry.iface = reader.nextString();
entry.uid = UID_ALL;
entry.set = SET_ALL;
entry.tag = TAG_NONE;
final boolean active = reader.nextInt() != 0;
// always include snapshot values
entry.rxBytes = reader.nextLong();
entry.rxPackets = reader.nextLong();
entry.txBytes = reader.nextLong();
entry.txPackets = reader.nextLong();
// fold in active numbers, but only when active
if (active) {
entry.rxBytes += reader.nextLong();
entry.rxPackets += reader.nextLong();
entry.txBytes += reader.nextLong();
entry.txPackets += reader.nextLong();
}
stats.addValues(entry);
reader.finishLine();
}
} catch (NullPointerException e) {
throw new ProtocolException("problem parsing stats", e);
} catch (NumberFormatException e) {
throw new ProtocolException("problem parsing stats", e);
} finally {
IoUtils.closeQuietly(reader);
StrictMode.setThreadPolicy(savedPolicy);
}
return stats;
}
use of android.net.NetworkStats in project android_frameworks_base by ResurrectionRemix.
the class NetworkStatsFactory method readNetworkStatsDetailInternal.
private NetworkStats readNetworkStatsDetailInternal(int limitUid, String[] limitIfaces, int limitTag, NetworkStats lastStats) throws IOException {
if (USE_NATIVE_PARSING) {
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, limitIfaces, limitTag);
assertEquals(javaStats, stats);
}
return stats;
} else {
return javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag);
}
}
use of android.net.NetworkStats in project android_frameworks_base by ResurrectionRemix.
the class NetworkStatsFactory method readNetworkStatsDetail.
public NetworkStats readNetworkStatsDetail(int limitUid, String[] limitIfaces, int limitTag, NetworkStats lastStats) throws IOException {
final NetworkStats stats = readNetworkStatsDetailInternal(limitUid, limitIfaces, limitTag, lastStats);
synchronized (sStackedIfaces) {
// Sigh, xt_qtaguid ends up double-counting tx traffic going through
// clatd interfaces, so we need to subtract it here.
final int size = sStackedIfaces.size();
for (int i = 0; i < size; i++) {
final String stackedIface = sStackedIfaces.keyAt(i);
final String baseIface = sStackedIfaces.valueAt(i);
// Count up the tx traffic and subtract from root UID on the
// base interface.
NetworkStats.Entry adjust = new NetworkStats.Entry(baseIface, 0, 0, 0, 0L, 0L, 0L, 0L, 0L);
NetworkStats.Entry entry = null;
for (int j = 0; j < stats.size(); j++) {
entry = stats.getValues(j, entry);
if (Objects.equals(entry.iface, stackedIface)) {
adjust.txBytes -= entry.txBytes;
adjust.txPackets -= entry.txPackets;
}
}
stats.combineValues(adjust);
}
}
// Double sigh, all rx traffic on clat needs to be tweaked to
// account for the dropped IPv6 header size post-unwrap.
NetworkStats.Entry entry = null;
for (int i = 0; i < stats.size(); i++) {
entry = stats.getValues(i, entry);
if (entry.iface != null && entry.iface.startsWith("clat")) {
// Delta between IPv4 header (20b) and IPv6 header (40b)
entry.rxBytes = entry.rxPackets * 20;
entry.rxPackets = 0;
entry.txBytes = 0;
entry.txPackets = 0;
stats.combineValues(entry);
}
}
return stats;
}
use of android.net.NetworkStats in project android_frameworks_base by ResurrectionRemix.
the class NetworkStatsFactory method readNetworkStatsSummaryXt.
/**
* Parse and return interface-level summary {@link NetworkStats}. Designed
* to return only IP layer traffic. Values monotonically increase since
* device boot, and may include details about inactive interfaces.
*
* @throws IllegalStateException when problem parsing stats.
*/
public NetworkStats readNetworkStatsSummaryXt() throws IOException {
final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
// return null when kernel doesn't support
if (!mStatsXtIfaceFmt.exists())
return null;
final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
final NetworkStats.Entry entry = new NetworkStats.Entry();
ProcFileReader reader = null;
try {
// open and consume header line
reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt));
reader.finishLine();
while (reader.hasMoreData()) {
entry.iface = reader.nextString();
entry.uid = UID_ALL;
entry.set = SET_ALL;
entry.tag = TAG_NONE;
entry.rxBytes = reader.nextLong();
entry.rxPackets = reader.nextLong();
entry.txBytes = reader.nextLong();
entry.txPackets = reader.nextLong();
stats.addValues(entry);
reader.finishLine();
}
} catch (NullPointerException e) {
throw new ProtocolException("problem parsing stats", e);
} catch (NumberFormatException e) {
throw new ProtocolException("problem parsing stats", e);
} finally {
IoUtils.closeQuietly(reader);
StrictMode.setThreadPolicy(savedPolicy);
}
return stats;
}
Aggregations