Skip to content

Commit ca35bc5

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Refine random stats generation."
2 parents 093f9b7 + 293779f commit ca35bc5

File tree

2 files changed

+94
-37
lines changed

2 files changed

+94
-37
lines changed

core/java/android/net/NetworkStatsHistory.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,21 +474,38 @@ public Entry getValues(long start, long end, long now, Entry recycle) {
474474
return entry;
475475
}
476476

477+
/**
478+
* @deprecated only for temporary testing
479+
*/
480+
@Deprecated
481+
public void generateRandom(long start, long end, long bytes) {
482+
final Random r = new Random();
483+
484+
final float fractionRx = r.nextFloat();
485+
final long rxBytes = (long) (bytes * fractionRx);
486+
final long txBytes = (long) (bytes * (1 - fractionRx));
487+
488+
final long rxPackets = rxBytes / 1024;
489+
final long txPackets = txBytes / 1024;
490+
final long operations = rxBytes / 2048;
491+
492+
generateRandom(start, end, rxBytes, rxPackets, txBytes, txPackets, operations, r);
493+
}
494+
477495
/**
478496
* @deprecated only for temporary testing
479497
*/
480498
@Deprecated
481499
public void generateRandom(long start, long end, long rxBytes, long rxPackets, long txBytes,
482-
long txPackets, long operations) {
500+
long txPackets, long operations, Random r) {
483501
ensureBuckets(start, end);
484502

485503
final NetworkStats.Entry entry = new NetworkStats.Entry(
486504
IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
487-
final Random r = new Random();
488505
while (rxBytes > 1024 || rxPackets > 128 || txBytes > 1024 || txPackets > 128
489506
|| operations > 32) {
490507
final long curStart = randomLong(r, start, end);
491-
final long curEnd = randomLong(r, curStart, end);
508+
final long curEnd = curStart + randomLong(r, 0, (end - curStart) / 2);
492509

493510
entry.rxBytes = randomLong(r, 0, rxBytes);
494511
entry.rxPackets = randomLong(r, 0, rxPackets);
@@ -506,7 +523,7 @@ public void generateRandom(long start, long end, long rxBytes, long rxPackets, l
506523
}
507524
}
508525

509-
private static long randomLong(Random r, long start, long end) {
526+
public static long randomLong(Random r, long start, long end) {
510527
return (long) (start + (r.nextFloat() * (end - start)));
511528
}
512529

services/java/com/android/server/net/NetworkStatsService.java

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static android.net.NetworkStats.SET_FOREGROUND;
3333
import static android.net.NetworkStats.TAG_NONE;
3434
import static android.net.NetworkStats.UID_ALL;
35+
import static android.net.NetworkStatsHistory.randomLong;
3536
import static android.net.NetworkTemplate.buildTemplateMobileAll;
3637
import static android.net.NetworkTemplate.buildTemplateWifi;
3738
import static android.net.TrafficStats.UID_REMOVED;
@@ -48,6 +49,7 @@
4849
import static android.text.format.DateUtils.HOUR_IN_MILLIS;
4950
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
5051
import static android.text.format.DateUtils.SECOND_IN_MILLIS;
52+
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
5153
import static com.android.internal.util.Preconditions.checkNotNull;
5254
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
5355
import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats;
@@ -62,6 +64,8 @@
6264
import android.content.Intent;
6365
import android.content.IntentFilter;
6466
import android.content.pm.ApplicationInfo;
67+
import android.content.pm.PackageManager;
68+
import android.content.pm.PackageManager.NameNotFoundException;
6569
import android.net.IConnectivityManager;
6670
import android.net.INetworkManagementEventObserver;
6771
import android.net.INetworkStatsService;
@@ -113,7 +117,7 @@
113117
import java.util.Collections;
114118
import java.util.HashMap;
115119
import java.util.HashSet;
116-
import java.util.List;
120+
import java.util.Random;
117121

118122
import libcore.io.IoUtils;
119123

@@ -1347,7 +1351,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
13471351
synchronized (mStatsLock) {
13481352
// TODO: remove this testing code, since it corrupts stats
13491353
if (argSet.contains("generate")) {
1350-
generateRandomLocked();
1354+
generateRandomLocked(args);
13511355
pw.println("Generated stub stats");
13521356
return;
13531357
}
@@ -1406,42 +1410,78 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
14061410
* @deprecated only for temporary testing
14071411
*/
14081412
@Deprecated
1409-
private void generateRandomLocked() {
1410-
final long NET_END = System.currentTimeMillis();
1411-
final long NET_START = NET_END - mSettings.getNetworkMaxHistory();
1412-
final long NET_RX_BYTES = 3 * GB_IN_BYTES;
1413-
final long NET_RX_PACKETS = NET_RX_BYTES / 1024;
1414-
final long NET_TX_BYTES = 2 * GB_IN_BYTES;
1415-
final long NET_TX_PACKETS = NET_TX_BYTES / 1024;
1416-
1417-
final long UID_END = System.currentTimeMillis();
1418-
final long UID_START = UID_END - mSettings.getUidMaxHistory();
1419-
final long UID_RX_BYTES = 500 * MB_IN_BYTES;
1420-
final long UID_RX_PACKETS = UID_RX_BYTES / 1024;
1421-
final long UID_TX_BYTES = 100 * MB_IN_BYTES;
1422-
final long UID_TX_PACKETS = UID_TX_BYTES / 1024;
1423-
final long UID_OPERATIONS = UID_RX_BYTES / 2048;
1424-
1425-
final List<ApplicationInfo> installedApps = mContext
1426-
.getPackageManager().getInstalledApplications(0);
1413+
private void generateRandomLocked(String[] args) {
1414+
final long totalBytes = Long.parseLong(args[1]);
1415+
final long totalTime = Long.parseLong(args[2]);
1416+
1417+
final PackageManager pm = mContext.getPackageManager();
1418+
final ArrayList<Integer> specialUidList = Lists.newArrayList();
1419+
for (int i = 3; i < args.length; i++) {
1420+
try {
1421+
specialUidList.add(pm.getApplicationInfo(args[i], 0).uid);
1422+
} catch (NameNotFoundException e) {
1423+
throw new RuntimeException(e);
1424+
}
1425+
}
1426+
1427+
final HashSet<Integer> otherUidSet = Sets.newHashSet();
1428+
for (ApplicationInfo info : pm.getInstalledApplications(0)) {
1429+
if (pm.checkPermission(android.Manifest.permission.INTERNET, info.packageName)
1430+
== PackageManager.PERMISSION_GRANTED && !specialUidList.contains(info.uid)) {
1431+
otherUidSet.add(info.uid);
1432+
}
1433+
}
1434+
1435+
final ArrayList<Integer> otherUidList = new ArrayList<Integer>(otherUidSet);
1436+
1437+
final long end = System.currentTimeMillis();
1438+
final long start = end - totalTime;
14271439

14281440
mNetworkDevStats.clear();
14291441
mNetworkXtStats.clear();
14301442
mUidStats.clear();
1443+
1444+
final Random r = new Random();
14311445
for (NetworkIdentitySet ident : mActiveIfaces.values()) {
1432-
findOrCreateNetworkDevStatsLocked(ident).generateRandom(NET_START, NET_END,
1433-
NET_RX_BYTES, NET_RX_PACKETS, NET_TX_BYTES, NET_TX_PACKETS, 0L);
1434-
findOrCreateNetworkXtStatsLocked(ident).generateRandom(NET_START, NET_END, NET_RX_BYTES,
1435-
NET_RX_PACKETS, NET_TX_BYTES, NET_TX_PACKETS, 0L);
1436-
1437-
for (ApplicationInfo info : installedApps) {
1438-
final int uid = info.uid;
1439-
findOrCreateUidStatsLocked(ident, uid, SET_DEFAULT, TAG_NONE).generateRandom(
1440-
UID_START, UID_END, UID_RX_BYTES, UID_RX_PACKETS, UID_TX_BYTES,
1441-
UID_TX_PACKETS, UID_OPERATIONS);
1442-
findOrCreateUidStatsLocked(ident, uid, SET_FOREGROUND, TAG_NONE).generateRandom(
1443-
UID_START, UID_END, UID_RX_BYTES, UID_RX_PACKETS, UID_TX_BYTES,
1444-
UID_TX_PACKETS, UID_OPERATIONS);
1446+
final NetworkStatsHistory devHistory = findOrCreateNetworkDevStatsLocked(ident);
1447+
final NetworkStatsHistory xtHistory = findOrCreateNetworkXtStatsLocked(ident);
1448+
1449+
final ArrayList<Integer> uidList = new ArrayList<Integer>();
1450+
uidList.addAll(specialUidList);
1451+
1452+
if (uidList.size() == 0) {
1453+
Collections.shuffle(otherUidList);
1454+
uidList.addAll(otherUidList);
1455+
}
1456+
1457+
boolean first = true;
1458+
long remainingBytes = totalBytes;
1459+
for (int uid : uidList) {
1460+
final NetworkStatsHistory defaultHistory = findOrCreateUidStatsLocked(
1461+
ident, uid, SET_DEFAULT, TAG_NONE);
1462+
final NetworkStatsHistory foregroundHistory = findOrCreateUidStatsLocked(
1463+
ident, uid, SET_FOREGROUND, TAG_NONE);
1464+
1465+
final long uidBytes = totalBytes / uidList.size();
1466+
1467+
final float fractionDefault = r.nextFloat();
1468+
final long defaultBytes = (long) (uidBytes * fractionDefault);
1469+
final long foregroundBytes = (long) (uidBytes * (1 - fractionDefault));
1470+
1471+
defaultHistory.generateRandom(start, end, defaultBytes);
1472+
foregroundHistory.generateRandom(start, end, foregroundBytes);
1473+
1474+
if (first) {
1475+
final long bumpTime = (start + end) / 2;
1476+
defaultHistory.recordData(
1477+
bumpTime, bumpTime + DAY_IN_MILLIS, 200 * MB_IN_BYTES, 0);
1478+
first = false;
1479+
}
1480+
1481+
devHistory.recordEntireHistory(defaultHistory);
1482+
devHistory.recordEntireHistory(foregroundHistory);
1483+
xtHistory.recordEntireHistory(defaultHistory);
1484+
xtHistory.recordEntireHistory(foregroundHistory);
14451485
}
14461486
}
14471487
}

0 commit comments

Comments
 (0)