Refactoring of deleting stats in NetworkStats based on uids.

In follow-up commits, current API would create new NetworkStats
every time when 464xlatAdjustment wants to filtered out some
uids.

This commit refactors it to delete stats in-place to get better
performance.

Bug: 118602783
Test: atest FrameworksNetTests
Change-Id: I858f95d1fa7733111786243b4e261ce8a70a068d
This commit is contained in:
junyulai
2018-10-29 22:26:22 +08:00
parent c6fcedd779
commit bbaddec5d7
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;
}
/**