Battery stats: wake locks, radio active, cleanup.
- Improve wake lock work source updates to also update the current
history tag, in case the new work source gets recorded in the
history.
- Fix bug in recording radio active time that was not distributing
any time to apps.
- No longer hold a wake lock while dispatching data conn active call,
since it comes with its own timestamp.
- Fix issue where the top app was not being cleared while the screen
was off.
- Remove obsolete STATS_LAST stats type.
- Fix bug that was not clearing the total run time when resetting
the stats.
Change-Id: Iabe17a9edf34f762374ae09fcffb8a819cf72e30
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index 87beb95..426f21e 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -118,19 +118,14 @@
public static final int STATS_SINCE_CHARGED = 0;
/**
- * Include only the last run in the stats.
- */
- public static final int STATS_LAST = 1;
-
- /**
* Include only the current run in the stats.
*/
- public static final int STATS_CURRENT = 2;
+ public static final int STATS_CURRENT = 1;
/**
* Include only the run since the last time the device was unplugged in the stats.
*/
- public static final int STATS_SINCE_UNPLUGGED = 3;
+ public static final int STATS_SINCE_UNPLUGGED = 2;
// NOTE: Update this list if you add/change any stats above.
// These characters are supposed to represent "total", "last", "current",
diff --git a/core/java/android/os/IPowerManager.aidl b/core/java/android/os/IPowerManager.aidl
index 069285a..d03b0c5 100644
--- a/core/java/android/os/IPowerManager.aidl
+++ b/core/java/android/os/IPowerManager.aidl
@@ -32,7 +32,7 @@
void releaseWakeLock(IBinder lock, int flags);
void updateWakeLockUids(IBinder lock, in int[] uids);
- void updateWakeLockWorkSource(IBinder lock, in WorkSource ws);
+ void updateWakeLockWorkSource(IBinder lock, in WorkSource ws, String historyTag);
boolean isWakeLockLevelSupported(int level);
void userActivity(long time, int event, int flags);
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java
index 74ca3bb..a195200 100644
--- a/core/java/android/os/PowerManager.java
+++ b/core/java/android/os/PowerManager.java
@@ -844,7 +844,7 @@
if (changed && mHeld) {
try {
- mService.updateWakeLockWorkSource(mToken, mWorkSource);
+ mService.updateWakeLockWorkSource(mToken, mWorkSource, mHistoryTag);
} catch (RemoteException e) {
}
}
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index 1d88533..39636fe 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -230,10 +230,8 @@
long mUptime;
long mUptimeStart;
- long mLastUptime;
long mRealtime;
long mRealtimeStart;
- long mLastRealtime;
int mWakeLockNesting;
boolean mWakeLockImportant;
@@ -408,9 +406,7 @@
private final ArrayList<TimeBaseObs> mObservers = new ArrayList<TimeBaseObs>();
private long mUptime;
- private long mLastUptime;
private long mRealtime;
- private long mLastRealtime;
private boolean mRunning;
@@ -427,14 +423,12 @@
sb.setLength(0);
sb.append(prefix);
sb.append("mUptime=");
- formatTimeMs(sb, mUptime / 1000); sb.append("mLastUptime=");
- formatTimeMs(sb, mLastUptime / 1000);
+ formatTimeMs(sb, mUptime / 1000);
pw.println(sb.toString());
sb.setLength(0);
sb.append(prefix);
sb.append("mRealtime=");
- formatTimeMs(sb, mRealtime / 1000); sb.append("mLastRealtime=");
- formatTimeMs(sb, mLastRealtime / 1000);
+ formatTimeMs(sb, mRealtime / 1000);
pw.println(sb.toString());
sb.setLength(0);
sb.append(prefix);
@@ -489,8 +483,6 @@
switch (which) {
case STATS_SINCE_CHARGED:
return mUptime + getUptime(curTime);
- case STATS_LAST:
- return mLastUptime;
case STATS_CURRENT:
return getUptime(curTime);
case STATS_SINCE_UNPLUGGED:
@@ -503,8 +495,6 @@
switch (which) {
case STATS_SINCE_CHARGED:
return mRealtime + getRealtime(curTime);
- case STATS_LAST:
- return mLastRealtime;
case STATS_CURRENT:
return getRealtime(curTime);
case STATS_SINCE_UNPLUGGED:
@@ -582,7 +572,6 @@
public void readFromParcel(Parcel in) {
mRunning = false;
mUptime = in.readLong();
- mLastUptime = 0;
mPastUptime = in.readLong();
mUptimeStart = in.readLong();
mPastRealtime = in.readLong();
@@ -663,16 +652,11 @@
@Override
public int getCountLocked(int which) {
- int val;
- if (which == STATS_LAST) {
- val = mLastCount;
- } else {
- val = mCount.get();
- if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedCount;
- } else if (which != STATS_SINCE_CHARGED) {
- val -= mLoadedCount;
- }
+ int val = mCount.get();
+ if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedCount;
+ } else if (which != STATS_SINCE_CHARGED) {
+ val -= mLoadedCount;
}
return val;
@@ -772,16 +756,11 @@
}
public long getCountLocked(int which) {
- long val;
- if (which == STATS_LAST) {
- val = mLastCount;
- } else {
- val = mCount;
- if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedCount;
- } else if (which != STATS_SINCE_CHARGED) {
- val -= mLoadedCount;
- }
+ long val = mCount;
+ if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedCount;
+ } else if (which != STATS_SINCE_CHARGED) {
+ val -= mLoadedCount;
}
return val;
@@ -970,16 +949,11 @@
@Override
public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
- long val;
- if (which == STATS_LAST) {
- val = mLastTime;
- } else {
- val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
- if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedTime;
- } else if (which != STATS_SINCE_CHARGED) {
- val -= mLoadedTime;
- }
+ long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
+ if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedTime;
+ } else if (which != STATS_SINCE_CHARGED) {
+ val -= mLoadedTime;
}
return val;
@@ -987,16 +961,11 @@
@Override
public int getCountLocked(int which) {
- int val;
- if (which == STATS_LAST) {
- val = mLastCount;
- } else {
- val = computeCurrentCountLocked();
- if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedCount;
- } else if (which != STATS_SINCE_CHARGED) {
- val -= mLoadedCount;
- }
+ int val = computeCurrentCountLocked();
+ if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedCount;
+ } else if (which != STATS_SINCE_CHARGED) {
+ val -= mLoadedCount;
}
return val;
@@ -2209,8 +2178,7 @@
mHistoryCur.eventTag = mHistoryCur.localEventTag;
mHistoryCur.eventTag.string = name;
mHistoryCur.eventTag.uid = uid;
- // XXX should be calling addHistoryRecordLocked()?
- addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
+ addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
}
void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
@@ -2797,8 +2765,8 @@
mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
} else {
mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
- mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
updateNetworkActivityLocked(NET_UPDATE_MOBILE, realElapsedRealtimeMs);
+ mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
}
}
}
@@ -4890,64 +4858,44 @@
@Override
public long getUserTime(int which) {
- long val;
- if (which == STATS_LAST) {
- val = mLastUserTime;
- } else {
- val = mUserTime;
- if (which == STATS_CURRENT) {
- val -= mLoadedUserTime;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedUserTime;
- }
+ long val = mUserTime;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedUserTime;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedUserTime;
}
return val;
}
@Override
public long getSystemTime(int which) {
- long val;
- if (which == STATS_LAST) {
- val = mLastSystemTime;
- } else {
- val = mSystemTime;
- if (which == STATS_CURRENT) {
- val -= mLoadedSystemTime;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedSystemTime;
- }
+ long val = mSystemTime;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedSystemTime;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedSystemTime;
}
return val;
}
@Override
public long getForegroundTime(int which) {
- long val;
- if (which == STATS_LAST) {
- val = mLastForegroundTime;
- } else {
- val = mForegroundTime;
- if (which == STATS_CURRENT) {
- val -= mLoadedForegroundTime;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedForegroundTime;
- }
+ long val = mForegroundTime;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedForegroundTime;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedForegroundTime;
}
return val;
}
@Override
public int getStarts(int which) {
- int val;
- if (which == STATS_LAST) {
- val = mLastStarts;
- } else {
- val = mStarts;
- if (which == STATS_CURRENT) {
- val -= mLoadedStarts;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedStarts;
- }
+ int val = mStarts;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedStarts;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedStarts;
}
return val;
}
@@ -5063,16 +5011,11 @@
@Override
public int getWakeups(int which) {
- int val;
- if (which == STATS_LAST) {
- val = mLastWakeups;
- } else {
- val = mWakeups;
- if (which == STATS_CURRENT) {
- val -= mLoadedWakeups;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedWakeups;
- }
+ int val = mWakeups;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedWakeups;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedWakeups;
}
return val;
@@ -5284,51 +5227,33 @@
@Override
public int getLaunches(int which) {
- int val;
-
- if (which == STATS_LAST) {
- val = mLastLaunches;
- } else {
- val = mLaunches;
- if (which == STATS_CURRENT) {
- val -= mLoadedLaunches;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedLaunches;
- }
+ int val = mLaunches;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedLaunches;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedLaunches;
}
-
return val;
}
@Override
public long getStartTime(long now, int which) {
- long val;
- if (which == STATS_LAST) {
- val = mLastStartTime;
- } else {
- val = getStartTimeToNowLocked(now);
- if (which == STATS_CURRENT) {
- val -= mLoadedStartTime;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedStartTime;
- }
+ long val = getStartTimeToNowLocked(now);
+ if (which == STATS_CURRENT) {
+ val -= mLoadedStartTime;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedStartTime;
}
-
return val;
}
@Override
public int getStarts(int which) {
- int val;
- if (which == STATS_LAST) {
- val = mLastStarts;
- } else {
- val = mStarts;
- if (which == STATS_CURRENT) {
- val -= mLoadedStarts;
- } else if (which == STATS_SINCE_UNPLUGGED) {
- val -= mUnpluggedStarts;
- }
+ int val = mStarts;
+ if (which == STATS_CURRENT) {
+ val -= mLoadedStarts;
+ } else if (which == STATS_SINCE_UNPLUGGED) {
+ val -= mUnpluggedStarts;
}
return val;
@@ -5591,8 +5516,6 @@
long uptime = SystemClock.uptimeMillis() * 1000;
long realtime = SystemClock.elapsedRealtime() * 1000;
initTimes(uptime, realtime);
- mUptimeStart = uptime;
- mRealtimeStart = realtime;
mDischargeStartLevel = 0;
mDischargeUnplugLevel = 0;
mDischargeCurrentLevel = 0;
@@ -5772,8 +5695,10 @@
mStartClockTime = System.currentTimeMillis();
mOnBatteryTimeBase.init(uptime, realtime);
mOnBatteryScreenOffTimeBase.init(uptime, realtime);
- mUptimeStart = uptime;
+ mRealtime = 0;
+ mUptime = 0;
mRealtimeStart = realtime;
+ mUptimeStart = uptime;
}
void initDischarge() {
@@ -6256,9 +6181,8 @@
public long computeUptime(long curTime, int which) {
switch (which) {
case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
- case STATS_LAST: return mLastUptime;
case STATS_CURRENT: return (curTime-mUptimeStart);
- case STATS_SINCE_UNPLUGGED: return (curTime- mOnBatteryTimeBase.getUptimeStart());
+ case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
}
return 0;
}
@@ -6267,9 +6191,8 @@
public long computeRealtime(long curTime, int which) {
switch (which) {
case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
- case STATS_LAST: return mLastRealtime;
case STATS_CURRENT: return (curTime-mRealtimeStart);
- case STATS_SINCE_UNPLUGGED: return (curTime- mOnBatteryTimeBase.getRealtimeStart());
+ case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
}
return 0;
}
@@ -7303,10 +7226,8 @@
mStartClockTime = in.readLong();
mUptime = in.readLong();
mUptimeStart = in.readLong();
- mLastUptime = 0;
mRealtime = in.readLong();
mRealtimeStart = in.readLong();
- mLastRealtime = 0;
mOnBattery = in.readInt() != 0;
mOnBatteryInternal = false; // we are no longer really running.
mOnBatteryTimeBase.readFromParcel(in);
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index 41ffdc1..168742f 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -1348,25 +1348,22 @@
try {
final boolean unimportant = pi == mTimeTickSender;
mWakeLock.setUnimportantForLogging(unimportant);
+ if (first || mLastWakeLockUnimportantForLogging) {
+ mWakeLock.setHistoryTag(pi.getTag(
+ type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP
+ ? "*walarm*:" : "*alarm*:"));
+ } else {
+ mWakeLock.setHistoryTag(null);
+ }
+ mLastWakeLockUnimportantForLogging = unimportant;
if (ws != null) {
- if (first || mLastWakeLockUnimportantForLogging) {
- mWakeLock.setHistoryTag(pi.getTag(
- type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP
- ? "*walarm*:" : "*alarm*:"));
- } else {
- mWakeLock.setHistoryTag(null);
- }
- mLastWakeLockUnimportantForLogging = unimportant;
mWakeLock.setWorkSource(ws);
return;
- } else {
- mLastWakeLockUnimportantForLogging = false;
}
final int uid = ActivityManagerNative.getDefault()
.getUidForIntentSender(pi.getTarget());
if (uid >= 0) {
- mWakeLock.setHistoryTag(null);
mWakeLock.setWorkSource(new WorkSource(uid));
return;
}
diff --git a/services/core/java/com/android/server/NativeDaemonConnector.java b/services/core/java/com/android/server/NativeDaemonConnector.java
index 265b957..62eb663 100644
--- a/services/core/java/com/android/server/NativeDaemonConnector.java
+++ b/services/core/java/com/android/server/NativeDaemonConnector.java
@@ -110,7 +110,7 @@
} catch (Exception e) {
loge("Error handling '" + event + "': " + e);
} finally {
- if (mCallbacks.onCheckHoldWakeLock(msg.what)) {
+ if (mCallbacks.onCheckHoldWakeLock(msg.what) && mWakeLock != null) {
mWakeLock.release();
}
}
@@ -171,7 +171,8 @@
rawEvent);
if (event.isClassUnsolicited()) {
// TODO: migrate to sending NativeDaemonEvent instances
- if (mCallbacks.onCheckHoldWakeLock(event.getCode())) {
+ if (mCallbacks.onCheckHoldWakeLock(event.getCode())
+ && mWakeLock != null) {
mWakeLock.acquire();
releaseWl = true;
}
diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java
index 5273cec..41832eb 100644
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -208,7 +208,11 @@
}
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
- PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
+ // Don't need this wake lock, since we now have a time stamp for when
+ // the network actually went inactive. (It might be nice to still do this,
+ // but I don't want to do it through the power manager because that pollutes the
+ // battery stats history with pointless noise.)
+ PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
mConnector = new NativeDaemonConnector(
new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl);
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 5500b9d..04a2fc3 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -15583,7 +15583,7 @@
ActivityRecord act = mStackSupervisor.resumedAppLocked();
String pkg;
int uid;
- if (act != null) {
+ if (act != null && !act.sleeping) {
pkg = act.packageName;
uid = act.info.applicationInfo.uid;
} else {
diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index fab972f..a388318 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -747,7 +747,7 @@
}
}
- private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws) {
+ private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws, String historyTag) {
synchronized (mLock) {
int index = findWakeLockIndexLocked(lock);
if (index < 0) {
@@ -767,7 +767,8 @@
if (!wakeLock.hasSameWorkSource(ws)) {
notifyWakeLockChangingLocked(wakeLock, wakeLock.mFlags, wakeLock.mTag,
wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
- ws, wakeLock.mHistoryTag);
+ ws, historyTag);
+ wakeLock.mHistoryTag = historyTag;
wakeLock.updateWorkSource(ws);
}
}
@@ -2600,11 +2601,11 @@
ws.add(uids[i]);
}
}
- updateWakeLockWorkSource(lock, ws);
+ updateWakeLockWorkSource(lock, ws, null);
}
@Override // Binder call
- public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
+ public void updateWakeLockWorkSource(IBinder lock, WorkSource ws, String historyTag) {
if (lock == null) {
throw new IllegalArgumentException("lock must not be null");
}
@@ -2619,7 +2620,7 @@
final long ident = Binder.clearCallingIdentity();
try {
- updateWakeLockWorkSourceInternal(lock, ws);
+ updateWakeLockWorkSourceInternal(lock, ws, historyTag);
} finally {
Binder.restoreCallingIdentity(ident);
}