Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.net; |
| 18 | |
| 19 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 20 | import static android.net.TrafficStats.KB_IN_BYTES; |
| 21 | import static android.net.TrafficStats.MB_IN_BYTES; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 22 | import static android.text.format.DateUtils.YEAR_IN_MILLIS; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 23 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 24 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 25 | |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 26 | import android.annotation.Nullable; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 27 | import android.net.NetworkStats; |
| 28 | import android.net.NetworkStats.NonMonotonicObserver; |
| 29 | import android.net.NetworkStatsHistory; |
| 30 | import android.net.NetworkTemplate; |
| 31 | import android.net.TrafficStats; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 32 | import android.os.Binder; |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 33 | import android.os.DropBoxManager; |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 34 | import android.service.NetworkStatsRecorderProto; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 36 | import android.util.MathUtils; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 37 | import android.util.Slog; |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 38 | import android.util.proto.ProtoOutputStream; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 39 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 40 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 41 | import com.android.internal.util.FileRotator; |
| 42 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 43 | |
| 44 | import libcore.io.IoUtils; |
| 45 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 46 | import com.google.android.collect.Sets; |
| 47 | |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 48 | import java.io.ByteArrayOutputStream; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 49 | import java.io.DataOutputStream; |
| 50 | import java.io.File; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
| 53 | import java.io.OutputStream; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 54 | import java.io.PrintWriter; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 55 | import java.lang.ref.WeakReference; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 56 | import java.util.Arrays; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 57 | import java.util.HashSet; |
| 58 | import java.util.Map; |
| 59 | |
| 60 | /** |
| 61 | * Logic to record deltas between periodic {@link NetworkStats} snapshots into |
| 62 | * {@link NetworkStatsHistory} that belong to {@link NetworkStatsCollection}. |
| 63 | * Keeps pending changes in memory until they pass a specific threshold, in |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 64 | * bytes. Uses {@link FileRotator} for persistence logic if present. |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 65 | * <p> |
| 66 | * Not inherently thread safe. |
| 67 | */ |
| 68 | public class NetworkStatsRecorder { |
| 69 | private static final String TAG = "NetworkStatsRecorder"; |
Jeff Sharkey | e7bb71d | 2012-02-28 15:13:08 -0800 | [diff] [blame] | 70 | private static final boolean LOGD = false; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 71 | private static final boolean LOGV = false; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 72 | |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 73 | private static final String TAG_NETSTATS_DUMP = "netstats_dump"; |
| 74 | |
| 75 | /** Dump before deleting in {@link #recoverFromWtf()}. */ |
| 76 | private static final boolean DUMP_BEFORE_DELETE = true; |
| 77 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 78 | private final FileRotator mRotator; |
| 79 | private final NonMonotonicObserver<String> mObserver; |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 80 | private final DropBoxManager mDropBox; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 81 | private final String mCookie; |
| 82 | |
| 83 | private final long mBucketDuration; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 84 | private final boolean mOnlyTags; |
| 85 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 86 | private long mPersistThresholdBytes = 2 * MB_IN_BYTES; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 87 | private NetworkStats mLastSnapshot; |
| 88 | |
| 89 | private final NetworkStatsCollection mPending; |
| 90 | private final NetworkStatsCollection mSinceBoot; |
| 91 | |
| 92 | private final CombiningRewriter mPendingRewriter; |
| 93 | |
| 94 | private WeakReference<NetworkStatsCollection> mComplete; |
| 95 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 96 | /** |
| 97 | * Non-persisted recorder, with only one bucket. Used by {@link NetworkStatsObservers}. |
| 98 | */ |
| 99 | public NetworkStatsRecorder() { |
| 100 | mRotator = null; |
| 101 | mObserver = null; |
| 102 | mDropBox = null; |
| 103 | mCookie = null; |
| 104 | |
| 105 | // set the bucket big enough to have all data in one bucket, but allow some |
| 106 | // slack to avoid overflow |
| 107 | mBucketDuration = YEAR_IN_MILLIS; |
| 108 | mOnlyTags = false; |
| 109 | |
| 110 | mPending = null; |
| 111 | mSinceBoot = new NetworkStatsCollection(mBucketDuration); |
| 112 | |
| 113 | mPendingRewriter = null; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Persisted recorder. |
| 118 | */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 119 | public NetworkStatsRecorder(FileRotator rotator, NonMonotonicObserver<String> observer, |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 120 | DropBoxManager dropBox, String cookie, long bucketDuration, boolean onlyTags) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 121 | mRotator = checkNotNull(rotator, "missing FileRotator"); |
| 122 | mObserver = checkNotNull(observer, "missing NonMonotonicObserver"); |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 123 | mDropBox = checkNotNull(dropBox, "missing DropBoxManager"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 124 | mCookie = cookie; |
| 125 | |
| 126 | mBucketDuration = bucketDuration; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 127 | mOnlyTags = onlyTags; |
| 128 | |
| 129 | mPending = new NetworkStatsCollection(bucketDuration); |
| 130 | mSinceBoot = new NetworkStatsCollection(bucketDuration); |
| 131 | |
| 132 | mPendingRewriter = new CombiningRewriter(mPending); |
| 133 | } |
| 134 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 135 | public void setPersistThreshold(long thresholdBytes) { |
| 136 | if (LOGV) Slog.v(TAG, "setPersistThreshold() with " + thresholdBytes); |
| 137 | mPersistThresholdBytes = MathUtils.constrain( |
| 138 | thresholdBytes, 1 * KB_IN_BYTES, 100 * MB_IN_BYTES); |
| 139 | } |
| 140 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 141 | public void resetLocked() { |
| 142 | mLastSnapshot = null; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 143 | if (mPending != null) { |
| 144 | mPending.reset(); |
| 145 | } |
| 146 | if (mSinceBoot != null) { |
| 147 | mSinceBoot.reset(); |
| 148 | } |
| 149 | if (mComplete != null) { |
| 150 | mComplete.clear(); |
| 151 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public NetworkStats.Entry getTotalSinceBootLocked(NetworkTemplate template) { |
Jeff Davidson | 1efb133 | 2015-12-09 18:04:50 -0800 | [diff] [blame] | 155 | return mSinceBoot.getSummary(template, Long.MIN_VALUE, Long.MAX_VALUE, |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 156 | NetworkStatsAccess.Level.DEVICE, Binder.getCallingUid()).getTotal(null); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 159 | public NetworkStatsCollection getSinceBoot() { |
| 160 | return mSinceBoot; |
| 161 | } |
| 162 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 163 | /** |
| 164 | * Load complete history represented by {@link FileRotator}. Caches |
| 165 | * internally as a {@link WeakReference}, and updated with future |
| 166 | * {@link #recordSnapshotLocked(NetworkStats, Map, long)} snapshots as long |
| 167 | * as reference is valid. |
| 168 | */ |
| 169 | public NetworkStatsCollection getOrLoadCompleteLocked() { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 170 | checkNotNull(mRotator, "missing FileRotator"); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 171 | NetworkStatsCollection res = mComplete != null ? mComplete.get() : null; |
| 172 | if (res == null) { |
| 173 | res = loadLocked(Long.MIN_VALUE, Long.MAX_VALUE); |
| 174 | mComplete = new WeakReference<NetworkStatsCollection>(res); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 175 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 176 | return res; |
| 177 | } |
| 178 | |
| 179 | public NetworkStatsCollection getOrLoadPartialLocked(long start, long end) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 180 | checkNotNull(mRotator, "missing FileRotator"); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 181 | NetworkStatsCollection res = mComplete != null ? mComplete.get() : null; |
| 182 | if (res == null) { |
| 183 | res = loadLocked(start, end); |
| 184 | } |
| 185 | return res; |
| 186 | } |
| 187 | |
| 188 | private NetworkStatsCollection loadLocked(long start, long end) { |
| 189 | if (LOGD) Slog.d(TAG, "loadLocked() reading from disk for " + mCookie); |
| 190 | final NetworkStatsCollection res = new NetworkStatsCollection(mBucketDuration); |
| 191 | try { |
| 192 | mRotator.readMatching(res, start, end); |
| 193 | res.recordCollection(mPending); |
| 194 | } catch (IOException e) { |
| 195 | Log.wtf(TAG, "problem completely reading network stats", e); |
| 196 | recoverFromWtf(); |
| 197 | } catch (OutOfMemoryError e) { |
| 198 | Log.wtf(TAG, "problem completely reading network stats", e); |
| 199 | recoverFromWtf(); |
| 200 | } |
| 201 | return res; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Record any delta that occurred since last {@link NetworkStats} snapshot, |
| 206 | * using the given {@link Map} to identify network interfaces. First |
| 207 | * snapshot is considered bootstrap, and is not counted as delta. |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 208 | * |
| 209 | * @param vpnArray Optional info about the currently active VPN, if any. This is used to |
| 210 | * redistribute traffic from the VPN app to the underlying responsible apps. |
| 211 | * This should always be set to null if the provided snapshot is aggregated |
| 212 | * across all UIDs (e.g. contains UID_ALL buckets), regardless of VPN state. |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 213 | */ |
| 214 | public void recordSnapshotLocked(NetworkStats snapshot, |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 215 | Map<String, NetworkIdentitySet> ifaceIdent, @Nullable VpnInfo[] vpnArray, |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 216 | long currentTimeMillis) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 217 | final HashSet<String> unknownIfaces = Sets.newHashSet(); |
| 218 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 219 | // skip recording when snapshot missing |
| 220 | if (snapshot == null) return; |
| 221 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 222 | // assume first snapshot is bootstrap and don't record |
| 223 | if (mLastSnapshot == null) { |
| 224 | mLastSnapshot = snapshot; |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | final NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null; |
| 229 | |
| 230 | final NetworkStats delta = NetworkStats.subtract( |
| 231 | snapshot, mLastSnapshot, mObserver, mCookie); |
| 232 | final long end = currentTimeMillis; |
| 233 | final long start = end - delta.getElapsedRealtime(); |
| 234 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 235 | if (vpnArray != null) { |
| 236 | for (VpnInfo info : vpnArray) { |
| 237 | delta.migrateTun(info.ownerUid, info.vpnIface, info.primaryUnderlyingIface); |
| 238 | } |
| 239 | } |
| 240 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 241 | NetworkStats.Entry entry = null; |
| 242 | for (int i = 0; i < delta.size(); i++) { |
| 243 | entry = delta.getValues(i, entry); |
Jeff Sharkey | b5a97e6 | 2018-05-22 11:35:29 -0600 | [diff] [blame] | 244 | |
| 245 | // As a last-ditch sanity check, report any negative values and |
| 246 | // clamp them so recording below doesn't croak. |
| 247 | if (entry.isNegative()) { |
| 248 | if (mObserver != null) { |
| 249 | mObserver.foundNonMonotonic(delta, i, mCookie); |
| 250 | } |
| 251 | entry.rxBytes = Math.max(entry.rxBytes, 0); |
| 252 | entry.rxPackets = Math.max(entry.rxPackets, 0); |
| 253 | entry.txBytes = Math.max(entry.txBytes, 0); |
| 254 | entry.txPackets = Math.max(entry.txPackets, 0); |
| 255 | entry.operations = Math.max(entry.operations, 0); |
| 256 | } |
| 257 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 258 | final NetworkIdentitySet ident = ifaceIdent.get(entry.iface); |
| 259 | if (ident == null) { |
| 260 | unknownIfaces.add(entry.iface); |
| 261 | continue; |
| 262 | } |
| 263 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 264 | // skip when no delta occurred |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 265 | if (entry.isEmpty()) continue; |
| 266 | |
| 267 | // only record tag data when requested |
| 268 | if ((entry.tag == TAG_NONE) != mOnlyTags) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 269 | if (mPending != null) { |
| 270 | mPending.recordData(ident, entry.uid, entry.set, entry.tag, start, end, entry); |
| 271 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 272 | |
| 273 | // also record against boot stats when present |
| 274 | if (mSinceBoot != null) { |
| 275 | mSinceBoot.recordData(ident, entry.uid, entry.set, entry.tag, start, end, entry); |
| 276 | } |
| 277 | |
| 278 | // also record against complete dataset when present |
| 279 | if (complete != null) { |
| 280 | complete.recordData(ident, entry.uid, entry.set, entry.tag, start, end, entry); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | mLastSnapshot = snapshot; |
| 286 | |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 287 | if (LOGV && unknownIfaces.size() > 0) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 288 | Slog.w(TAG, "unknown interfaces " + unknownIfaces + ", ignoring those stats"); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Consider persisting any pending deltas, if they are beyond |
| 294 | * {@link #mPersistThresholdBytes}. |
| 295 | */ |
| 296 | public void maybePersistLocked(long currentTimeMillis) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 297 | checkNotNull(mRotator, "missing FileRotator"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 298 | final long pendingBytes = mPending.getTotalBytes(); |
| 299 | if (pendingBytes >= mPersistThresholdBytes) { |
| 300 | forcePersistLocked(currentTimeMillis); |
| 301 | } else { |
| 302 | mRotator.maybeRotate(currentTimeMillis); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Force persisting any pending deltas. |
| 308 | */ |
| 309 | public void forcePersistLocked(long currentTimeMillis) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 310 | checkNotNull(mRotator, "missing FileRotator"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 311 | if (mPending.isDirty()) { |
| 312 | if (LOGD) Slog.d(TAG, "forcePersistLocked() writing for " + mCookie); |
| 313 | try { |
| 314 | mRotator.rewriteActive(mPendingRewriter, currentTimeMillis); |
| 315 | mRotator.maybeRotate(currentTimeMillis); |
| 316 | mPending.reset(); |
| 317 | } catch (IOException e) { |
| 318 | Log.wtf(TAG, "problem persisting pending stats", e); |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 319 | recoverFromWtf(); |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 320 | } catch (OutOfMemoryError e) { |
| 321 | Log.wtf(TAG, "problem persisting pending stats", e); |
| 322 | recoverFromWtf(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Remove the given UID from all {@link FileRotator} history, migrating it |
| 329 | * to {@link TrafficStats#UID_REMOVED}. |
| 330 | */ |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 331 | public void removeUidsLocked(int[] uids) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 332 | if (mRotator != null) { |
| 333 | try { |
| 334 | // Rewrite all persisted data to migrate UID stats |
| 335 | mRotator.rewriteAll(new RemoveUidRewriter(mBucketDuration, uids)); |
| 336 | } catch (IOException e) { |
| 337 | Log.wtf(TAG, "problem removing UIDs " + Arrays.toString(uids), e); |
| 338 | recoverFromWtf(); |
| 339 | } catch (OutOfMemoryError e) { |
| 340 | Log.wtf(TAG, "problem removing UIDs " + Arrays.toString(uids), e); |
| 341 | recoverFromWtf(); |
| 342 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 345 | // Remove any pending stats |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 346 | if (mPending != null) { |
| 347 | mPending.removeUids(uids); |
| 348 | } |
| 349 | if (mSinceBoot != null) { |
| 350 | mSinceBoot.removeUids(uids); |
| 351 | } |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 352 | |
| 353 | // Clear UID from current stats snapshot |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 354 | if (mLastSnapshot != null) { |
junyulai | 8b8684a | 2018-10-29 22:26:22 +0800 | [diff] [blame] | 355 | mLastSnapshot.removeUids(uids); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 356 | } |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 357 | |
| 358 | final NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null; |
| 359 | if (complete != null) { |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 360 | complete.removeUids(uids); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 361 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Rewriter that will combine current {@link NetworkStatsCollection} values |
| 366 | * with anything read from disk, and write combined set to disk. Clears the |
| 367 | * original {@link NetworkStatsCollection} when finished writing. |
| 368 | */ |
| 369 | private static class CombiningRewriter implements FileRotator.Rewriter { |
| 370 | private final NetworkStatsCollection mCollection; |
| 371 | |
| 372 | public CombiningRewriter(NetworkStatsCollection collection) { |
| 373 | mCollection = checkNotNull(collection, "missing NetworkStatsCollection"); |
| 374 | } |
| 375 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 376 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 377 | public void reset() { |
| 378 | // ignored |
| 379 | } |
| 380 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 381 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 382 | public void read(InputStream in) throws IOException { |
| 383 | mCollection.read(in); |
| 384 | } |
| 385 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 386 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 387 | public boolean shouldWrite() { |
| 388 | return true; |
| 389 | } |
| 390 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 391 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 392 | public void write(OutputStream out) throws IOException { |
| 393 | mCollection.write(new DataOutputStream(out)); |
| 394 | mCollection.reset(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Rewriter that will remove any {@link NetworkStatsHistory} attributed to |
| 400 | * the requested UID, only writing data back when modified. |
| 401 | */ |
| 402 | public static class RemoveUidRewriter implements FileRotator.Rewriter { |
| 403 | private final NetworkStatsCollection mTemp; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 404 | private final int[] mUids; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 405 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 406 | public RemoveUidRewriter(long bucketDuration, int[] uids) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 407 | mTemp = new NetworkStatsCollection(bucketDuration); |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 408 | mUids = uids; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 411 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 412 | public void reset() { |
| 413 | mTemp.reset(); |
| 414 | } |
| 415 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 416 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 417 | public void read(InputStream in) throws IOException { |
| 418 | mTemp.read(in); |
| 419 | mTemp.clearDirty(); |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 420 | mTemp.removeUids(mUids); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 423 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 424 | public boolean shouldWrite() { |
| 425 | return mTemp.isDirty(); |
| 426 | } |
| 427 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 428 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 429 | public void write(OutputStream out) throws IOException { |
| 430 | mTemp.write(new DataOutputStream(out)); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | public void importLegacyNetworkLocked(File file) throws IOException { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 435 | checkNotNull(mRotator, "missing FileRotator"); |
| 436 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 437 | // legacy file still exists; start empty to avoid double importing |
| 438 | mRotator.deleteAll(); |
| 439 | |
| 440 | final NetworkStatsCollection collection = new NetworkStatsCollection(mBucketDuration); |
| 441 | collection.readLegacyNetwork(file); |
| 442 | |
| 443 | final long startMillis = collection.getStartMillis(); |
| 444 | final long endMillis = collection.getEndMillis(); |
| 445 | |
| 446 | if (!collection.isEmpty()) { |
| 447 | // process legacy data, creating active file at starting time, then |
| 448 | // using end time to possibly trigger rotation. |
| 449 | mRotator.rewriteActive(new CombiningRewriter(collection), startMillis); |
| 450 | mRotator.maybeRotate(endMillis); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | public void importLegacyUidLocked(File file) throws IOException { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 455 | checkNotNull(mRotator, "missing FileRotator"); |
| 456 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 457 | // legacy file still exists; start empty to avoid double importing |
| 458 | mRotator.deleteAll(); |
| 459 | |
| 460 | final NetworkStatsCollection collection = new NetworkStatsCollection(mBucketDuration); |
| 461 | collection.readLegacyUid(file, mOnlyTags); |
| 462 | |
| 463 | final long startMillis = collection.getStartMillis(); |
| 464 | final long endMillis = collection.getEndMillis(); |
| 465 | |
| 466 | if (!collection.isEmpty()) { |
| 467 | // process legacy data, creating active file at starting time, then |
| 468 | // using end time to possibly trigger rotation. |
| 469 | mRotator.rewriteActive(new CombiningRewriter(collection), startMillis); |
| 470 | mRotator.maybeRotate(endMillis); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | public void dumpLocked(IndentingPrintWriter pw, boolean fullHistory) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 475 | if (mPending != null) { |
| 476 | pw.print("Pending bytes: "); pw.println(mPending.getTotalBytes()); |
| 477 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 478 | if (fullHistory) { |
| 479 | pw.println("Complete history:"); |
| 480 | getOrLoadCompleteLocked().dump(pw); |
| 481 | } else { |
| 482 | pw.println("History since boot:"); |
| 483 | mSinceBoot.dump(pw); |
| 484 | } |
| 485 | } |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 486 | |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 487 | public void writeToProtoLocked(ProtoOutputStream proto, long tag) { |
| 488 | final long start = proto.start(tag); |
| 489 | if (mPending != null) { |
| 490 | proto.write(NetworkStatsRecorderProto.PENDING_TOTAL_BYTES, mPending.getTotalBytes()); |
| 491 | } |
| 492 | getOrLoadCompleteLocked().writeToProto(proto, NetworkStatsRecorderProto.COMPLETE_HISTORY); |
| 493 | proto.end(start); |
| 494 | } |
| 495 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 496 | public void dumpCheckin(PrintWriter pw, long start, long end) { |
| 497 | // Only load and dump stats from the requested window |
| 498 | getOrLoadPartialLocked(start, end).dumpCheckin(pw, start, end); |
| 499 | } |
| 500 | |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 501 | /** |
| 502 | * Recover from {@link FileRotator} failure by dumping state to |
| 503 | * {@link DropBoxManager} and deleting contents. |
| 504 | */ |
| 505 | private void recoverFromWtf() { |
| 506 | if (DUMP_BEFORE_DELETE) { |
| 507 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 508 | try { |
| 509 | mRotator.dumpAll(os); |
| 510 | } catch (IOException e) { |
| 511 | // ignore partial contents |
| 512 | os.reset(); |
| 513 | } finally { |
| 514 | IoUtils.closeQuietly(os); |
| 515 | } |
| 516 | mDropBox.addData(TAG_NETSTATS_DUMP, os.toByteArray(), 0); |
| 517 | } |
| 518 | |
| 519 | mRotator.deleteAll(); |
| 520 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 521 | } |