Loading core/java/com/android/internal/os/BatteryStatsImpl.java +33 −9 Original line number Diff line number Diff line Loading @@ -191,6 +191,11 @@ public class BatteryStatsImpl extends BatteryStats { @VisibleForTesting public static final int WAKE_LOCK_WEIGHT = 50; public static final int RESET_REASON_CORRUPT_FILE = 1; public static final int RESET_REASON_ADB_COMMAND = 2; public static final int RESET_REASON_FULL_CHARGE = 3; public static final int RESET_REASON_MEASURED_ENERGY_BUCKETS_CHANGE = 4; protected Clocks mClocks; private final AtomicFile mStatsFile; Loading Loading @@ -348,8 +353,9 @@ public class BatteryStatsImpl extends BatteryStats { /** * Callback invoked immediately prior to resetting battery stats. * @param resetReason One of the RESET_REASON_* constants. */ void prepareForBatteryStatsReset(); void prepareForBatteryStatsReset(int resetReason); } private BatteryResetListener mBatteryResetListener; Loading Loading @@ -747,6 +753,7 @@ public class BatteryStatsImpl extends BatteryStats { // CPU update, even if we aren't currently running wake locks. boolean mDistributeWakelockCpu; private boolean mSystemReady; boolean mShuttingDown; final HistoryEventTracker mActiveEvents = new HistoryEventTracker(); Loading Loading @@ -11210,7 +11217,7 @@ public class BatteryStatsImpl extends BatteryStats { long uptimeUs = mSecUptime * 1000; long mSecRealtime = mClocks.elapsedRealtime(); long realtimeUs = mSecRealtime * 1000; resetAllStatsLocked(mSecUptime, mSecRealtime); resetAllStatsLocked(mSecUptime, mSecRealtime, RESET_REASON_ADB_COMMAND); mDischargeStartLevel = mHistoryCur.batteryLevel; pullPendingStateUpdatesLocked(); addHistoryRecordLocked(mSecRealtime, mSecUptime); Loading Loading @@ -11239,9 +11246,10 @@ public class BatteryStatsImpl extends BatteryStats { initActiveHistoryEventsLocked(mSecRealtime, mSecUptime); } private void resetAllStatsLocked(long uptimeMillis, long elapsedRealtimeMillis) { private void resetAllStatsLocked(long uptimeMillis, long elapsedRealtimeMillis, int resetReason) { if (mBatteryResetListener != null) { mBatteryResetListener.prepareForBatteryStatsReset(); mBatteryResetListener.prepareForBatteryStatsReset(resetReason); } final long uptimeUs = uptimeMillis * 1000; Loading Loading @@ -13477,6 +13485,13 @@ public class BatteryStatsImpl extends BatteryStats { return false; } /** * Notifies BatteryStatsImpl that the system server is ready. */ public void onSystemReady() { mSystemReady = true; } @GuardedBy("this") protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery, final int oldStatus, final int level, final int chargeUah) { Loading @@ -13493,10 +13508,17 @@ public class BatteryStatsImpl extends BatteryStats { // battery was last full, or the level is at 100, or // we have gone through a significant charge (from a very low // level to a now very high level). // Also, we will reset the stats if battery got partially charged // and discharged repeatedly without ever reaching the full charge. // This reset is done in order to prevent stats sessions from going on forever. // Exceedingly long battery sessions would lead to an overflow of // data structures such as mWakeupReasonStats. boolean reset = false; if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL if (!mNoAutoReset && mSystemReady && (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 90 || (mDischargeCurrentLevel < 20 && level >= 80))) { || (mDischargeCurrentLevel < 20 && level >= 80) || getHighDischargeAmountSinceCharge() >= 200)) { Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus + " dischargeLevel=" + mDischargeCurrentLevel + " lowAmount=" + getLowDischargeAmountSinceCharge() Loading Loading @@ -13534,7 +13556,7 @@ public class BatteryStatsImpl extends BatteryStats { }); } doWrite = true; resetAllStatsLocked(mSecUptime, mSecRealtime); resetAllStatsLocked(mSecUptime, mSecRealtime, RESET_REASON_FULL_CHARGE); if (chargeUah > 0 && level > 0) { // Only use the reported coulomb charge value if it is supported and reported. mEstimatedBatteryCapacityMah = (int) ((chargeUah / 1000) / (level / 100.0)); Loading Loading @@ -14502,7 +14524,8 @@ public class BatteryStatsImpl extends BatteryStats { ? null : new MeasuredEnergyStats(supportedStandardBuckets, customBucketNames); // Supported power buckets changed since last boot. // Existing data is no longer reliable. resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime()); resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime(), RESET_REASON_MEASURED_ENERGY_BUCKETS_CHANGE); } } Loading Loading @@ -14949,7 +14972,8 @@ public class BatteryStatsImpl extends BatteryStats { } } catch (Exception e) { Slog.e(TAG, "Error reading battery statistics", e); resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime()); resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime(), RESET_REASON_CORRUPT_FILE); } finally { stats.recycle(); } core/java/com/android/internal/os/BatteryUsageStatsStore.java +13 −1 Original line number Diff line number Diff line Loading @@ -69,6 +69,7 @@ public class BatteryUsageStatsStore { private final Context mContext; private final BatteryStatsImpl mBatteryStats; private boolean mSystemReady; private final File mStoreDir; private final File mLockFile; private final AtomicFile mConfigFile; Loading @@ -95,7 +96,18 @@ public class BatteryUsageStatsStore { mBatteryUsageStatsProvider = new BatteryUsageStatsProvider(mContext, mBatteryStats); } private void prepareForBatteryStatsReset() { /** * Notifies BatteryUsageStatsStore that the system server is ready. */ public void onSystemReady() { mSystemReady = true; } private void prepareForBatteryStatsReset(int resetReason) { if (resetReason == BatteryStatsImpl.RESET_REASON_CORRUPT_FILE || !mSystemReady) { return; } final List<BatteryUsageStats> stats = mBatteryUsageStatsProvider.getBatteryUsageStats(BATTERY_USAGE_STATS_QUERY); if (stats.isEmpty()) { Loading core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsProviderTest.java +1 −0 Original line number Diff line number Diff line Loading @@ -204,6 +204,7 @@ public class BatteryUsageStatsProviderTest { BatteryUsageStatsStore batteryUsageStatsStore = new BatteryUsageStatsStore(context, batteryStats, new File(context.getCacheDir(), "BatteryUsageStatsProviderTest"), new TestHandler(), Integer.MAX_VALUE); batteryUsageStatsStore.onSystemReady(); BatteryUsageStatsProvider provider = new BatteryUsageStatsProvider(context, batteryStats, batteryUsageStatsStore); Loading core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsRule.java +1 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,7 @@ public class BatteryUsageStatsRule implements TestRule { mMockClocks.currentTime = currentTime; mBatteryStats = new MockBatteryStatsImpl(mMockClocks); mBatteryStats.setPowerProfile(mPowerProfile); mBatteryStats.onSystemReady(); } public BatteryUsageStatsRule setAveragePower(String key, double value) { Loading core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsStoreTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -59,6 +59,7 @@ public class BatteryUsageStatsStoreTest { mBatteryStats = new MockBatteryStatsImpl(mMockClocks); mBatteryStats.setNoAutoReset(true); mBatteryStats.setPowerProfile(mock(PowerProfile.class)); mBatteryStats.onSystemReady(); Context context = InstrumentationRegistry.getContext(); Loading @@ -67,6 +68,7 @@ public class BatteryUsageStatsStoreTest { mBatteryUsageStatsStore = new BatteryUsageStatsStore(context, mBatteryStats, mStoreDirectory, new TestHandler(), MAX_BATTERY_STATS_SNAPSHOT_STORAGE_BYTES); mBatteryUsageStatsStore.onSystemReady(); mBatteryUsageStatsProvider = new BatteryUsageStatsProvider(context, mBatteryStats); } Loading Loading
core/java/com/android/internal/os/BatteryStatsImpl.java +33 −9 Original line number Diff line number Diff line Loading @@ -191,6 +191,11 @@ public class BatteryStatsImpl extends BatteryStats { @VisibleForTesting public static final int WAKE_LOCK_WEIGHT = 50; public static final int RESET_REASON_CORRUPT_FILE = 1; public static final int RESET_REASON_ADB_COMMAND = 2; public static final int RESET_REASON_FULL_CHARGE = 3; public static final int RESET_REASON_MEASURED_ENERGY_BUCKETS_CHANGE = 4; protected Clocks mClocks; private final AtomicFile mStatsFile; Loading Loading @@ -348,8 +353,9 @@ public class BatteryStatsImpl extends BatteryStats { /** * Callback invoked immediately prior to resetting battery stats. * @param resetReason One of the RESET_REASON_* constants. */ void prepareForBatteryStatsReset(); void prepareForBatteryStatsReset(int resetReason); } private BatteryResetListener mBatteryResetListener; Loading Loading @@ -747,6 +753,7 @@ public class BatteryStatsImpl extends BatteryStats { // CPU update, even if we aren't currently running wake locks. boolean mDistributeWakelockCpu; private boolean mSystemReady; boolean mShuttingDown; final HistoryEventTracker mActiveEvents = new HistoryEventTracker(); Loading Loading @@ -11210,7 +11217,7 @@ public class BatteryStatsImpl extends BatteryStats { long uptimeUs = mSecUptime * 1000; long mSecRealtime = mClocks.elapsedRealtime(); long realtimeUs = mSecRealtime * 1000; resetAllStatsLocked(mSecUptime, mSecRealtime); resetAllStatsLocked(mSecUptime, mSecRealtime, RESET_REASON_ADB_COMMAND); mDischargeStartLevel = mHistoryCur.batteryLevel; pullPendingStateUpdatesLocked(); addHistoryRecordLocked(mSecRealtime, mSecUptime); Loading Loading @@ -11239,9 +11246,10 @@ public class BatteryStatsImpl extends BatteryStats { initActiveHistoryEventsLocked(mSecRealtime, mSecUptime); } private void resetAllStatsLocked(long uptimeMillis, long elapsedRealtimeMillis) { private void resetAllStatsLocked(long uptimeMillis, long elapsedRealtimeMillis, int resetReason) { if (mBatteryResetListener != null) { mBatteryResetListener.prepareForBatteryStatsReset(); mBatteryResetListener.prepareForBatteryStatsReset(resetReason); } final long uptimeUs = uptimeMillis * 1000; Loading Loading @@ -13477,6 +13485,13 @@ public class BatteryStatsImpl extends BatteryStats { return false; } /** * Notifies BatteryStatsImpl that the system server is ready. */ public void onSystemReady() { mSystemReady = true; } @GuardedBy("this") protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery, final int oldStatus, final int level, final int chargeUah) { Loading @@ -13493,10 +13508,17 @@ public class BatteryStatsImpl extends BatteryStats { // battery was last full, or the level is at 100, or // we have gone through a significant charge (from a very low // level to a now very high level). // Also, we will reset the stats if battery got partially charged // and discharged repeatedly without ever reaching the full charge. // This reset is done in order to prevent stats sessions from going on forever. // Exceedingly long battery sessions would lead to an overflow of // data structures such as mWakeupReasonStats. boolean reset = false; if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL if (!mNoAutoReset && mSystemReady && (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 90 || (mDischargeCurrentLevel < 20 && level >= 80))) { || (mDischargeCurrentLevel < 20 && level >= 80) || getHighDischargeAmountSinceCharge() >= 200)) { Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus + " dischargeLevel=" + mDischargeCurrentLevel + " lowAmount=" + getLowDischargeAmountSinceCharge() Loading Loading @@ -13534,7 +13556,7 @@ public class BatteryStatsImpl extends BatteryStats { }); } doWrite = true; resetAllStatsLocked(mSecUptime, mSecRealtime); resetAllStatsLocked(mSecUptime, mSecRealtime, RESET_REASON_FULL_CHARGE); if (chargeUah > 0 && level > 0) { // Only use the reported coulomb charge value if it is supported and reported. mEstimatedBatteryCapacityMah = (int) ((chargeUah / 1000) / (level / 100.0)); Loading Loading @@ -14502,7 +14524,8 @@ public class BatteryStatsImpl extends BatteryStats { ? null : new MeasuredEnergyStats(supportedStandardBuckets, customBucketNames); // Supported power buckets changed since last boot. // Existing data is no longer reliable. resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime()); resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime(), RESET_REASON_MEASURED_ENERGY_BUCKETS_CHANGE); } } Loading Loading @@ -14949,7 +14972,8 @@ public class BatteryStatsImpl extends BatteryStats { } } catch (Exception e) { Slog.e(TAG, "Error reading battery statistics", e); resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime()); resetAllStatsLocked(SystemClock.uptimeMillis(), SystemClock.elapsedRealtime(), RESET_REASON_CORRUPT_FILE); } finally { stats.recycle(); }
core/java/com/android/internal/os/BatteryUsageStatsStore.java +13 −1 Original line number Diff line number Diff line Loading @@ -69,6 +69,7 @@ public class BatteryUsageStatsStore { private final Context mContext; private final BatteryStatsImpl mBatteryStats; private boolean mSystemReady; private final File mStoreDir; private final File mLockFile; private final AtomicFile mConfigFile; Loading @@ -95,7 +96,18 @@ public class BatteryUsageStatsStore { mBatteryUsageStatsProvider = new BatteryUsageStatsProvider(mContext, mBatteryStats); } private void prepareForBatteryStatsReset() { /** * Notifies BatteryUsageStatsStore that the system server is ready. */ public void onSystemReady() { mSystemReady = true; } private void prepareForBatteryStatsReset(int resetReason) { if (resetReason == BatteryStatsImpl.RESET_REASON_CORRUPT_FILE || !mSystemReady) { return; } final List<BatteryUsageStats> stats = mBatteryUsageStatsProvider.getBatteryUsageStats(BATTERY_USAGE_STATS_QUERY); if (stats.isEmpty()) { Loading
core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsProviderTest.java +1 −0 Original line number Diff line number Diff line Loading @@ -204,6 +204,7 @@ public class BatteryUsageStatsProviderTest { BatteryUsageStatsStore batteryUsageStatsStore = new BatteryUsageStatsStore(context, batteryStats, new File(context.getCacheDir(), "BatteryUsageStatsProviderTest"), new TestHandler(), Integer.MAX_VALUE); batteryUsageStatsStore.onSystemReady(); BatteryUsageStatsProvider provider = new BatteryUsageStatsProvider(context, batteryStats, batteryUsageStatsStore); Loading
core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsRule.java +1 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,7 @@ public class BatteryUsageStatsRule implements TestRule { mMockClocks.currentTime = currentTime; mBatteryStats = new MockBatteryStatsImpl(mMockClocks); mBatteryStats.setPowerProfile(mPowerProfile); mBatteryStats.onSystemReady(); } public BatteryUsageStatsRule setAveragePower(String key, double value) { Loading
core/tests/coretests/src/com/android/internal/os/BatteryUsageStatsStoreTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -59,6 +59,7 @@ public class BatteryUsageStatsStoreTest { mBatteryStats = new MockBatteryStatsImpl(mMockClocks); mBatteryStats.setNoAutoReset(true); mBatteryStats.setPowerProfile(mock(PowerProfile.class)); mBatteryStats.onSystemReady(); Context context = InstrumentationRegistry.getContext(); Loading @@ -67,6 +68,7 @@ public class BatteryUsageStatsStoreTest { mBatteryUsageStatsStore = new BatteryUsageStatsStore(context, mBatteryStats, mStoreDirectory, new TestHandler(), MAX_BATTERY_STATS_SNAPSHOT_STORAGE_BYTES); mBatteryUsageStatsStore.onSystemReady(); mBatteryUsageStatsProvider = new BatteryUsageStatsProvider(context, mBatteryStats); } Loading