Refine random stats generation.

Offer parameters to pick out specific list of packages, and to change
duration and total data.

Bug: 5416742
Change-Id: I6970a5e76f3198ef23a400c079037212b000ca6b
This commit is contained in:
Jeff Sharkey
2011-10-05 23:31:57 -07:00
parent f1369459ed
commit 0787a3616f
2 changed files with 92 additions and 35 deletions

View File

@@ -474,21 +474,38 @@ public class NetworkStatsHistory implements Parcelable {
return entry; return entry;
} }
/**
* @deprecated only for temporary testing
*/
@Deprecated
public void generateRandom(long start, long end, long bytes) {
final Random r = new Random();
final float fractionRx = r.nextFloat();
final long rxBytes = (long) (bytes * fractionRx);
final long txBytes = (long) (bytes * (1 - fractionRx));
final long rxPackets = rxBytes / 1024;
final long txPackets = txBytes / 1024;
final long operations = rxBytes / 2048;
generateRandom(start, end, rxBytes, rxPackets, txBytes, txPackets, operations, r);
}
/** /**
* @deprecated only for temporary testing * @deprecated only for temporary testing
*/ */
@Deprecated @Deprecated
public void generateRandom(long start, long end, long rxBytes, long rxPackets, long txBytes, public void generateRandom(long start, long end, long rxBytes, long rxPackets, long txBytes,
long txPackets, long operations) { long txPackets, long operations, Random r) {
ensureBuckets(start, end); ensureBuckets(start, end);
final NetworkStats.Entry entry = new NetworkStats.Entry( final NetworkStats.Entry entry = new NetworkStats.Entry(
IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L); IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
final Random r = new Random();
while (rxBytes > 1024 || rxPackets > 128 || txBytes > 1024 || txPackets > 128 while (rxBytes > 1024 || rxPackets > 128 || txBytes > 1024 || txPackets > 128
|| operations > 32) { || operations > 32) {
final long curStart = randomLong(r, start, end); final long curStart = randomLong(r, start, end);
final long curEnd = randomLong(r, curStart, end); final long curEnd = curStart + randomLong(r, 0, (end - curStart) / 2);
entry.rxBytes = randomLong(r, 0, rxBytes); entry.rxBytes = randomLong(r, 0, rxBytes);
entry.rxPackets = randomLong(r, 0, rxPackets); entry.rxPackets = randomLong(r, 0, rxPackets);
@@ -506,7 +523,7 @@ public class NetworkStatsHistory implements Parcelable {
} }
} }
private static long randomLong(Random r, long start, long end) { public static long randomLong(Random r, long start, long end) {
return (long) (start + (r.nextFloat() * (end - start))); return (long) (start + (r.nextFloat() * (end - start)));
} }

View File

@@ -32,6 +32,7 @@ import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.SET_FOREGROUND; import static android.net.NetworkStats.SET_FOREGROUND;
import static android.net.NetworkStats.TAG_NONE; import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL; import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkStatsHistory.randomLong;
import static android.net.NetworkTemplate.buildTemplateMobileAll; import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateWifi; import static android.net.NetworkTemplate.buildTemplateWifi;
import static android.net.TrafficStats.UID_REMOVED; import static android.net.TrafficStats.UID_REMOVED;
@@ -48,6 +49,7 @@ import static android.text.format.DateUtils.DAY_IN_MILLIS;
import static android.text.format.DateUtils.HOUR_IN_MILLIS; import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.SECOND_IN_MILLIS; import static android.text.format.DateUtils.SECOND_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats;
@@ -62,6 +64,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.IConnectivityManager; import android.net.IConnectivityManager;
import android.net.INetworkManagementEventObserver; import android.net.INetworkManagementEventObserver;
import android.net.INetworkStatsService; import android.net.INetworkStatsService;
@@ -113,7 +117,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.Random;
import libcore.io.IoUtils; import libcore.io.IoUtils;
@@ -1347,7 +1351,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
synchronized (mStatsLock) { synchronized (mStatsLock) {
// TODO: remove this testing code, since it corrupts stats // TODO: remove this testing code, since it corrupts stats
if (argSet.contains("generate")) { if (argSet.contains("generate")) {
generateRandomLocked(); generateRandomLocked(args);
pw.println("Generated stub stats"); pw.println("Generated stub stats");
return; return;
} }
@@ -1406,42 +1410,78 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
* @deprecated only for temporary testing * @deprecated only for temporary testing
*/ */
@Deprecated @Deprecated
private void generateRandomLocked() { private void generateRandomLocked(String[] args) {
final long NET_END = System.currentTimeMillis(); final long totalBytes = Long.parseLong(args[1]);
final long NET_START = NET_END - mSettings.getNetworkMaxHistory(); final long totalTime = Long.parseLong(args[2]);
final long NET_RX_BYTES = 3 * GB_IN_BYTES;
final long NET_RX_PACKETS = NET_RX_BYTES / 1024;
final long NET_TX_BYTES = 2 * GB_IN_BYTES;
final long NET_TX_PACKETS = NET_TX_BYTES / 1024;
final long UID_END = System.currentTimeMillis(); final PackageManager pm = mContext.getPackageManager();
final long UID_START = UID_END - mSettings.getUidMaxHistory(); final ArrayList<Integer> specialUidList = Lists.newArrayList();
final long UID_RX_BYTES = 500 * MB_IN_BYTES; for (int i = 3; i < args.length; i++) {
final long UID_RX_PACKETS = UID_RX_BYTES / 1024; try {
final long UID_TX_BYTES = 100 * MB_IN_BYTES; specialUidList.add(pm.getApplicationInfo(args[i], 0).uid);
final long UID_TX_PACKETS = UID_TX_BYTES / 1024; } catch (NameNotFoundException e) {
final long UID_OPERATIONS = UID_RX_BYTES / 2048; throw new RuntimeException(e);
}
}
final List<ApplicationInfo> installedApps = mContext final HashSet<Integer> otherUidSet = Sets.newHashSet();
.getPackageManager().getInstalledApplications(0); for (ApplicationInfo info : pm.getInstalledApplications(0)) {
if (pm.checkPermission(android.Manifest.permission.INTERNET, info.packageName)
== PackageManager.PERMISSION_GRANTED && !specialUidList.contains(info.uid)) {
otherUidSet.add(info.uid);
}
}
final ArrayList<Integer> otherUidList = new ArrayList<Integer>(otherUidSet);
final long end = System.currentTimeMillis();
final long start = end - totalTime;
mNetworkDevStats.clear(); mNetworkDevStats.clear();
mNetworkXtStats.clear(); mNetworkXtStats.clear();
mUidStats.clear(); mUidStats.clear();
for (NetworkIdentitySet ident : mActiveIfaces.values()) {
findOrCreateNetworkDevStatsLocked(ident).generateRandom(NET_START, NET_END,
NET_RX_BYTES, NET_RX_PACKETS, NET_TX_BYTES, NET_TX_PACKETS, 0L);
findOrCreateNetworkXtStatsLocked(ident).generateRandom(NET_START, NET_END, NET_RX_BYTES,
NET_RX_PACKETS, NET_TX_BYTES, NET_TX_PACKETS, 0L);
for (ApplicationInfo info : installedApps) { final Random r = new Random();
final int uid = info.uid; for (NetworkIdentitySet ident : mActiveIfaces.values()) {
findOrCreateUidStatsLocked(ident, uid, SET_DEFAULT, TAG_NONE).generateRandom( final NetworkStatsHistory devHistory = findOrCreateNetworkDevStatsLocked(ident);
UID_START, UID_END, UID_RX_BYTES, UID_RX_PACKETS, UID_TX_BYTES, final NetworkStatsHistory xtHistory = findOrCreateNetworkXtStatsLocked(ident);
UID_TX_PACKETS, UID_OPERATIONS);
findOrCreateUidStatsLocked(ident, uid, SET_FOREGROUND, TAG_NONE).generateRandom( final ArrayList<Integer> uidList = new ArrayList<Integer>();
UID_START, UID_END, UID_RX_BYTES, UID_RX_PACKETS, UID_TX_BYTES, uidList.addAll(specialUidList);
UID_TX_PACKETS, UID_OPERATIONS);
if (uidList.size() == 0) {
Collections.shuffle(otherUidList);
uidList.addAll(otherUidList);
}
boolean first = true;
long remainingBytes = totalBytes;
for (int uid : uidList) {
final NetworkStatsHistory defaultHistory = findOrCreateUidStatsLocked(
ident, uid, SET_DEFAULT, TAG_NONE);
final NetworkStatsHistory foregroundHistory = findOrCreateUidStatsLocked(
ident, uid, SET_FOREGROUND, TAG_NONE);
final long uidBytes = totalBytes / uidList.size();
final float fractionDefault = r.nextFloat();
final long defaultBytes = (long) (uidBytes * fractionDefault);
final long foregroundBytes = (long) (uidBytes * (1 - fractionDefault));
defaultHistory.generateRandom(start, end, defaultBytes);
foregroundHistory.generateRandom(start, end, foregroundBytes);
if (first) {
final long bumpTime = (start + end) / 2;
defaultHistory.recordData(
bumpTime, bumpTime + DAY_IN_MILLIS, 200 * MB_IN_BYTES, 0);
first = false;
}
devHistory.recordEntireHistory(defaultHistory);
devHistory.recordEntireHistory(foregroundHistory);
xtHistory.recordEntireHistory(defaultHistory);
xtHistory.recordEntireHistory(foregroundHistory);
} }
} }
} }