Merge "Add SystemBatteryConsumer.getPowerConsumedByApps()" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
eb2a57b807
@@ -18,6 +18,7 @@ package android.os;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.annotation.NonNull;
|
||||
import android.util.Slog;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -31,6 +32,7 @@ import java.util.List;
|
||||
* {@hide}
|
||||
*/
|
||||
public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable {
|
||||
private static final String TAG = "SystemBatteryConsumer";
|
||||
|
||||
// ****************
|
||||
// This list must be kept current with atoms.proto (frameworks/base/cmds/statsd/src/atoms.proto)
|
||||
@@ -72,6 +74,8 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
|
||||
@DrainType
|
||||
private final int mDrainType;
|
||||
|
||||
private final double mPowerConsumedByAppsMah;
|
||||
|
||||
@DrainType
|
||||
public int getDrainType() {
|
||||
return mDrainType;
|
||||
@@ -80,11 +84,23 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
|
||||
private SystemBatteryConsumer(@NonNull SystemBatteryConsumer.Builder builder) {
|
||||
super(builder.mPowerComponentsBuilder.build());
|
||||
mDrainType = builder.mDrainType;
|
||||
mPowerConsumedByAppsMah = builder.mPowerConsumedByAppsMah;
|
||||
if (mPowerConsumedByAppsMah > getConsumedPower()) {
|
||||
Slog.wtf(TAG,
|
||||
"Power attributed to apps exceeds total: drain type = " + mDrainType
|
||||
+ " total consumed power = " + getConsumedPower()
|
||||
+ " power consumed by apps = " + mPowerConsumedByAppsMah);
|
||||
}
|
||||
}
|
||||
|
||||
private SystemBatteryConsumer(Parcel in) {
|
||||
super(new PowerComponents(in));
|
||||
mDrainType = in.readInt();
|
||||
mPowerConsumedByAppsMah = in.readDouble();
|
||||
}
|
||||
|
||||
public double getPowerConsumedByApps() {
|
||||
return mPowerConsumedByAppsMah;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,6 +110,7 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
|
||||
public void writeToParcel(@NonNull Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeInt(mDrainType);
|
||||
dest.writeDouble(mPowerConsumedByAppsMah);
|
||||
}
|
||||
|
||||
public static final Creator<SystemBatteryConsumer> CREATOR =
|
||||
@@ -120,6 +137,7 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
|
||||
public static final class Builder extends BaseBuilder<Builder> {
|
||||
@DrainType
|
||||
private final int mDrainType;
|
||||
private double mPowerConsumedByAppsMah;
|
||||
private List<UidBatteryConsumer.Builder> mUidBatteryConsumers;
|
||||
|
||||
Builder(int customPowerComponentCount, int customTimeComponentCount,
|
||||
@@ -128,6 +146,15 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable
|
||||
mDrainType = drainType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the amount of power used by this system component that is attributed to apps.
|
||||
* It should not exceed the total consumed power.
|
||||
*/
|
||||
public Builder setPowerConsumedByApps(double powerConsumedByAppsMah) {
|
||||
mPowerConsumedByAppsMah = powerConsumedByAppsMah;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a UidBatteryConsumer to this SystemBatteryConsumer. For example,
|
||||
* the UidBatteryConsumer with the UID == {@link Process#BLUETOOTH_UID} should
|
||||
|
||||
@@ -84,16 +84,16 @@ public class BluetoothPowerCalculator extends PowerCalculator {
|
||||
|
||||
// Subtract what the apps used, but clamp to 0.
|
||||
final long systemComponentDurationMs = Math.max(0, systemDurationMs - total.durationMs);
|
||||
final double systemComponentPowerMah = Math.max(0, systemPowerMah - total.powerMah);
|
||||
if (DEBUG && systemComponentPowerMah != 0) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Bluetooth active: time=" + (systemComponentDurationMs)
|
||||
+ " power=" + formatCharge(systemComponentPowerMah));
|
||||
+ " power=" + formatCharge(systemPowerMah));
|
||||
}
|
||||
systemBatteryConsumerBuilder
|
||||
.setUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_BLUETOOTH,
|
||||
systemComponentDurationMs)
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_BLUETOOTH,
|
||||
systemComponentPowerMah);
|
||||
Math.max(systemPowerMah, total.powerMah))
|
||||
.setPowerConsumedByApps(total.powerMah);
|
||||
}
|
||||
|
||||
private void calculateApp(UidBatteryConsumer.Builder app, PowerAndDuration total,
|
||||
|
||||
@@ -42,8 +42,9 @@ public class MobileRadioPowerCalculator extends PowerCalculator {
|
||||
|
||||
private static class PowerAndDuration {
|
||||
public long durationMs;
|
||||
public double powerMah;
|
||||
public double remainingPowerMah;
|
||||
public long totalAppDurationMs;
|
||||
public double totalAppPowerMah;
|
||||
public long signalDurationMs;
|
||||
public long noCoverageDurationMs;
|
||||
}
|
||||
@@ -103,12 +104,14 @@ public class MobileRadioPowerCalculator extends PowerCalculator {
|
||||
calculateRemaining(total, batteryStats, rawRealtimeUs,
|
||||
query.shouldForceUsePowerProfileModel());
|
||||
|
||||
if (total.powerMah != 0) {
|
||||
if (total.remainingPowerMah != 0 || total.totalAppPowerMah != 0) {
|
||||
builder.getOrCreateSystemBatteryConsumerBuilder(
|
||||
SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO)
|
||||
.setUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_MOBILE_RADIO,
|
||||
total.durationMs)
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO, total.powerMah);
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO,
|
||||
total.remainingPowerMah + total.totalAppPowerMah)
|
||||
.setPowerConsumedByApps(total.totalAppPowerMah);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +123,7 @@ public class MobileRadioPowerCalculator extends PowerCalculator {
|
||||
|
||||
final double powerMah = calculatePower(u, powerPerPacketMah, radioActiveDurationMs,
|
||||
shouldForceUsePowerProfileModel);
|
||||
total.totalAppPowerMah += powerMah;
|
||||
|
||||
app.setUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_MOBILE_RADIO,
|
||||
radioActiveDurationMs)
|
||||
@@ -142,14 +146,14 @@ public class MobileRadioPowerCalculator extends PowerCalculator {
|
||||
|
||||
BatterySipper radio = new BatterySipper(BatterySipper.DrainType.CELL, null, 0);
|
||||
calculateRemaining(total, batteryStats, rawRealtimeUs, false);
|
||||
if (total.powerMah != 0) {
|
||||
if (total.remainingPowerMah != 0) {
|
||||
if (total.signalDurationMs != 0) {
|
||||
radio.noCoveragePercent =
|
||||
total.noCoverageDurationMs * 100.0 / total.signalDurationMs;
|
||||
}
|
||||
radio.mobileActive = total.durationMs;
|
||||
radio.mobileActiveCount = batteryStats.getMobileRadioActiveUnknownCount(statsType);
|
||||
radio.mobileRadioPowerMah = total.powerMah;
|
||||
radio.mobileRadioPowerMah = total.remainingPowerMah;
|
||||
radio.sumPower();
|
||||
}
|
||||
if (radio.totalPowerMah > 0) {
|
||||
@@ -265,7 +269,7 @@ public class MobileRadioPowerCalculator extends PowerCalculator {
|
||||
}
|
||||
}
|
||||
total.durationMs = radioActiveTimeMs;
|
||||
total.powerMah = powerMah;
|
||||
total.remainingPowerMah = powerMah;
|
||||
total.signalDurationMs = signalTimeMs;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,11 +66,7 @@ public class ScreenPowerCalculator extends PowerCalculator {
|
||||
batteryStats, rawRealtimeUs, BatteryStats.STATS_SINCE_CHARGED,
|
||||
query.shouldForceUsePowerProfileModel());
|
||||
|
||||
builder.getOrCreateSystemBatteryConsumerBuilder(SystemBatteryConsumer.DRAIN_TYPE_SCREEN)
|
||||
.setUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_USAGE,
|
||||
totalPowerAndDuration.durationMs)
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_USAGE,
|
||||
totalPowerAndDuration.powerMah);
|
||||
double totalAppPower = 0;
|
||||
|
||||
// Now deal with each app's UidBatteryConsumer. The results are stored in the
|
||||
// BatteryConsumer.POWER_COMPONENT_SCREEN power component, which is considered smeared,
|
||||
@@ -87,11 +83,20 @@ public class ScreenPowerCalculator extends PowerCalculator {
|
||||
appPowerAndDuration.durationMs)
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_SCREEN,
|
||||
appPowerAndDuration.powerMah);
|
||||
totalAppPower += appPowerAndDuration.powerMah;
|
||||
}
|
||||
} else {
|
||||
smearScreenBatteryDrain(uidBatteryConsumerBuilders, totalPowerAndDuration,
|
||||
rawRealtimeUs);
|
||||
totalAppPower = totalPowerAndDuration.powerMah;
|
||||
}
|
||||
|
||||
builder.getOrCreateSystemBatteryConsumerBuilder(SystemBatteryConsumer.DRAIN_TYPE_SCREEN)
|
||||
.setUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_USAGE,
|
||||
totalPowerAndDuration.durationMs)
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_USAGE,
|
||||
Math.max(totalPowerAndDuration.powerMah, totalAppPower))
|
||||
.setPowerConsumedByApps(totalAppPower);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -121,7 +121,8 @@ public class WifiPowerCalculator extends PowerCalculator {
|
||||
.setUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_WIFI,
|
||||
powerDurationAndTraffic.durationMs)
|
||||
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_WIFI,
|
||||
powerDurationAndTraffic.powerMah);
|
||||
totalAppPowerMah + powerDurationAndTraffic.powerMah)
|
||||
.setPowerConsumedByApps(totalAppPowerMah);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -96,7 +96,8 @@ public class BatteryUsageStatsTest {
|
||||
.setUsageDurationMillis(
|
||||
BatteryConsumer.TIME_COMPONENT_CPU, 10300)
|
||||
.setUsageDurationForCustomComponentMillis(
|
||||
BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID, 10400);
|
||||
BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID, 10400)
|
||||
.setPowerConsumedByApps(20000);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
@@ -148,6 +149,7 @@ public class BatteryUsageStatsTest {
|
||||
assertThat(systemBatteryConsumer.getUsageDurationForCustomComponentMillis(
|
||||
BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID)).isEqualTo(10400);
|
||||
assertThat(systemBatteryConsumer.getConsumedPower()).isEqualTo(20300);
|
||||
assertThat(systemBatteryConsumer.getPowerConsumedByApps()).isEqualTo(20000);
|
||||
} else {
|
||||
fail("Unexpected drain type " + systemBatteryConsumer.getDrainType());
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class BluetoothPowerCalculatorTest {
|
||||
0.24722, 15000);
|
||||
assertBluetoothPowerAndDuration(
|
||||
mStatsRule.getSystemBatteryConsumer(SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH),
|
||||
0.15833, 9000);
|
||||
0.51944, 9000, 0.51944, 0.36111);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -98,7 +98,7 @@ public class BluetoothPowerCalculatorTest {
|
||||
0.2, 15000);
|
||||
assertBluetoothPowerAndDuration(
|
||||
mStatsRule.getSystemBatteryConsumer(SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH),
|
||||
0.15, 9000);
|
||||
0.45, 9000, 0.45, 0.3);
|
||||
}
|
||||
|
||||
private void setDurationsAndPower(
|
||||
@@ -123,4 +123,15 @@ public class BluetoothPowerCalculatorTest {
|
||||
|
||||
assertThat(usageDurationMillis).isEqualTo(durationMs);
|
||||
}
|
||||
|
||||
private void assertBluetoothPowerAndDuration(@Nullable SystemBatteryConsumer batteryConsumer,
|
||||
double powerMah, int durationMs, double consumedPower, double attributedPower) {
|
||||
assertBluetoothPowerAndDuration(batteryConsumer, powerMah, durationMs);
|
||||
|
||||
assertThat(batteryConsumer.getConsumedPower())
|
||||
.isWithin(PRECISION).of(consumedPower);
|
||||
|
||||
assertThat(batteryConsumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(attributedPower);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,11 @@ public class MobileRadioPowerCalculatorTest {
|
||||
SystemBatteryConsumer consumer =
|
||||
mStatsRule.getSystemBatteryConsumer(SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO);
|
||||
assertThat(consumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO))
|
||||
.isWithin(PRECISION).of(1.44440);
|
||||
.isWithin(PRECISION).of(2.2444);
|
||||
assertThat(consumer.getConsumedPower())
|
||||
.isWithin(PRECISION).of(2.2444);
|
||||
assertThat(consumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(0.8);
|
||||
|
||||
UidBatteryConsumer uidConsumer = mStatsRule.getUidBatteryConsumer(APP_UID);
|
||||
assertThat(uidConsumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO))
|
||||
@@ -156,9 +160,11 @@ public class MobileRadioPowerCalculatorTest {
|
||||
SystemBatteryConsumer consumer =
|
||||
mStatsRule.getSystemBatteryConsumer(SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO);
|
||||
|
||||
// 100000000 uAs * (1 mA / 1000 uA) * (1 h / 3600 s) = 2.77777 mAh
|
||||
// 100000000 uAs * (1 mA / 1000 uA) * (1 h / 3600 s) + 1.53934 (apps)= 4.31711 mAh
|
||||
assertThat(consumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO))
|
||||
.isWithin(PRECISION).of(2.77777);
|
||||
.isWithin(PRECISION).of(4.31711);
|
||||
assertThat(consumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(1.53934);
|
||||
|
||||
UidBatteryConsumer uidConsumer = mStatsRule.getUidBatteryConsumer(APP_UID);
|
||||
assertThat(uidConsumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO))
|
||||
|
||||
@@ -52,17 +52,26 @@ public class ScreenPowerCalculatorTest {
|
||||
BatteryStatsImpl batteryStats = mStatsRule.getBatteryStats();
|
||||
|
||||
batteryStats.noteScreenStateLocked(Display.STATE_ON, 0, 0, 0);
|
||||
batteryStats.updateDisplayMeasuredEnergyStatsLocked(0, Display.STATE_ON, 2 * MINUTE_IN_MS);
|
||||
batteryStats.updateDisplayMeasuredEnergyStatsLocked(0, Display.STATE_ON, 0);
|
||||
setProcState(APP_UID1, ActivityManager.PROCESS_STATE_TOP, true,
|
||||
0, 0);
|
||||
|
||||
setFgState(APP_UID1, true, 2 * MINUTE_IN_MS, 2 * MINUTE_IN_MS);
|
||||
setFgState(APP_UID1, false, 20 * MINUTE_IN_MS, 20 * MINUTE_IN_MS);
|
||||
setFgState(APP_UID2, true, 30 * MINUTE_IN_MS, 30 * MINUTE_IN_MS);
|
||||
batteryStats.updateDisplayMeasuredEnergyStatsLocked(200_000_000, Display.STATE_ON,
|
||||
15 * MINUTE_IN_MS);
|
||||
|
||||
setProcState(APP_UID1, ActivityManager.PROCESS_STATE_CACHED_EMPTY, false,
|
||||
20 * MINUTE_IN_MS, 20 * MINUTE_IN_MS);
|
||||
|
||||
setProcState(APP_UID2, ActivityManager.PROCESS_STATE_TOP, true,
|
||||
20 * MINUTE_IN_MS, 20 * MINUTE_IN_MS);
|
||||
|
||||
batteryStats.updateDisplayMeasuredEnergyStatsLocked(300_000_000, Display.STATE_ON,
|
||||
60 * MINUTE_IN_MS);
|
||||
|
||||
batteryStats.noteScreenStateLocked(Display.STATE_OFF,
|
||||
80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS);
|
||||
setProcState(APP_UID2, ActivityManager.PROCESS_STATE_TOP_SLEEPING, false,
|
||||
80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS);
|
||||
|
||||
batteryStats.updateDisplayMeasuredEnergyStatsLocked(100_000_000, Display.STATE_DOZE,
|
||||
120 * MINUTE_IN_MS);
|
||||
@@ -79,28 +88,33 @@ public class ScreenPowerCalculatorTest {
|
||||
assertThat(consumer.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_USAGE))
|
||||
.isEqualTo(80 * MINUTE_IN_MS);
|
||||
|
||||
// 400000000 uAs * (1 mA / 1000 uA) * (1 h / 3600 s) = 111.11111 mAh
|
||||
// 600000000 uAs * (1 mA / 1000 uA) * (1 h / 3600 s) = 166.66666 mAh
|
||||
assertThat(consumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_USAGE))
|
||||
.isWithin(PRECISION).of(111.11111);
|
||||
.isWithin(PRECISION).of(166.66666);
|
||||
assertThat(consumer.getConsumedPower())
|
||||
.isWithin(PRECISION).of(166.66666);
|
||||
assertThat(consumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(166.66666);
|
||||
|
||||
UidBatteryConsumer uid1 = mStatsRule.getUidBatteryConsumer(APP_UID1);
|
||||
assertThat(uid1.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_SCREEN))
|
||||
.isEqualTo(18 * MINUTE_IN_MS);
|
||||
.isEqualTo(20 * MINUTE_IN_MS);
|
||||
|
||||
// Uid1 ran for 18 minutes out of the total 48 min of foreground time during the first
|
||||
// Display update. Uid1 charge = 18 / 48 * 300000000 uAs = 31.25 mAh
|
||||
// Uid1 took all of the foreground time during the first Display update.
|
||||
// It also ran for 5 out of 45 min during the second Display update:
|
||||
// Uid1 charge = 200000000 + 5 / 45 * 300000000 mAs = 64.81 mAh
|
||||
assertThat(uid1.getConsumedPower(BatteryConsumer.POWER_COMPONENT_SCREEN))
|
||||
.isWithin(PRECISION).of(31.25);
|
||||
.isWithin(PRECISION).of(64.81481);
|
||||
|
||||
UidBatteryConsumer uid2 = mStatsRule.getUidBatteryConsumer(APP_UID2);
|
||||
assertThat(uid2.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_SCREEN))
|
||||
.isEqualTo(90 * MINUTE_IN_MS);
|
||||
.isEqualTo(60 * MINUTE_IN_MS);
|
||||
|
||||
// Uid2 ran for 30 minutes out of the total 48 min of foreground time during the first
|
||||
// Display update and then took all of the time during the second Display update.
|
||||
// Uid1 charge = 30 / 48 * 300000000 + 100000000 mAs = 79.86111 mAh
|
||||
// Uid2 ran for 40 minutes out of the total 45 min of foreground time during the second
|
||||
// Display update and then took all of the time during the third Display update.
|
||||
// Uid2 charge = 40 / 45 * 300000000 + 100000000 mAs = 101.85 mAh
|
||||
assertThat(uid2.getConsumedPower(BatteryConsumer.POWER_COMPONENT_SCREEN))
|
||||
.isWithin(PRECISION).of(79.86111);
|
||||
.isWithin(PRECISION).of(101.85185);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,18 +122,22 @@ public class ScreenPowerCalculatorTest {
|
||||
BatteryStatsImpl batteryStats = mStatsRule.getBatteryStats();
|
||||
|
||||
batteryStats.noteScreenStateLocked(Display.STATE_ON, 0, 0, 0);
|
||||
|
||||
setFgState(APP_UID1, true, 2 * MINUTE_IN_MS, 2 * MINUTE_IN_MS);
|
||||
batteryStats.noteScreenBrightnessLocked(255, 0, 0);
|
||||
setProcState(APP_UID1, ActivityManager.PROCESS_STATE_TOP, true,
|
||||
0, 0);
|
||||
|
||||
batteryStats.noteScreenBrightnessLocked(100, 5 * MINUTE_IN_MS, 5 * MINUTE_IN_MS);
|
||||
batteryStats.noteScreenBrightnessLocked(200, 10 * MINUTE_IN_MS, 10 * MINUTE_IN_MS);
|
||||
|
||||
setFgState(APP_UID1, false, 20 * MINUTE_IN_MS, 20 * MINUTE_IN_MS);
|
||||
|
||||
setFgState(APP_UID2, true, 30 * MINUTE_IN_MS, 30 * MINUTE_IN_MS);
|
||||
setProcState(APP_UID1, ActivityManager.PROCESS_STATE_CACHED_EMPTY, false,
|
||||
20 * MINUTE_IN_MS, 20 * MINUTE_IN_MS);
|
||||
setProcState(APP_UID2, ActivityManager.PROCESS_STATE_TOP, true,
|
||||
20 * MINUTE_IN_MS, 20 * MINUTE_IN_MS);
|
||||
|
||||
batteryStats.noteScreenStateLocked(Display.STATE_OFF,
|
||||
80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS);
|
||||
setProcState(APP_UID2, ActivityManager.PROCESS_STATE_TOP_SLEEPING, false,
|
||||
80 * MINUTE_IN_MS, 80 * MINUTE_IN_MS);
|
||||
|
||||
mStatsRule.setTime(120 * MINUTE_IN_US, 120 * MINUTE_IN_US);
|
||||
|
||||
@@ -134,31 +152,39 @@ public class ScreenPowerCalculatorTest {
|
||||
assertThat(consumer.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_USAGE))
|
||||
.isEqualTo(80 * MINUTE_IN_MS);
|
||||
assertThat(consumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_USAGE))
|
||||
.isWithin(PRECISION).of(88.4);
|
||||
.isWithin(PRECISION).of(92.0);
|
||||
assertThat(consumer.getConsumedPower())
|
||||
.isWithin(PRECISION).of(92.0);
|
||||
assertThat(consumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(92.0);
|
||||
|
||||
UidBatteryConsumer uid1 = mStatsRule.getUidBatteryConsumer(APP_UID1);
|
||||
assertThat(uid1.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_SCREEN))
|
||||
.isEqualTo(18 * MINUTE_IN_MS);
|
||||
.isEqualTo(20 * MINUTE_IN_MS);
|
||||
|
||||
// Uid1 took 20 out of the total of 80 min of foreground activity
|
||||
// Uid1 charge = 20 / 80 * 92.0 = 23.0 mAh
|
||||
assertThat(uid1.getConsumedPower(BatteryConsumer.POWER_COMPONENT_SCREEN))
|
||||
.isWithin(PRECISION).of(14.73333);
|
||||
.isWithin(PRECISION).of(23.0);
|
||||
|
||||
UidBatteryConsumer uid2 = mStatsRule.getUidBatteryConsumer(APP_UID2);
|
||||
assertThat(uid2.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_SCREEN))
|
||||
.isEqualTo(90 * MINUTE_IN_MS);
|
||||
.isEqualTo(60 * MINUTE_IN_MS);
|
||||
|
||||
// Uid2 took 60 out of the total of 80 min of foreground activity
|
||||
// Uid2 charge = 60 / 80 * 92.0 = 69.0 mAh
|
||||
assertThat(uid2.getConsumedPower(BatteryConsumer.POWER_COMPONENT_SCREEN))
|
||||
.isWithin(PRECISION).of(73.66666);
|
||||
.isWithin(PRECISION).of(69.0);
|
||||
}
|
||||
|
||||
private void setFgState(int uid, boolean fgOn, long realtimeMs, long uptimeMs) {
|
||||
private void setProcState(int uid, int procState, boolean resumed, long realtimeMs,
|
||||
long uptimeMs) {
|
||||
BatteryStatsImpl batteryStats = mStatsRule.getBatteryStats();
|
||||
if (fgOn) {
|
||||
batteryStats.noteUidProcessStateLocked(uid, procState, realtimeMs, uptimeMs);
|
||||
if (resumed) {
|
||||
batteryStats.noteActivityResumedLocked(uid, realtimeMs, uptimeMs);
|
||||
batteryStats.noteUidProcessStateLocked(uid, ActivityManager.PROCESS_STATE_TOP,
|
||||
realtimeMs, uptimeMs);
|
||||
} else {
|
||||
batteryStats.noteActivityPausedLocked(uid, realtimeMs, uptimeMs);
|
||||
batteryStats.noteUidProcessStateLocked(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY,
|
||||
realtimeMs, uptimeMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,9 @@ public class WifiPowerCalculatorTest {
|
||||
assertThat(systemConsumer.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_WIFI))
|
||||
.isEqualTo(5577);
|
||||
assertThat(systemConsumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_WIFI))
|
||||
.isWithin(PRECISION).of(0.645200);
|
||||
.isWithin(PRECISION).of(1.11153);
|
||||
assertThat(systemConsumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(0.466333);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -125,7 +127,9 @@ public class WifiPowerCalculatorTest {
|
||||
.isEqualTo(5577);
|
||||
/* Same ratio as in testPowerControllerBasedModel_nonMeasured but scaled by 1_000_000uC. */
|
||||
assertThat(systemConsumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_WIFI))
|
||||
.isWithin(PRECISION).of(0.645200 / (0.2214666 + 0.645200) * 1_000_000 / 3600000);
|
||||
.isWithin(PRECISION).of(1.11153 / (0.2214666 + 0.645200) * 1_000_000 / 3600000);
|
||||
assertThat(systemConsumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(0.14946);
|
||||
}
|
||||
|
||||
/** Sets up batterystats object with prepopulated network & timer data for Timer-model tests. */
|
||||
@@ -163,7 +167,9 @@ public class WifiPowerCalculatorTest {
|
||||
assertThat(systemConsumer.getUsageDurationMillis(BatteryConsumer.TIME_COMPONENT_WIFI))
|
||||
.isEqualTo(2222);
|
||||
assertThat(systemConsumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_WIFI))
|
||||
.isWithin(PRECISION).of(0.8759216);
|
||||
.isWithin(PRECISION).of(2.575000);
|
||||
assertThat(systemConsumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(1.69907);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -190,6 +196,8 @@ public class WifiPowerCalculatorTest {
|
||||
.isEqualTo(2222);
|
||||
/* Same ratio as in testTimerBasedModel_nonMeasured but scaled by 1_000_000uC. */
|
||||
assertThat(systemConsumer.getConsumedPower(BatteryConsumer.POWER_COMPONENT_WIFI))
|
||||
.isWithin(PRECISION).of(0.8759216 / (0.8231573 + 0.8759216) * 1_000_000 / 3600000);
|
||||
.isWithin(PRECISION).of(2.575000 / (0.8231573 + 0.8759216) * 1_000_000 / 3600000);
|
||||
assertThat(systemConsumer.getPowerConsumedByApps())
|
||||
.isWithin(PRECISION).of(0.277777);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user