Cumulative wakelock time per uid
Currently, the time for each partial wakelock was tracked. If one
wants the total time that a uid held partial wakelocks (over all of its
wakelocks), they could sum over all the uid's partial wakelock
totalTimes, but this would underestimate the value because totalTimes
are pooled amongst all uids. Alternatively, they could sum over all the
uid's partial wakelock totalDurations, but this would overestimate the
value because totalDurations are not pooled within the uid (so there
will be double-counting if two wakelocks are held simultaneously). This
cl adds a new timer that specifically tracks the actual total time that
the uid spent holding wakelocks, and also provides the ability to see
how much time the uid was in the background when doing so.
Bug: 38198272
Test: runtest -x frameworks/base/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
Change-Id: I20ea3546338c44ffdf06859bc840d9059fb18321
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index b9d3a30..fcca2f9 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -116,7 +116,7 @@
private static final int MAGIC = 0xBA757475; // 'BATSTATS'
// Current on-disk Parcel version
- private static final int VERSION = 157 + (USE_OLD_HISTORY ? 1000 : 0);
+ private static final int VERSION = 158 + (USE_OLD_HISTORY ? 1000 : 0);
// Maximum number of items we will record in the history.
private static final int MAX_HISTORY_ITEMS = 2000;
@@ -3427,8 +3427,8 @@
boolean batteryStatusChanged = mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
if (batteryStatusChanged) {
- for (int i=0; i<mUidStats.size(); i++) {
- mUidStats.valueAt(i).updateBgTimeBase(uptime, realtime);
+ for (int i = 0; i < mUidStats.size(); i++) {
+ mUidStats.valueAt(i).updateOnBatteryBgTimeBase(uptime, realtime);
}
}
@@ -3442,6 +3442,9 @@
}
updateCpuTimeLocked();
mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
+ for (int i = 0; i < mUidStats.size(); i++) {
+ mUidStats.valueAt(i).updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
+ }
}
}
@@ -5541,6 +5544,8 @@
/** TimeBase for when uid is in background and device is on battery. */
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public final TimeBase mOnBatteryBackgroundTimeBase;
+ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+ public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
boolean mWifiRunning;
StopwatchTimer mWifiRunningTimer;
@@ -5563,6 +5568,8 @@
StopwatchTimer mFlashlightTurnedOnTimer;
StopwatchTimer mCameraTurnedOnTimer;
StopwatchTimer mForegroundActivityTimer;
+ /** Total time spent by the uid holding any partial wakelocks. */
+ DualTimer mAggregatedPartialWakelockTimer;
DualTimer mBluetoothScanTimer;
Counter mBluetoothScanResultCounter;
@@ -5664,6 +5671,10 @@
mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
mBsi.mClocks.elapsedRealtime() * 1000);
+ mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
+ mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
+ mBsi.mClocks.elapsedRealtime() * 1000);
+
mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
@@ -5734,6 +5745,11 @@
}
@Override
+ public Timer getAggregatedPartialWakelockTimer() {
+ return mAggregatedPartialWakelockTimer;
+ }
+
+ @Override
public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
return mWakelockStats.getMap();
}
@@ -6022,6 +6038,15 @@
return mForegroundActivityTimer;
}
+ public DualTimer createAggregatedPartialWakelockTimerLocked() {
+ if (mAggregatedPartialWakelockTimer == null) {
+ mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
+ AGGREGATED_WAKE_TYPE_PARTIAL, null,
+ mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
+ }
+ return mAggregatedPartialWakelockTimer;
+ }
+
public DualTimer createBluetoothScanTimerLocked() {
if (mBluetoothScanTimer == null) {
mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
@@ -6464,6 +6489,7 @@
active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
+ active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
if (mBluetoothScanResultCounter != null) {
mBluetoothScanResultCounter.reset(false);
@@ -6616,6 +6642,8 @@
mOnBatteryBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
mBsi.mClocks.uptimeMillis() * 1000);
+ mOnBatteryScreenOffBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
+ mBsi.mClocks.uptimeMillis() * 1000);
if (!active) {
if (mWifiRunningTimer != null) {
@@ -6655,6 +6683,10 @@
mForegroundActivityTimer.detach();
mForegroundActivityTimer = null;
}
+ if (mAggregatedPartialWakelockTimer != null) {
+ mAggregatedPartialWakelockTimer.detach();
+ mAggregatedPartialWakelockTimer = null;
+ }
if (mBluetoothScanTimer != null) {
mBluetoothScanTimer.detach();
mBluetoothScanTimer = null;
@@ -6720,6 +6752,7 @@
void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
+ mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
int NW = wakeStats.size();
@@ -6834,6 +6867,12 @@
} else {
out.writeInt(0);
}
+ if (mAggregatedPartialWakelockTimer != null) {
+ out.writeInt(1);
+ mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
+ } else {
+ out.writeInt(0);
+ }
if (mBluetoothScanTimer != null) {
out.writeInt(1);
mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
@@ -6957,6 +6996,7 @@
void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
mOnBatteryBackgroundTimeBase.readFromParcel(in);
+ mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
int numWakelocks = in.readInt();
mWakelockStats.clear();
@@ -7083,6 +7123,14 @@
mForegroundActivityTimer = null;
}
if (in.readInt() != 0) {
+ mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
+ AGGREGATED_WAKE_TYPE_PARTIAL, null,
+ mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
+ in);
+ } else {
+ mAggregatedPartialWakelockTimer = null;
+ }
+ if (in.readInt() != 0) {
mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
mOnBatteryBackgroundTimeBase, in);
@@ -8202,15 +8250,25 @@
mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
}
- updateBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
+ updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
+ updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
}
- public boolean updateBgTimeBase(long uptimeUs, long realtimeUs) {
+ /** Whether to consider Uid to be in the background for background timebase purposes. */
+ public boolean isInBackground() {
// Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
// also considered to be 'background' for our purposes, because it's not foreground.
- boolean isBgAndUnplugged = mBsi.mOnBatteryTimeBase.isRunning()
- && mProcessState >= PROCESS_STATE_BACKGROUND;
- return mOnBatteryBackgroundTimeBase.setRunning(isBgAndUnplugged, uptimeUs, realtimeUs);
+ return mProcessState >= PROCESS_STATE_BACKGROUND;
+ }
+
+ public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
+ boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
+ return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
+ }
+
+ public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
+ boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
+ return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
}
public SparseArray<? extends Pid> getPidStats() {
@@ -8341,10 +8399,13 @@
if (wl != null) {
wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
}
- if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
- Pid p = getPidStatsLocked(pid);
- if (p.mWakeNesting++ == 0) {
- p.mWakeStartMs = elapsedRealtimeMs;
+ if (type == WAKE_TYPE_PARTIAL) {
+ createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
+ if (pid >= 0) {
+ Pid p = getPidStatsLocked(pid);
+ if (p.mWakeNesting++ == 0) {
+ p.mWakeStartMs = elapsedRealtimeMs;
+ }
}
}
}
@@ -8354,12 +8415,17 @@
if (wl != null) {
wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
}
- if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
- Pid p = mPids.get(pid);
- if (p != null && p.mWakeNesting > 0) {
- if (p.mWakeNesting-- == 1) {
- p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
- p.mWakeStartMs = 0;
+ if (type == WAKE_TYPE_PARTIAL) {
+ if (mAggregatedPartialWakelockTimer != null) {
+ mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
+ }
+ if (pid >= 0) {
+ Pid p = mPids.get(pid);
+ if (p != null && p.mWakeNesting > 0) {
+ if (p.mWakeNesting-- == 1) {
+ p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
+ p.mWakeStartMs = 0;
+ }
}
}
}
@@ -11243,6 +11309,7 @@
mUidStats.put(uid, u);
u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
+ u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
u.mWifiRunning = false;
if (in.readInt() != 0) {
@@ -11283,6 +11350,9 @@
u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
}
if (in.readInt() != 0) {
+ u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
+ }
+ if (in.readInt() != 0) {
u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
}
if (in.readInt() != 0) {
@@ -11628,6 +11698,7 @@
Uid u = mUidStats.valueAt(iu);
u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
+ u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
if (u.mWifiRunningTimer != null) {
out.writeInt(1);
@@ -11691,6 +11762,12 @@
} else {
out.writeInt(0);
}
+ if (u.mAggregatedPartialWakelockTimer != null) {
+ out.writeInt(1);
+ u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
+ } else {
+ out.writeInt(0);
+ }
if (u.mBluetoothScanTimer != null) {
out.writeInt(1);
u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);