Skip to content

Commit 3359aca

Browse files
committed
Bootstrap stats before registering listeners.
Also move ISE to handled NonMonotonicException. Bug: 5570872 Change-Id: I793f349287ef5589185f117dfbec6f1fad73a661
1 parent 6dee1a9 commit 3359aca

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

core/java/android/net/NetworkStats.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ private Entry getTotal(Entry recycle, HashSet<String> limitIface, int limitUid)
455455
public NetworkStats subtract(NetworkStats value) throws NonMonotonicException {
456456
final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime;
457457
if (deltaRealtime < 0) {
458-
throw new IllegalArgumentException("found non-monotonic realtime");
458+
throw new NonMonotonicException(this, value);
459459
}
460460

461461
// result will have our rows, and elapsed time between snapshots
@@ -575,7 +575,8 @@ public void dump(String prefix, PrintWriter pw) {
575575
pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
576576
for (int i = 0; i < size; i++) {
577577
pw.print(prefix);
578-
pw.print(" iface="); pw.print(iface[i]);
578+
pw.print(" ["); pw.print(i); pw.print("]");
579+
pw.print(" iface="); pw.print(iface[i]);
579580
pw.print(" uid="); pw.print(uid[i]);
580581
pw.print(" set="); pw.print(setToString(set[i]));
581582
pw.print(" tag="); pw.print(tagToString(tag[i]));
@@ -638,6 +639,10 @@ public static class NonMonotonicException extends Exception {
638639
public final int leftIndex;
639640
public final int rightIndex;
640641

642+
public NonMonotonicException(NetworkStats left, NetworkStats right) {
643+
this(left, -1, right, -1);
644+
}
645+
641646
public NonMonotonicException(
642647
NetworkStats left, int leftIndex, NetworkStats right, int rightIndex) {
643648
this.left = checkNotNull(left, "missing left");

services/java/com/android/server/net/NetworkStatsService.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
152152

153153
private static final String TAG_NETSTATS_ERROR = "netstats_error";
154154

155-
private static final String DEV = "dev";
156-
private static final String XT = "xt";
157-
private static final String UID = "uid";
158-
159155
private final Context mContext;
160156
private final INetworkManagementService mNetworkManager;
161157
private final IAlarmManager mAlarmManager;
@@ -278,6 +274,9 @@ public void systemReady() {
278274
readNetworkXtStatsLocked();
279275
}
280276

277+
// bootstrap initial stats to prevent double-counting later
278+
bootstrapStats();
279+
281280
// watch for network interfaces to be claimed
282281
final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE);
283282
mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
@@ -311,9 +310,6 @@ public void systemReady() {
311310
registerPollAlarmLocked();
312311
registerGlobalAlert();
313312

314-
// bootstrap initial stats to prevent double-counting later
315-
bootstrapStats();
316-
317313
mDropBox = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
318314
}
319315

@@ -837,9 +833,9 @@ private void performPollLocked(int flags) {
837833

838834
// persist when enough network data has occurred
839835
final long persistNetworkDevDelta = computeStatsDelta(
840-
mLastPersistNetworkDevSnapshot, networkDevSnapshot, true, DEV).getTotalBytes();
836+
mLastPersistNetworkDevSnapshot, networkDevSnapshot, true, "devp").getTotalBytes();
841837
final long persistNetworkXtDelta = computeStatsDelta(
842-
mLastPersistNetworkXtSnapshot, networkXtSnapshot, true, XT).getTotalBytes();
838+
mLastPersistNetworkXtSnapshot, networkXtSnapshot, true, "xtp").getTotalBytes();
843839
final boolean networkOverThreshold = persistNetworkDevDelta > threshold
844840
|| persistNetworkXtDelta > threshold;
845841
if (persistForce || (persistNetwork && networkOverThreshold)) {
@@ -851,7 +847,7 @@ private void performPollLocked(int flags) {
851847

852848
// persist when enough uid data has occurred
853849
final long persistUidDelta = computeStatsDelta(
854-
mLastPersistUidSnapshot, uidSnapshot, true, UID).getTotalBytes();
850+
mLastPersistUidSnapshot, uidSnapshot, true, "uidp").getTotalBytes();
855851
if (persistForce || (persistUid && persistUidDelta > threshold)) {
856852
writeUidStatsLocked();
857853
mLastPersistUidSnapshot = uidSnapshot;
@@ -880,7 +876,7 @@ private void performNetworkDevPollLocked(NetworkStats networkDevSnapshot, long c
880876
final HashSet<String> unknownIface = Sets.newHashSet();
881877

882878
final NetworkStats delta = computeStatsDelta(
883-
mLastPollNetworkDevSnapshot, networkDevSnapshot, false, DEV);
879+
mLastPollNetworkDevSnapshot, networkDevSnapshot, false, "dev");
884880
final long timeStart = currentTime - delta.getElapsedRealtime();
885881

886882
NetworkStats.Entry entry = null;
@@ -910,7 +906,7 @@ private void performNetworkXtPollLocked(NetworkStats networkXtSnapshot, long cur
910906
final HashSet<String> unknownIface = Sets.newHashSet();
911907

912908
final NetworkStats delta = computeStatsDelta(
913-
mLastPollNetworkXtSnapshot, networkXtSnapshot, false, XT);
909+
mLastPollNetworkXtSnapshot, networkXtSnapshot, false, "xt");
914910
final long timeStart = currentTime - delta.getElapsedRealtime();
915911

916912
NetworkStats.Entry entry = null;
@@ -940,9 +936,9 @@ private void performUidPollLocked(NetworkStats uidSnapshot, long currentTime) {
940936
ensureUidStatsLoadedLocked();
941937

942938
final NetworkStats delta = computeStatsDelta(
943-
mLastPollUidSnapshot, uidSnapshot, false, UID);
939+
mLastPollUidSnapshot, uidSnapshot, false, "uid");
944940
final NetworkStats operationsDelta = computeStatsDelta(
945-
mLastPollOperationsSnapshot, mOperations, false, UID);
941+
mLastPollOperationsSnapshot, mOperations, false, "uidop");
946942
final long timeStart = currentTime - delta.getElapsedRealtime();
947943

948944
NetworkStats.Entry entry = null;
@@ -1516,7 +1512,7 @@ private NetworkStats computeStatsDelta(
15161512

15171513
// record error for debugging
15181514
final StringBuilder builder = new StringBuilder();
1519-
builder.append("found non-monotonic " + type + "values at left[" + e.leftIndex
1515+
builder.append("found non-monotonic " + type + " values at left[" + e.leftIndex
15201516
+ "] - right[" + e.rightIndex + "]\n");
15211517
builder.append("left=").append(e.left).append('\n');
15221518
builder.append("right=").append(e.right).append('\n');

0 commit comments

Comments
 (0)