Loading core/java/android/net/INetworkStatsService.aidl +2 −0 Original line number Diff line number Diff line Loading @@ -42,5 +42,7 @@ interface INetworkStatsService { void setUidForeground(int uid, boolean uidForeground); /** Force update of statistics. */ void forceUpdate(); /** Advise persistance threshold; may be overridden internally. */ void advisePersistThreshold(long thresholdBytes); } core/java/android/util/MathUtils.java +4 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,10 @@ public final class MathUtils { return amount < low ? low : (amount > high ? high : amount); } public static long constrain(long amount, long low, long high) { return amount < low ? low : (amount > high ? high : amount); } public static float constrain(float amount, float low, float high) { return amount < low ? low : (amount > high ? high : amount); } Loading services/java/com/android/server/net/NetworkPolicyManagerService.java +21 −1 Original line number Diff line number Diff line Loading @@ -118,6 +118,7 @@ import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.text.format.Time; import android.util.Log; import android.util.MathUtils; import android.util.NtpTrustedTime; import android.util.Slog; import android.util.SparseArray; Loading Loading @@ -166,7 +167,7 @@ import libcore.io.IoUtils; */ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { private static final String TAG = "NetworkPolicy"; private static final boolean LOGD = true; private static final boolean LOGD = false; private static final boolean LOGV = false; private static final int VERSION_INIT = 1; Loading Loading @@ -962,6 +963,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { } } long lowestRule = Long.MAX_VALUE; final HashSet<String> newMeteredIfaces = Sets.newHashSet(); // apply each policy that we found ifaces for; compute remaining data Loading @@ -985,6 +987,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { + Arrays.toString(ifaces)); } final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; if (hasLimit || policy.metered) { final long quotaBytes; Loading Loading @@ -1014,6 +1017,23 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { newMeteredIfaces.add(iface); } } // keep track of lowest warning or limit of active policies if (hasWarning && policy.warningBytes < lowestRule) { lowestRule = policy.warningBytes; } if (hasLimit && policy.limitBytes < lowestRule) { lowestRule = policy.limitBytes; } } try { // make sure stats are recorded frequently enough; we aim for 2MB // threshold for 2GB/month rules. final long persistThreshold = lowestRule / 1000; mNetworkStats.advisePersistThreshold(persistThreshold); } catch (RemoteException e) { // ignored; service lives in system_server } // remove quota on any trailing interfaces Loading services/java/com/android/server/net/NetworkStatsCollection.java +5 −5 Original line number Diff line number Diff line Loading @@ -186,12 +186,12 @@ public class NetworkStatsCollection implements FileRotator.Reader { if (history.size() == 0) return; noteRecordedHistory(history.getStart(), history.getEnd(), history.getTotalBytes()); final NetworkStatsHistory existing = mStats.get(key); if (existing != null) { existing.recordEntireHistory(history); } else { mStats.put(key, history); NetworkStatsHistory target = mStats.get(key); if (target == null) { target = new NetworkStatsHistory(history.getBucketDuration()); mStats.put(key, target); } target.recordEntireHistory(history); } /** Loading services/java/com/android/server/net/NetworkStatsRecorder.java +12 −4 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ package com.android.server.net; import static android.net.NetworkStats.TAG_NONE; import static android.net.TrafficStats.KB_IN_BYTES; import static android.net.TrafficStats.MB_IN_BYTES; import static com.android.internal.util.Preconditions.checkNotNull; import android.net.NetworkStats; Loading @@ -25,6 +27,7 @@ import android.net.NetworkStatsHistory; import android.net.NetworkTemplate; import android.net.TrafficStats; import android.util.Log; import android.util.MathUtils; import android.util.Slog; import com.android.internal.util.FileRotator; Loading Loading @@ -58,9 +61,9 @@ public class NetworkStatsRecorder { private final String mCookie; private final long mBucketDuration; private final long mPersistThresholdBytes; private final boolean mOnlyTags; private long mPersistThresholdBytes = 2 * MB_IN_BYTES; private NetworkStats mLastSnapshot; private final NetworkStatsCollection mPending; Loading @@ -71,13 +74,12 @@ public class NetworkStatsRecorder { private WeakReference<NetworkStatsCollection> mComplete; public NetworkStatsRecorder(FileRotator rotator, NonMonotonicObserver<String> observer, String cookie, long bucketDuration, long persistThresholdBytes, boolean onlyTags) { String cookie, long bucketDuration, boolean onlyTags) { mRotator = checkNotNull(rotator, "missing FileRotator"); mObserver = checkNotNull(observer, "missing NonMonotonicObserver"); mCookie = cookie; mBucketDuration = bucketDuration; mPersistThresholdBytes = persistThresholdBytes; mOnlyTags = onlyTags; mPending = new NetworkStatsCollection(bucketDuration); Loading @@ -86,6 +88,12 @@ public class NetworkStatsRecorder { mPendingRewriter = new CombiningRewriter(mPending); } public void setPersistThreshold(long thresholdBytes) { if (LOGV) Slog.v(TAG, "setPersistThreshold() with " + thresholdBytes); mPersistThresholdBytes = MathUtils.constrain( thresholdBytes, 1 * KB_IN_BYTES, 100 * MB_IN_BYTES); } public void resetLocked() { mLastSnapshot = null; mPending.reset(); Loading Loading @@ -153,7 +161,7 @@ public class NetworkStatsRecorder { continue; } // skip when no delta occured // skip when no delta occurred if (entry.isEmpty()) continue; // only record tag data when requested Loading Loading
core/java/android/net/INetworkStatsService.aidl +2 −0 Original line number Diff line number Diff line Loading @@ -42,5 +42,7 @@ interface INetworkStatsService { void setUidForeground(int uid, boolean uidForeground); /** Force update of statistics. */ void forceUpdate(); /** Advise persistance threshold; may be overridden internally. */ void advisePersistThreshold(long thresholdBytes); }
core/java/android/util/MathUtils.java +4 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,10 @@ public final class MathUtils { return amount < low ? low : (amount > high ? high : amount); } public static long constrain(long amount, long low, long high) { return amount < low ? low : (amount > high ? high : amount); } public static float constrain(float amount, float low, float high) { return amount < low ? low : (amount > high ? high : amount); } Loading
services/java/com/android/server/net/NetworkPolicyManagerService.java +21 −1 Original line number Diff line number Diff line Loading @@ -118,6 +118,7 @@ import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.text.format.Time; import android.util.Log; import android.util.MathUtils; import android.util.NtpTrustedTime; import android.util.Slog; import android.util.SparseArray; Loading Loading @@ -166,7 +167,7 @@ import libcore.io.IoUtils; */ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { private static final String TAG = "NetworkPolicy"; private static final boolean LOGD = true; private static final boolean LOGD = false; private static final boolean LOGV = false; private static final int VERSION_INIT = 1; Loading Loading @@ -962,6 +963,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { } } long lowestRule = Long.MAX_VALUE; final HashSet<String> newMeteredIfaces = Sets.newHashSet(); // apply each policy that we found ifaces for; compute remaining data Loading @@ -985,6 +987,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { + Arrays.toString(ifaces)); } final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; if (hasLimit || policy.metered) { final long quotaBytes; Loading Loading @@ -1014,6 +1017,23 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { newMeteredIfaces.add(iface); } } // keep track of lowest warning or limit of active policies if (hasWarning && policy.warningBytes < lowestRule) { lowestRule = policy.warningBytes; } if (hasLimit && policy.limitBytes < lowestRule) { lowestRule = policy.limitBytes; } } try { // make sure stats are recorded frequently enough; we aim for 2MB // threshold for 2GB/month rules. final long persistThreshold = lowestRule / 1000; mNetworkStats.advisePersistThreshold(persistThreshold); } catch (RemoteException e) { // ignored; service lives in system_server } // remove quota on any trailing interfaces Loading
services/java/com/android/server/net/NetworkStatsCollection.java +5 −5 Original line number Diff line number Diff line Loading @@ -186,12 +186,12 @@ public class NetworkStatsCollection implements FileRotator.Reader { if (history.size() == 0) return; noteRecordedHistory(history.getStart(), history.getEnd(), history.getTotalBytes()); final NetworkStatsHistory existing = mStats.get(key); if (existing != null) { existing.recordEntireHistory(history); } else { mStats.put(key, history); NetworkStatsHistory target = mStats.get(key); if (target == null) { target = new NetworkStatsHistory(history.getBucketDuration()); mStats.put(key, target); } target.recordEntireHistory(history); } /** Loading
services/java/com/android/server/net/NetworkStatsRecorder.java +12 −4 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ package com.android.server.net; import static android.net.NetworkStats.TAG_NONE; import static android.net.TrafficStats.KB_IN_BYTES; import static android.net.TrafficStats.MB_IN_BYTES; import static com.android.internal.util.Preconditions.checkNotNull; import android.net.NetworkStats; Loading @@ -25,6 +27,7 @@ import android.net.NetworkStatsHistory; import android.net.NetworkTemplate; import android.net.TrafficStats; import android.util.Log; import android.util.MathUtils; import android.util.Slog; import com.android.internal.util.FileRotator; Loading Loading @@ -58,9 +61,9 @@ public class NetworkStatsRecorder { private final String mCookie; private final long mBucketDuration; private final long mPersistThresholdBytes; private final boolean mOnlyTags; private long mPersistThresholdBytes = 2 * MB_IN_BYTES; private NetworkStats mLastSnapshot; private final NetworkStatsCollection mPending; Loading @@ -71,13 +74,12 @@ public class NetworkStatsRecorder { private WeakReference<NetworkStatsCollection> mComplete; public NetworkStatsRecorder(FileRotator rotator, NonMonotonicObserver<String> observer, String cookie, long bucketDuration, long persistThresholdBytes, boolean onlyTags) { String cookie, long bucketDuration, boolean onlyTags) { mRotator = checkNotNull(rotator, "missing FileRotator"); mObserver = checkNotNull(observer, "missing NonMonotonicObserver"); mCookie = cookie; mBucketDuration = bucketDuration; mPersistThresholdBytes = persistThresholdBytes; mOnlyTags = onlyTags; mPending = new NetworkStatsCollection(bucketDuration); Loading @@ -86,6 +88,12 @@ public class NetworkStatsRecorder { mPendingRewriter = new CombiningRewriter(mPending); } public void setPersistThreshold(long thresholdBytes) { if (LOGV) Slog.v(TAG, "setPersistThreshold() with " + thresholdBytes); mPersistThresholdBytes = MathUtils.constrain( thresholdBytes, 1 * KB_IN_BYTES, 100 * MB_IN_BYTES); } public void resetLocked() { mLastSnapshot = null; mPending.reset(); Loading Loading @@ -153,7 +161,7 @@ public class NetworkStatsRecorder { continue; } // skip when no delta occured // skip when no delta occurred if (entry.isEmpty()) continue; // only record tag data when requested Loading