Skip to content

Commit bb1492f

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Reduce persist threshold for lower warning/limit." into jb-dev
2 parents c065a8a + ac3fcb1 commit bb1492f

File tree

7 files changed

+142
-41
lines changed

7 files changed

+142
-41
lines changed

core/java/android/net/INetworkStatsService.aidl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ interface INetworkStatsService {
4242
void setUidForeground(int uid, boolean uidForeground);
4343
/** Force update of statistics. */
4444
void forceUpdate();
45+
/** Advise persistance threshold; may be overridden internally. */
46+
void advisePersistThreshold(long thresholdBytes);
4547

4648
}

core/java/android/util/MathUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static int constrain(int amount, int low, int high) {
3939
return amount < low ? low : (amount > high ? high : amount);
4040
}
4141

42+
public static long constrain(long amount, long low, long high) {
43+
return amount < low ? low : (amount > high ? high : amount);
44+
}
45+
4246
public static float constrain(float amount, float low, float high) {
4347
return amount < low ? low : (amount > high ? high : amount);
4448
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import android.text.format.Formatter;
119119
import android.text.format.Time;
120120
import android.util.Log;
121+
import android.util.MathUtils;
121122
import android.util.NtpTrustedTime;
122123
import android.util.Slog;
123124
import android.util.SparseArray;
@@ -166,7 +167,7 @@
166167
*/
167168
public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
168169
private static final String TAG = "NetworkPolicy";
169-
private static final boolean LOGD = true;
170+
private static final boolean LOGD = false;
170171
private static final boolean LOGV = false;
171172

172173
private static final int VERSION_INIT = 1;
@@ -962,6 +963,7 @@ private void updateNetworkRulesLocked() {
962963
}
963964
}
964965

966+
long lowestRule = Long.MAX_VALUE;
965967
final HashSet<String> newMeteredIfaces = Sets.newHashSet();
966968

967969
// apply each policy that we found ifaces for; compute remaining data
@@ -985,6 +987,7 @@ private void updateNetworkRulesLocked() {
985987
+ Arrays.toString(ifaces));
986988
}
987989

990+
final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED;
988991
final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED;
989992
if (hasLimit || policy.metered) {
990993
final long quotaBytes;
@@ -1014,6 +1017,23 @@ private void updateNetworkRulesLocked() {
10141017
newMeteredIfaces.add(iface);
10151018
}
10161019
}
1020+
1021+
// keep track of lowest warning or limit of active policies
1022+
if (hasWarning && policy.warningBytes < lowestRule) {
1023+
lowestRule = policy.warningBytes;
1024+
}
1025+
if (hasLimit && policy.limitBytes < lowestRule) {
1026+
lowestRule = policy.limitBytes;
1027+
}
1028+
}
1029+
1030+
try {
1031+
// make sure stats are recorded frequently enough; we aim for 2MB
1032+
// threshold for 2GB/month rules.
1033+
final long persistThreshold = lowestRule / 1000;
1034+
mNetworkStats.advisePersistThreshold(persistThreshold);
1035+
} catch (RemoteException e) {
1036+
// ignored; service lives in system_server
10171037
}
10181038

