Use delta reads for per uid traffic stats
For the detailed network stats of each uid and tag, the native
implementation provides delta read instead of consistent stats
data since boot. Every time the readNetworkStatsDetail function gets
called, the data pulled will be cleared. We will keep a persistent data
since boot in NetworkStatsService instead.
Fixed some checkstyle complains in related files as well.
Bug: 79171384
Test: android.app.usage.cts.NetworkUsageStatsTest
NetworkStatsServiceTest
Change-Id: I50bcf278739135081d522e50b0e88d76fd9ab131
This commit is contained in:
@@ -64,6 +64,9 @@ public class NetworkStatsFactory {
|
|||||||
|
|
||||||
private boolean mUseBpfStats;
|
private boolean mUseBpfStats;
|
||||||
|
|
||||||
|
// A persistent Snapshot since device start for eBPF stats
|
||||||
|
private final NetworkStats mPersistSnapshot;
|
||||||
|
|
||||||
// TODO: only do adjustments in NetworkStatsService and remove this.
|
// TODO: only do adjustments in NetworkStatsService and remove this.
|
||||||
/**
|
/**
|
||||||
* (Stacked interface) -> (base interface) association for all connected ifaces since boot.
|
* (Stacked interface) -> (base interface) association for all connected ifaces since boot.
|
||||||
@@ -135,6 +138,7 @@ public class NetworkStatsFactory {
|
|||||||
mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt");
|
mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt");
|
||||||
mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats");
|
mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats");
|
||||||
mUseBpfStats = useBpfStats;
|
mUseBpfStats = useBpfStats;
|
||||||
|
mPersistSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NetworkStats readBpfNetworkStatsDev() throws IOException {
|
public NetworkStats readBpfNetworkStatsDev() throws IOException {
|
||||||
@@ -268,6 +272,7 @@ public class NetworkStatsFactory {
|
|||||||
return stats;
|
return stats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: delete the lastStats parameter
|
||||||
private NetworkStats readNetworkStatsDetailInternal(int limitUid, String[] limitIfaces,
|
private NetworkStats readNetworkStatsDetailInternal(int limitUid, String[] limitIfaces,
|
||||||
int limitTag, NetworkStats lastStats) throws IOException {
|
int limitTag, NetworkStats lastStats) throws IOException {
|
||||||
if (USE_NATIVE_PARSING) {
|
if (USE_NATIVE_PARSING) {
|
||||||
@@ -278,16 +283,28 @@ public class NetworkStatsFactory {
|
|||||||
} else {
|
} else {
|
||||||
stats = new NetworkStats(SystemClock.elapsedRealtime(), -1);
|
stats = new NetworkStats(SystemClock.elapsedRealtime(), -1);
|
||||||
}
|
}
|
||||||
if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid,
|
if (mUseBpfStats) {
|
||||||
limitIfaces, limitTag, mUseBpfStats) != 0) {
|
if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), UID_ALL,
|
||||||
throw new IOException("Failed to parse network stats");
|
null, TAG_ALL, mUseBpfStats) != 0) {
|
||||||
|
throw new IOException("Failed to parse network stats");
|
||||||
|
}
|
||||||
|
mPersistSnapshot.setElapsedRealtime(stats.getElapsedRealtime());
|
||||||
|
mPersistSnapshot.combineAllValues(stats);
|
||||||
|
NetworkStats result = mPersistSnapshot.clone();
|
||||||
|
result.filter(limitUid, limitIfaces, limitTag);
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
if (nativeReadNetworkStatsDetail(stats, mStatsXtUid.getAbsolutePath(), limitUid,
|
||||||
|
limitIfaces, limitTag, mUseBpfStats) != 0) {
|
||||||
|
throw new IOException("Failed to parse network stats");
|
||||||
|
}
|
||||||
|
if (SANITY_CHECK_NATIVE) {
|
||||||
|
final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid,
|
||||||
|
limitIfaces, limitTag);
|
||||||
|
assertEquals(javaStats, stats);
|
||||||
|
}
|
||||||
|
return stats;
|
||||||
}
|
}
|
||||||
if (SANITY_CHECK_NATIVE) {
|
|
||||||
final NetworkStats javaStats = javaReadNetworkStatsDetail(mStatsXtUid, limitUid,
|
|
||||||
limitIfaces, limitTag);
|
|
||||||
assertEquals(javaStats, stats);
|
|
||||||
}
|
|
||||||
return stats;
|
|
||||||
} else {
|
} else {
|
||||||
return javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag);
|
return javaReadNetworkStatsDetail(mStatsXtUid, limitUid, limitIfaces, limitTag);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user