OSDN Git Service

Throttle fetching RPM stats
authorBookatz <bookatz@google.com>
Wed, 13 Sep 2017 18:51:52 +0000 (11:51 -0700)
committerBookatz <bookatz@google.com>
Wed, 13 Sep 2017 21:31:14 +0000 (14:31 -0700)
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

core/java/com/android/internal/os/BatteryStatsImpl.java

index 90455f6..54c32e8 100644 (file)
@@ -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<String, RpmStats.PowerStatePlatformSleepState> pstate
                 : mTmpRpmStats.mPlatformLowPowerStats.entrySet()) {