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