Fix issue #16467284: Make noteStartVideo and noteStartAudio nesting

Also add new methods to clear these states, so we can avoid any
nesting issues from getting the tracking state stuck.

Change-Id: Iba0eaba2a9a186355c24d392f16118982c5331ed
This commit is contained in:
Dianne Hackborn
2014-07-22 22:54:55 -07:00
parent a55ccc02f5
commit 10eaa8574b
4 changed files with 99 additions and 51 deletions

View File

@@ -346,10 +346,6 @@ public abstract class BatteryStats implements Parcelable {
public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
public abstract void noteActivityResumedLocked(long elapsedRealtime);
public abstract void noteActivityPausedLocked(long elapsedRealtime);
public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);

View File

@@ -31,6 +31,8 @@ interface IBatteryStats {
void noteStopVideo(int uid);
void noteStartAudio(int uid);
void noteStopAudio(int uid);
void noteResetVideo();
void noteResetAudio();
// Remaining methods are only used in Java.
byte[] getStatistics();

View File

@@ -172,6 +172,8 @@ public final class BatteryStatsImpl extends BatteryStats {
final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<StopwatchTimer>();
final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers =
new SparseArray<ArrayList<StopwatchTimer>>();
final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<StopwatchTimer>();
final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<StopwatchTimer>();
// Last partial timers we use for distributing CPU usage.
final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<StopwatchTimer>();
@@ -256,10 +258,10 @@ public final class BatteryStatsImpl extends BatteryStats {
boolean mPhoneOn;
StopwatchTimer mPhoneOnTimer;
boolean mAudioOn;
int mAudioOnNesting;
StopwatchTimer mAudioOnTimer;
boolean mVideoOn;
int mVideoOnNesting;
StopwatchTimer mVideoOnTimer;
boolean mFlashlightOn;
@@ -1472,6 +1474,13 @@ public final class BatteryStatsImpl extends BatteryStats {
}
}
void stopAllRunningLocked(long elapsedRealtimeMs) {
if (mNesting > 0) {
mNesting = 1;
stopRunningLocked(elapsedRealtimeMs);
}
}
// Update the total time for all other running Timers with the same type as this Timer
// due to a change in timer count
private static long refreshTimersLocked(long batteryRealtime,
@@ -3187,27 +3196,29 @@ public final class BatteryStatsImpl extends BatteryStats {
uid = mapUid(uid);
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
if (!mAudioOn) {
if (mAudioOnNesting == 0) {
mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
+ Integer.toHexString(mHistoryCur.states));
addHistoryRecordLocked(elapsedRealtime, uptime);
mAudioOn = true;
mAudioOnTimer.startRunningLocked(elapsedRealtime);
}
mAudioOnNesting++;
getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
}
public void noteAudioOffLocked(int uid) {
if (mAudioOnNesting == 0) {
return;
}
uid = mapUid(uid);
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
if (mAudioOn) {
if (--mAudioOnNesting == 0) {
mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
+ Integer.toHexString(mHistoryCur.states));
addHistoryRecordLocked(elapsedRealtime, uptime);
mAudioOn = false;
mAudioOnTimer.stopRunningLocked(elapsedRealtime);
}
getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
@@ -3217,32 +3228,68 @@ public final class BatteryStatsImpl extends BatteryStats {
uid = mapUid(uid);
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
if (!mVideoOn) {
if (mVideoOnNesting == 0) {
mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
+ Integer.toHexString(mHistoryCur.states));
addHistoryRecordLocked(elapsedRealtime, uptime);
mVideoOn = true;
mVideoOnTimer.startRunningLocked(elapsedRealtime);
}
mVideoOnNesting++;
getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
}
public void noteVideoOffLocked(int uid) {
if (mVideoOnNesting == 0) {
return;
}
uid = mapUid(uid);
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
if (mVideoOn) {
if (--mVideoOnNesting == 0) {
mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
+ Integer.toHexString(mHistoryCur.states));
addHistoryRecordLocked(elapsedRealtime, uptime);
mVideoOn = false;
mVideoOnTimer.stopRunningLocked(elapsedRealtime);
}
getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
}
public void noteResetAudioLocked() {
if (mAudioOnNesting > 0) {
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
mAudioOnNesting = 0;
mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
+ Integer.toHexString(mHistoryCur.states));
addHistoryRecordLocked(elapsedRealtime, uptime);
mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
for (int i=0; i<mUidStats.size(); i++) {
BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
uid.noteResetAudioLocked(elapsedRealtime);
}
}
}
public void noteResetVideoLocked() {
if (mVideoOnNesting > 0) {
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
mAudioOnNesting = 0;
mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
+ Integer.toHexString(mHistoryCur.states));
addHistoryRecordLocked(elapsedRealtime, uptime);
mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
for (int i=0; i<mUidStats.size(); i++) {
BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
uid.noteResetVideoLocked(elapsedRealtime);
}
}
}
public void noteActivityResumedLocked(int uid) {
uid = mapUid(uid);
getUidStatsLocked(uid).noteActivityResumedLocked(SystemClock.elapsedRealtime());
@@ -3855,10 +3902,7 @@ public final class BatteryStatsImpl extends BatteryStats {
boolean mWifiMulticastEnabled;
StopwatchTimer mWifiMulticastTimer;
boolean mAudioTurnedOn;
StopwatchTimer mAudioTurnedOnTimer;
boolean mVideoTurnedOn;
StopwatchTimer mVideoTurnedOnTimer;
StopwatchTimer mForegroundActivityTimer;
@@ -4073,52 +4117,48 @@ public final class BatteryStatsImpl extends BatteryStats {
public StopwatchTimer createAudioTurnedOnTimerLocked() {
if (mAudioTurnedOnTimer == null) {
mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
null, mOnBatteryTimeBase);
mAudioTurnedOnTimers, mOnBatteryTimeBase);
}
return mAudioTurnedOnTimer;
}
@Override
public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
if (!mAudioTurnedOn) {
mAudioTurnedOn = true;
createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
}
public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
if (mAudioTurnedOnTimer != null) {
mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
}
}
@Override
public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
if (mAudioTurnedOn) {
mAudioTurnedOn = false;
if (mAudioTurnedOnTimer != null) {
mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
}
public void noteResetAudioLocked(long elapsedRealtimeMs) {
if (mAudioTurnedOnTimer != null) {
mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
}
}
public StopwatchTimer createVideoTurnedOnTimerLocked() {
if (mVideoTurnedOnTimer == null) {
mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
null, mOnBatteryTimeBase);
mVideoTurnedOnTimers, mOnBatteryTimeBase);
}
return mVideoTurnedOnTimer;
}
@Override
public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
if (!mVideoTurnedOn) {
mVideoTurnedOn = true;
createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
}
public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
if (mVideoTurnedOnTimer != null) {
mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
}
}
@Override
public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
if (mVideoTurnedOn) {
mVideoTurnedOn = false;
if (mVideoTurnedOnTimer != null) {
mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
}
public void noteResetVideoLocked(long elapsedRealtimeMs) {
if (mVideoTurnedOnTimer != null) {
mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
}
}
@@ -4416,11 +4456,9 @@ public final class BatteryStatsImpl extends BatteryStats {
}
if (mAudioTurnedOnTimer != null) {
active |= !mAudioTurnedOnTimer.reset(false);
active |= mAudioTurnedOn;
}
if (mVideoTurnedOnTimer != null) {
active |= !mVideoTurnedOnTimer.reset(false);
active |= mVideoTurnedOn;
}
if (mForegroundActivityTimer != null) {
active |= !mForegroundActivityTimer.reset(false);
@@ -4803,17 +4841,15 @@ public final class BatteryStatsImpl extends BatteryStats {
} else {
mWifiMulticastTimer = null;
}
mAudioTurnedOn = false;
if (in.readInt() != 0) {
mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
null, mOnBatteryTimeBase, in);
mAudioTurnedOnTimers, mOnBatteryTimeBase, in);
} else {
mAudioTurnedOnTimer = null;
}
mVideoTurnedOn = false;
if (in.readInt() != 0) {
mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
null, mOnBatteryTimeBase, in);
mVideoTurnedOnTimers, mOnBatteryTimeBase, in);
} else {
mVideoTurnedOnTimer = null;
}
@@ -7554,11 +7590,9 @@ public final class BatteryStatsImpl extends BatteryStats {
if (in.readInt() != 0) {
u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
}
u.mAudioTurnedOn = false;
if (in.readInt() != 0) {
u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
}
u.mVideoTurnedOn = false;
if (in.readInt() != 0) {
u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
}
@@ -8071,9 +8105,9 @@ public final class BatteryStatsImpl extends BatteryStats {
mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i,
null, mOnBatteryTimeBase, in);
}
mAudioOn = false;
mAudioOnNesting = 0;
mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
mVideoOn = false;
mVideoOnNesting = 0;
mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
mFlashlightOn = false;
mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
@@ -8125,6 +8159,8 @@ public final class BatteryStatsImpl extends BatteryStats {
mWifiScanTimers.clear();
mWifiBatchedScanTimers.clear();
mWifiMulticastTimers.clear();
mAudioTurnedOnTimers.clear();
mVideoTurnedOnTimers.clear();
sNumSpeedSteps = in.readInt();

View File

@@ -413,6 +413,20 @@ public final class BatteryStatsService extends IBatteryStats.Stub
}
}
public void noteResetAudio() {
enforceCallingPermission();
synchronized (mStats) {
mStats.noteResetAudioLocked();
}
}
public void noteResetVideo() {
enforceCallingPermission();
synchronized (mStats) {
mStats.noteResetVideoLocked();
}
}
public void noteFlashlightOn() {
enforceCallingPermission();
synchronized (mStats) {