Clear TrafficStats cache before getting readings from TrafficStats

Currently, TrafficStats have rate-limit on all get* APIs.
It is possible that result of uid traffic is updated but result
of iface is not. Thus, clear cache before read.

Test: atest CtsNetTestCases:android.net.cts.IpSecManagerTest#testIkeOverUdpEncapSocket --rerun-until-failure 100
Test: atest FrameworksNetIntegrationTests:com.android.server.net.integrationtests.NetworkStatsIntegrationTest
Bug: N/A
Change-Id: I1b7574d3062837fdded79cb945b582f2afad859b
This commit is contained in:
Junyu Lai 2024-02-21 11:10:58 +08:00
commit 6c277682d5
2 changed files with 21 additions and 0 deletions

View file

@ -81,6 +81,7 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.android.modules.utils.build.SdkLevel;
import com.android.testutils.ConnectivityModuleTest;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.SkipMainlinePresubmit;
@ -101,6 +102,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@ConnectivityModuleTest
@RunWith(AndroidJUnit4.class)
@AppModeFull(reason = "Socket cannot bind in instant app mode")
public class IpSecManagerTest extends IpSecBaseTest {
@ -444,6 +446,11 @@ public class IpSecManagerTest extends IpSecBaseTest {
long uidTxDelta = 0;
long uidRxDelta = 0;
for (int i = 0; i < 100; i++) {
// Clear TrafficStats cache is needed to avoid rate-limit caching for
// TrafficStats API results on V+ devices.
if (SdkLevel.isAtLeastV()) {
runAsShell(NETWORK_SETTINGS, () -> TrafficStats.clearRateLimitCaches());
}
uidTxDelta = TrafficStats.getUidTxPackets(Os.getuid()) - uidTxPackets;
uidRxDelta = TrafficStats.getUidRxPackets(Os.getuid()) - uidRxPackets;
@ -518,6 +525,11 @@ public class IpSecManagerTest extends IpSecBaseTest {
}
private static void initStatsChecker() throws Exception {
// Clear TrafficStats cache is needed to avoid rate-limit caching for
// TrafficStats API results on V+ devices.
if (SdkLevel.isAtLeastV()) {
runAsShell(NETWORK_SETTINGS, () -> TrafficStats.clearRateLimitCaches());
}
uidTxBytes = TrafficStats.getUidTxBytes(Os.getuid());
uidRxBytes = TrafficStats.getUidRxBytes(Os.getuid());
uidTxPackets = TrafficStats.getUidTxPackets(Os.getuid());

View file

@ -38,6 +38,7 @@ import android.net.TrafficStats
import android.os.Build
import android.os.Process
import androidx.test.platform.app.InstrumentationRegistry
import com.android.modules.utils.build.SdkLevel
import com.android.server.net.integrationtests.NetworkStatsIntegrationTest.Direction.DOWNLOAD
import com.android.server.net.integrationtests.NetworkStatsIntegrationTest.Direction.UPLOAD
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
@ -214,6 +215,11 @@ class NetworkStatsIntegrationTest {
// In practice, for one way 10k download payload, the download usage is about
// 11222~12880 bytes, with 14~17 packets. And the upload usage is about 1279~1626 bytes
// with 14~17 packets, which is majorly contributed by TCP ACK packets.
// Clear TrafficStats cache is needed to avoid rate-limit caching for
// TrafficStats API results on V+ devices.
if (SdkLevel.isAtLeastV()) {
TrafficStats.clearRateLimitCaches()
}
val snapshotAfterDownload = StatsSnapshot(context, internalInterfaceName)
val (expectedDownloadLower, expectedDownloadUpper) = getExpectedStatsBounds(
TEST_DOWNLOAD_SIZE,
@ -236,6 +242,9 @@ class NetworkStatsIntegrationTest {
)
// Verify upload data usage accounting.
if (SdkLevel.isAtLeastV()) {
TrafficStats.clearRateLimitCaches()
}
val snapshotAfterUpload = StatsSnapshot(context, internalInterfaceName)
val (expectedUploadLower, expectedUploadUpper) = getExpectedStatsBounds(
TEST_UPLOAD_SIZE,