Merge "Refactoring of deleting stats in NetworkStats based on uids."

am: 0882543a12

Change-Id: I60b086861a9a91cdd54c56c7caf21d26a6dd294a
This commit is contained in:
Junyu Lai
2018-12-09 21:38:59 -08:00
committed by android-build-merger
3 changed files with 80 additions and 26 deletions

View File

@@ -44,6 +44,7 @@ import java.util.Objects;
* *
* @hide * @hide
*/ */
// @NotThreadSafe
public class NetworkStats implements Parcelable { public class NetworkStats implements Parcelable {
private static final String TAG = "NetworkStats"; private static final String TAG = "NetworkStats";
/** {@link #iface} value when interface details unavailable. */ /** {@link #iface} value when interface details unavailable. */
@@ -443,6 +444,26 @@ public class NetworkStats implements Parcelable {
return entry; return entry;
} }
/**
* If @{code dest} is not equal to @{code src}, copy entry from index @{code src} to index
* @{code dest}.
*/
private void maybeCopyEntry(int dest, int src) {
if (dest == src) return;
iface[dest] = iface[src];
uid[dest] = uid[src];
set[dest] = set[src];
tag[dest] = tag[src];
metered[dest] = metered[src];
roaming[dest] = roaming[src];
defaultNetwork[dest] = defaultNetwork[src];
rxBytes[dest] = rxBytes[src];
rxPackets[dest] = rxPackets[src];
txBytes[dest] = txBytes[src];
txPackets[dest] = txPackets[src];
operations[dest] = operations[src];
}
public long getElapsedRealtime() { public long getElapsedRealtime() {
return elapsedRealtime; return elapsedRealtime;
} }
@@ -941,21 +962,18 @@ public class NetworkStats implements Parcelable {
} }
/** /**
* Return all rows except those attributed to the requested UID; doesn't * Remove all rows that match one of specified UIDs.
* mutate the original structure.
*/ */
public NetworkStats withoutUids(int[] uids) { public void removeUids(int[] uids) {
final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); int nextOutputEntry = 0;
Entry entry = new Entry();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
entry = getValues(i, entry); if (!ArrayUtils.contains(uids, uid[i])) {
if (!ArrayUtils.contains(uids, entry.uid)) { maybeCopyEntry(nextOutputEntry, i);
stats.addValues(entry); nextOutputEntry++;
} }
} }
return stats; size = nextOutputEntry;
} }
/** /**

View File

@@ -352,7 +352,7 @@ public class NetworkStatsRecorder {
// Clear UID from current stats snapshot // Clear UID from current stats snapshot
if (mLastSnapshot != null) { if (mLastSnapshot != null) {
mLastSnapshot = mLastSnapshot.withoutUids(uids); mLastSnapshot.removeUids(uids);
} }
final NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null; final NetworkStatsCollection complete = mComplete != null ? mComplete.get() : null;

View File

@@ -448,22 +448,58 @@ public class NetworkStatsTest {
} }
@Test @Test
public void testWithoutUid() throws Exception { public void testRemoveUids() throws Exception {
final NetworkStats before = new NetworkStats(TEST_START, 3) final NetworkStats before = new NetworkStats(TEST_START, 3);
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 64L, 4L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 512L, 32L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L);
final NetworkStats after = before.withoutUids(new int[] { 100 }); // Test 0 item stats.
assertEquals(6, before.size()); NetworkStats after = before.clone();
assertEquals(2, after.size()); after.removeUids(new int[0]);
assertValues(after, 0, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, assertEquals(0, after.size());
DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L); after.removeUids(new int[] {100});
assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, assertEquals(0, after.size());
DEFAULT_NETWORK_NO, 128L, 8L, 0L, 0L, 0L);
// Test 1 item stats.
before.addValues(TEST_IFACE, 99, SET_DEFAULT, TAG_NONE, 1L, 128L, 0L, 2L, 20L);
after = before.clone();
after.removeUids(new int[0]);
assertEquals(1, after.size());
assertValues(after, 0, TEST_IFACE, 99, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO, 1L, 128L, 0L, 2L, 20L);
after.removeUids(new int[] {99});
assertEquals(0, after.size());
// Append remaining test items.
before.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 2L, 64L, 0L, 2L, 20L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 4L, 32L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 8L, 16L, 0L, 0L, 0L)
.addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 16L, 8L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 32L, 4L, 0L, 0L, 0L)
.addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 64L, 2L, 0L, 0L, 0L);
assertEquals(7, before.size());
// Test remove with empty uid list.
after = before.clone();
after.removeUids(new int[0]);
assertValues(after.getTotalIncludingTags(null), 127L, 254L, 0L, 4L, 40L);
// Test remove uids don't exist in stats.
after.removeUids(new int[] {98, 0, Integer.MIN_VALUE, Integer.MAX_VALUE});
assertValues(after.getTotalIncludingTags(null), 127L, 254L, 0L, 4L, 40L);
// Test remove all uids.
after.removeUids(new int[] {99, 100, 100, 101});
assertEquals(0, after.size());
// Test remove in the middle.
after = before.clone();
after.removeUids(new int[] {100});
assertEquals(3, after.size());
assertValues(after, 0, TEST_IFACE, 99, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO, 1L, 128L, 0L, 2L, 20L);
assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO, 32L, 4L, 0L, 0L, 0L);
assertValues(after, 2, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO,
DEFAULT_NETWORK_NO, 64L, 2L, 0L, 0L, 0L);
} }
@Test @Test