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

am: c8dcfb8b4b

Change-Id: I60b086861a9a91cdd54c56c7caf21d26a6dd294a
This commit is contained in:
Junyu Lai
2018-12-09 21:38:59 -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
*/
// @NotThreadSafe
public class NetworkStats implements Parcelable {
private static final String TAG = "NetworkStats";
/** {@link #iface} value when interface details unavailable. */
@@ -443,6 +444,26 @@ public class NetworkStats implements Parcelable {
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() {
return elapsedRealtime;
}
@@ -941,21 +962,18 @@ public class NetworkStats implements Parcelable {
}
/**
* Return all rows except those attributed to the requested UID; doesn't
* mutate the original structure.
* Remove all rows that match one of specified UIDs.
*/
public NetworkStats withoutUids(int[] uids) {
final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
Entry entry = new Entry();
public void removeUids(int[] uids) {
int nextOutputEntry = 0;
for (int i = 0; i < size; i++) {
entry = getValues(i, entry);
if (!ArrayUtils.contains(uids, entry.uid)) {
stats.addValues(entry);
if (!ArrayUtils.contains(uids, uid[i])) {
maybeCopyEntry(nextOutputEntry, i);
nextOutputEntry++;
}
}
return stats;
size = nextOutputEntry;
}
/**

View File

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