|
32 | 32 | import static android.net.NetworkStats.SET_FOREGROUND; |
33 | 33 | import static android.net.NetworkStats.TAG_NONE; |
34 | 34 | import static android.net.NetworkStats.UID_ALL; |
| 35 | +import static android.net.NetworkStatsHistory.randomLong; |
35 | 36 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
36 | 37 | import static android.net.NetworkTemplate.buildTemplateWifi; |
37 | 38 | import static android.net.TrafficStats.UID_REMOVED; |
|
48 | 49 | import static android.text.format.DateUtils.HOUR_IN_MILLIS; |
49 | 50 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
50 | 51 | import static android.text.format.DateUtils.SECOND_IN_MILLIS; |
| 52 | +import static android.text.format.DateUtils.WEEK_IN_MILLIS; |
51 | 53 | import static com.android.internal.util.Preconditions.checkNotNull; |
52 | 54 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
53 | 55 | import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; |
|
62 | 64 | import android.content.Intent; |
63 | 65 | import android.content.IntentFilter; |
64 | 66 | import android.content.pm.ApplicationInfo; |
| 67 | +import android.content.pm.PackageManager; |
| 68 | +import android.content.pm.PackageManager.NameNotFoundException; |
65 | 69 | import android.net.IConnectivityManager; |
66 | 70 | import android.net.INetworkManagementEventObserver; |
67 | 71 | import android.net.INetworkStatsService; |
|
113 | 117 | import java.util.Collections; |
114 | 118 | import java.util.HashMap; |
115 | 119 | import java.util.HashSet; |
116 | | -import java.util.List; |
| 120 | +import java.util.Random; |
117 | 121 |
|
118 | 122 | import libcore.io.IoUtils; |
119 | 123 |
|
@@ -1347,7 +1351,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
1347 | 1351 | synchronized (mStatsLock) { |
1348 | 1352 | // TODO: remove this testing code, since it corrupts stats |
1349 | 1353 | if (argSet.contains("generate")) { |
1350 | | - generateRandomLocked(); |
| 1354 | + generateRandomLocked(args); |
1351 | 1355 | pw.println("Generated stub stats"); |
1352 | 1356 | return; |
1353 | 1357 | } |
@@ -1406,42 +1410,78 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
1406 | 1410 | * @deprecated only for temporary testing |
1407 | 1411 | */ |
1408 | 1412 | @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; |
1427 | 1439 |
|
1428 | 1440 | mNetworkDevStats.clear(); |
1429 | 1441 | mNetworkXtStats.clear(); |
1430 | 1442 | mUidStats.clear(); |
| 1443 | + |
| 1444 | + final Random r = new Random(); |
1431 | 1445 | 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); |
1445 | 1485 | } |
1446 | 1486 | } |
1447 | 1487 | } |
|
0 commit comments