Fix counting problems in StopwatchTimer.

Changed StopwatchTimer so that its count only increases if the timer is
started when its time base is running. Previously, if the time base was
off, the timer was started, the time base was turned on, and then the
timer was stopped, the count would be increased; now, it will not
(because the time base was off when the timer started). Moreover, this
likely fixes the count==-1 bug that previously could occur, since the
count will no longer be decremented if the timer is stopped after a reset.

Fixes: 36730213
Bug: 30099724
Test: runtest -x
frameworks/base/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java

Change-Id: Iad195e431618629ce432074e0c1bd217f9818cb1
This commit is contained in:
Bookatz
2017-04-06 11:59:13 -07:00
parent 67ee79e84f
commit ceebafe41a
5 changed files with 173 additions and 48 deletions

View File

@@ -1798,9 +1798,10 @@ public class BatteryStatsImpl extends BatteryStats {
/**
* The total time at which the timer was acquired, to determine if it
* was actually held for an interesting duration.
* was actually held for an interesting duration. If time base was not running when timer
* was acquired, will be -1.
*/
long mAcquireTime;
long mAcquireTime = -1;
long mTimeout;
@@ -1864,9 +1865,13 @@ public class BatteryStatsImpl extends BatteryStats {
// Add this timer to the active pool
mTimerPool.add(this);
}
// Increment the count
mCount++;
mAcquireTime = mTotalTime;
if (mTimeBase.isRunning()) {
// Increment the count
mCount++;
mAcquireTime = mTotalTime;
} else {
mAcquireTime = -1;
}
if (DEBUG && mType < 0) {
Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
+ " mTotalTime=" + mTotalTime + " mCount=" + mCount
@@ -1904,7 +1909,7 @@ public class BatteryStatsImpl extends BatteryStats {
+ " mAcquireTime=" + mAcquireTime);
}
if (mTotalTime == mAcquireTime) {
if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
// If there was no change in the time, then discard this
// count. A somewhat cheezy strategy, but hey.
mCount--;
@@ -1963,7 +1968,7 @@ public class BatteryStatsImpl extends BatteryStats {
if (mNesting > 0) {
mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
}
mAcquireTime = mTotalTime;
mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
return canDetach;
}