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