From 98f0d4692c6634ff5d4ffbbc150d405da66c4c38 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Tue, 19 Apr 2016 16:46:20 -0700 Subject: [PATCH] Record in progress count when parceling Timer The total time is computed before parceling, but the total count is not. Bug:27747854 Change-Id: If3afdca9605f74d96a54fb9df17f6df761b99e55 --- .../android/internal/os/BatteryStatsImpl.java | 13 +-- .../os/BatteryStatsSamplingTimerTest.java | 88 +++++++++++++++++++ .../internal/os/BatteryStatsServTest.java | 9 +- .../internal/os/BatteryStatsTests.java | 1 + .../internal/os/BatteryStatsTimeBaseTest.java | 3 +- .../internal/os/BatteryStatsTimerTest.java | 19 ++-- 6 files changed, 109 insertions(+), 24 deletions(-) create mode 100644 core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 10afdb8ef109e..250d9b77473de 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -60,6 +60,7 @@ import android.util.TimeUtils; import android.util.Xml; import android.view.Display; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.net.NetworkStatsFactory; import com.android.internal.util.ArrayUtils; import com.android.internal.util.FastPrintWriter; @@ -1092,7 +1093,7 @@ public class BatteryStatsImpl extends BatteryStats { public void writeToParcel(Parcel out, long elapsedRealtimeUs) { if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime=" + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs))); - out.writeInt(mCount); + out.writeInt(computeCurrentCountLocked()); out.writeInt(mLoadedCount); out.writeInt(mUnpluggedCount); out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs))); @@ -1109,7 +1110,7 @@ public class BatteryStatsImpl extends BatteryStats { + " old mUnpluggedCount=" + mUnpluggedCount); } mUnpluggedTime = computeRunTimeLocked(baseRealtime); - mUnpluggedCount = mCount; + mUnpluggedCount = computeCurrentCountLocked(); if (DEBUG && mType < 0) { Log.v(TAG, "unplug #" + mType + ": new mUnpluggedTime=" + mUnpluggedTime @@ -1192,7 +1193,7 @@ public class BatteryStatsImpl extends BatteryStats { public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) { long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)); out.writeLong(runTime); - out.writeInt(mCount); + out.writeInt(computeCurrentCountLocked()); } public void readSummaryFromParcelLocked(Parcel in) { @@ -1249,7 +1250,8 @@ public class BatteryStatsImpl extends BatteryStats { */ int mUpdateVersion; - SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) { + @VisibleForTesting + public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) { super(clocks, 0, timeBase, in); mCurrentReportedCount = in.readInt(); mUnpluggedReportedCount = in.readInt(); @@ -1259,7 +1261,8 @@ public class BatteryStatsImpl extends BatteryStats { mTimeBaseRunning = timeBase.isRunning(); } - SamplingTimer(Clocks clocks, TimeBase timeBase, boolean trackReportedValues) { + @VisibleForTesting + public SamplingTimer(Clocks clocks, TimeBase timeBase, boolean trackReportedValues) { super(clocks, 0, timeBase); mTrackingReportedValues = trackReportedValues; mTimeBaseRunning = timeBase.isRunning(); diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java new file mode 100644 index 0000000000000..51d41a40d71ab --- /dev/null +++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.android.internal.os; + +import android.os.BatteryStats; +import android.os.Parcel; +import android.support.test.filters.SmallTest; + +import junit.framework.TestCase; + +public class BatteryStatsSamplingTimerTest extends TestCase { + + @SmallTest + public void testSampleTimerSummaryParceling() throws Exception { + final MockClocks clocks = new MockClocks(); + clocks.realtime = 0; + clocks.uptime = 0; + + final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase(); + timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime()); + + BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase, + true); + + // Start running on battery. + timeBase.setRunning(true, clocks.uptimeMillis(), clocks.elapsedRealtime()); + + // The first update on battery consumes the values as a way of starting cleanly. + timer.addCurrentReportedTotalTime(10); + timer.addCurrentReportedCount(1); + + timer.addCurrentReportedTotalTime(10); + timer.addCurrentReportedCount(1); + + clocks.realtime = 20; + clocks.uptime = 20; + + assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(), + BatteryStats.STATS_SINCE_CHARGED)); + assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); + + // Grab a summary parcel while on battery. + final Parcel onBatterySummaryParcel = Parcel.obtain(); + timer.writeSummaryFromParcelLocked(onBatterySummaryParcel, clocks.elapsedRealtime() * 1000); + onBatterySummaryParcel.setDataPosition(0); + + // Stop running on battery. + timeBase.setRunning(false, clocks.uptimeMillis(), clocks.elapsedRealtime()); + + assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(), + BatteryStats.STATS_SINCE_CHARGED)); + assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); + + // Grab a summary parcel while not on battery. + final Parcel offBatterySummaryParcel = Parcel.obtain(); + timer.writeSummaryFromParcelLocked(offBatterySummaryParcel, + clocks.elapsedRealtime() * 1000); + offBatterySummaryParcel.setDataPosition(0); + + // Read the on battery summary from the parcel. + BatteryStatsImpl.SamplingTimer unparceledTimer = new BatteryStatsImpl.SamplingTimer( + clocks, timeBase, true); + unparceledTimer.readSummaryFromParcelLocked(onBatterySummaryParcel); + + assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED)); + assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); + + // Read the off battery summary from the parcel. + unparceledTimer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase, true); + unparceledTimer.readSummaryFromParcelLocked(offBatterySummaryParcel); + + assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED)); + assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED)); + } +} diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsServTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsServTest.java index 1c3cd3872d78a..5fd822549ff8c 100644 --- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsServTest.java +++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsServTest.java @@ -16,20 +16,13 @@ package com.android.internal.os; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.ArrayList; - import android.os.BatteryStats; import android.os.Parcel; -import android.test.suitebuilder.annotation.SmallTest; -import android.util.Log; +import android.support.test.filters.SmallTest; import junit.framework.Assert; import junit.framework.TestCase; -import com.android.internal.os.BatteryStatsImpl; - /** * Provides test cases for android.os.BatteryStats. */ diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java index 05aa53cacbc50..78bcbbc4ecb94 100644 --- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java +++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java @@ -5,6 +5,7 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ + BatteryStatsSamplingTimerTest.class, BatteryStatsServTest.class, BatteryStatsTimeBaseTest.class, BatteryStatsTimerTest.class, diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimeBaseTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimeBaseTest.java index ab92f158db84b..3190d9e77c770 100644 --- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimeBaseTest.java +++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimeBaseTest.java @@ -18,11 +18,10 @@ package com.android.internal.os; import java.io.PrintWriter; import java.io.StringWriter; -import java.util.ArrayList; import android.os.BatteryStats; import android.os.Parcel; -import android.test.suitebuilder.annotation.SmallTest; +import android.support.test.filters.SmallTest; import android.util.Log; import junit.framework.Assert; diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimerTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimerTest.java index 3e17fcb8843d3..98d0f7fdce5c5 100644 --- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimerTest.java +++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTimerTest.java @@ -18,8 +18,7 @@ package com.android.internal.os; import android.os.BatteryStats; import android.os.Parcel; -import android.test.suitebuilder.annotation.SmallTest; -import android.util.Log; +import android.support.test.filters.SmallTest; import android.util.StringBuilderPrinter; import junit.framework.Assert; @@ -148,7 +147,7 @@ public class BatteryStatsTimerTest extends TestCase { timer.onTimeStarted(10, 20, 50); Assert.assertEquals(50, timer.lastComputeRunTimeRealtime); Assert.assertEquals(4, timer.getUnpluggedTime()); - Assert.assertEquals(0, timer.getUnpluggedCount()); + Assert.assertEquals(3000, timer.getUnpluggedCount()); // Test that stopping the timer updates mTotalTime and mCount timer.nextComputeRunTime = 17; @@ -168,15 +167,16 @@ public class BatteryStatsTimerTest extends TestCase { // Test write then read TestTimer timer1 = new TestTimer(clocks, 0, timeBase); timer1.setCount(1); - timer1.setLoadedCount(2); - timer1.setLastCount(3); - timer1.setUnpluggedCount(4); + timer1.setLoadedCount(3); + timer1.setLastCount(4); + timer1.setUnpluggedCount(5); timer1.setTotalTime(9223372036854775807L); timer1.setLoadedTime(9223372036854775806L); timer1.setLastTime(9223372036854775805L); timer1.setUnpluggedTime(9223372036854775804L); timer1.setTimeBeforeMark(9223372036854775803L); timer1.nextComputeRunTime = 201; + timer1.nextComputeCurrentCount = 2; Parcel parcel = Parcel.obtain(); Timer.writeTimerToParcel(parcel, timer1, 77); @@ -185,10 +185,10 @@ public class BatteryStatsTimerTest extends TestCase { Assert.assertTrue("parcel null object", parcel.readInt() != 0); TestTimer timer2 = new TestTimer(clocks, 0, timeBase, parcel); - Assert.assertEquals(1, timer2.getCount()); - Assert.assertEquals(2, timer2.getLoadedCount()); + Assert.assertEquals(2, timer2.getCount()); // from computeTotalCountLocked() + Assert.assertEquals(3, timer2.getLoadedCount()); Assert.assertEquals(0, timer2.getLastCount()); // NOT saved - Assert.assertEquals(4, timer2.getUnpluggedCount()); + Assert.assertEquals(5, timer2.getUnpluggedCount()); Assert.assertEquals(201, timer2.getTotalTime()); // from computeRunTimeLocked() Assert.assertEquals(9223372036854775806L, timer2.getLoadedTime()); Assert.assertEquals(0, timer2.getLastTime()); // NOT saved @@ -309,6 +309,7 @@ public class BatteryStatsTimerTest extends TestCase { Parcel parcel = Parcel.obtain(); timer1.nextComputeRunTime = 9223372036854775800L; + timer1.nextComputeCurrentCount = 1; timer1.writeSummaryFromParcelLocked(parcel, 201); Assert.assertEquals(40, timer1.lastComputeRunTimeRealtime);