Loading core/java/android/net/NetworkStatsHistory.java +5 −0 Original line number Original line Diff line number Diff line Loading @@ -270,6 +270,11 @@ public class NetworkStatsHistory implements Parcelable { || entry.operations < 0) { || entry.operations < 0) { throw new IllegalArgumentException("tried recording negative data"); throw new IllegalArgumentException("tried recording negative data"); } } if (entry.rxBytes == 0 && entry.rxPackets == 0 && entry.txBytes == 0 && entry.txPackets == 0 && entry.operations == 0) { // nothing to record; skip return; } // create any buckets needed by this range // create any buckets needed by this range ensureBuckets(start, end); ensureBuckets(start, end); Loading services/java/com/android/server/ConnectivityService.java +4 −3 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,7 @@ import android.net.EthernetDataTracker; import android.net.IConnectivityManager; import android.net.IConnectivityManager; import android.net.INetworkPolicyListener; import android.net.INetworkPolicyListener; import android.net.INetworkPolicyManager; import android.net.INetworkPolicyManager; import android.net.INetworkStatsService; import android.net.LinkAddress; import android.net.LinkAddress; import android.net.LinkProperties; import android.net.LinkProperties; import android.net.LinkProperties.CompareResult; import android.net.LinkProperties.CompareResult; Loading Loading @@ -306,8 +307,8 @@ public class ConnectivityService extends IConnectivityManager.Stub { // the set of network types that can only be enabled by system/sig apps // the set of network types that can only be enabled by system/sig apps List mProtectedNetworks; List mProtectedNetworks; public ConnectivityService( public ConnectivityService(Context context, INetworkManagementService netd, Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) { INetworkStatsService statsService, INetworkPolicyManager policyManager) { if (DBG) log("ConnectivityService starting up"); if (DBG) log("ConnectivityService starting up"); HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); Loading Loading @@ -496,7 +497,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b); INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b); mTethering = new Tethering(mContext, nmService, mHandler.getLooper()); mTethering = new Tethering(mContext, nmService, statsService, mHandler.getLooper()); mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 || mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 || mTethering.getTetherableWifiRegexs().length != 0 || mTethering.getTetherableWifiRegexs().length != 0 || mTethering.getTetherableBluetoothRegexs().length != 0) && mTethering.getTetherableBluetoothRegexs().length != 0) && Loading services/java/com/android/server/SystemServer.java +2 −1 Original line number Original line Diff line number Diff line Loading @@ -364,7 +364,8 @@ class ServerThread extends Thread { try { try { Slog.i(TAG, "Connectivity Service"); Slog.i(TAG, "Connectivity Service"); connectivity = new ConnectivityService(context, networkManagement, networkPolicy); connectivity = new ConnectivityService( context, networkManagement, networkStats, networkPolicy); ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); networkStats.bindConnectivityManager(connectivity); networkStats.bindConnectivityManager(connectivity); networkPolicy.bindConnectivityManager(connectivity); networkPolicy.bindConnectivityManager(connectivity); Loading services/java/com/android/server/connectivity/Tethering.java +15 −2 Original line number Original line Diff line number Diff line Loading @@ -29,6 +29,7 @@ import android.hardware.usb.UsbManager; import android.net.ConnectivityManager; import android.net.ConnectivityManager; import android.net.IConnectivityManager; import android.net.IConnectivityManager; import android.net.INetworkManagementEventObserver; import android.net.INetworkManagementEventObserver; import android.net.INetworkStatsService; import android.net.InterfaceConfiguration; import android.net.InterfaceConfiguration; import android.net.LinkAddress; import android.net.LinkAddress; import android.net.LinkProperties; import android.net.LinkProperties; Loading Loading @@ -88,7 +89,8 @@ public class Tethering extends INetworkManagementEventObserver.Stub { // upstream type list and the DUN_REQUIRED secure-setting // upstream type list and the DUN_REQUIRED secure-setting private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE; private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE; private INetworkManagementService mNMService; private final INetworkManagementService mNMService; private final INetworkStatsService mStatsService; private Looper mLooper; private Looper mLooper; private HandlerThread mThread; private HandlerThread mThread; Loading Loading @@ -124,9 +126,11 @@ public class Tethering extends INetworkManagementEventObserver.Stub { private boolean mUsbTetherRequested; // true if USB tethering should be started private boolean mUsbTetherRequested; // true if USB tethering should be started // when RNDIS is enabled // when RNDIS is enabled public Tethering(Context context, INetworkManagementService nmService, Looper looper) { public Tethering(Context context, INetworkManagementService nmService, INetworkStatsService statsService, Looper looper) { mContext = context; mContext = context; mNMService = nmService; mNMService = nmService; mStatsService = statsService; mLooper = looper; mLooper = looper; mIfaces = new HashMap<String, TetherInterfaceSM>(); mIfaces = new HashMap<String, TetherInterfaceSM>(); Loading Loading @@ -913,6 +917,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub { case CMD_INTERFACE_DOWN: case CMD_INTERFACE_DOWN: if (mMyUpstreamIfaceName != null) { if (mMyUpstreamIfaceName != null) { try { try { // about to tear down NAT; gather remaining statistics mStatsService.forceUpdate(); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mMyUpstreamIfaceName = null; mMyUpstreamIfaceName = null; } catch (Exception e) { } catch (Exception e) { Loading Loading @@ -957,6 +964,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub { } } if (mMyUpstreamIfaceName != null) { if (mMyUpstreamIfaceName != null) { try { try { // about to tear down NAT; gather remaining statistics mStatsService.forceUpdate(); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mMyUpstreamIfaceName = null; mMyUpstreamIfaceName = null; } catch (Exception e) { } catch (Exception e) { Loading Loading @@ -995,6 +1005,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub { case CMD_TETHER_MODE_DEAD: case CMD_TETHER_MODE_DEAD: if (mMyUpstreamIfaceName != null) { if (mMyUpstreamIfaceName != null) { try { try { // about to tear down NAT; gather remaining statistics mStatsService.forceUpdate(); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mMyUpstreamIfaceName = null; mMyUpstreamIfaceName = null; } catch (Exception e) { } catch (Exception e) { Loading services/java/com/android/server/net/NetworkStatsService.java +75 −24 Original line number Original line Diff line number Diff line Loading @@ -24,8 +24,8 @@ import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; import static android.content.Intent.ACTION_SHUTDOWN; import static android.content.Intent.ACTION_SHUTDOWN; import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_DEFAULT; Loading @@ -43,9 +43,12 @@ import static android.provider.Settings.Secure.NETSTATS_POLL_INTERVAL; import static android.provider.Settings.Secure.NETSTATS_TAG_MAX_HISTORY; import static android.provider.Settings.Secure.NETSTATS_TAG_MAX_HISTORY; import static android.provider.Settings.Secure.NETSTATS_UID_BUCKET_DURATION; import static android.provider.Settings.Secure.NETSTATS_UID_BUCKET_DURATION; import static android.provider.Settings.Secure.NETSTATS_UID_MAX_HISTORY; import static android.provider.Settings.Secure.NETSTATS_UID_MAX_HISTORY; import static android.telephony.PhoneStateListener.LISTEN_DATA_CONNECTION_STATE; import static android.telephony.PhoneStateListener.LISTEN_NONE; import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.HOUR_IN_MILLIS; import static android.text.format.DateUtils.HOUR_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.SECOND_IN_MILLIS; import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; Loading Loading @@ -80,6 +83,7 @@ import android.os.PowerManager; import android.os.RemoteException; import android.os.RemoteException; import android.os.SystemClock; import android.os.SystemClock; import android.provider.Settings; import android.provider.Settings; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.telephony.TelephonyManager; import android.util.EventLog; import android.util.EventLog; import android.util.Log; import android.util.Log; Loading Loading @@ -121,7 +125,7 @@ import libcore.io.IoUtils; */ */ public class NetworkStatsService extends INetworkStatsService.Stub { public class NetworkStatsService extends INetworkStatsService.Stub { private static final String TAG = "NetworkStats"; private static final String TAG = "NetworkStats"; private static final boolean LOGD = true; private static final boolean LOGD = false; private static final boolean LOGV = false; private static final boolean LOGV = false; /** File header magic number: "ANET" */ /** File header magic number: "ANET" */ Loading @@ -132,7 +136,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private static final int VERSION_UID_WITH_TAG = 3; private static final int VERSION_UID_WITH_TAG = 3; private static final int VERSION_UID_WITH_SET = 4; private static final int VERSION_UID_WITH_SET = 4; private static final int MSG_PERFORM_POLL = 0x1; private static final int MSG_PERFORM_POLL = 1; private static final int MSG_UPDATE_IFACES = 2; /** Flags to control detail level of poll event. */ /** Flags to control detail level of poll event. */ private static final int FLAG_PERSIST_NETWORK = 0x10; private static final int FLAG_PERSIST_NETWORK = 0x10; Loading @@ -144,6 +149,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private final INetworkManagementService mNetworkManager; private final INetworkManagementService mNetworkManager; private final IAlarmManager mAlarmManager; private final IAlarmManager mAlarmManager; private final TrustedTime mTime; private final TrustedTime mTime; private final TelephonyManager mTeleManager; private final NetworkStatsSettings mSettings; private final NetworkStatsSettings mSettings; private final PowerManager.WakeLock mWakeLock; private final PowerManager.WakeLock mWakeLock; Loading Loading @@ -227,6 +233,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { mNetworkManager = checkNotNull(networkManager, "missing INetworkManagementService"); mNetworkManager = checkNotNull(networkManager, "missing INetworkManagementService"); mAlarmManager = checkNotNull(alarmManager, "missing IAlarmManager"); mAlarmManager = checkNotNull(alarmManager, "missing IAlarmManager"); mTime = checkNotNull(time, "missing TrustedTime"); mTime = checkNotNull(time, "missing TrustedTime"); mTeleManager = checkNotNull(TelephonyManager.getDefault(), "missing TelephonyManager"); mSettings = checkNotNull(settings, "missing NetworkStatsSettings"); mSettings = checkNotNull(settings, "missing NetworkStatsSettings"); final PowerManager powerManager = (PowerManager) context.getSystemService( final PowerManager powerManager = (PowerManager) context.getSystemService( Loading Loading @@ -279,6 +286,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // ignored; service lives in system_server // ignored; service lives in system_server } } // watch for networkType changes that aren't broadcast through // CONNECTIVITY_ACTION_IMMEDIATE above. mTeleManager.listen(mPhoneListener, LISTEN_DATA_CONNECTION_STATE); registerPollAlarmLocked(); registerPollAlarmLocked(); registerGlobalAlert(); registerGlobalAlert(); Loading @@ -288,10 +299,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private void shutdownLocked() { private void shutdownLocked() { mContext.unregisterReceiver(mConnReceiver); mContext.unregisterReceiver(mConnReceiver); mContext.unregisterReceiver(mTetherReceiver); mContext.unregisterReceiver(mPollReceiver); mContext.unregisterReceiver(mPollReceiver); mContext.unregisterReceiver(mRemovedReceiver); mContext.unregisterReceiver(mRemovedReceiver); mContext.unregisterReceiver(mShutdownReceiver); mContext.unregisterReceiver(mShutdownReceiver); mTeleManager.listen(mPhoneListener, LISTEN_NONE); writeNetworkStatsLocked(); writeNetworkStatsLocked(); if (mUidStatsLoaded) { if (mUidStatsLoaded) { writeUidStatsLocked(); writeUidStatsLocked(); Loading Loading @@ -535,14 +549,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) { // on background handler thread, and verified CONNECTIVITY_INTERNAL // on background handler thread, and verified CONNECTIVITY_INTERNAL // permission above. // permission above. synchronized (mStatsLock) { updateIfaces(); mWakeLock.acquire(); try { updateIfacesLocked(); } finally { mWakeLock.release(); } } } } }; }; Loading Loading @@ -619,6 +626,46 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } } }; }; private int mLastPhoneState = TelephonyManager.DATA_UNKNOWN; private int mLastPhoneNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; /** * Receiver that watches for {@link TelephonyManager} changes, such as * transitioning between network types. */ private PhoneStateListener mPhoneListener = new PhoneStateListener() { @Override public void onDataConnectionStateChanged(int state, int networkType) { final boolean stateChanged = state != mLastPhoneState; final boolean networkTypeChanged = networkType != mLastPhoneNetworkType; if (networkTypeChanged && !stateChanged) { // networkType changed without a state change, which means we // need to roll our own update. delay long enough for // ConnectivityManager to process. // TODO: add direct event to ConnectivityService instead of // relying on this delay. if (LOGV) Slog.v(TAG, "triggering delayed updateIfaces()"); mHandler.sendMessageDelayed( mHandler.obtainMessage(MSG_UPDATE_IFACES), SECOND_IN_MILLIS); } mLastPhoneState = state; mLastPhoneNetworkType = networkType; } }; private void updateIfaces() { synchronized (mStatsLock) { mWakeLock.acquire(); try { updateIfacesLocked(); } finally { mWakeLock.release(); } } } /** /** * Inspect all current {@link NetworkState} to derive mapping from {@code * Inspect all current {@link NetworkState} to derive mapping from {@code * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo} * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo} Loading Loading @@ -713,19 +760,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final long threshold = mSettings.getPersistThreshold(); final long threshold = mSettings.getPersistThreshold(); try { try { // record network stats final NetworkStats networkSnapshot = mNetworkManager.getNetworkStatsSummary(); performNetworkPollLocked(networkSnapshot, currentTime); // persist when enough network data has occurred final NetworkStats persistNetworkDelta = computeStatsDelta( mLastPersistNetworkSnapshot, networkSnapshot, true); final boolean networkPastThreshold = persistNetworkDelta.getTotalBytes() > threshold; if (persistForce || (persistNetwork && networkPastThreshold)) { writeNetworkStatsLocked(); mLastPersistNetworkSnapshot = networkSnapshot; } // record tethering stats; persisted during normal UID cycle below // record tethering stats; persisted during normal UID cycle below final String[] ifacePairs = mConnManager.getTetheredIfacePairs(); final String[] ifacePairs = mConnManager.getTetheredIfacePairs(); final NetworkStats tetherSnapshot = mNetworkManager.getNetworkStatsTethering( final NetworkStats tetherSnapshot = mNetworkManager.getNetworkStatsTethering( Loading @@ -744,6 +778,19 @@ public class NetworkStatsService extends INetworkStatsService.Stub { writeUidStatsLocked(); writeUidStatsLocked(); mLastPersistUidSnapshot = uidSnapshot; mLastPersistUidSnapshot = uidSnapshot; } } // record network stats final NetworkStats networkSnapshot = mNetworkManager.getNetworkStatsSummary(); performNetworkPollLocked(networkSnapshot, currentTime); // persist when enough network data has occurred final NetworkStats persistNetworkDelta = computeStatsDelta( mLastPersistNetworkSnapshot, networkSnapshot, true); final boolean networkPastThreshold = persistNetworkDelta.getTotalBytes() > threshold; if (persistForce || (persistNetwork && networkPastThreshold)) { writeNetworkStatsLocked(); mLastPersistNetworkSnapshot = networkSnapshot; } } catch (IllegalStateException e) { } catch (IllegalStateException e) { Log.wtf(TAG, "problem reading network stats", e); Log.wtf(TAG, "problem reading network stats", e); } catch (RemoteException e) { } catch (RemoteException e) { Loading Loading @@ -1356,6 +1403,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub { performPoll(flags); performPoll(flags); return true; return true; } } case MSG_UPDATE_IFACES: { updateIfaces(); return true; } default: { default: { return false; return false; } } Loading Loading
core/java/android/net/NetworkStatsHistory.java +5 −0 Original line number Original line Diff line number Diff line Loading @@ -270,6 +270,11 @@ public class NetworkStatsHistory implements Parcelable { || entry.operations < 0) { || entry.operations < 0) { throw new IllegalArgumentException("tried recording negative data"); throw new IllegalArgumentException("tried recording negative data"); } } if (entry.rxBytes == 0 && entry.rxPackets == 0 && entry.txBytes == 0 && entry.txPackets == 0 && entry.operations == 0) { // nothing to record; skip return; } // create any buckets needed by this range // create any buckets needed by this range ensureBuckets(start, end); ensureBuckets(start, end); Loading
services/java/com/android/server/ConnectivityService.java +4 −3 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,7 @@ import android.net.EthernetDataTracker; import android.net.IConnectivityManager; import android.net.IConnectivityManager; import android.net.INetworkPolicyListener; import android.net.INetworkPolicyListener; import android.net.INetworkPolicyManager; import android.net.INetworkPolicyManager; import android.net.INetworkStatsService; import android.net.LinkAddress; import android.net.LinkAddress; import android.net.LinkProperties; import android.net.LinkProperties; import android.net.LinkProperties.CompareResult; import android.net.LinkProperties.CompareResult; Loading Loading @@ -306,8 +307,8 @@ public class ConnectivityService extends IConnectivityManager.Stub { // the set of network types that can only be enabled by system/sig apps // the set of network types that can only be enabled by system/sig apps List mProtectedNetworks; List mProtectedNetworks; public ConnectivityService( public ConnectivityService(Context context, INetworkManagementService netd, Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) { INetworkStatsService statsService, INetworkPolicyManager policyManager) { if (DBG) log("ConnectivityService starting up"); if (DBG) log("ConnectivityService starting up"); HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); Loading Loading @@ -496,7 +497,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b); INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b); mTethering = new Tethering(mContext, nmService, mHandler.getLooper()); mTethering = new Tethering(mContext, nmService, statsService, mHandler.getLooper()); mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 || mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 || mTethering.getTetherableWifiRegexs().length != 0 || mTethering.getTetherableWifiRegexs().length != 0 || mTethering.getTetherableBluetoothRegexs().length != 0) && mTethering.getTetherableBluetoothRegexs().length != 0) && Loading
services/java/com/android/server/SystemServer.java +2 −1 Original line number Original line Diff line number Diff line Loading @@ -364,7 +364,8 @@ class ServerThread extends Thread { try { try { Slog.i(TAG, "Connectivity Service"); Slog.i(TAG, "Connectivity Service"); connectivity = new ConnectivityService(context, networkManagement, networkPolicy); connectivity = new ConnectivityService( context, networkManagement, networkStats, networkPolicy); ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); networkStats.bindConnectivityManager(connectivity); networkStats.bindConnectivityManager(connectivity); networkPolicy.bindConnectivityManager(connectivity); networkPolicy.bindConnectivityManager(connectivity); Loading
services/java/com/android/server/connectivity/Tethering.java +15 −2 Original line number Original line Diff line number Diff line Loading @@ -29,6 +29,7 @@ import android.hardware.usb.UsbManager; import android.net.ConnectivityManager; import android.net.ConnectivityManager; import android.net.IConnectivityManager; import android.net.IConnectivityManager; import android.net.INetworkManagementEventObserver; import android.net.INetworkManagementEventObserver; import android.net.INetworkStatsService; import android.net.InterfaceConfiguration; import android.net.InterfaceConfiguration; import android.net.LinkAddress; import android.net.LinkAddress; import android.net.LinkProperties; import android.net.LinkProperties; Loading Loading @@ -88,7 +89,8 @@ public class Tethering extends INetworkManagementEventObserver.Stub { // upstream type list and the DUN_REQUIRED secure-setting // upstream type list and the DUN_REQUIRED secure-setting private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE; private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE; private INetworkManagementService mNMService; private final INetworkManagementService mNMService; private final INetworkStatsService mStatsService; private Looper mLooper; private Looper mLooper; private HandlerThread mThread; private HandlerThread mThread; Loading Loading @@ -124,9 +126,11 @@ public class Tethering extends INetworkManagementEventObserver.Stub { private boolean mUsbTetherRequested; // true if USB tethering should be started private boolean mUsbTetherRequested; // true if USB tethering should be started // when RNDIS is enabled // when RNDIS is enabled public Tethering(Context context, INetworkManagementService nmService, Looper looper) { public Tethering(Context context, INetworkManagementService nmService, INetworkStatsService statsService, Looper looper) { mContext = context; mContext = context; mNMService = nmService; mNMService = nmService; mStatsService = statsService; mLooper = looper; mLooper = looper; mIfaces = new HashMap<String, TetherInterfaceSM>(); mIfaces = new HashMap<String, TetherInterfaceSM>(); Loading Loading @@ -913,6 +917,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub { case CMD_INTERFACE_DOWN: case CMD_INTERFACE_DOWN: if (mMyUpstreamIfaceName != null) { if (mMyUpstreamIfaceName != null) { try { try { // about to tear down NAT; gather remaining statistics mStatsService.forceUpdate(); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mMyUpstreamIfaceName = null; mMyUpstreamIfaceName = null; } catch (Exception e) { } catch (Exception e) { Loading Loading @@ -957,6 +964,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub { } } if (mMyUpstreamIfaceName != null) { if (mMyUpstreamIfaceName != null) { try { try { // about to tear down NAT; gather remaining statistics mStatsService.forceUpdate(); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mMyUpstreamIfaceName = null; mMyUpstreamIfaceName = null; } catch (Exception e) { } catch (Exception e) { Loading Loading @@ -995,6 +1005,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub { case CMD_TETHER_MODE_DEAD: case CMD_TETHER_MODE_DEAD: if (mMyUpstreamIfaceName != null) { if (mMyUpstreamIfaceName != null) { try { try { // about to tear down NAT; gather remaining statistics mStatsService.forceUpdate(); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); mMyUpstreamIfaceName = null; mMyUpstreamIfaceName = null; } catch (Exception e) { } catch (Exception e) { Loading
services/java/com/android/server/net/NetworkStatsService.java +75 −24 Original line number Original line Diff line number Diff line Loading @@ -24,8 +24,8 @@ import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; import static android.content.Intent.ACTION_SHUTDOWN; import static android.content.Intent.ACTION_SHUTDOWN; import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.ACTION_UID_REMOVED; import static android.content.Intent.EXTRA_UID; import static android.content.Intent.EXTRA_UID; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_ALL; import static android.net.NetworkStats.SET_DEFAULT; import static android.net.NetworkStats.SET_DEFAULT; Loading @@ -43,9 +43,12 @@ import static android.provider.Settings.Secure.NETSTATS_POLL_INTERVAL; import static android.provider.Settings.Secure.NETSTATS_TAG_MAX_HISTORY; import static android.provider.Settings.Secure.NETSTATS_TAG_MAX_HISTORY; import static android.provider.Settings.Secure.NETSTATS_UID_BUCKET_DURATION; import static android.provider.Settings.Secure.NETSTATS_UID_BUCKET_DURATION; import static android.provider.Settings.Secure.NETSTATS_UID_MAX_HISTORY; import static android.provider.Settings.Secure.NETSTATS_UID_MAX_HISTORY; import static android.telephony.PhoneStateListener.LISTEN_DATA_CONNECTION_STATE; import static android.telephony.PhoneStateListener.LISTEN_NONE; import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.HOUR_IN_MILLIS; import static android.text.format.DateUtils.HOUR_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.SECOND_IN_MILLIS; import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; Loading Loading @@ -80,6 +83,7 @@ import android.os.PowerManager; import android.os.RemoteException; import android.os.RemoteException; import android.os.SystemClock; import android.os.SystemClock; import android.provider.Settings; import android.provider.Settings; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.telephony.TelephonyManager; import android.util.EventLog; import android.util.EventLog; import android.util.Log; import android.util.Log; Loading Loading @@ -121,7 +125,7 @@ import libcore.io.IoUtils; */ */ public class NetworkStatsService extends INetworkStatsService.Stub { public class NetworkStatsService extends INetworkStatsService.Stub { private static final String TAG = "NetworkStats"; private static final String TAG = "NetworkStats"; private static final boolean LOGD = true; private static final boolean LOGD = false; private static final boolean LOGV = false; private static final boolean LOGV = false; /** File header magic number: "ANET" */ /** File header magic number: "ANET" */ Loading @@ -132,7 +136,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private static final int VERSION_UID_WITH_TAG = 3; private static final int VERSION_UID_WITH_TAG = 3; private static final int VERSION_UID_WITH_SET = 4; private static final int VERSION_UID_WITH_SET = 4; private static final int MSG_PERFORM_POLL = 0x1; private static final int MSG_PERFORM_POLL = 1; private static final int MSG_UPDATE_IFACES = 2; /** Flags to control detail level of poll event. */ /** Flags to control detail level of poll event. */ private static final int FLAG_PERSIST_NETWORK = 0x10; private static final int FLAG_PERSIST_NETWORK = 0x10; Loading @@ -144,6 +149,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private final INetworkManagementService mNetworkManager; private final INetworkManagementService mNetworkManager; private final IAlarmManager mAlarmManager; private final IAlarmManager mAlarmManager; private final TrustedTime mTime; private final TrustedTime mTime; private final TelephonyManager mTeleManager; private final NetworkStatsSettings mSettings; private final NetworkStatsSettings mSettings; private final PowerManager.WakeLock mWakeLock; private final PowerManager.WakeLock mWakeLock; Loading Loading @@ -227,6 +233,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { mNetworkManager = checkNotNull(networkManager, "missing INetworkManagementService"); mNetworkManager = checkNotNull(networkManager, "missing INetworkManagementService"); mAlarmManager = checkNotNull(alarmManager, "missing IAlarmManager"); mAlarmManager = checkNotNull(alarmManager, "missing IAlarmManager"); mTime = checkNotNull(time, "missing TrustedTime"); mTime = checkNotNull(time, "missing TrustedTime"); mTeleManager = checkNotNull(TelephonyManager.getDefault(), "missing TelephonyManager"); mSettings = checkNotNull(settings, "missing NetworkStatsSettings"); mSettings = checkNotNull(settings, "missing NetworkStatsSettings"); final PowerManager powerManager = (PowerManager) context.getSystemService( final PowerManager powerManager = (PowerManager) context.getSystemService( Loading Loading @@ -279,6 +286,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // ignored; service lives in system_server // ignored; service lives in system_server } } // watch for networkType changes that aren't broadcast through // CONNECTIVITY_ACTION_IMMEDIATE above. mTeleManager.listen(mPhoneListener, LISTEN_DATA_CONNECTION_STATE); registerPollAlarmLocked(); registerPollAlarmLocked(); registerGlobalAlert(); registerGlobalAlert(); Loading @@ -288,10 +299,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private void shutdownLocked() { private void shutdownLocked() { mContext.unregisterReceiver(mConnReceiver); mContext.unregisterReceiver(mConnReceiver); mContext.unregisterReceiver(mTetherReceiver); mContext.unregisterReceiver(mPollReceiver); mContext.unregisterReceiver(mPollReceiver); mContext.unregisterReceiver(mRemovedReceiver); mContext.unregisterReceiver(mRemovedReceiver); mContext.unregisterReceiver(mShutdownReceiver); mContext.unregisterReceiver(mShutdownReceiver); mTeleManager.listen(mPhoneListener, LISTEN_NONE); writeNetworkStatsLocked(); writeNetworkStatsLocked(); if (mUidStatsLoaded) { if (mUidStatsLoaded) { writeUidStatsLocked(); writeUidStatsLocked(); Loading Loading @@ -535,14 +549,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) { // on background handler thread, and verified CONNECTIVITY_INTERNAL // on background handler thread, and verified CONNECTIVITY_INTERNAL // permission above. // permission above. synchronized (mStatsLock) { updateIfaces(); mWakeLock.acquire(); try { updateIfacesLocked(); } finally { mWakeLock.release(); } } } } }; }; Loading Loading @@ -619,6 +626,46 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } } }; }; private int mLastPhoneState = TelephonyManager.DATA_UNKNOWN; private int mLastPhoneNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; /** * Receiver that watches for {@link TelephonyManager} changes, such as * transitioning between network types. */ private PhoneStateListener mPhoneListener = new PhoneStateListener() { @Override public void onDataConnectionStateChanged(int state, int networkType) { final boolean stateChanged = state != mLastPhoneState; final boolean networkTypeChanged = networkType != mLastPhoneNetworkType; if (networkTypeChanged && !stateChanged) { // networkType changed without a state change, which means we // need to roll our own update. delay long enough for // ConnectivityManager to process. // TODO: add direct event to ConnectivityService instead of // relying on this delay. if (LOGV) Slog.v(TAG, "triggering delayed updateIfaces()"); mHandler.sendMessageDelayed( mHandler.obtainMessage(MSG_UPDATE_IFACES), SECOND_IN_MILLIS); } mLastPhoneState = state; mLastPhoneNetworkType = networkType; } }; private void updateIfaces() { synchronized (mStatsLock) { mWakeLock.acquire(); try { updateIfacesLocked(); } finally { mWakeLock.release(); } } } /** /** * Inspect all current {@link NetworkState} to derive mapping from {@code * Inspect all current {@link NetworkState} to derive mapping from {@code * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo} * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo} Loading Loading @@ -713,19 +760,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final long threshold = mSettings.getPersistThreshold(); final long threshold = mSettings.getPersistThreshold(); try { try { // record network stats final NetworkStats networkSnapshot = mNetworkManager.getNetworkStatsSummary(); performNetworkPollLocked(networkSnapshot, currentTime); // persist when enough network data has occurred final NetworkStats persistNetworkDelta = computeStatsDelta( mLastPersistNetworkSnapshot, networkSnapshot, true); final boolean networkPastThreshold = persistNetworkDelta.getTotalBytes() > threshold; if (persistForce || (persistNetwork && networkPastThreshold)) { writeNetworkStatsLocked(); mLastPersistNetworkSnapshot = networkSnapshot; } // record tethering stats; persisted during normal UID cycle below // record tethering stats; persisted during normal UID cycle below final String[] ifacePairs = mConnManager.getTetheredIfacePairs(); final String[] ifacePairs = mConnManager.getTetheredIfacePairs(); final NetworkStats tetherSnapshot = mNetworkManager.getNetworkStatsTethering( final NetworkStats tetherSnapshot = mNetworkManager.getNetworkStatsTethering( Loading @@ -744,6 +778,19 @@ public class NetworkStatsService extends INetworkStatsService.Stub { writeUidStatsLocked(); writeUidStatsLocked(); mLastPersistUidSnapshot = uidSnapshot; mLastPersistUidSnapshot = uidSnapshot; } } // record network stats final NetworkStats networkSnapshot = mNetworkManager.getNetworkStatsSummary(); performNetworkPollLocked(networkSnapshot, currentTime); // persist when enough network data has occurred final NetworkStats persistNetworkDelta = computeStatsDelta( mLastPersistNetworkSnapshot, networkSnapshot, true); final boolean networkPastThreshold = persistNetworkDelta.getTotalBytes() > threshold; if (persistForce || (persistNetwork && networkPastThreshold)) { writeNetworkStatsLocked(); mLastPersistNetworkSnapshot = networkSnapshot; } } catch (IllegalStateException e) { } catch (IllegalStateException e) { Log.wtf(TAG, "problem reading network stats", e); Log.wtf(TAG, "problem reading network stats", e); } catch (RemoteException e) { } catch (RemoteException e) { Loading Loading @@ -1356,6 +1403,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub { performPoll(flags); performPoll(flags); return true; return true; } } case MSG_UPDATE_IFACES: { updateIfaces(); return true; } default: { default: { return false; return false; } } Loading