UID network stats, secure settings, and random.

Collect UID-granularity network stats during regular poll event.  Add
dumpsys argument to generate fake historical data for debugging, and
move stats parameters to Settings.Secure.

Change-Id: I09b36a2955dc10c697d4b9c3ff23dcb3ac37bd70
This commit is contained in:
Jeff Sharkey
2011-05-29 22:50:42 -07:00
parent 1a3eb889f0
commit b06fe7ad72
3 changed files with 255 additions and 71 deletions

View File

@@ -19,6 +19,7 @@ package android.net;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.SparseBooleanArray;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
@@ -125,7 +126,7 @@ public class NetworkStats implements Parcelable {
/**
* Return list of unique interfaces known by this data structure.
*/
public String[] getKnownIfaces() {
public String[] getUniqueIfaces() {
final HashSet<String> ifaces = new HashSet<String>();
for (String iface : this.iface) {
if (iface != IFACE_ALL) {
@@ -135,6 +136,23 @@ public class NetworkStats implements Parcelable {
return ifaces.toArray(new String[ifaces.size()]);
}
/**
* Return list of unique UIDs known by this data structure.
*/
public int[] getUniqueUids() {
final SparseBooleanArray uids = new SparseBooleanArray();
for (int uid : this.uid) {
uids.put(uid, true);
}
final int size = uids.size();
final int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = uids.keyAt(i);
}
return result;
}
/**
* Subtract the given {@link NetworkStats}, effectively leaving the delta
* between two snapshots in time. Assumes that statistics rows collect over

View File

@@ -24,7 +24,9 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ProtocolException;
import java.util.Arrays;
import java.util.Random;
/**
* Collection of historical network statistics, recorded into equally-sized
@@ -38,7 +40,7 @@ import java.util.Arrays;
* @hide
*/
public class NetworkStatsHistory implements Parcelable {
private static final int VERSION = 1;
private static final int VERSION_CURRENT = 1;
// TODO: teach about zigzag encoding to use less disk space
// TODO: teach how to convert between bucket sizes
@@ -76,15 +78,23 @@ public class NetworkStatsHistory implements Parcelable {
public NetworkStatsHistory(DataInputStream in) throws IOException {
final int version = in.readInt();
bucketDuration = in.readLong();
bucketStart = readLongArray(in);
rx = readLongArray(in);
tx = readLongArray(in);
bucketCount = bucketStart.length;
switch (version) {
case VERSION_CURRENT: {
bucketDuration = in.readLong();
bucketStart = readLongArray(in);
rx = readLongArray(in);
tx = readLongArray(in);
bucketCount = bucketStart.length;
break;
}
default: {
throw new ProtocolException("unexpected version: " + version);
}
}
}
public void writeToStream(DataOutputStream out) throws IOException {
out.writeInt(VERSION);
out.writeInt(VERSION_CURRENT);
out.writeLong(bucketDuration);
writeLongArray(out, bucketStart, bucketCount);
writeLongArray(out, rx, bucketCount);
@@ -192,12 +202,37 @@ public class NetworkStatsHistory implements Parcelable {
}
}
/**
* @deprecated only for temporary testing
*/
@Deprecated
public void generateRandom(long start, long end, long rx, long tx) {
ensureBuckets(start, end);
final Random r = new Random();
while (rx > 1024 && tx > 1024) {
final long curStart = randomLong(r, start, end);
final long curEnd = randomLong(r, curStart, end);
final long curRx = randomLong(r, 0, rx);
final long curTx = randomLong(r, 0, tx);
recordData(curStart, curEnd, curRx, curTx);
rx -= curRx;
tx -= curTx;
}
}
private static long randomLong(Random r, long start, long end) {
return (long) (start + (r.nextFloat() * (end - start)));
}
public void dump(String prefix, PrintWriter pw) {
pw.print(prefix);
pw.println("NetworkStatsHistory:");
pw.print("NetworkStatsHistory: bucketDuration="); pw.println(bucketDuration);
for (int i = 0; i < bucketCount; i++) {
pw.print(prefix);
pw.print(" timestamp="); pw.print(bucketStart[i]);
pw.print(" bucketStart="); pw.print(bucketStart[i]);
pw.print(" rx="); pw.print(rx[i]);
pw.print(" tx="); pw.println(tx[i]);
}