3636import static android .net .NetworkStats .UID_ALL ;
3737import static android .net .NetworkTemplate .buildTemplateMobileWildcard ;
3838import static android .net .NetworkTemplate .buildTemplateWifiWildcard ;
39+ import static android .net .TrafficStats .KB_IN_BYTES ;
3940import static android .net .TrafficStats .MB_IN_BYTES ;
4041import static android .provider .Settings .Secure .NETSTATS_DEV_BUCKET_DURATION ;
4142import static android .provider .Settings .Secure .NETSTATS_DEV_DELETE_AGE ;
4950import static android .provider .Settings .Secure .NETSTATS_UID_DELETE_AGE ;
5051import static android .provider .Settings .Secure .NETSTATS_UID_PERSIST_BYTES ;
5152import static android .provider .Settings .Secure .NETSTATS_UID_ROTATE_AGE ;
53+ import static android .provider .Settings .Secure .NETSTATS_UID_TAG_BUCKET_DURATION ;
54+ import static android .provider .Settings .Secure .NETSTATS_UID_TAG_DELETE_AGE ;
55+ import static android .provider .Settings .Secure .NETSTATS_UID_TAG_PERSIST_BYTES ;
56+ import static android .provider .Settings .Secure .NETSTATS_UID_TAG_ROTATE_AGE ;
5257import static android .telephony .PhoneStateListener .LISTEN_DATA_CONNECTION_STATE ;
5358import static android .telephony .PhoneStateListener .LISTEN_NONE ;
5459import static android .text .format .DateUtils .DAY_IN_MILLIS ;
9499import android .os .RemoteException ;
95100import android .os .SystemClock ;
96101import android .provider .Settings ;
102+ import android .provider .Settings .Secure ;
97103import android .telephony .PhoneStateListener ;
98104import android .telephony .TelephonyManager ;
99105import android .util .EventLog ;
100106import android .util .Log ;
107+ import android .util .MathUtils ;
101108import android .util .NtpTrustedTime ;
102109import android .util .Slog ;
103110import android .util .SparseIntArray ;
@@ -169,19 +176,15 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
169176 public interface NetworkStatsSettings {
170177 public long getPollInterval ();
171178 public long getTimeCacheMaxAge ();
172- public long getGlobalAlertBytes ();
173179 public boolean getSampleEnabled ();
174180
175181 public static class Config {
176182 public final long bucketDuration ;
177- public final long persistBytes ;
178183 public final long rotateAgeMillis ;
179184 public final long deleteAgeMillis ;
180185
181- public Config (long bucketDuration , long persistBytes , long rotateAgeMillis ,
182- long deleteAgeMillis ) {
186+ public Config (long bucketDuration , long rotateAgeMillis , long deleteAgeMillis ) {
183187 this .bucketDuration = bucketDuration ;
184- this .persistBytes = persistBytes ;
185188 this .rotateAgeMillis = rotateAgeMillis ;
186189 this .deleteAgeMillis = deleteAgeMillis ;
187190 }
@@ -191,6 +194,12 @@ public Config(long bucketDuration, long persistBytes, long rotateAgeMillis,
191194 public Config getXtConfig ();
192195 public Config getUidConfig ();
193196 public Config getUidTagConfig ();
197+
198+ public long getGlobalAlertBytes (long def );
199+ public long getDevPersistBytes (long def );
200+ public long getXtPersistBytes (long def );
201+ public long getUidPersistBytes (long def );
202+ public long getUidTagPersistBytes (long def );
194203 }
195204
196205 private final Object mStatsLock = new Object ();
@@ -223,6 +232,8 @@ public Config(long bucketDuration, long persistBytes, long rotateAgeMillis,
223232 private final Handler mHandler ;
224233
225234 private boolean mSystemReady ;
235+ private long mPersistThreshold = 2 * MB_IN_BYTES ;
236+ private long mGlobalAlertBytes ;
226237
227238 public NetworkStatsService (
228239 Context context , INetworkManagementService networkManager , IAlarmManager alarmManager ) {
@@ -275,6 +286,8 @@ public void systemReady() {
275286 mUidRecorder = buildRecorder (PREFIX_UID , mSettings .getUidConfig (), false );
276287 mUidTagRecorder = buildRecorder (PREFIX_UID_TAG , mSettings .getUidTagConfig (), true );
277288
289+ updatePersistThresholds ();
290+
278291 synchronized (mStatsLock ) {
279292 // upgrade any legacy stats, migrating them to rotated files
280293 maybeUpgradeLegacyStatsLocked ();
@@ -325,10 +338,9 @@ public void systemReady() {
325338
326339 private NetworkStatsRecorder buildRecorder (
327340 String prefix , NetworkStatsSettings .Config config , boolean includeTags ) {
328- return new NetworkStatsRecorder (
329- new FileRotator (mBaseDir , prefix , config .rotateAgeMillis , config .deleteAgeMillis ),
330- mNonMonotonicObserver , prefix , config .bucketDuration , config .persistBytes ,
331- includeTags );
341+ return new NetworkStatsRecorder (new FileRotator (
342+ mBaseDir , prefix , config .rotateAgeMillis , config .deleteAgeMillis ),
343+ mNonMonotonicObserver , prefix , config .bucketDuration , includeTags );
332344 }
333345
334346 private void shutdownLocked () {
@@ -414,8 +426,7 @@ private void registerPollAlarmLocked() {
414426 */
415427 private void registerGlobalAlert () {
416428 try {
417- final long alertBytes = mSettings .getGlobalAlertBytes ();
418- mNetworkManager .setGlobalAlert (alertBytes );
429+ mNetworkManager .setGlobalAlert (mGlobalAlertBytes );
419430 } catch (IllegalStateException e ) {
420431 Slog .w (TAG , "problem registering for global alert: " + e );
421432 } catch (RemoteException e ) {
@@ -437,14 +448,18 @@ public INetworkStatsSession openSession() {
437448
438449 private NetworkStatsCollection getUidComplete () {
439450 if (mUidComplete == null ) {
440- mUidComplete = mUidRecorder .getOrLoadCompleteLocked ();
451+ synchronized (mStatsLock ) {
452+ mUidComplete = mUidRecorder .getOrLoadCompleteLocked ();
453+ }
441454 }
442455 return mUidComplete ;
443456 }
444457
445458 private NetworkStatsCollection getUidTagComplete () {
446459 if (mUidTagComplete == null ) {
447- mUidTagComplete = mUidTagRecorder .getOrLoadCompleteLocked ();
460+ synchronized (mStatsLock ) {
461+ mUidTagComplete = mUidTagRecorder .getOrLoadCompleteLocked ();
462+ }
448463 }
449464 return mUidTagComplete ;
450465 }
@@ -584,6 +599,45 @@ public void forceUpdate() {
584599 }
585600 }
586601
602+ @ Override
603+ public void advisePersistThreshold (long thresholdBytes ) {
604+ mContext .enforceCallingOrSelfPermission (MODIFY_NETWORK_ACCOUNTING , TAG );
605+ assertBandwidthControlEnabled ();
606+
607+ // clamp threshold into safe range
608+ mPersistThreshold = MathUtils .constrain (thresholdBytes , 128 * KB_IN_BYTES , 2 * MB_IN_BYTES );
609+ updatePersistThresholds ();
610+
611+ if (LOGV ) {
612+ Slog .v (TAG , "advisePersistThreshold() given " + thresholdBytes + ", clamped to "
613+ + mPersistThreshold );
614+ }
615+
616+ // persist if beyond new thresholds
617+ final long currentTime = mTime .hasCache () ? mTime .currentTimeMillis ()
618+ : System .currentTimeMillis ();
619+ mDevRecorder .maybePersistLocked (currentTime );
620+ mXtRecorder .maybePersistLocked (currentTime );
621+ mUidRecorder .maybePersistLocked (currentTime );
622+ mUidTagRecorder .maybePersistLocked (currentTime );
623+
624+ // re-arm global alert
625+ registerGlobalAlert ();
626+ }
627+
628+ /**
629+ * Update {@link NetworkStatsRecorder} and {@link #mGlobalAlertBytes} to
630+ * reflect current {@link #mPersistThreshold} value. Always defers to
631+ * {@link Secure} values when defined.
632+ */
633+ private void updatePersistThresholds () {
634+ mDevRecorder .setPersistThreshold (mSettings .getDevPersistBytes (mPersistThreshold ));
635+ mXtRecorder .setPersistThreshold (mSettings .getXtPersistBytes (mPersistThreshold ));
636+ mUidRecorder .setPersistThreshold (mSettings .getUidPersistBytes (mPersistThreshold ));
637+ mUidTagRecorder .setPersistThreshold (mSettings .getUidTagPersistBytes (mPersistThreshold ));
638+ mGlobalAlertBytes = mSettings .getGlobalAlertBytes (mPersistThreshold );
639+ }
640+
587641 /**
588642 * Receiver that watches for {@link IConnectivityManager} to claim network
589643 * interfaces. Used to associate {@link TelephonyManager#getSubscriberId()}
@@ -1122,41 +1176,50 @@ public long getTimeCacheMaxAge() {
11221176 return getSecureLong (NETSTATS_TIME_CACHE_MAX_AGE , DAY_IN_MILLIS );
11231177 }
11241178 @ Override
1125- public long getGlobalAlertBytes () {
1126- return getSecureLong (NETSTATS_GLOBAL_ALERT_BYTES , 2 * MB_IN_BYTES );
1179+ public long getGlobalAlertBytes (long def ) {
1180+ return getSecureLong (NETSTATS_GLOBAL_ALERT_BYTES , def );
11271181 }
11281182 @ Override
11291183 public boolean getSampleEnabled () {
11301184 return getSecureBoolean (NETSTATS_SAMPLE_ENABLED , true );
11311185 }
1132-
11331186 @ Override
11341187 public Config getDevConfig () {
11351188 return new Config (getSecureLong (NETSTATS_DEV_BUCKET_DURATION , HOUR_IN_MILLIS ),
1136- getSecureLong (NETSTATS_DEV_PERSIST_BYTES , 2 * MB_IN_BYTES ),
11371189 getSecureLong (NETSTATS_DEV_ROTATE_AGE , 15 * DAY_IN_MILLIS ),
11381190 getSecureLong (NETSTATS_DEV_DELETE_AGE , 90 * DAY_IN_MILLIS ));
11391191 }
1140-
11411192 @ Override
11421193 public Config getXtConfig () {
11431194 return getDevConfig ();
11441195 }
1145-
11461196 @ Override
11471197 public Config getUidConfig () {
11481198 return new Config (getSecureLong (NETSTATS_UID_BUCKET_DURATION , 2 * HOUR_IN_MILLIS ),
1149- getSecureLong (NETSTATS_UID_PERSIST_BYTES , 2 * MB_IN_BYTES ),
11501199 getSecureLong (NETSTATS_UID_ROTATE_AGE , 15 * DAY_IN_MILLIS ),
11511200 getSecureLong (NETSTATS_UID_DELETE_AGE , 90 * DAY_IN_MILLIS ));
11521201 }
1153-
11541202 @ Override
11551203 public Config getUidTagConfig () {
1156- return new Config (getSecureLong (NETSTATS_UID_BUCKET_DURATION , 2 * HOUR_IN_MILLIS ),
1157- getSecureLong (NETSTATS_UID_PERSIST_BYTES , 2 * MB_IN_BYTES ),
1158- getSecureLong (NETSTATS_UID_ROTATE_AGE , 5 * DAY_IN_MILLIS ),
1159- getSecureLong (NETSTATS_UID_DELETE_AGE , 15 * DAY_IN_MILLIS ));
1204+ return new Config (getSecureLong (NETSTATS_UID_TAG_BUCKET_DURATION , 2 * HOUR_IN_MILLIS ),
1205+ getSecureLong (NETSTATS_UID_TAG_ROTATE_AGE , 5 * DAY_IN_MILLIS ),
1206+ getSecureLong (NETSTATS_UID_TAG_DELETE_AGE , 15 * DAY_IN_MILLIS ));
1207+ }
1208+ @ Override
1209+ public long getDevPersistBytes (long def ) {
1210+ return getSecureLong (NETSTATS_DEV_PERSIST_BYTES , def );
1211+ }
1212+ @ Override
1213+ public long getXtPersistBytes (long def ) {
1214+ return getDevPersistBytes (def );
1215+ }
1216+ @ Override
1217+ public long getUidPersistBytes (long def ) {
1218+ return getSecureLong (NETSTATS_UID_PERSIST_BYTES , def );
1219+ }
1220+ @ Override
1221+ public long getUidTagPersistBytes (long def ) {
1222+ return getSecureLong (NETSTATS_UID_TAG_PERSIST_BYTES , def );
11601223 }
11611224 }
11621225}
0 commit comments