10191039
// remove quota on any trailing interfaces

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ private void recordHistory(Key key, NetworkStatsHistory history) {
186186
if (history.size() == 0) return;
187187
noteRecordedHistory(history.getStart(), history.getEnd(), history.getTotalBytes());
188188

189-
final NetworkStatsHistory existing = mStats.get(key);
190-
if (existing != null) {
191-
existing.recordEntireHistory(history);
192-
} else {
193-
mStats.put(key, history);
189+
NetworkStatsHistory target = mStats.get(key);
190+
if (target == null) {
191+
target = new NetworkStatsHistory(history.getBucketDuration());
192+
mStats.put(key, target);
194193
}
194+
target.recordEntireHistory(history);
195195
}
196196

197197
/**

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.android.server.net;
1818

1919
import static android.net.NetworkStats.TAG_NONE;
20+
import static android.net.TrafficStats.KB_IN_BYTES;
21+
import static android.net.TrafficStats.MB_IN_BYTES;
2022
import static com.android.internal.util.Preconditions.checkNotNull;
2123

2224
import android.net.NetworkStats;
@@ -25,6 +27,7 @@
2527
import android.net.NetworkTemplate;
2628
import android.net.TrafficStats;
2729
import android.util.Log;
30+
import android.util.MathUtils;
2831
import android.util.Slog;
2932

3033
import com.android.internal.util.FileRotator;
@@ -58,9 +61,9 @@ public class NetworkStatsRecorder {
5861
private final String mCookie;
5962

6063
private final long mBucketDuration;
61-
private final long mPersistThresholdBytes;
6264
private final boolean mOnlyTags;
6365

66+
private long mPersistThresholdBytes = 2 * MB_IN_BYTES;
6467
private NetworkStats mLastSnapshot;
6568

6669
private final NetworkStatsCollection mPending;
@@ -71,13 +74,12 @@ public class NetworkStatsRecorder {
7174
private WeakReference<NetworkStatsCollection> mComplete;
7275

7376
public NetworkStatsRecorder(FileRotator rotator, NonMonotonicObserver<String> observer,
74-
String cookie, long bucketDuration, long persistThresholdBytes, boolean onlyTags) {
77+
String cookie, long bucketDuration, boolean onlyTags) {
7578
mRotator = checkNotNull(rotator, "missing FileRotator");
7679
mObserver = checkNotNull(observer, "missing NonMonotonicObserver");
7780
mCookie = cookie;
7881

7982
mBucketDuration = bucketDuration;
80-
mPersistThresholdBytes = persistThresholdBytes;
8183
mOnlyTags = onlyTags;
8284

8385
mPending = new NetworkStatsCollection(bucketDuration);
@@ -86,6 +88,12 @@ public NetworkStatsRecorder(FileRotator rotator, NonMonotonicObserver<String> ob
8688
mPendingRewriter = new CombiningRewriter(mPending);
8789
}
8890

91+
public void setPersistThreshold(long thresholdBytes) {
92+
if (LOGV) Slog.v(TAG, "setPersistThreshold() with " + thresholdBytes);
93+
mPersistThresholdBytes = MathUtils.constrain(
94+
thresholdBytes, 1 * KB_IN_BYTES, 100 * MB_IN_BYTES);
95+
}
96+
8997
public void resetLocked() {
9098
mLastSnapshot = null;
9199
mPending.reset();
@@ -153,7 +161,7 @@ public void recordSnapshotLocked(NetworkStats snapshot,
153161
continue;
154162
}
155163

156-
// skip when no delta occured
164+
// skip when no delta occurred
157165
if (entry.isEmpty()) continue;
158166

159167
// only record tag data when requested

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

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static android.net.NetworkStats.UID_ALL;
3737
import static android.net.NetworkTemplate.buildTemplateMobileWildcard;
3838
import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
39+
import static android.net.TrafficStats.KB_IN_BYTES;
3940
import static android.net.TrafficStats.MB_IN_BYTES;
4041
import static android.provider.Settings.Secure.NETSTATS_DEV_BUCKET_DURATION;
4142
import static android.provider.Settings.Secure.NETSTATS_DEV_DELETE_AGE;
@@ -49,6 +50,10 @@
4950
import static android.provider.Settings.Secure.NETSTATS_UID_DELETE_AGE;
5051
import static android.provider.Settings.Secure.NETSTATS_UID_PERSIST_BYTES;
5152
import 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;
5257
import static android.telephony.PhoneStateListener.LISTEN_DATA_CONNECTION_STATE;
5358
import static android.telephony.PhoneStateListener.LISTEN_NONE;
5459
import static android.text.format.DateUtils.DAY_IN_MILLIS;
@@ -94,10 +99,12 @@
9499
import android.os.RemoteException;
95100
import android.os.SystemClock;
96101
import android.provider.Settings;
102+
import android.provider.Settings.Secure;
97103
import android.telephony.PhoneStateListener;
98104
import android.telephony.TelephonyManager;
99105
import android.util.EventLog;
100106
import android.util.Log;
107+
import android.util.MathUtils;
101108
import android.util.NtpTrustedTime;
102109
import android.util.Slog;
103110
import 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

Comments
 (0)