From: Bookatz Date: Wed, 13 Sep 2017 18:51:52 +0000 (-0700) Subject: Throttle fetching RPM stats X-Git-Tag: android-x86-8.1-r1~75^2~45^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=0b8a050853e9665152e139e9b7e1b90825f2004f;p=android-x86%2Fframeworks-base.git Throttle fetching RPM stats RPM stats are expensive to fetch. However, updateRpmStatsLocked can get called over and over again. E.g. if BatteryStatsService.getStatistics is called multiple times in quick succession, the RPM stats would be fetched repeatedly. Because it is expensive, if therefore makes sense to throttle it to ensure that the fetching is not done too frequently. The data last fetched is cached. When asked to update, it will only actually do so if it is sufficiently old. Bug: 62549421 Bug: 65629008 Test: manual Change-Id: I6b7530d203deb9ad5bfb3415336a0de6a55bd89b --- diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 90455f643f35..54c32e816bec 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -183,8 +183,12 @@ public class BatteryStatsImpl extends BatteryStats { return mKernelMemoryStats; } - /** Temporary container for Resource Power Manager stats. */ + /** Container for Resource Power Manager stats. Updated by updateRpmStatsLocked. */ private final RpmStats mTmpRpmStats = new RpmStats(); + /** The soonest the RPM stats can be updated after it was last updated. */ + private static final long RPM_STATS_UPDATE_FREQ_MS = 1000; + /** Last time that RPM stats were updated by updateRpmStatsLocked. */ + private long mLastRpmStatsUpdateTimeMs = -RPM_STATS_UPDATE_FREQ_MS; public interface BatteryCallback { public void batteryNeedsCpuUpdate(); @@ -10240,11 +10244,17 @@ public class BatteryStatsImpl extends BatteryStats { } /** - * Read and record Resource Power Manager state and voter times. + * Read and record Resource Power Manager (RPM) state and voter times. + * If RPM stats were fetched more recently than RPM_STATS_UPDATE_FREQ_MS ago, uses the old data + * instead of fetching it anew. */ public void updateRpmStatsLocked() { if (mPlatformIdleStateCallback == null) return; - mPlatformIdleStateCallback.fillLowPowerStats(mTmpRpmStats); + long now = SystemClock.elapsedRealtime(); + if (now - mLastRpmStatsUpdateTimeMs >= RPM_STATS_UPDATE_FREQ_MS) { + mPlatformIdleStateCallback.fillLowPowerStats(mTmpRpmStats); + mLastRpmStatsUpdateTimeMs = now; + } for (Map.Entry pstate : mTmpRpmStats.mPlatformLowPowerStats.entrySet()) {