Fix issue #17397177: BatteryStats reporting error on job start/stop

Dumb, dumb, dumb mistake.

Also fix battery stats wakeup reason tracking to use a SamplingTimer
(like kernel wake locks) so we can track both the duration and count
for each wakeup reason.

Change-Id: I89d69661006dc533622b1b7e68a139166d3a6975
This commit is contained in:
Dianne Hackborn
2014-09-05 15:50:25 -07:00
parent 71cc3b8e84
commit c3940bc1ba
2 changed files with 53 additions and 43 deletions

View File

@@ -1667,7 +1667,7 @@ public abstract class BatteryStats implements Parcelable {
*/
public abstract long[] getChargeStepDurationsArray();
public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
public abstract Map<String, ? extends Timer> getWakeupReasonStats();
public abstract Map<String, ? extends Timer> getKernelWakelockStats();
@@ -2045,11 +2045,15 @@ public abstract class BatteryStats implements Parcelable {
sb.toString());
}
}
Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
if (wakeupReasons.size() > 0) {
for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
// Not doing the regular wake lock formatting to remain compatible
// with the old checkin format.
long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
int count = ent.getValue().getCountLocked(which);
dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
"\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
"\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
}
}
}
@@ -2921,14 +2925,14 @@ public abstract class BatteryStats implements Parcelable {
pw.println();
}
Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
if (wakeupReasons.size() > 0) {
pw.print(prefix); pw.println(" All wakeup reasons:");
final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
BatteryStats.LongCounter counter = ent.getValue();
reasons.add(new TimerEntry(ent.getKey(), 0, null,
ent.getValue().getCountLocked(which)));
for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Timer timer = ent.getValue();
reasons.add(new TimerEntry(ent.getKey(), 0, timer,
timer.getCountLocked(which)));
}
Collections.sort(reasons, timerComparator);
for (int i=0; i<reasons.size(); i++) {
@@ -2938,9 +2942,8 @@ public abstract class BatteryStats implements Parcelable {
sb.append(prefix);
sb.append(" Wakeup reason ");
sb.append(timer.mName);
sb.append(": ");
formatTimeMs(sb, timer.mTime);
sb.append("realtime");
printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
sb.append(" realtime");
pw.println(sb.toString());
}
pw.println();
@@ -3138,7 +3141,7 @@ public abstract class BatteryStats implements Parcelable {
}
Map<String, ? extends Timer> jobs = u.getJobStats();
if (syncs.size() > 0) {
if (jobs.size() > 0) {
for (Map.Entry<String, ? extends Timer> ent : jobs.entrySet()) {
Timer timer = ent.getValue();
// Convert from microseconds to milliseconds with rounding
@@ -3952,7 +3955,7 @@ public abstract class BatteryStats implements Parcelable {
prepareForDumpLocked();
dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
"10", getParcelVersion(), getStartPlatformVersion(), getEndPlatformVersion());
"11", getParcelVersion(), getStartPlatformVersion(), getEndPlatformVersion());
long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();

View File

@@ -94,7 +94,7 @@ public final class BatteryStatsImpl extends BatteryStats {
private static final int MAGIC = 0xBA757475; // 'BATSTATS'
// Current on-disk Parcel version
private static final int VERSION = 113 + (USE_OLD_HISTORY ? 1000 : 0);
private static final int VERSION = 114 + (USE_OLD_HISTORY ? 1000 : 0);
// Maximum number of items we will record in the history.
private static final int MAX_HISTORY_ITEMS = 2000;
@@ -385,10 +385,9 @@ public final class BatteryStatsImpl extends BatteryStats {
String mLastWakeupReason = null;
long mLastWakeupUptimeMs = 0;
private final HashMap<String, LongSamplingCounter> mWakeupReasonStats =
new HashMap<String, LongSamplingCounter>();
private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
public Map<String, ? extends LongCounter> getWakeupReasonStats() {
public Map<String, ? extends Timer> getWakeupReasonStats() {
return mWakeupReasonStats;
}
@@ -1131,6 +1130,10 @@ public final class BatteryStatsImpl extends BatteryStats {
mCurrentReportedCount = count;
}
public void addCurrentReportedCount(int delta) {
updateCurrentReportedCount(mCurrentReportedCount + delta);
}
public void updateCurrentReportedTotalTime(long totalTime) {
if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
// Updating the reported value for the first time.
@@ -1141,6 +1144,10 @@ public final class BatteryStatsImpl extends BatteryStats {
mCurrentReportedTotalTime = totalTime;
}
public void addCurrentReportedTotalTime(long delta) {
updateCurrentReportedTotalTime(mCurrentReportedTotalTime + delta);
}
public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
if (mTrackingReportedValues) {
@@ -1688,13 +1695,13 @@ public final class BatteryStatsImpl extends BatteryStats {
* Get the wakeup reason counter, and create a new one if one
* doesn't already exist.
*/
public LongSamplingCounter getWakeupReasonCounterLocked(String name) {
LongSamplingCounter counter = mWakeupReasonStats.get(name);
if (counter == null) {
counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
mWakeupReasonStats.put(name, counter);
public SamplingTimer getWakeupReasonTimerLocked(String name) {
SamplingTimer timer = mWakeupReasonStats.get(name);
if (timer == null) {
timer = new SamplingTimer(mOnBatteryTimeBase, true);
mWakeupReasonStats.put(name, timer);
}
return counter;
return timer;
}
private final Map<String, KernelWakelockStats> readKernelWakelockStats() {
@@ -2753,8 +2760,9 @@ public final class BatteryStatsImpl extends BatteryStats {
void aggregateLastWakeupUptimeLocked(long uptimeMs) {
if (mLastWakeupReason != null) {
long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
LongSamplingCounter timer = getWakeupReasonCounterLocked(mLastWakeupReason);
timer.addCountLocked(deltaUptime);
SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
timer.addCurrentReportedCount(1);
timer.addCurrentReportedTotalTime(deltaUptime * 1000); // time is in microseconds
mLastWakeupReason = null;
}
}
@@ -2762,7 +2770,7 @@ public final class BatteryStatsImpl extends BatteryStats {
public void noteWakeupReasonLocked(String reason) {
final long elapsedRealtime = SystemClock.elapsedRealtime();
final long uptime = SystemClock.uptimeMillis();
if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason reason \"" + reason +"\": "
if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
+ Integer.toHexString(mHistoryCur.states));
aggregateLastWakeupUptimeLocked(uptime);
mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
@@ -6193,7 +6201,7 @@ public final class BatteryStatsImpl extends BatteryStats {
}
public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
StopwatchTimer t = mJobStats.stopObject(name);
StopwatchTimer t = mJobStats.startObject(name);
if (t != null) {
t.startRunningLocked(elapsedRealtimeMs);
}
@@ -6636,8 +6644,8 @@ public final class BatteryStatsImpl extends BatteryStats {
}
if (mWakeupReasonStats.size() > 0) {
for (LongSamplingCounter timer : mWakeupReasonStats.values()) {
mOnBatteryScreenOffTimeBase.remove(timer);
for (SamplingTimer timer : mWakeupReasonStats.values()) {
mOnBatteryTimeBase.remove(timer);
}
mWakeupReasonStats.clear();
}
@@ -7848,7 +7856,7 @@ public final class BatteryStatsImpl extends BatteryStats {
for (int iwr = 0; iwr < NWR; iwr++) {
if (in.readInt() != 0) {
String reasonName = in.readString();
getWakeupReasonCounterLocked(reasonName).readSummaryFromParcelLocked(in);
getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
}
}
@@ -8122,12 +8130,12 @@ public final class BatteryStatsImpl extends BatteryStats {
}
out.writeInt(mWakeupReasonStats.size());
for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
LongSamplingCounter counter = ent.getValue();
if (counter != null) {
for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
SamplingTimer timer = ent.getValue();
if (timer != null) {
out.writeInt(1);
out.writeString(ent.getKey());
counter.writeSummaryFromParcelLocked(out);
timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
} else {
out.writeInt(0);
}
@@ -8438,7 +8446,7 @@ public final class BatteryStatsImpl extends BatteryStats {
for (int ikw = 0; ikw < NKW; ikw++) {
if (in.readInt() != 0) {
String wakelockName = in.readString();
SamplingTimer kwlt = new SamplingTimer(mOnBatteryTimeBase, in);
SamplingTimer kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, in);
mKernelWakelockStats.put(wakelockName, kwlt);
}
}
@@ -8448,9 +8456,8 @@ public final class BatteryStatsImpl extends BatteryStats {
for (int iwr = 0; iwr < NWR; iwr++) {
if (in.readInt() != 0) {
String reasonName = in.readString();
LongSamplingCounter counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase,
in);
mWakeupReasonStats.put(reasonName, counter);
SamplingTimer timer = new SamplingTimer(mOnBatteryTimeBase, in);
mWakeupReasonStats.put(reasonName, timer);
}
}
@@ -8585,12 +8592,12 @@ public final class BatteryStatsImpl extends BatteryStats {
}
}
out.writeInt(mWakeupReasonStats.size());
for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
LongSamplingCounter counter = ent.getValue();
if (counter != null) {
for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
SamplingTimer timer = ent.getValue();
if (timer != null) {
out.writeInt(1);
out.writeString(ent.getKey());
counter.writeToParcel(out);
timer.writeToParcel(out, uSecRealtime);
} else {
out.writeInt(0);
}