Search in sources :

Example 1 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by ParanoidAndroid.

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;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream)

Example 2 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by ParanoidAndroid.

the class NetworkStatsFactory method readNetworkStatsDetail.

public NetworkStats readNetworkStatsDetail(int limitUid) throws IOException {
    if (USE_NATIVE_PARSING) {
        final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 0);
        if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid) != 0) {
            throw new IOException("Failed to parse network stats");
        }
        if (SANITY_CHECK_NATIVE) {
            final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid);
            assertEquals(javaStats, stats);
        }
        return stats;
    } else {
        return javaReadNetworkStatsDetail(mStatsXtUid, limitUid);
    }
}
Also used : NetworkStats(android.net.NetworkStats) IOException(java.io.IOException)

Example 3 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by ParanoidAndroid.

the class NetworkStatsFactory method javaReadNetworkStatsDetail.

/**
     * Parse and return {@link NetworkStats} with UID-level details. Values are
     * expected to monotonically increase since device boot.
     */
@VisibleForTesting
public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid) throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    int idx = 1;
    int lastIdx = 1;
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(detailPath));
        reader.finishLine();
        while (reader.hasMoreData()) {
            idx = reader.nextInt();
            if (idx != lastIdx + 1) {
                throw new ProtocolException("inconsistent idx=" + idx + " after lastIdx=" + lastIdx);
            }
            lastIdx = idx;
            entry.iface = reader.nextString();
            entry.tag = kernelToTag(reader.nextString());
            entry.uid = reader.nextInt();
            entry.set = reader.nextInt();
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            if (limitUid == UID_ALL || limitUid == entry.uid) {
                stats.addValues(entry);
            }
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 4 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by ParanoidAndroid.

the class BatteryStatsImpl method doPlugLocked.

public void doPlugLocked(long elapsedRealtime, long batteryUptime, long batteryRealtime) {
    NetworkStats.Entry entry = null;
    for (int iu = mUidStats.size() - 1; iu >= 0; iu--) {
        Uid u = mUidStats.valueAt(iu);
        if (u.mStartedTcpBytesReceived >= 0) {
            u.mCurrentTcpBytesReceived = u.computeCurrentTcpBytesReceived();
            u.mStartedTcpBytesReceived = -1;
        }
        if (u.mStartedTcpBytesSent >= 0) {
            u.mCurrentTcpBytesSent = u.computeCurrentTcpBytesSent();
            u.mStartedTcpBytesSent = -1;
        }
    }
    for (int i = mUnpluggables.size() - 1; i >= 0; i--) {
        mUnpluggables.get(i).plug(elapsedRealtime, batteryUptime, batteryRealtime);
    }
    // Track both mobile and total overall data
    final NetworkStats ifaceStats = getNetworkStatsSummary();
    entry = ifaceStats.getTotal(entry, mMobileIfaces);
    doDataPlug(mMobileDataRx, entry.rxBytes);
    doDataPlug(mMobileDataTx, entry.txBytes);
    entry = ifaceStats.getTotal(entry);
    doDataPlug(mTotalDataRx, entry.rxBytes);
    doDataPlug(mTotalDataTx, entry.txBytes);
    // Track radio awake time
    mRadioDataUptime = getRadioDataUptime();
    mRadioDataStart = -1;
    // Track bt headset ping count
    mBluetoothPingCount = getBluetoothPingCount();
    mBluetoothPingStart = -1;
}
Also used : NetworkStats(android.net.NetworkStats)

Example 5 with NetworkStats

use of android.net.NetworkStats in project android_frameworks_base by ParanoidAndroid.

the class BandwidthTest method downloadFile.

/**
     * Helper method that downloads a file using http connection from a test server and reports the
     * data usage stats to instrumentation out.
     */
protected void downloadFile() throws Exception {
    NetworkStats pre_test_stats = fetchDataFromProc(mUid);
    String ts = Long.toString(System.currentTimeMillis());
    String targetUrl = BandwidthTestUtil.buildDownloadUrl(mTestServer, FILE_SIZE, mDeviceId, ts);
    TrafficStats.startDataProfiling(mContext);
    File tmpSaveFile = new File(BASE_DIR + File.separator + TMP_FILENAME);
    assertTrue(BandwidthTestUtil.DownloadFromUrl(targetUrl, tmpSaveFile));
    NetworkStats prof_stats = TrafficStats.stopDataProfiling(mContext);
    Log.d(LOG_TAG, prof_stats.toString());
    NetworkStats post_test_stats = fetchDataFromProc(mUid);
    NetworkStats proc_stats = post_test_stats.subtract(pre_test_stats);
    // Output measurements to instrumentation out, so that it can be compared to that of
    // the server.
    Bundle results = new Bundle();
    results.putString("device_id", mDeviceId);
    results.putString("timestamp", ts);
    results.putInt("size", FILE_SIZE);
    AddStatsToResults(PROF_LABEL, prof_stats, results);
    AddStatsToResults(PROC_LABEL, proc_stats, results);
    getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, results);
    // Clean up.
    assertTrue(cleanUpFile(tmpSaveFile));
}
Also used : Bundle(android.os.Bundle) NetworkStats(android.net.NetworkStats) File(java.io.File)

Aggregations

NetworkStats (android.net.NetworkStats)271 File (java.io.File)49 DataUsageRequest (android.net.DataUsageRequest)32 Intent (android.content.Intent)29 NetworkIdentity (android.net.NetworkIdentity)28 NetworkStatsHistory (android.net.NetworkStatsHistory)21 Bundle (android.os.Bundle)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 FileInputStream (java.io.FileInputStream)18 ProtocolException (java.net.ProtocolException)18 PendingIntent (android.app.PendingIntent)17 IOException (java.io.IOException)17 NetworkPolicy (android.net.NetworkPolicy)15 NetworkState (android.net.NetworkState)15 Test (org.junit.Test)14 Suppress (android.test.suitebuilder.annotation.Suppress)13 VpnInfo (com.android.internal.net.VpnInfo)10 RemoteException (android.os.RemoteException)8 NetworkTemplate (android.net.NetworkTemplate)6