diff options
author | Dmitri Plotnikov <dplotnikov@google.com> | 2021-06-29 15:47:32 -0700 |
---|---|---|
committer | Dmitri Plotnikov <dplotnikov@google.com> | 2021-07-01 22:04:28 -0700 |
commit | dc3a78354e5a25432825ec0ef11cfcb6a75fad52 (patch) | |
tree | 0844f13d966891908f421c8faaf1c29f5f19299f /core/tests | |
parent | fb0faec367832e8fdc05508d831a697972dfa4f2 (diff) |
Address race condition in procstate time tracking
The issue occurs when the application proc state changes
rapidly, e.g. from FOREGROUND to BACKGROUND to CACHED.
The original code would sometimes attribute the time slice
to the wrong proc state.
Bug: 192550308
Test: (on cuttlefish) atest --rerun-until-failure 300 FrameworksCoreTests:com.android.internal.os.BstatsCpuTimesValidationTest -- --abi x86_64
Change-Id: Ic22bbfa3aae701014fc016ec9e2d32b1c528d462
Diffstat (limited to 'core/tests')
-rw-r--r-- | core/tests/coretests/src/com/android/internal/os/BstatsCpuTimesValidationTest.java | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/core/tests/coretests/src/com/android/internal/os/BstatsCpuTimesValidationTest.java b/core/tests/coretests/src/com/android/internal/os/BstatsCpuTimesValidationTest.java index 1fc3a2118385..4ef45e2b126b 100644 --- a/core/tests/coretests/src/com/android/internal/os/BstatsCpuTimesValidationTest.java +++ b/core/tests/coretests/src/com/android/internal/os/BstatsCpuTimesValidationTest.java @@ -584,7 +584,7 @@ public class BstatsCpuTimesValidationTest { actualCpuTimeMs += cpuTimesMs[i]; } assertApproximateValue("Incorrect total cpu time, " + msgCpuTimes, - 2 * WORK_DURATION_MS, actualCpuTimeMs); + WORK_DURATION_MS, actualCpuTimeMs); batteryOffScreenOn(); } finally { @@ -656,8 +656,14 @@ public class BstatsCpuTimesValidationTest { } } - private void assertApproximateValue(String errorPrefix, long expectedValue, long actualValue) { - assertValueRange(errorPrefix, actualValue, expectedValue * 0.5, expectedValue * 1.5); + private void assertApproximateValue(String errorPrefix, long expectedValueMs, + long actualValueMs) { + // Allow the actual value to be 1 second smaller than the expected. + // Also allow it to be up to 5 seconds larger, to accommodate the arbitrary + // latency introduced by BatteryExternalStatsWorker.scheduleReadProcStateCpuTimes + assertValueRange(errorPrefix, actualValueMs, + expectedValueMs - 1000, + expectedValueMs + 5000); } private void assertValueRange(String errorPrefix, |