blob: 2137c3a30aa5f2368fae869ccd6f7c9d95eb77fa [file] [log] [blame]
Ken Shirrifff7d0b012009-12-07 15:56:05 -08001/*
2 * Copyright (C) 2007 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 android.net;
18
Christopher Tatea2496de2014-08-06 14:19:56 -070019import android.annotation.SystemApi;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070020import android.app.DownloadManager;
21import android.app.backup.BackupManager;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070022import android.content.Context;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070023import android.media.MediaPlayer;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070024import android.os.RemoteException;
25import android.os.ServiceManager;
26
Jesse Wilson8568db52011-06-28 19:06:31 -070027import com.android.server.NetworkManagementSocketTagger;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080028
Jesse Wilson8568db52011-06-28 19:06:31 -070029import dalvik.system.SocketTagger;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070030
Jeff Sharkey43be1742011-04-21 18:45:43 -070031import java.net.Socket;
32import java.net.SocketException;
Ken Shirrifff7d0b012009-12-07 15:56:05 -080033
34/**
Dan Egnor2b4abcd2010-04-07 17:30:50 -070035 * Class that provides network traffic statistics. These statistics include
36 * bytes transmitted and received and network packets transmitted and received,
37 * over all interfaces, over the mobile interface, and on a per-UID basis.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080038 * <p>
Dan Egnor2b4abcd2010-04-07 17:30:50 -070039 * These statistics may not be available on all platforms. If the statistics
40 * are not supported by this device, {@link #UNSUPPORTED} will be returned.
Ken Shirrifff7d0b012009-12-07 15:56:05 -080041 */
42public class TrafficStats {
43 /**
44 * The return value to indicate that the device does not support the statistic.
45 */
46 public final static int UNSUPPORTED = -1;
47
Jeff Sharkey241dde22012-02-03 14:50:07 -080048 /** @hide */
49 public static final long KB_IN_BYTES = 1024;
50 /** @hide */
51 public static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
52 /** @hide */
53 public static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
Jeff Sharkey48877892015-03-18 11:27:19 -070054 /** @hide */
55 public static final long TB_IN_BYTES = GB_IN_BYTES * 1024;
Jeff Sharkeyb521fea2015-06-15 21:09:10 -070056 /** @hide */
57 public static final long PB_IN_BYTES = TB_IN_BYTES * 1024;
Jeff Sharkey241dde22012-02-03 14:50:07 -080058
Jeff Sharkeyd2a45872011-05-28 20:56:34 -070059 /**
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070060 * Special UID value used when collecting {@link NetworkStatsHistory} for
61 * removed applications.
62 *
63 * @hide
64 */
65 public static final int UID_REMOVED = -4;
66
67 /**
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070068 * Special UID value used when collecting {@link NetworkStatsHistory} for
69 * tethering traffic.
70 *
71 * @hide
72 */
73 public static final int UID_TETHERING = -5;
74
75 /**
Jeff Sharkey4414cea2011-06-24 17:05:24 -070076 * Default tag value for {@link DownloadManager} traffic.
77 *
78 * @hide
79 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070080 public static final int TAG_SYSTEM_DOWNLOAD = 0xFFFFFF01;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070081
82 /**
83 * Default tag value for {@link MediaPlayer} traffic.
84 *
85 * @hide
86 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070087 public static final int TAG_SYSTEM_MEDIA = 0xFFFFFF02;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070088
89 /**
Christopher Tate0a61b362015-11-10 10:49:20 -080090 * Default tag value for {@link BackupManager} backup traffic; that is,
91 * traffic from the device to the storage backend.
Jeff Sharkey4414cea2011-06-24 17:05:24 -070092 *
93 * @hide
94 */
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -070095 public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070096
Christopher Tate0a61b362015-11-10 10:49:20 -080097 /**
98 * Default tag value for {@link BackupManager} restore traffic; that is,
99 * app data retrieved from the storage backend at install time.
100 *
101 * @hide
102 */
103 public static final int TAG_SYSTEM_RESTORE = 0xFFFFFF04;
104
Jeff Sharkey234766a2012-04-10 19:48:07 -0700105 private static INetworkStatsService sStatsService;
106
107 private synchronized static INetworkStatsService getStatsService() {
108 if (sStatsService == null) {
109 sStatsService = INetworkStatsService.Stub.asInterface(
110 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
111 }
112 return sStatsService;
113 }
114
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700115 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700116 * Snapshot of {@link NetworkStats} when the currently active profiling
117 * session started, or {@code null} if no session active.
118 *
119 * @see #startDataProfiling(Context)
120 * @see #stopDataProfiling(Context)
121 */
122 private static NetworkStats sActiveProfilingStart;
123
124 private static Object sProfilingLock = new Object();
125
126 /**
Jeff Sharkey43be1742011-04-21 18:45:43 -0700127 * Set active tag to use when accounting {@link Socket} traffic originating
128 * from the current thread. Only one active tag per thread is supported.
129 * <p>
130 * Changes only take effect during subsequent calls to
131 * {@link #tagSocket(Socket)}.
Jeff Sharkeycdd02c5d2011-09-16 01:52:49 -0700132 * <p>
133 * Tags between {@code 0xFFFFFF00} and {@code 0xFFFFFFFF} are reserved and
134 * used internally by system services like {@link DownloadManager} when
135 * performing traffic on behalf of an application.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700136 *
137 * @see #clearThreadStatsTag()
Jeff Sharkey43be1742011-04-21 18:45:43 -0700138 */
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700139 public static void setThreadStatsTag(int tag) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700140 NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700141 }
142
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700143 /**
Christopher Tatea2496de2014-08-06 14:19:56 -0700144 * System API for backup-related support components to tag network traffic
145 * appropriately.
146 * @hide
147 */
148 @SystemApi
149 public static void setThreadStatsTagBackup() {
150 setThreadStatsTag(TAG_SYSTEM_BACKUP);
151 }
152
153 /**
Christopher Tate0a61b362015-11-10 10:49:20 -0800154 * System API for restore-related support components to tag network traffic
155 * appropriately.
156 * @hide
157 */
158 @SystemApi
159 public static void setThreadStatsTagRestore() {
160 setThreadStatsTag(TAG_SYSTEM_RESTORE);
161 }
162
163 /**
Alon Alberteaef3512011-07-19 11:16:09 +0300164 * Get the active tag used when accounting {@link Socket} traffic originating
165 * from the current thread. Only one active tag per thread is supported.
166 * {@link #tagSocket(Socket)}.
Jeff Sharkeydddace72013-03-26 13:46:05 -0700167 *
168 * @see #setThreadStatsTag(int)
Alon Alberteaef3512011-07-19 11:16:09 +0300169 */
170 public static int getThreadStatsTag() {
171 return NetworkManagementSocketTagger.getThreadSocketStatsTag();
172 }
173
Jeff Sharkeydddace72013-03-26 13:46:05 -0700174 /**
175 * Clear any active tag set to account {@link Socket} traffic originating
176 * from the current thread.
177 *
178 * @see #setThreadStatsTag(int)
179 */
Jeff Sharkey43be1742011-04-21 18:45:43 -0700180 public static void clearThreadStatsTag() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700181 NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700182 }
183
184 /**
185 * Set specific UID to use when accounting {@link Socket} traffic
186 * originating from the current thread. Designed for use when performing an
187 * operation on behalf of another application.
188 * <p>
189 * Changes only take effect during subsequent calls to
190 * {@link #tagSocket(Socket)}.
191 * <p>
192 * To take effect, caller must hold
193 * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
194 *
Jeff Sharkeydddace72013-03-26 13:46:05 -0700195 * @hide
Jeff Sharkey43be1742011-04-21 18:45:43 -0700196 */
Christopher Tatea2496de2014-08-06 14:19:56 -0700197 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700198 public static void setThreadStatsUid(int uid) {
Jesse Wilson8568db52011-06-28 19:06:31 -0700199 NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700200 }
201
202 /** {@hide} */
Christopher Tatea2496de2014-08-06 14:19:56 -0700203 @SystemApi
Jeff Sharkey43be1742011-04-21 18:45:43 -0700204 public static void clearThreadStatsUid() {
Jesse Wilson8568db52011-06-28 19:06:31 -0700205 NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700206 }
207
208 /**
209 * Tag the given {@link Socket} with any statistics parameters active for
210 * the current thread. Subsequent calls always replace any existing
211 * parameters. When finished, call {@link #untagSocket(Socket)} to remove
212 * statistics parameters.
213 *
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700214 * @see #setThreadStatsTag(int)
Jeff Sharkey43be1742011-04-21 18:45:43 -0700215 * @see #setThreadStatsUid(int)
216 */
217 public static void tagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700218 SocketTagger.get().tag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700219 }
220
221 /**
222 * Remove any statistics parameters from the given {@link Socket}.
223 */
224 public static void untagSocket(Socket socket) throws SocketException {
Jesse Wilson8568db52011-06-28 19:06:31 -0700225 SocketTagger.get().untag(socket);
Jeff Sharkey43be1742011-04-21 18:45:43 -0700226 }
227
228 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700229 * Start profiling data usage for current UID. Only one profiling session
230 * can be active at a time.
231 *
232 * @hide
233 */
234 public static void startDataProfiling(Context context) {
235 synchronized (sProfilingLock) {
236 if (sActiveProfilingStart != null) {
237 throw new IllegalStateException("already profiling data");
238 }
239
240 // take snapshot in time; we calculate delta later
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700241 sActiveProfilingStart = getDataLayerSnapshotForUid(context);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700242 }
243 }
244
245 /**
246 * Stop profiling data usage for current UID.
247 *
248 * @return Detailed {@link NetworkStats} of data that occurred since last
249 * {@link #startDataProfiling(Context)} call.
250 * @hide
251 */
252 public static NetworkStats stopDataProfiling(Context context) {
253 synchronized (sProfilingLock) {
254 if (sActiveProfilingStart == null) {
255 throw new IllegalStateException("not profiling data");
256 }
257
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800258 // subtract starting values and return delta
259 final NetworkStats profilingStop = getDataLayerSnapshotForUid(context);
260 final NetworkStats profilingDelta = NetworkStats.subtract(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800261 profilingStop, sActiveProfilingStart, null, null);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800262 sActiveProfilingStart = null;
263 return profilingDelta;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700264 }
265 }
266
267 /**
Jeff Sharkey558a2322011-08-24 15:42:09 -0700268 * Increment count of network operations performed under the accounting tag
269 * currently active on the calling thread. This can be used to derive
270 * bytes-per-operation.
271 *
272 * @param operationCount Number of operations to increment count by.
273 */
274 public static void incrementOperationCount(int operationCount) {
275 final int tag = getThreadStatsTag();
276 incrementOperationCount(tag, operationCount);
277 }
278
279 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700280 * Increment count of network operations performed under the given
281 * accounting tag. This can be used to derive bytes-per-operation.
282 *
283 * @param tag Accounting tag used in {@link #setThreadStatsTag(int)}.
284 * @param operationCount Number of operations to increment count by.
285 */
286 public static void incrementOperationCount(int tag, int operationCount) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700287 final int uid = android.os.Process.myUid();
288 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700289 getStatsService().incrementOperationCount(uid, tag, operationCount);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700290 } catch (RemoteException e) {
291 throw new RuntimeException(e);
292 }
293 }
294
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700295 /** {@hide} */
296 public static void closeQuietly(INetworkStatsSession session) {
297 // TODO: move to NetworkStatsService once it exists
298 if (session != null) {
299 try {
300 session.close();
301 } catch (RuntimeException rethrown) {
302 throw rethrown;
303 } catch (Exception ignored) {
304 }
305 }
306 }
307
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700308 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700309 * Return number of packets transmitted across mobile networks since device
310 * boot. Counts packets across all mobile network interfaces, and always
311 * increases monotonically since device boot. Statistics are measured at the
312 * network layer, so they include both TCP and UDP usage.
313 * <p>
314 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
315 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800316 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700317 public static long getMobileTxPackets() {
318 long total = 0;
319 for (String iface : getMobileIfaces()) {
320 total += getTxPackets(iface);
321 }
322 return total;
323 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800324
325 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700326 * Return number of packets received across mobile networks since device
327 * boot. Counts packets across all mobile network interfaces, and always
328 * increases monotonically since device boot. Statistics are measured at the
329 * network layer, so they include both TCP and UDP usage.
330 * <p>
331 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
332 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800333 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700334 public static long getMobileRxPackets() {
335 long total = 0;
336 for (String iface : getMobileIfaces()) {
337 total += getRxPackets(iface);
338 }
339 return total;
340 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800341
342 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700343 * Return number of bytes transmitted across mobile networks since device
344 * boot. Counts packets across all mobile network interfaces, and always
345 * increases monotonically since device boot. Statistics are measured at the
346 * network layer, so they include both TCP and UDP usage.
347 * <p>
348 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
349 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800350 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700351 public static long getMobileTxBytes() {
352 long total = 0;
353 for (String iface : getMobileIfaces()) {
354 total += getTxBytes(iface);
355 }
356 return total;
357 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800358
359 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700360 * Return number of bytes received across mobile networks since device boot.
361 * Counts packets across all mobile network interfaces, and always increases
362 * monotonically since device boot. Statistics are measured at the network
363 * layer, so they include both TCP and UDP usage.
364 * <p>
365 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
366 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800367 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700368 public static long getMobileRxBytes() {
369 long total = 0;
370 for (String iface : getMobileIfaces()) {
371 total += getRxBytes(iface);
372 }
373 return total;
374 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800375
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800376 /** {@hide} */
377 public static long getMobileTcpRxPackets() {
378 long total = 0;
379 for (String iface : getMobileIfaces()) {
380 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_RX_PACKETS);
381 if (stat != UNSUPPORTED) {
382 total += stat;
383 }
384 }
385 return total;
386 }
387
388 /** {@hide} */
389 public static long getMobileTcpTxPackets() {
390 long total = 0;
391 for (String iface : getMobileIfaces()) {
392 final long stat = nativeGetIfaceStat(iface, TYPE_TCP_TX_PACKETS);
393 if (stat != UNSUPPORTED) {
394 total += stat;
395 }
396 }
397 return total;
398 }
399
Jeff Sharkeydddace72013-03-26 13:46:05 -0700400 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700401 public static long getTxPackets(String iface) {
402 return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
403 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800404
Jeff Sharkeydddace72013-03-26 13:46:05 -0700405 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700406 public static long getRxPackets(String iface) {
407 return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
408 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800409
Jeff Sharkeydddace72013-03-26 13:46:05 -0700410 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700411 public static long getTxBytes(String iface) {
412 return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
413 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800414
Jeff Sharkeydddace72013-03-26 13:46:05 -0700415 /** {@hide} */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700416 public static long getRxBytes(String iface) {
417 return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
418 }
Irfan Sheriff227bec42011-02-15 19:30:27 -0800419
420 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700421 * Return number of packets transmitted since device boot. Counts packets
422 * across all network interfaces, and always increases monotonically since
423 * device boot. Statistics are measured at the network layer, so they
424 * include both TCP and UDP usage.
425 * <p>
426 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
427 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800428 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700429 public static long getTotalTxPackets() {
430 return nativeGetTotalStat(TYPE_TX_PACKETS);
431 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800432
433 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700434 * Return number of packets received since device boot. Counts packets
435 * across all network interfaces, and always increases monotonically since
436 * device boot. Statistics are measured at the network layer, so they
437 * include both TCP and UDP usage.
438 * <p>
439 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
440 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800441 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700442 public static long getTotalRxPackets() {
443 return nativeGetTotalStat(TYPE_RX_PACKETS);
444 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800445
446 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700447 * Return number of bytes transmitted since device boot. Counts packets
448 * across all network interfaces, and always increases monotonically since
449 * device boot. Statistics are measured at the network layer, so they
450 * include both TCP and UDP usage.
451 * <p>
452 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
453 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800454 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700455 public static long getTotalTxBytes() {
456 return nativeGetTotalStat(TYPE_TX_BYTES);
457 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800458
459 /**
Jeff Sharkeydddace72013-03-26 13:46:05 -0700460 * Return number of bytes received since device boot. Counts packets across
461 * all network interfaces, and always increases monotonically since device
462 * boot. Statistics are measured at the network layer, so they include both
463 * TCP and UDP usage.
464 * <p>
465 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may
466 * return {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800467 */
Jeff Sharkey234766a2012-04-10 19:48:07 -0700468 public static long getTotalRxBytes() {
469 return nativeGetTotalStat(TYPE_RX_BYTES);
470 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800471
472 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800473 * Return number of bytes transmitted by the given UID since device boot.
474 * Counts packets across all network interfaces, and always increases
475 * monotonically since device boot. Statistics are measured at the network
476 * layer, so they include both TCP and UDP usage.
477 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800478 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800479 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800480 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800481 * @see android.os.Process#myUid()
482 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800483 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800484 public static long getUidTxBytes(int uid) {
485 return nativeGetUidStat(uid, TYPE_TX_BYTES);
486 }
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800487
488 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800489 * Return number of bytes received by the given UID since device boot.
490 * Counts packets across all network interfaces, and always increases
491 * monotonically since device boot. Statistics are measured at the network
492 * layer, so they include both TCP and UDP usage.
493 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800494 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800495 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800496 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800497 * @see android.os.Process#myUid()
498 * @see android.content.pm.ApplicationInfo#uid
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800499 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800500 public static long getUidRxBytes(int uid) {
501 return nativeGetUidStat(uid, TYPE_RX_BYTES);
502 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800503
504 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800505 * Return number of packets transmitted by the given UID since device boot.
506 * Counts packets across all network interfaces, and always increases
507 * monotonically since device boot. Statistics are measured at the network
508 * layer, so they include both TCP and UDP usage.
509 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800510 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800511 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800512 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800513 * @see android.os.Process#myUid()
514 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800515 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800516 public static long getUidTxPackets(int uid) {
517 return nativeGetUidStat(uid, TYPE_TX_PACKETS);
518 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800519
520 /**
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800521 * Return number of packets received by the given UID since device boot.
522 * Counts packets across all network interfaces, and always increases
523 * monotonically since device boot. Statistics are measured at the network
524 * layer, so they include both TCP and UDP usage.
525 * <p>
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800526 * Before {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, this may return
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800527 * {@link #UNSUPPORTED} on devices where statistics aren't available.
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800528 *
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800529 * @see android.os.Process#myUid()
530 * @see android.content.pm.ApplicationInfo#uid
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800531 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800532 public static long getUidRxPackets(int uid) {
533 return nativeGetUidStat(uid, TYPE_RX_PACKETS);
534 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800535
536 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800537 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800538 * transport layer statistics are no longer available, and will
539 * always return {@link #UNSUPPORTED}.
540 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800541 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800542 @Deprecated
543 public static long getUidTcpTxBytes(int uid) {
544 return UNSUPPORTED;
545 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800546
547 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800548 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800549 * transport layer statistics are no longer available, and will
550 * always return {@link #UNSUPPORTED}.
551 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800552 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800553 @Deprecated
554 public static long getUidTcpRxBytes(int uid) {
555 return UNSUPPORTED;
556 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800557
558 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800559 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800560 * transport layer statistics are no longer available, and will
561 * always return {@link #UNSUPPORTED}.
562 * @see #getUidTxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800563 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800564 @Deprecated
565 public static long getUidUdpTxBytes(int uid) {
566 return UNSUPPORTED;
567 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800568
569 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800570 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800571 * transport layer statistics are no longer available, and will
572 * always return {@link #UNSUPPORTED}.
573 * @see #getUidRxBytes(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800574 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800575 @Deprecated
576 public static long getUidUdpRxBytes(int uid) {
577 return UNSUPPORTED;
578 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800579
580 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800581 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800582 * transport layer statistics are no longer available, and will
583 * always return {@link #UNSUPPORTED}.
584 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800585 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800586 @Deprecated
587 public static long getUidTcpTxSegments(int uid) {
588 return UNSUPPORTED;
589 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800590
591 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800592 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800593 * transport layer statistics are no longer available, and will
594 * always return {@link #UNSUPPORTED}.
595 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800596 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800597 @Deprecated
598 public static long getUidTcpRxSegments(int uid) {
599 return UNSUPPORTED;
600 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800601
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800602 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800603 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800604 * transport layer statistics are no longer available, and will
605 * always return {@link #UNSUPPORTED}.
606 * @see #getUidTxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800607 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800608 @Deprecated
609 public static long getUidUdpTxPackets(int uid) {
610 return UNSUPPORTED;
611 }
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800612
613 /**
Dianne Hackborn45e9ede2013-02-25 15:55:37 -0800614 * @deprecated Starting in {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800615 * transport layer statistics are no longer available, and will
616 * always return {@link #UNSUPPORTED}.
617 * @see #getUidRxPackets(int)
Ashish Sharmac39c1d42011-01-27 15:52:38 -0800618 */
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800619 @Deprecated
620 public static long getUidUdpRxPackets(int uid) {
621 return UNSUPPORTED;
622 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700623
624 /**
625 * Return detailed {@link NetworkStats} for the current UID. Requires no
626 * special permission.
627 */
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700628 private static NetworkStats getDataLayerSnapshotForUid(Context context) {
Jeff Sharkeydddace72013-03-26 13:46:05 -0700629 // TODO: take snapshot locally, since proc file is now visible
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700630 final int uid = android.os.Process.myUid();
631 try {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700632 return getStatsService().getDataLayerSnapshotForUid(uid);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700633 } catch (RemoteException e) {
634 throw new RuntimeException(e);
635 }
636 }
Jeff Sharkey234766a2012-04-10 19:48:07 -0700637
638 /**
639 * Return set of any ifaces associated with mobile networks since boot.
640 * Interfaces are never removed from this list, so counters should always be
641 * monotonic.
642 */
643 private static String[] getMobileIfaces() {
644 try {
645 return getStatsService().getMobileIfaces();
646 } catch (RemoteException e) {
647 throw new RuntimeException(e);
648 }
649 }
650
651 // NOTE: keep these in sync with android_net_TrafficStats.cpp
652 private static final int TYPE_RX_BYTES = 0;
653 private static final int TYPE_RX_PACKETS = 1;
654 private static final int TYPE_TX_BYTES = 2;
655 private static final int TYPE_TX_PACKETS = 3;
Jeff Sharkey4b17a132013-02-05 21:32:33 -0800656 private static final int TYPE_TCP_RX_PACKETS = 4;
657 private static final int TYPE_TCP_TX_PACKETS = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -0700658
659 private static native long nativeGetTotalStat(int type);
660 private static native long nativeGetIfaceStat(String iface, int type);
Jeff Sharkey92be93a2013-01-15 17:25:09 -0800661 private static native long nativeGetUidStat(int uid, int type);
Ken Shirrifff7d0b012009-12-07 15:56:05 -0800662}