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

am: 2d00948a0c

Change-Id: Icfe86f71f9416dcd5fcc9aafd8c346197cde7cff
This commit is contained in:
Junyu Lai
2018-12-09 21:46:07 -08:00
committed by android-build-merger
2 changed files with 29 additions and 11 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;