blob: 2b32b41506172c41075eeee9b6478d2913a03bca [file] [log] [blame]
Jeff Sharkey63abc372012-01-11 18:38:16 -08001/*
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
17package com.android.server.net;
18
19import static android.net.NetworkStats.TAG_NONE;
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -070020import static android.net.TrafficStats.KB_IN_BYTES;
21import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkey63abc372012-01-11 18:38:16 -080022import static com.android.internal.util.Preconditions.checkNotNull;
23
24import android.net.NetworkStats;
25import android.net.NetworkStats.NonMonotonicObserver;
26import android.net.NetworkStatsHistory;
27import android.net.NetworkTemplate;
28import android.net.TrafficStats;
Jeff Sharkey6de357e2012-05-09 13:33:52 -070029import android.os.DropBoxManager;
Jeff Sharkey63abc372012-01-11 18:38:16 -080030import android.util.Log;
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -070031import android.util.MathUtils;
Jeff Sharkey63abc372012-01-11 18:38:16 -080032import android.util.Slog;
33
34import com.android.internal.util.FileRotator;
35import com.android.internal.util.IndentingPrintWriter;
36import com.google.android.collect.Sets;
37
Jeff Sharkey6de357e2012-05-09 13:33:52 -070038import java.io.ByteArrayOutputStream;
Jeff Sharkey63abc372012-01-11 18:38:16 -080039import java.io.DataOutputStream;
40import java.io.File;
41import java.io.IOException;
42import java.io.InputStream;
43import java.io.OutputStream;
44import java.lang.ref.WeakReference;
Jeff Sharkeydaa57e82012-09-19 14:10:39 -070045import java.util.Arrays;
Jeff Sharkey63abc372012-01-11 18:38:16 -080046import java.util.HashSet;
47import java.util.Map;
48
Jeff Sharkey6de357e2012-05-09 13:33:52 -070049import libcore.io.IoUtils;
50
Jeff Sharkey63abc372012-01-11 18:38:16 -080051/**
52 * Logic to record deltas between periodic {@link NetworkStats} snapshots into
53 * {@link NetworkStatsHistory} that belong to {@link NetworkStatsCollection}.
54 * Keeps pending changes in memory until they pass a specific threshold, in
55 * bytes. Uses {@link FileRotator} for persistence logic.
56 * <p>
57 * Not inherently thread safe.
58 */
59public class NetworkStatsRecorder {
60 private static final String TAG = "NetworkStatsRecorder";
Jeff Sharkeye7bb71d2012-02-28 15:13:08 -080061 private static final boolean LOGD = false;
Jeff Sharkey1f8ea2d2012-02-07 12:05:43 -080062 private static final boolean LOGV = false;
Jeff Sharkey63abc372012-01-11 18:38:16 -080063
Jeff Sharkey6de357e2012-05-09 13:33:52 -070064 private static final String TAG_NETSTATS_DUMP = "netstats_dump";
65
66 /** Dump before deleting in {@link #recoverFromWtf()}. */
67 private static final boolean DUMP_BEFORE_DELETE = true;
68
Jeff Sharkey63abc372012-01-11 18:38:16 -080069 private final FileRotator mRotator;
70 private final NonMonotonicObserver<String> mObserver;
Jeff Sharkey6de357e2012-05-09 13:33:52 -070071 private final DropBoxManager mDropBox;
Jeff Sharkey63abc372012-01-11 18:38:16 -080072 private final String mCookie;
73
74 private final long mBucketDuration;
Jeff Sharkey63abc372012-01-11 18:38:16 -080075 private final boolean mOnlyTags;
76
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -070077 private long mPersistThresholdBytes = 2 * MB_IN_BYTES;
Jeff Sharkey63abc372012-01-11 18:38:16 -080078 private NetworkStats mLastSnapshot;
79
80 private final NetworkStatsCollection mPending;
81 private final NetworkStatsCollection mSinceBoot;
82
83 private final CombiningRewriter mPendingRewriter;
84
85 private WeakReference<NetworkStatsCollection> mComplete;
86
87 public NetworkStatsRecorder(FileRotator rotator, NonMonotonicObserver<String> observer,
Jeff Sharkey6de357e2012-05-09 13:33:52 -070088 DropBoxManager dropBox, String cookie, long bucketDuration, boolean onlyTags) {
Jeff Sharkey63abc372012-01-11 18:38:16 -080089 mRotator = checkNotNull(rotator, "missing FileRotator");
90 mObserver = checkNotNull(observer, "missing NonMonotonicObserver");
Jeff Sharkey6de357e2012-05-09 13:33:52 -070091 mDropBox = checkNotNull(dropBox, "missing DropBoxManager");
Jeff Sharkey63abc372012-01-11 18:38:16 -080092 mCookie = cookie;
93
94 mBucketDuration = bucketDuration;
Jeff Sharkey63abc372012-01-11 18:38:16 -080095 mOnlyTags = onlyTags;
96
97 mPending = new NetworkStatsCollection(bucketDuration);
98 mSinceBoot = new NetworkStatsCollection(bucketDuration);
99
100 mPendingRewriter = new CombiningRewriter(mPending);
101 }
102
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700103 public void setPersistThreshold(long thresholdBytes) {
104 if (LOGV) Slog.v(TAG, "setPersistThreshold() with " + thresholdBytes);
105 mPersistThresholdBytes = MathUtils.constrain(
106 thresholdBytes, 1 * KB_IN_BYTES, 100 * MB_IN_BYTES);
107 }
108
Jeff Sharkey63abc372012-01-11 18:38:16 -0800109 public void resetLocked() {
110 mLastSnapshot = null;
111 mPending.reset();
112 mSinceBoot.reset();
113 mComplete.clear();
114 }
115
116 public NetworkStats.Entry getTotalSinceBootLocked(NetworkTemplate template) {
117 return mSinceBoot.getSummary(template, Long.MIN_VALUE, Long.MAX_VALUE).getTotal(null);
118 }
119
120 /**
121 * Load complete history represented by {@link FileRotator}. Caches
122 * internally as a {@link WeakReference}, and updated with future
123 * {@link #recordSnapshotLocked(NetworkStats, Map, long)} snapshots as long
124 * as reference is valid.
125 */
126 public NetworkStatsCollection getOrLoadCompleteLocked() {
127 NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null;
128 if (complete == null) {
129 if (LOGD) Slog.d(TAG, "getOrLoadCompleteLocked() reading from disk for " + mCookie);
130 try {
131 complete = new NetworkStatsCollection(mBucketDuration);
132 mRotator.readMatching(complete, Long.MIN_VALUE, Long.MAX_VALUE);
133 complete.recordCollection(mPending);
134 mComplete = new WeakReference<NetworkStatsCollection>(complete);
135 } catch (IOException e) {
136 Log.wtf(TAG, "problem completely reading network stats", e);
Jeff Sharkey6de357e2012-05-09 13:33:52 -0700137 recoverFromWtf();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800138 }
139 }
140 return complete;
141 }
142
143 /**
144 * Record any delta that occurred since last {@link NetworkStats} snapshot,
145 * using the given {@link Map} to identify network interfaces. First
146 * snapshot is considered bootstrap, and is not counted as delta.
147 */
148 public void recordSnapshotLocked(NetworkStats snapshot,
149 Map<String, NetworkIdentitySet> ifaceIdent, long currentTimeMillis) {
150 final HashSet<String> unknownIfaces = Sets.newHashSet();
151
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700152 // skip recording when snapshot missing
153 if (snapshot == null) return;
154
Jeff Sharkey63abc372012-01-11 18:38:16 -0800155 // assume first snapshot is bootstrap and don't record
156 if (mLastSnapshot == null) {
157 mLastSnapshot = snapshot;
158 return;
159 }
160
161 final NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null;
162
163 final NetworkStats delta = NetworkStats.subtract(
164 snapshot, mLastSnapshot, mObserver, mCookie);
165 final long end = currentTimeMillis;
166 final long start = end - delta.getElapsedRealtime();
167
168 NetworkStats.Entry entry = null;
169 for (int i = 0; i < delta.size(); i++) {
170 entry = delta.getValues(i, entry);
171 final NetworkIdentitySet ident = ifaceIdent.get(entry.iface);
172 if (ident == null) {
173 unknownIfaces.add(entry.iface);
174 continue;
175 }
176
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700177 // skip when no delta occurred
Jeff Sharkey63abc372012-01-11 18:38:16 -0800178 if (entry.isEmpty()) continue;
179
180 // only record tag data when requested
181 if ((entry.tag == TAG_NONE) != mOnlyTags) {
182 mPending.recordData(ident, entry.uid, entry.set, entry.tag, start, end, entry);
183
184 // also record against boot stats when present
185 if (mSinceBoot != null) {
186 mSinceBoot.recordData(ident, entry.uid, entry.set, entry.tag, start, end, entry);
187 }
188
189 // also record against complete dataset when present
190 if (complete != null) {
191 complete.recordData(ident, entry.uid, entry.set, entry.tag, start, end, entry);
192 }
193 }
194 }
195
196 mLastSnapshot = snapshot;
197
Jeff Sharkey1f8ea2d2012-02-07 12:05:43 -0800198 if (LOGV && unknownIfaces.size() > 0) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800199 Slog.w(TAG, "unknown interfaces " + unknownIfaces + ", ignoring those stats");
200 }
201 }
202
203 /**
204 * Consider persisting any pending deltas, if they are beyond
205 * {@link #mPersistThresholdBytes}.
206 */
207 public void maybePersistLocked(long currentTimeMillis) {
208 final long pendingBytes = mPending.getTotalBytes();
209 if (pendingBytes >= mPersistThresholdBytes) {
210 forcePersistLocked(currentTimeMillis);
211 } else {
212 mRotator.maybeRotate(currentTimeMillis);
213 }
214 }
215
216 /**
217 * Force persisting any pending deltas.
218 */
219 public void forcePersistLocked(long currentTimeMillis) {
220 if (mPending.isDirty()) {
221 if (LOGD) Slog.d(TAG, "forcePersistLocked() writing for " + mCookie);
222 try {
223 mRotator.rewriteActive(mPendingRewriter, currentTimeMillis);
224 mRotator.maybeRotate(currentTimeMillis);
225 mPending.reset();
226 } catch (IOException e) {
227 Log.wtf(TAG, "problem persisting pending stats", e);
Jeff Sharkey6de357e2012-05-09 13:33:52 -0700228 recoverFromWtf();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800229 }
230 }
231 }
232
233 /**
234 * Remove the given UID from all {@link FileRotator} history, migrating it
235 * to {@link TrafficStats#UID_REMOVED}.
236 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700237 public void removeUidsLocked(int[] uids) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800238 try {
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700239 // Rewrite all persisted data to migrate UID stats
240 mRotator.rewriteAll(new RemoveUidRewriter(mBucketDuration, uids));
Jeff Sharkey63abc372012-01-11 18:38:16 -0800241 } catch (IOException e) {
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700242 Log.wtf(TAG, "problem removing UIDs " + Arrays.toString(uids), e);
Jeff Sharkey6de357e2012-05-09 13:33:52 -0700243 recoverFromWtf();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800244 }
245
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700246 // Remove any pending stats
247 mPending.removeUids(uids);
248 mSinceBoot.removeUids(uids);
249
250 // Clear UID from current stats snapshot
Jeff Sharkey63abc372012-01-11 18:38:16 -0800251 if (mLastSnapshot != null) {
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700252 mLastSnapshot = mLastSnapshot.withoutUids(uids);
Jeff Sharkey63abc372012-01-11 18:38:16 -0800253 }
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700254
255 final NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null;
256 if (complete != null) {
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700257 complete.removeUids(uids);
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700258 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800259 }
260
261 /**
262 * Rewriter that will combine current {@link NetworkStatsCollection} values
263 * with anything read from disk, and write combined set to disk. Clears the
264 * original {@link NetworkStatsCollection} when finished writing.
265 */
266 private static class CombiningRewriter implements FileRotator.Rewriter {
267 private final NetworkStatsCollection mCollection;
268
269 public CombiningRewriter(NetworkStatsCollection collection) {
270 mCollection = checkNotNull(collection, "missing NetworkStatsCollection");
271 }
272
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700273 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800274 public void reset() {
275 // ignored
276 }
277
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700278 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800279 public void read(InputStream in) throws IOException {
280 mCollection.read(in);
281 }
282
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700283 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800284 public boolean shouldWrite() {
285 return true;
286 }
287
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700288 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800289 public void write(OutputStream out) throws IOException {
290 mCollection.write(new DataOutputStream(out));
291 mCollection.reset();
292 }
293 }
294
295 /**
296 * Rewriter that will remove any {@link NetworkStatsHistory} attributed to
297 * the requested UID, only writing data back when modified.
298 */
299 public static class RemoveUidRewriter implements FileRotator.Rewriter {
300 private final NetworkStatsCollection mTemp;
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700301 private final int[] mUids;
Jeff Sharkey63abc372012-01-11 18:38:16 -0800302
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700303 public RemoveUidRewriter(long bucketDuration, int[] uids) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800304 mTemp = new NetworkStatsCollection(bucketDuration);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700305 mUids = uids;
Jeff Sharkey63abc372012-01-11 18:38:16 -0800306 }
307
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700308 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800309 public void reset() {
310 mTemp.reset();
311 }
312
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700313 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800314 public void read(InputStream in) throws IOException {
315 mTemp.read(in);
316 mTemp.clearDirty();
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700317 mTemp.removeUids(mUids);
Jeff Sharkey63abc372012-01-11 18:38:16 -0800318 }
319
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700320 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800321 public boolean shouldWrite() {
322 return mTemp.isDirty();
323 }
324
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700325 @Override
Jeff Sharkey63abc372012-01-11 18:38:16 -0800326 public void write(OutputStream out) throws IOException {
327 mTemp.write(new DataOutputStream(out));
328 }
329 }
330
331 public void importLegacyNetworkLocked(File file) throws IOException {
332 // legacy file still exists; start empty to avoid double importing
333 mRotator.deleteAll();
334
335 final NetworkStatsCollection collection = new NetworkStatsCollection(mBucketDuration);
336 collection.readLegacyNetwork(file);
337
338 final long startMillis = collection.getStartMillis();
339 final long endMillis = collection.getEndMillis();
340
341 if (!collection.isEmpty()) {
342 // process legacy data, creating active file at starting time, then
343 // using end time to possibly trigger rotation.
344 mRotator.rewriteActive(new CombiningRewriter(collection), startMillis);
345 mRotator.maybeRotate(endMillis);
346 }
347 }
348
349 public void importLegacyUidLocked(File file) throws IOException {
350 // legacy file still exists; start empty to avoid double importing
351 mRotator.deleteAll();
352
353 final NetworkStatsCollection collection = new NetworkStatsCollection(mBucketDuration);
354 collection.readLegacyUid(file, mOnlyTags);
355
356 final long startMillis = collection.getStartMillis();
357 final long endMillis = collection.getEndMillis();
358
359 if (!collection.isEmpty()) {
360 // process legacy data, creating active file at starting time, then
361 // using end time to possibly trigger rotation.
362 mRotator.rewriteActive(new CombiningRewriter(collection), startMillis);
363 mRotator.maybeRotate(endMillis);
364 }
365 }
366
367 public void dumpLocked(IndentingPrintWriter pw, boolean fullHistory) {
368 pw.print("Pending bytes: "); pw.println(mPending.getTotalBytes());
369 if (fullHistory) {
370 pw.println("Complete history:");
371 getOrLoadCompleteLocked().dump(pw);
372 } else {
373 pw.println("History since boot:");
374 mSinceBoot.dump(pw);
375 }
376 }
Jeff Sharkey6de357e2012-05-09 13:33:52 -0700377
378 /**
379 * Recover from {@link FileRotator} failure by dumping state to
380 * {@link DropBoxManager} and deleting contents.
381 */
382 private void recoverFromWtf() {
383 if (DUMP_BEFORE_DELETE) {
384 final ByteArrayOutputStream os = new ByteArrayOutputStream();
385 try {
386 mRotator.dumpAll(os);
387 } catch (IOException e) {
388 // ignore partial contents
389 os.reset();
390 } finally {
391 IoUtils.closeQuietly(os);
392 }
393 mDropBox.addData(TAG_NETSTATS_DUMP, os.toByteArray(), 0);
394 }
395
396 mRotator.deleteAll();
397 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800398}