Search in sources :

Example 21 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsFactoryTest method testNetworkStatsDetail.

public void testNetworkStatsDetail() throws Exception {
    stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
    final NetworkStats stats = mFactory.readNetworkStatsDetail();
    assertEquals(70, stats.size());
    assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L);
    assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L);
    assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L);
    assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L);
    assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L);
}
Also used : NetworkStats(android.net.NetworkStats) File(java.io.File)

Example 22 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsFactoryTest method testNetworkStatsSingle.

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.readNetworkStatsSummaryDev();
    assertEquals(6, stats.size());
    assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L);
    assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L);
    assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L);
}
Also used : NetworkStats(android.net.NetworkStats) File(java.io.File)

Example 23 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkManagementService method getNetworkStatsTethering.

@Override
public NetworkStats getNetworkStatsTethering() {
    mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 1);
    try {
        final NativeDaemonEvent[] events = mConnector.executeForList("bandwidth", "gettetherstats");
        for (NativeDaemonEvent event : events) {
            if (event.getCode() != TetheringStatsListResult)
                continue;
            // 114 ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
            final StringTokenizer tok = new StringTokenizer(event.getMessage());
            try {
                final String ifaceIn = tok.nextToken();
                final String ifaceOut = tok.nextToken();
                final NetworkStats.Entry entry = new NetworkStats.Entry();
                entry.iface = ifaceOut;
                entry.uid = UID_TETHERING;
                entry.set = SET_DEFAULT;
                entry.tag = TAG_NONE;
                entry.rxBytes = Long.parseLong(tok.nextToken());
                entry.rxPackets = Long.parseLong(tok.nextToken());
                entry.txBytes = Long.parseLong(tok.nextToken());
                entry.txPackets = Long.parseLong(tok.nextToken());
                stats.combineValues(entry);
            } catch (NoSuchElementException e) {
                throw new IllegalStateException("problem parsing tethering stats: " + event);
            } catch (NumberFormatException e) {
                throw new IllegalStateException("problem parsing tethering stats: " + event);
            }
        }
    } catch (NativeDaemonConnectorException e) {
        throw e.rethrowAsParcelableException();
    }
    return stats;
}
Also used : StringTokenizer(java.util.StringTokenizer) NetworkStats(android.net.NetworkStats) NoSuchElementException(java.util.NoSuchElementException)

Example 24 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class BatteryStatsImpl method init.

private void init(Clocks clocks) {
    mClocks = clocks;
    mMobileNetworkStats = new NetworkStats[] { new NetworkStats(mClocks.elapsedRealtime(), 50), new NetworkStats(mClocks.elapsedRealtime(), 50), new NetworkStats(mClocks.elapsedRealtime(), 50) };
    mWifiNetworkStats = new NetworkStats[] { new NetworkStats(mClocks.elapsedRealtime(), 50), new NetworkStats(mClocks.elapsedRealtime(), 50), new NetworkStats(mClocks.elapsedRealtime(), 50) };
}
Also used : NetworkStats(android.net.NetworkStats)

Example 25 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class BatteryStatsImpl method updateMobileRadioStateLocked.

/**
     * Distribute Cell radio energy info and network traffic to apps.
     */
public void updateMobileRadioStateLocked(final long elapsedRealtimeMs, final ModemActivityInfo activityInfo) {
    if (DEBUG_ENERGY) {
        Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
    }
    NetworkStats delta = null;
    try {
        if (!ArrayUtils.isEmpty(mMobileIfaces)) {
            delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
        }
    } catch (IOException e) {
        Slog.wtf(TAG, "Failed to get mobile network stats", e);
        return;
    }
    if (!mOnBatteryInternal) {
        return;
    }
    long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000);
    mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
    long totalRxPackets = 0;
    long totalTxPackets = 0;
    if (delta != null) {
        final int size = delta.size();
        for (int i = 0; i < size; i++) {
            final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
            if (entry.rxPackets == 0 && entry.txPackets == 0) {
                continue;
            }
            if (DEBUG_ENERGY) {
                Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets + " txPackets=" + entry.txPackets);
            }
            totalRxPackets += entry.rxPackets;
            totalTxPackets += entry.txPackets;
            final Uid u = getUidStatsLocked(mapUid(entry.uid));
            u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
            u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
            mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(entry.rxBytes);
            mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(entry.txBytes);
            mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(entry.rxPackets);
            mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(entry.txPackets);
        }
        // Now distribute proportional blame to the apps that did networking.
        long totalPackets = totalRxPackets + totalTxPackets;
        if (totalPackets > 0) {
            for (int i = 0; i < size; i++) {
                final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
                if (entry.rxPackets == 0 && entry.txPackets == 0) {
                    continue;
                }
                final Uid u = getUidStatsLocked(mapUid(entry.uid));
                // Distribute total radio active time in to this app.
                final long appPackets = entry.rxPackets + entry.txPackets;
                final long appRadioTime = (radioTime * appPackets) / totalPackets;
                u.noteMobileRadioActiveTimeLocked(appRadioTime);
                // Remove this app from the totals, so that we don't lose any time
                // due to rounding.
                radioTime -= appRadioTime;
                totalPackets -= appPackets;
                if (activityInfo != null) {
                    ControllerActivityCounterImpl activityCounter = u.getOrCreateModemControllerActivityLocked();
                    if (totalRxPackets > 0 && entry.rxPackets > 0) {
                        final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis()) / totalRxPackets;
                        activityCounter.getRxTimeCounter().addCountLocked(rxMs);
                    }
                    if (totalTxPackets > 0 && entry.txPackets > 0) {
                        for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
                            long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
                            txMs /= totalTxPackets;
                            activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
                        }
                    }
                }
            }
        }
        if (radioTime > 0) {
            // Whoops, there is some radio time we can't blame on an app!
            mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
            mMobileRadioActiveUnknownCount.addCountLocked(1);
        }
    }
    if (activityInfo != null) {
        mHasModemReporting = true;
        mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
        mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
        for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
            mModemActivity.getTxTimeCounters()[lvl].addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
        }
        // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
        final double opVolt = mPowerProfile.getAveragePower(PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
        if (opVolt != 0) {
            // We store the power drain as mAms.
            mModemActivity.getPowerCounter().addCountLocked((long) (activityInfo.getEnergyUsed() / opVolt));
        }
    }
}
Also used : NetworkStats(android.net.NetworkStats) IOException(java.io.IOException)

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