Skip to content

Commit 1233287

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Move TrafficStats iface counters to xt_qtaguid."
2 parents c0e5b8c + 234766a commit 1233287

File tree

5 files changed

+225
-155
lines changed

5 files changed

+225
-155
lines changed

core/java/android/net/INetworkStatsService.aidl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ interface INetworkStatsService {
3232

3333
/** Return data layer snapshot of UID network usage. */
3434
NetworkStats getDataLayerSnapshotForUid(int uid);
35+
/** Return set of any ifaces associated with mobile networks since boot. */
36+
String[] getMobileIfaces();
37+
3538
/** Increment data layer count of operations performed for UID and tag. */
3639
void incrementOperationCount(int uid, int tag, int operationCount);
3740

core/java/android/net/NetworkTemplate.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class NetworkTemplate implements Parcelable {
4848
public static final int MATCH_MOBILE_4G = 3;
4949
public static final int MATCH_WIFI = 4;
5050
public static final int MATCH_ETHERNET = 5;
51+
public static final int MATCH_MOBILE_WILDCARD = 6;
52+
public static final int MATCH_WIFI_WILDCARD = 7;
5153

5254
/**
5355
* Set of {@link NetworkInfo#getType()} that reflect data usage.
@@ -85,12 +87,20 @@ public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
8587
return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
8688
}
8789

90+
/**
91+
* Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
92+
* regardless of IMSI.
93+
*/
94+
public static NetworkTemplate buildTemplateMobileWildcard() {
95+
return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
96+
}
97+
8898
/**
8999
* Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
90100
* regardless of SSID.
91101
*/
92102
public static NetworkTemplate buildTemplateWifiWildcard() {
93-
return new NetworkTemplate(MATCH_WIFI, null, null);
103+
return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
94104
}
95105

96106
@Deprecated
@@ -198,6 +208,10 @@ public boolean matches(NetworkIdentity ident) {
198208
return matchesWifi(ident);
199209
case MATCH_ETHERNET:
200210
return matchesEthernet(ident);
211+
case MATCH_MOBILE_WILDCARD:
212+
return matchesMobileWildcard(ident);
213+
case MATCH_WIFI_WILDCARD:
214+
return matchesWifiWildcard(ident);
201215
default:
202216
throw new IllegalArgumentException("unknown network template");
203217
}
@@ -257,13 +271,7 @@ private boolean matchesMobile4g(NetworkIdentity ident) {
257271
private boolean matchesWifi(NetworkIdentity ident) {
258272
switch (ident.mType) {
259273
case TYPE_WIFI:
260-
if (mNetworkId == null) {
261-
return true;
262-
} else {
263-
return Objects.equal(mNetworkId, ident.mNetworkId);
264-
}
265-
case TYPE_WIFI_P2P:
266-
return mNetworkId == null;
274+
return Objects.equal(mNetworkId, ident.mNetworkId);
267275
default:
268276
return false;
269277
}
@@ -279,6 +287,24 @@ private boolean matchesEthernet(NetworkIdentity ident) {
279287
return false;
280288
}
281289

290+
private boolean matchesMobileWildcard(NetworkIdentity ident) {
291+
if (ident.mType == TYPE_WIMAX) {
292+
return true;
293+
} else {
294+
return contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
295+
}
296+
}
297+
298+
private boolean matchesWifiWildcard(NetworkIdentity ident) {
299+
switch (ident.mType) {
300+
case TYPE_WIFI:
301+
case TYPE_WIFI_P2P:
302+
return true;
303+
default:
304+
return false;
305+
}
306+
}
307+
282308
private static String getMatchRuleName(int matchRule) {
283309
switch (matchRule) {
284310
case MATCH_MOBILE_3G_LOWER:
@@ -291,6 +317,10 @@ private static String getMatchRuleName(int matchRule) {
291317
return "WIFI";
292318
case MATCH_ETHERNET:
293319
return "ETHERNET";
320+
case MATCH_MOBILE_WILDCARD:
321+
return "MOBILE_WILDCARD";
322+
case MATCH_WIFI_WILDCARD:
323+
return "WIFI_WILDCARD";
294324
default:
295325
return "UNKNOWN";
296326
}

core/java/android/net/TrafficStats.java

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ public class TrafficStats {
8888
*/
8989
public static final int TAG_SYSTEM_BACKUP = 0xFFFFFF03;
9090

91+
private static INetworkStatsService sStatsService;
92+
93+
private synchronized static INetworkStatsService getStatsService() {
94+
if (sStatsService == null) {
95+
sStatsService = INetworkStatsService.Stub.asInterface(
96+
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
97+
}
98+
return sStatsService;
99+
}
100+
91101
/**
92102
* Snapshot of {@link NetworkStats} when the currently active profiling
93103
* session started, or {@code null} if no session active.
@@ -228,11 +238,9 @@ public static void incrementOperationCount(int operationCount) {
228238
* @param operationCount Number of operations to increment count by.
229239
*/
230240
public static void incrementOperationCount(int tag, int operationCount) {
231-
final INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
232-
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
233241
final int uid = android.os.Process.myUid();
234242
try {
235-
statsService.incrementOperationCount(uid, tag, operationCount);
243+
getStatsService().incrementOperationCount(uid, tag, operationCount);
236244
} catch (RemoteException e) {
237245
throw new RuntimeException(e);
238246
}
@@ -257,31 +265,55 @@ public static void closeQuietly(INetworkStatsSession session) {
257265
* @return number of packets. If the statistics are not supported by this device,
258266
* {@link #UNSUPPORTED} will be returned.
259267
*/
260-
public static native long getMobileTxPackets();
268+
public static long getMobileTxPackets() {
269+
long total = 0;
270+
for (String iface : getMobileIfaces()) {
271+
total += getTxPackets(iface);
272+
}
273+
return total;
274+
}
261275

262276
/**
263277
* Get the total number of packets received through the mobile interface.
264278
*
265279
* @return number of packets. If the statistics are not supported by this device,
266280
* {@link #UNSUPPORTED} will be returned.
267281
*/
268-
public static native long getMobileRxPackets();
282+
public static long getMobileRxPackets() {
283+
long total = 0;
284+
for (String iface : getMobileIfaces()) {
285+
total += getRxPackets(iface);
286+
}
287+
return total;
288+
}
269289

270290
/**
271291
* Get the total number of bytes transmitted through the mobile interface.
272292
*
273293
* @return number of bytes. If the statistics are not supported by this device,
274294
* {@link #UNSUPPORTED} will be returned.
275295
*/
276-
public static native long getMobileTxBytes();
296+
public static long getMobileTxBytes() {
297+
long total = 0;
298+
for (String iface : getMobileIfaces()) {
299+
total += getTxBytes(iface);
300+
}
301+
return total;
302+
}
277303

278304
/**
279305
* Get the total number of bytes received through the mobile interface.
280306
*
281307
* @return number of bytes. If the statistics are not supported by this device,
282308
* {@link #UNSUPPORTED} will be returned.
283309
*/
284-
public static native long getMobileRxBytes();
310+
public static long getMobileRxBytes() {
311+
long total = 0;
312+
for (String iface : getMobileIfaces()) {
313+
total += getRxBytes(iface);
314+
}
315+
return total;
316+
}
285317

286318
/**
287319
* Get the total number of packets transmitted through the specified interface.
@@ -290,7 +322,9 @@ public static void closeQuietly(INetworkStatsSession session) {
290322
* {@link #UNSUPPORTED} will be returned.
291323
* @hide
292324
*/
293-
public static native long getTxPackets(String iface);
325+
public static long getTxPackets(String iface) {
326+
return nativeGetIfaceStat(iface, TYPE_TX_PACKETS);
327+
}
294328

295329
/**
296330
* Get the total number of packets received through the specified interface.
@@ -299,7 +333,9 @@ public static void closeQuietly(INetworkStatsSession session) {
299333
* {@link #UNSUPPORTED} will be returned.
300334
* @hide
301335
*/
302-
public static native long getRxPackets(String iface);
336+
public static long getRxPackets(String iface) {
337+
return nativeGetIfaceStat(iface, TYPE_RX_PACKETS);
338+
}
303339

304340
/**
305341
* Get the total number of bytes transmitted through the specified interface.
@@ -308,7 +344,9 @@ public static void closeQuietly(INetworkStatsSession session) {
308344
* {@link #UNSUPPORTED} will be returned.
309345
* @hide
310346
*/
311-
public static native long getTxBytes(String iface);
347+
public static long getTxBytes(String iface) {
348+
return nativeGetIfaceStat(iface, TYPE_TX_BYTES);
349+
}
312350

313351
/**
314352
* Get the total number of bytes received through the specified interface.
@@ -317,40 +355,49 @@ public static void closeQuietly(INetworkStatsSession session) {
317355
* {@link #UNSUPPORTED} will be returned.
318356
* @hide
319357
*/
320-
public static native long getRxBytes(String iface);
321-
358+
public static long getRxBytes(String iface) {
359+
return nativeGetIfaceStat(iface, TYPE_RX_BYTES);
360+
}
322361

323362
/**
324363
* Get the total number of packets sent through all network interfaces.
325364
*
326365
* @return the number of packets. If the statistics are not supported by this device,
327366
* {@link #UNSUPPORTED} will be returned.
328367
*/
329-
public static native long getTotalTxPackets();
368+
public static long getTotalTxPackets() {
369+
return nativeGetTotalStat(TYPE_TX_PACKETS);
370+
}
330371

331372
/**
332373
* Get the total number of packets received through all network interfaces.
333374
*
334375
* @return number of packets. If the statistics are not supported by this device,
335376
* {@link #UNSUPPORTED} will be returned.
336377
*/
337-
public static native long getTotalRxPackets();
378+
public static long getTotalRxPackets() {
379+
return nativeGetTotalStat(TYPE_RX_PACKETS);
380+
}
338381

339382
/**
340383
* Get the total number of bytes sent through all network interfaces.
341384
*
342385
* @return number of bytes. If the statistics are not supported by this device,
343386
* {@link #UNSUPPORTED} will be returned.
344387
*/
345-
public static native long getTotalTxBytes();
388+
public static long getTotalTxBytes() {
389+
return nativeGetTotalStat(TYPE_TX_BYTES);
390+
}
346391

347392
/**
348393
* Get the total number of bytes received through all network interfaces.
349394
*
350395
* @return number of bytes. If the statistics are not supported by this device,
351396
* {@link #UNSUPPORTED} will be returned.
352397
*/
353-
public static native long getTotalRxBytes();
398+
public static long getTotalRxBytes() {
399+
return nativeGetTotalStat(TYPE_RX_BYTES);
400+
}
354401

355402
/**
356403
* Get the number of bytes sent through the network for this UID.
@@ -483,7 +530,6 @@ public static void closeQuietly(INetworkStatsSession session) {
483530
*/
484531
public static native long getUidTcpRxSegments(int uid);
485532

486-
487533
/**
488534
* Get the number of UDP packets sent for this UID.
489535
* Includes DNS requests.
@@ -515,13 +561,33 @@ public static void closeQuietly(INetworkStatsSession session) {
515561
* special permission.
516562
*/
517563
private static NetworkStats getDataLayerSnapshotForUid(Context context) {
518-
final INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
519-
ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
520564
final int uid = android.os.Process.myUid();
521565
try {
522-
return statsService.getDataLayerSnapshotForUid(uid);
566+
return getStatsService().getDataLayerSnapshotForUid(uid);
567+
} catch (RemoteException e) {
568+
throw new RuntimeException(e);
569+
}
570+
}
571+
572+
/**
573+
* Return set of any ifaces associated with mobile networks since boot.
574+
* Interfaces are never removed from this list, so counters should always be
575+
* monotonic.
576+
*/
577+
private static String[] getMobileIfaces() {
578+
try {
579+
return getStatsService().getMobileIfaces();
523580
} catch (RemoteException e) {
524581
throw new RuntimeException(e);
525582
}
526583
}
584+
585+
// NOTE: keep these in sync with android_net_TrafficStats.cpp
586+
private static final int TYPE_RX_BYTES = 0;
587+
private static final int TYPE_RX_PACKETS = 1;
588+
private static final int TYPE_TX_BYTES = 2;
589+
private static final int TYPE_TX_PACKETS = 3;
590+
591+
private static native long nativeGetTotalStat(int type);
592+
private static native long nativeGetIfaceStat(String iface, int type);
527593
}

0 commit comments

Comments
 (0)