Register for kernel global data usage alerts.

Instead of polling every 15 minutes, register for alerts that trigger
when system-wide traffic passes a threshold.  Still mixed with polling
to persist UID stats, but relaxed to 30 minutes.  Currently watches
for every 512kB.

Make persistence decision separately for network versus UID, and use
total delta bytes when making decision.  Use light bootstrap during
systemReady() instead of heavy poll, which had been force-loading all
UID data unnecessarily.

Bug: 5023631
Change-Id: I04b723d6c4bf872fb1028071122dba66a8e1b576
This commit is contained in:
Jeff Sharkey
2011-08-23 18:37:23 -07:00
parent 2665eeb9d4
commit 93db945091
2 changed files with 150 additions and 58 deletions

View File

@@ -312,6 +312,22 @@ public class NetworkStats implements Parcelable {
return result; return result;
} }
/**
* Return total bytes represented by this snapshot object, usually used when
* checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
*/
public long getTotalBytes() {
long totalBytes = 0;
for (int i = 0; i < size; i++) {
// skip specific tags, since already counted in TAG_NONE
if (tag[i] != TAG_NONE) continue;
totalBytes += rxBytes[i];
totalBytes += txBytes[i];
}
return totalBytes;
}
/** /**
* Subtract the given {@link NetworkStats}, effectively leaving the delta * Subtract the given {@link NetworkStats}, effectively leaving the delta
* between two snapshots in time. Assumes that statistics rows collect over * between two snapshots in time. Assumes that statistics rows collect over

View File

@@ -43,6 +43,7 @@ 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 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.NetworkManagementSocketTagger.resetKernelUidStats; import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats;
import static com.android.server.NetworkManagementSocketTagger.setKernelCounterSet; import static com.android.server.NetworkManagementSocketTagger.setKernelCounterSet;
@@ -56,6 +57,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.net.IConnectivityManager; import android.net.IConnectivityManager;
import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsService; import android.net.INetworkStatsService;
import android.net.NetworkIdentity; import android.net.NetworkIdentity;
import android.net.NetworkInfo; import android.net.NetworkInfo;
@@ -121,7 +123,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_FORCE_UPDATE = 0x1; private static final int MSG_PERFORM_POLL = 0x1;
private static final int MSG_PERFORM_POLL_DETAILED = 0x2;
private final Context mContext; private final Context mContext;
private final INetworkManagementService mNetworkManager; private final INetworkManagementService mNetworkManager;
@@ -141,7 +144,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private PendingIntent mPollIntent; private PendingIntent mPollIntent;
// TODO: listen for kernel push events through netd instead of polling
// TODO: trim empty history objects entirely // TODO: trim empty history objects entirely
private static final long KB_IN_BYTES = 1024; private static final long KB_IN_BYTES = 1024;
@@ -174,17 +176,18 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
/** Flag if {@link #mUidStats} have been loaded from disk. */ /** Flag if {@link #mUidStats} have been loaded from disk. */
private boolean mUidStatsLoaded = false; private boolean mUidStatsLoaded = false;
private NetworkStats mLastNetworkSnapshot; private NetworkStats mLastPollNetworkSnapshot;
private NetworkStats mLastPersistNetworkSnapshot; private NetworkStats mLastPollUidSnapshot;
private NetworkStats mLastPollOperationsSnapshot;
private NetworkStats mLastUidSnapshot; private NetworkStats mLastPersistNetworkSnapshot;
private NetworkStats mLastPersistUidSnapshot;
/** Current counter sets for each UID. */ /** Current counter sets for each UID. */
private SparseIntArray mActiveUidCounterSet = new SparseIntArray(); private SparseIntArray mActiveUidCounterSet = new SparseIntArray();
/** Data layer operation counters for splicing into other structures. */ /** Data layer operation counters for splicing into other structures. */
private NetworkStats mOperations = new NetworkStats(0L, 10); private NetworkStats mOperations = new NetworkStats(0L, 10);
private NetworkStats mLastOperationsSnapshot;
private final HandlerThread mHandlerThread; private final HandlerThread mHandlerThread;
private final Handler mHandler; private final Handler mHandler;
@@ -252,13 +255,18 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mContext.registerReceiver(mShutdownReceiver, shutdownFilter); mContext.registerReceiver(mShutdownReceiver, shutdownFilter);
try { try {
registerPollAlarmLocked(); mNetworkManager.registerObserver(mAlertObserver);
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.w(TAG, "unable to register poll alarm"); // ouch, no push updates means we fall back to
// ACTION_NETWORK_STATS_POLL intervals.
Slog.e(TAG, "unable to register INetworkManagementEventObserver", e);
} }
// kick off background poll to bootstrap deltas registerPollAlarmLocked();
mHandler.obtainMessage(MSG_FORCE_UPDATE).sendToTarget(); registerGlobalAlert();
// bootstrap initial stats to prevent double-counting later
bootstrapStats();
} }
private void shutdownLocked() { private void shutdownLocked() {
@@ -280,17 +288,37 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
* Clear any existing {@link #ACTION_NETWORK_STATS_POLL} alarms, and * Clear any existing {@link #ACTION_NETWORK_STATS_POLL} alarms, and
* reschedule based on current {@link NetworkStatsSettings#getPollInterval()}. * reschedule based on current {@link NetworkStatsSettings#getPollInterval()}.
*/ */
private void registerPollAlarmLocked() throws RemoteException { private void registerPollAlarmLocked() {
if (mPollIntent != null) { try {
mAlarmManager.remove(mPollIntent); if (mPollIntent != null) {
mAlarmManager.remove(mPollIntent);
}
mPollIntent = PendingIntent.getBroadcast(
mContext, 0, new Intent(ACTION_NETWORK_STATS_POLL), 0);
final long currentRealtime = SystemClock.elapsedRealtime();
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime,
mSettings.getPollInterval(), mPollIntent);
} catch (RemoteException e) {
Slog.w(TAG, "problem registering for poll alarm: " + e);
} }
}
mPollIntent = PendingIntent.getBroadcast( /**
mContext, 0, new Intent(ACTION_NETWORK_STATS_POLL), 0); * Register for a global alert that is delivered through
* {@link INetworkManagementEventObserver} once a threshold amount of data
final long currentRealtime = SystemClock.elapsedRealtime(); * has been transferred.
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime, */
mSettings.getPollInterval(), mPollIntent); private void registerGlobalAlert() {
try {
final long alertBytes = mSettings.getPersistThreshold();
mNetworkManager.setGlobalAlert(alertBytes);
} catch (IllegalStateException e) {
Slog.w(TAG, "problem registering for global alert: " + e);
} catch (RemoteException e) {
Slog.w(TAG, "problem registering for global alert: " + e);
}
} }
@Override @Override
@@ -475,10 +503,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@Override @Override
public void forceUpdate() { public void forceUpdate() {
mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG);
performPoll(true, false);
synchronized (mStatsLock) {
performPollLocked(true, false);
}
} }
/** /**
@@ -507,14 +532,10 @@ 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 UPDATE_DEVICE_STATS // on background handler thread, and verified UPDATE_DEVICE_STATS
// permission above. // permission above.
synchronized (mStatsLock) { performPoll(true, false);
mWakeLock.acquire();
try { // verify that we're watching global alert
performPollLocked(true, false); registerGlobalAlert();
} finally {
mWakeLock.release();
}
}
} }
}; };
@@ -546,6 +567,26 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
}; };
/**
* Observer that watches for {@link INetworkManagementService} alerts.
*/
private INetworkManagementEventObserver mAlertObserver = new NetworkAlertObserver() {
@Override
public void limitReached(String limitName, String iface) {
// only someone like NMS should be calling us
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
if (LIMIT_GLOBAL_ALERT.equals(limitName)) {
// kick off background poll to collect network stats; UID stats
// are handled during normal polling interval.
mHandler.obtainMessage(MSG_PERFORM_POLL).sendToTarget();
// re-arm global alert for next update
registerGlobalAlert();
}
}
};
/** /**
* 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}
@@ -587,6 +628,33 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
} }
/**
* Bootstrap initial stats snapshot, usually during {@link #systemReady()}
* so we have baseline values without double-counting.
*/
private void bootstrapStats() {
try {
mLastPollNetworkSnapshot = mNetworkManager.getNetworkStatsSummary();
mLastPollUidSnapshot = mNetworkManager.getNetworkStatsUidDetail(UID_ALL);
mLastPollOperationsSnapshot = new NetworkStats(0L, 0);
} catch (IllegalStateException e) {
Slog.w(TAG, "problem reading network stats: " + e);
} catch (RemoteException e) {
Slog.w(TAG, "problem reading network stats: " + e);
}
}
private void performPoll(boolean detailedPoll, boolean forcePersist) {
synchronized (mStatsLock) {
mWakeLock.acquire();
try {
performPollLocked(detailedPoll, forcePersist);
} finally {
mWakeLock.release();
}
}
}
/** /**
* Periodic poll operation, reading current statistics and recording into * Periodic poll operation, reading current statistics and recording into
* {@link NetworkStatsHistory}. * {@link NetworkStatsHistory}.
@@ -596,6 +664,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
*/ */
private void performPollLocked(boolean detailedPoll, boolean forcePersist) { private void performPollLocked(boolean detailedPoll, boolean forcePersist) {
if (LOGV) Slog.v(TAG, "performPollLocked()"); if (LOGV) Slog.v(TAG, "performPollLocked()");
final long startRealtime = SystemClock.elapsedRealtime();
// try refreshing time source when stale // try refreshing time source when stale
if (mTime.getCacheAge() > mSettings.getTimeCacheMaxAge()) { if (mTime.getCacheAge() > mSettings.getTimeCacheMaxAge()) {
@@ -605,6 +674,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// TODO: consider marking "untrusted" times in historical stats // TODO: consider marking "untrusted" times in historical stats
final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis()
: System.currentTimeMillis(); : System.currentTimeMillis();
final long persistThreshold = mSettings.getPersistThreshold();
final NetworkStats networkSnapshot; final NetworkStats networkSnapshot;
final NetworkStats uidSnapshot; final NetworkStats uidSnapshot;
@@ -620,30 +690,32 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
performNetworkPollLocked(networkSnapshot, currentTime); performNetworkPollLocked(networkSnapshot, currentTime);
if (detailedPoll) {
performUidPollLocked(uidSnapshot, currentTime); // persist when enough network data has occurred
final NetworkStats persistNetworkDelta = computeStatsDelta(
mLastPersistNetworkSnapshot, networkSnapshot, true);
if (forcePersist || persistNetworkDelta.getTotalBytes() > persistThreshold) {
writeNetworkStatsLocked();
mLastPersistNetworkSnapshot = networkSnapshot;
} }
// decide if enough has changed to trigger persist if (detailedPoll) {
final NetworkStats persistDelta = computeStatsDelta( performUidPollLocked(uidSnapshot, currentTime);
mLastPersistNetworkSnapshot, networkSnapshot, true);
final long persistThreshold = mSettings.getPersistThreshold();
NetworkStats.Entry entry = null; // persist when enough network data has occurred
for (String iface : persistDelta.getUniqueIfaces()) { final NetworkStats persistUidDelta = computeStatsDelta(
final int index = persistDelta.findIndex(iface, UID_ALL, SET_DEFAULT, TAG_NONE); mLastPersistUidSnapshot, uidSnapshot, true);
entry = persistDelta.getValues(index, entry); if (forcePersist || persistUidDelta.getTotalBytes() > persistThreshold) {
if (forcePersist || entry.rxBytes > persistThreshold writeUidStatsLocked();
|| entry.txBytes > persistThreshold) {
writeNetworkStatsLocked();
if (mUidStatsLoaded) {
writeUidStatsLocked();
}
mLastPersistNetworkSnapshot = networkSnapshot; mLastPersistNetworkSnapshot = networkSnapshot;
break;
} }
} }
if (LOGV) {
final long duration = SystemClock.elapsedRealtime() - startRealtime;
Slog.v(TAG, "performPollLocked() took " + duration + "ms");
}
// finally, dispatch updated event to any listeners // finally, dispatch updated event to any listeners
final Intent updatedIntent = new Intent(ACTION_NETWORK_STATS_UPDATED); final Intent updatedIntent = new Intent(ACTION_NETWORK_STATS_UPDATED);
updatedIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); updatedIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
@@ -656,7 +728,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private void performNetworkPollLocked(NetworkStats networkSnapshot, long currentTime) { private void performNetworkPollLocked(NetworkStats networkSnapshot, long currentTime) {
final HashSet<String> unknownIface = Sets.newHashSet(); final HashSet<String> unknownIface = Sets.newHashSet();
final NetworkStats delta = computeStatsDelta(mLastNetworkSnapshot, networkSnapshot, false); final NetworkStats delta = computeStatsDelta(mLastPollNetworkSnapshot, networkSnapshot, false);
final long timeStart = currentTime - delta.getElapsedRealtime(); final long timeStart = currentTime - delta.getElapsedRealtime();
NetworkStats.Entry entry = null; NetworkStats.Entry entry = null;
@@ -678,7 +750,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
history.removeBucketsBefore(currentTime - maxHistory); history.removeBucketsBefore(currentTime - maxHistory);
} }
mLastNetworkSnapshot = networkSnapshot; mLastPollNetworkSnapshot = networkSnapshot;
if (LOGD && unknownIface.size() > 0) { if (LOGD && unknownIface.size() > 0) {
Slog.w(TAG, "unknown interfaces " + unknownIface.toString() + ", ignoring those stats"); Slog.w(TAG, "unknown interfaces " + unknownIface.toString() + ", ignoring those stats");
@@ -691,9 +763,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private void performUidPollLocked(NetworkStats uidSnapshot, long currentTime) { private void performUidPollLocked(NetworkStats uidSnapshot, long currentTime) {
ensureUidStatsLoadedLocked(); ensureUidStatsLoadedLocked();
final NetworkStats delta = computeStatsDelta(mLastUidSnapshot, uidSnapshot, false); final NetworkStats delta = computeStatsDelta(mLastPollUidSnapshot, uidSnapshot, false);
final NetworkStats operationsDelta = computeStatsDelta( final NetworkStats operationsDelta = computeStatsDelta(
mLastOperationsSnapshot, mOperations, false); mLastPollOperationsSnapshot, mOperations, false);
final long timeStart = currentTime - delta.getElapsedRealtime(); final long timeStart = currentTime - delta.getElapsedRealtime();
NetworkStats.Entry entry = null; NetworkStats.Entry entry = null;
@@ -731,8 +803,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
} }
mLastUidSnapshot = uidSnapshot; mLastPollUidSnapshot = uidSnapshot;
mLastOperationsSnapshot = mOperations; mLastPollOperationsSnapshot = mOperations;
mOperations = new NetworkStats(0L, 10); mOperations = new NetworkStats(0L, 10);
} }
@@ -1162,8 +1234,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
/** {@inheritDoc} */ /** {@inheritDoc} */
public boolean handleMessage(Message msg) { public boolean handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case MSG_FORCE_UPDATE: { case MSG_PERFORM_POLL: {
forceUpdate(); performPoll(false, false);
return true;
}
case MSG_PERFORM_POLL_DETAILED: {
performPoll(true, false);
return true; return true;
} }
default: { default: {
@@ -1226,10 +1302,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
public long getPollInterval() { public long getPollInterval() {
return getSecureLong(NETSTATS_POLL_INTERVAL, 15 * MINUTE_IN_MILLIS); return getSecureLong(NETSTATS_POLL_INTERVAL, 30 * MINUTE_IN_MILLIS);
} }
public long getPersistThreshold() { public long getPersistThreshold() {
return getSecureLong(NETSTATS_PERSIST_THRESHOLD, 16 * KB_IN_BYTES); return getSecureLong(NETSTATS_PERSIST_THRESHOLD, 512 * KB_IN_BYTES);
} }
public long getNetworkBucketDuration() { public long getNetworkBucketDuration() {
return getSecureLong(NETSTATS_NETWORK_BUCKET_DURATION, HOUR_IN_MILLIS); return getSecureLong(NETSTATS_NETWORK_BUCKET_DURATION, HOUR_IN_MILLIS);