@@ -472,6 +472,18 @@ private NetworkStats getSummaryForNetwork(NetworkTemplate template, long start,
472472 }
473473 }
474474
475+ private long getHistoryStartLocked (
476+ NetworkTemplate template , HashMap <NetworkIdentitySet , NetworkStatsHistory > source ) {
477+ long start = Long .MAX_VALUE ;
478+ for (NetworkIdentitySet ident : source .keySet ()) {
479+ if (templateMatches (template , ident )) {
480+ final NetworkStatsHistory history = source .get (ident );
481+ start = Math .min (start , history .getStart ());
482+ }
483+ }
484+ return start ;
485+ }
486+
475487 @ Override
476488 public NetworkStats getSummaryForAllUid (
477489 NetworkTemplate template , long start , long end , boolean includeTags ) {
@@ -771,6 +783,12 @@ private void bootstrapStats() {
771783 private void performPoll (int flags ) {
772784 synchronized (mStatsLock ) {
773785 mWakeLock .acquire ();
786+
787+ // try refreshing time source when stale
788+ if (mTime .getCacheAge () > mSettings .getTimeCacheMaxAge ()) {
789+ mTime .forceRefresh ();
790+ }
791+
774792 try {
775793 performPollLocked (flags );
776794 } finally {
@@ -791,11 +809,6 @@ private void performPollLocked(int flags) {
791809 final boolean persistUid = (flags & FLAG_PERSIST_UID ) != 0 ;
792810 final boolean persistForce = (flags & FLAG_PERSIST_FORCE ) != 0 ;
793811
794- // try refreshing time source when stale
795- if (mTime .getCacheAge () > mSettings .getTimeCacheMaxAge ()) {
796- mTime .forceRefresh ();
797- }
798-
799812 // TODO: consider marking "untrusted" times in historical stats
800813 final long currentTime = mTime .hasCache () ? mTime .currentTimeMillis ()
801814 : System .currentTimeMillis ();
@@ -981,6 +994,7 @@ private void performSample() {
981994 final long start = end - largestBucketSize ;
982995
983996 final long trustedTime = mTime .hasCache () ? mTime .currentTimeMillis () : -1 ;
997+ long devHistoryStart = Long .MAX_VALUE ;
984998
985999 NetworkTemplate template = null ;
9861000 NetworkStats .Entry devTotal = null ;
@@ -990,24 +1004,27 @@ private void performSample() {
9901004 // collect mobile sample
9911005 template = buildTemplateMobileAll (getActiveSubscriberId (mContext ));
9921006 devTotal = getSummaryForNetworkDev (template , start , end ).getTotal (devTotal );
1007+ devHistoryStart = getHistoryStartLocked (template , mNetworkDevStats );
9931008 xtTotal = getSummaryForNetworkXt (template , start , end ).getTotal (xtTotal );
9941009 uidTotal = getSummaryForAllUid (template , start , end , false ).getTotal (uidTotal );
1010+
9951011 EventLogTags .writeNetstatsMobileSample (
9961012 devTotal .rxBytes , devTotal .rxPackets , devTotal .txBytes , devTotal .txPackets ,
9971013 xtTotal .rxBytes , xtTotal .rxPackets , xtTotal .txBytes , xtTotal .txPackets ,
9981014 uidTotal .rxBytes , uidTotal .rxPackets , uidTotal .txBytes , uidTotal .txPackets ,
999- trustedTime );
1015+ trustedTime , devHistoryStart );
10001016
10011017 // collect wifi sample
10021018 template = buildTemplateWifi ();
10031019 devTotal = getSummaryForNetworkDev (template , start , end ).getTotal (devTotal );
1020+ devHistoryStart = getHistoryStartLocked (template , mNetworkDevStats );
10041021 xtTotal = getSummaryForNetworkXt (template , start , end ).getTotal (xtTotal );
10051022 uidTotal = getSummaryForAllUid (template , start , end , false ).getTotal (uidTotal );
10061023 EventLogTags .writeNetstatsWifiSample (
10071024 devTotal .rxBytes , devTotal .rxPackets , devTotal .txBytes , devTotal .txPackets ,
10081025 xtTotal .rxBytes , xtTotal .rxPackets , xtTotal .txBytes , xtTotal .txPackets ,
10091026 uidTotal .rxBytes , uidTotal .rxPackets , uidTotal .txBytes , uidTotal .txPackets ,
1010- trustedTime );
1027+ trustedTime , devHistoryStart );
10111028 }
10121029
10131030 /**
@@ -1243,11 +1260,28 @@ private void writeNetworkStats(
12431260
12441261 // trim any history beyond max
12451262 if (mTime .hasCache ()) {
1246- final long currentTime = Math .min (
1247- System .currentTimeMillis (), mTime .currentTimeMillis ());
1263+ final long systemCurrentTime = System .currentTimeMillis ();
1264+ final long trustedCurrentTime = mTime .currentTimeMillis ();
1265+
1266+ final long currentTime = Math .min (systemCurrentTime , trustedCurrentTime );
12481267 final long maxHistory = mSettings .getNetworkMaxHistory ();
1268+
12491269 for (NetworkStatsHistory history : input .values ()) {
1270+ final int beforeSize = history .size ();
12501271 history .removeBucketsBefore (currentTime - maxHistory );
1272+ final int afterSize = history .size ();
1273+
1274+ if (beforeSize > 24 && afterSize < beforeSize / 2 ) {
1275+ // yikes, dropping more than half of significant history
1276+ final StringBuilder builder = new StringBuilder ();
1277+ builder .append ("yikes, dropping more than half of history" ).append ('\n' );
1278+ builder .append ("systemCurrentTime=" ).append (systemCurrentTime ).append ('\n' );
1279+ builder .append ("trustedCurrentTime=" ).append (trustedCurrentTime ).append ('\n' );
1280+ builder .append ("maxHistory=" ).append (maxHistory ).append ('\n' );
1281+ builder .append ("beforeSize=" ).append (beforeSize ).append ('\n' );
1282+ builder .append ("afterSize=" ).append (afterSize ).append ('\n' );
1283+ mDropBox .addText (TAG_NETSTATS_ERROR , builder .toString ());
1284+ }
12511285 }
12521286 }
12531287
0 commit comments