From 6c277682d55136fbfdfb4f8de5ac175208071767 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Wed, 21 Feb 2024 11:10:58 +0800 Subject: [PATCH] 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 --- .../net/src/android/net/cts/IpSecManagerTest.java | 12 ++++++++++++ .../integrationtests/NetworkStatsIntegrationTest.kt | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/cts/net/src/android/net/cts/IpSecManagerTest.java b/tests/cts/net/src/android/net/cts/IpSecManagerTest.java index a40ed0f127..b703f773af 100644 --- a/tests/cts/net/src/android/net/cts/IpSecManagerTest.java +++ b/tests/cts/net/src/android/net/cts/IpSecManagerTest.java @@ -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()); diff --git a/tests/integration/src/com/android/server/net/integrationtests/NetworkStatsIntegrationTest.kt b/tests/integration/src/com/android/server/net/integrationtests/NetworkStatsIntegrationTest.kt index 52e502dd4f..4780c5d4b9 100644 --- a/tests/integration/src/com/android/server/net/integrationtests/NetworkStatsIntegrationTest.kt +++ b/tests/integration/src/com/android/server/net/integrationtests/NetworkStatsIntegrationTest.kt @@ -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,