Merge "Add phenotype flag to disable battery saver tron log" into pi-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
fc1784233b
@@ -69,6 +69,7 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
private static final String KEY_FORCE_BACKGROUND_CHECK = "force_background_check";
|
||||
private static final String KEY_OPTIONAL_SENSORS_DISABLED = "optional_sensors_disabled";
|
||||
private static final String KEY_AOD_DISABLED = "aod_disabled";
|
||||
private static final String KEY_SEND_TRON_LOG = "send_tron_log";
|
||||
|
||||
private static final String KEY_CPU_FREQ_INTERACTIVE = "cpufreq-i";
|
||||
private static final String KEY_CPU_FREQ_NONINTERACTIVE = "cpufreq-n";
|
||||
@@ -212,6 +213,12 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
@GuardedBy("mLock")
|
||||
private boolean mAodDisabled;
|
||||
|
||||
/**
|
||||
* Whether BatterySavingStats should send tron events.
|
||||
*/
|
||||
@GuardedBy("mLock")
|
||||
private boolean mSendTronLog;
|
||||
|
||||
@GuardedBy("mLock")
|
||||
private Context mContext;
|
||||
|
||||
@@ -347,6 +354,7 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
mForceBackgroundCheck = parser.getBoolean(KEY_FORCE_BACKGROUND_CHECK, true);
|
||||
mOptionalSensorsDisabled = parser.getBoolean(KEY_OPTIONAL_SENSORS_DISABLED, true);
|
||||
mAodDisabled = parser.getBoolean(KEY_AOD_DISABLED, true);
|
||||
mSendTronLog = parser.getBoolean(KEY_SEND_TRON_LOG, true);
|
||||
|
||||
// Get default value from Settings.Secure
|
||||
final int defaultGpsMode = Settings.Secure.getInt(mContentResolver, SECURE_KEY_GPS_MODE,
|
||||
@@ -384,10 +392,13 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
if (mLaunchBoostDisabled) sb.append("l");
|
||||
if (mOptionalSensorsDisabled) sb.append("S");
|
||||
if (mAodDisabled) sb.append("o");
|
||||
if (mSendTronLog) sb.append("t");
|
||||
|
||||
sb.append(mGpsMode);
|
||||
|
||||
mEventLogKeys = sb.toString();
|
||||
|
||||
BatterySavingStats.getInstance().setSendTronLog(mSendTronLog);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -483,7 +494,10 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
public void dump(PrintWriter pw) {
|
||||
synchronized (mLock) {
|
||||
pw.println();
|
||||
pw.println("Battery saver policy");
|
||||
BatterySavingStats.getInstance().dump(pw, "");
|
||||
|
||||
pw.println();
|
||||
pw.println("Battery saver policy (*NOTE* they only apply when battery saver is ON):");
|
||||
pw.println(" Settings: " + Settings.Global.BATTERY_SAVER_CONSTANTS);
|
||||
pw.println(" value: " + mSettings);
|
||||
pw.println(" Settings: " + mDeviceSpecificSettingsSource);
|
||||
@@ -504,6 +518,7 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
pw.println(" " + KEY_FORCE_BACKGROUND_CHECK + "=" + mForceBackgroundCheck);
|
||||
pw.println(" " + KEY_OPTIONAL_SENSORS_DISABLED + "=" + mOptionalSensorsDisabled);
|
||||
pw.println(" " + KEY_AOD_DISABLED + "=" + mAodDisabled);
|
||||
pw.println(" " + KEY_SEND_TRON_LOG + "=" + mSendTronLog);
|
||||
pw.println();
|
||||
|
||||
pw.print(" Interactive File values:\n");
|
||||
@@ -512,9 +527,6 @@ public class BatterySaverPolicy extends ContentObserver {
|
||||
|
||||
pw.print(" Noninteractive File values:\n");
|
||||
dumpMap(pw, " ", mFilesForNoninteractive);
|
||||
pw.println();
|
||||
pw.println();
|
||||
BatterySavingStats.getInstance().dump(pw, " ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import android.os.BatteryManagerInternal;
|
||||
import android.os.SystemClock;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Slog;
|
||||
import android.util.TimeUtils;
|
||||
|
||||
import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
@@ -31,6 +32,8 @@ import com.android.server.LocalServices;
|
||||
import com.android.server.power.BatterySaverPolicy;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* This class keeps track of battery drain rate.
|
||||
@@ -46,9 +49,6 @@ public class BatterySavingStats {
|
||||
|
||||
private static final boolean DEBUG = BatterySaverPolicy.DEBUG;
|
||||
|
||||
@VisibleForTesting
|
||||
static final boolean SEND_TRON_EVENTS = true;
|
||||
|
||||
private final Object mLock = new Object();
|
||||
|
||||
/** Whether battery saver is on or off. */
|
||||
@@ -159,8 +159,18 @@ public class BatterySavingStats {
|
||||
@GuardedBy("mLock")
|
||||
final ArrayMap<Integer, Stat> mStats = new ArrayMap<>();
|
||||
|
||||
@GuardedBy("mLock")
|
||||
private int mBatterySaverEnabledCount = 0;
|
||||
|
||||
@GuardedBy("mLock")
|
||||
private long mLastBatterySaverEnabledTime = 0;
|
||||
|
||||
private final MetricsLoggerHelper mMetricsLoggerHelper = new MetricsLoggerHelper();
|
||||
|
||||
@VisibleForTesting
|
||||
@GuardedBy("mLock")
|
||||
private boolean mSendTronLog;
|
||||
|
||||
/**
|
||||
* Don't call it directly -- use {@link #getInstance()}. Not private for testing.
|
||||
* @param metricsLogger
|
||||
@@ -178,6 +188,12 @@ public class BatterySavingStats {
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public void setSendTronLog(boolean send) {
|
||||
synchronized (mLock) {
|
||||
mSendTronLog = send;
|
||||
}
|
||||
}
|
||||
|
||||
private BatteryManagerInternal getBatteryManagerInternal() {
|
||||
if (mBatteryManagerInternal == null) {
|
||||
mBatteryManagerInternal = LocalServices.getService(BatteryManagerInternal.class);
|
||||
@@ -291,9 +307,22 @@ public class BatterySavingStats {
|
||||
final int batteryLevel = injectBatteryLevel();
|
||||
final int batteryPercent = injectBatteryPercent();
|
||||
|
||||
final boolean oldBatterySaverEnabled =
|
||||
BatterySaverState.fromIndex(mCurrentState) != BatterySaverState.OFF;
|
||||
final boolean newBatterySaverEnabled =
|
||||
BatterySaverState.fromIndex(newState) != BatterySaverState.OFF;
|
||||
if (oldBatterySaverEnabled != newBatterySaverEnabled) {
|
||||
if (newBatterySaverEnabled) {
|
||||
mBatterySaverEnabledCount++;
|
||||
mLastBatterySaverEnabledTime = injectCurrentTime();
|
||||
} else {
|
||||
mLastBatterySaverEnabledTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
endLastStateLocked(now, batteryLevel, batteryPercent);
|
||||
startNewStateLocked(newState, now, batteryLevel, batteryPercent);
|
||||
mMetricsLoggerHelper.transitionState(newState, now, batteryLevel, batteryPercent);
|
||||
mMetricsLoggerHelper.transitionStateLocked(newState, now, batteryLevel, batteryPercent);
|
||||
}
|
||||
|
||||
@GuardedBy("mLock")
|
||||
@@ -358,12 +387,39 @@ public class BatterySavingStats {
|
||||
public void dump(PrintWriter pw, String indent) {
|
||||
synchronized (mLock) {
|
||||
pw.print(indent);
|
||||
pw.println("Battery Saving Stats:");
|
||||
pw.println("Battery saving stats:");
|
||||
|
||||
indent = indent + " ";
|
||||
|
||||
pw.print(indent);
|
||||
pw.println("Battery Saver: w/Off w/On");
|
||||
pw.print("Battery Saver state: ");
|
||||
if (mLastBatterySaverEnabledTime == 0) {
|
||||
pw.print("OFF");
|
||||
} else {
|
||||
pw.print("ON since ");
|
||||
|
||||
final long now = System.currentTimeMillis();
|
||||
final long nowElapsed = injectCurrentTime();
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
pw.print(sdf.format(new Date(now - nowElapsed + mLastBatterySaverEnabledTime)));
|
||||
|
||||
pw.print(" ");
|
||||
TimeUtils.formatDuration(mLastBatterySaverEnabledTime, nowElapsed, pw);
|
||||
}
|
||||
pw.println();
|
||||
|
||||
pw.print(indent);
|
||||
pw.print("Times enabled: ");
|
||||
pw.println(mBatterySaverEnabledCount);
|
||||
|
||||
pw.println();
|
||||
|
||||
pw.print(indent);
|
||||
pw.println("Drain stats:");
|
||||
|
||||
pw.print(indent);
|
||||
pw.println(" Battery saver OFF ON");
|
||||
dumpLineLocked(pw, indent, InteractiveState.NON_INTERACTIVE, "NonIntr",
|
||||
DozeState.NOT_DOZING, "NonDoze");
|
||||
dumpLineLocked(pw, indent, InteractiveState.INTERACTIVE, " Intr",
|
||||
@@ -378,8 +434,6 @@ public class BatterySavingStats {
|
||||
DozeState.LIGHT, "Light ");
|
||||
dumpLineLocked(pw, indent, InteractiveState.INTERACTIVE, " Intr",
|
||||
DozeState.LIGHT, " ");
|
||||
|
||||
pw.println();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,7 +449,7 @@ public class BatterySavingStats {
|
||||
final Stat offStat = getStat(BatterySaverState.OFF, interactiveState, dozeState);
|
||||
final Stat onStat = getStat(BatterySaverState.ON, interactiveState, dozeState);
|
||||
|
||||
pw.println(String.format("%6dm %6dmA (%3d%%) %8.1fmA/h %6dm %6dmA (%3d%%) %8.1fmA/h",
|
||||
pw.println(String.format("%6dm %6dmAh(%3d%%) %8.1fmAh/h %6dm %6dmAh(%3d%%) %8.1fmAh/h",
|
||||
offStat.totalMinutes(),
|
||||
offStat.totalBatteryDrain / 1000,
|
||||
offStat.totalBatteryDrainPercent,
|
||||
@@ -417,7 +471,8 @@ public class BatterySavingStats {
|
||||
(BatterySaverState.MASK << BatterySaverState.SHIFT) |
|
||||
(InteractiveState.MASK << InteractiveState.SHIFT);
|
||||
|
||||
public void transitionState(int newState, long now, int batteryLevel, int batteryPercent) {
|
||||
public void transitionStateLocked(
|
||||
int newState, long now, int batteryLevel, int batteryPercent) {
|
||||
final boolean stateChanging =
|
||||
((mLastState >= 0) ^ (newState >= 0)) ||
|
||||
(((mLastState ^ newState) & STATE_CHANGE_DETECT_MASK) != 0);
|
||||
@@ -425,7 +480,7 @@ public class BatterySavingStats {
|
||||
if (mLastState >= 0) {
|
||||
final long deltaTime = now - mStartTime;
|
||||
|
||||
report(mLastState, deltaTime, mStartBatteryLevel, mStartPercent,
|
||||
reportLocked(mLastState, deltaTime, mStartBatteryLevel, mStartPercent,
|
||||
batteryLevel, batteryPercent);
|
||||
}
|
||||
mStartTime = now;
|
||||
@@ -435,10 +490,10 @@ public class BatterySavingStats {
|
||||
mLastState = newState;
|
||||
}
|
||||
|
||||
void report(int state, long deltaTimeMs,
|
||||
void reportLocked(int state, long deltaTimeMs,
|
||||
int startBatteryLevelUa, int startBatteryLevelPercent,
|
||||
int endBatteryLevelUa, int endBatteryLevelPercent) {
|
||||
if (!SEND_TRON_EVENTS) {
|
||||
if (!mSendTronLog) {
|
||||
return;
|
||||
}
|
||||
final boolean batterySaverOn =
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.android.server.power.batterysaver;
|
||||
|
||||
import static com.android.server.power.batterysaver.BatterySavingStats.SEND_TRON_EVENTS;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
@@ -105,9 +103,23 @@ public class BatterySavingStatsTest {
|
||||
|
||||
public MetricsLogger mMetricsLogger = mock(MetricsLogger.class);
|
||||
|
||||
private boolean sendTronEvents;
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
public void testAll_withTron() {
|
||||
sendTronEvents = true;
|
||||
checkAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAll_noTron() {
|
||||
sendTronEvents = false;
|
||||
checkAll();
|
||||
}
|
||||
|
||||
private void checkAll() {
|
||||
final BatterySavingStatsTestable target = new BatterySavingStatsTestable();
|
||||
target.setSendTronLog(sendTronEvents);
|
||||
|
||||
target.assertDumpable();
|
||||
|
||||
@@ -229,7 +241,7 @@ public class BatterySavingStatsTest {
|
||||
|
||||
private void assertLog(boolean batterySaver, boolean interactive, long deltaTimeMs,
|
||||
int deltaBatteryLevelUa, int deltaBatteryLevelPercent) {
|
||||
if (SEND_TRON_EVENTS) {
|
||||
if (sendTronEvents) {
|
||||
ArgumentCaptor<LogMaker> ac = ArgumentCaptor.forClass(LogMaker.class);
|
||||
verify(mMetricsLogger, times(1)).write(ac.capture());
|
||||
|
||||
@@ -251,9 +263,22 @@ public class BatterySavingStatsTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMetricsLogger() {
|
||||
public void testMetricsLogger_withTron() {
|
||||
sendTronEvents = true;
|
||||
checkMetricsLogger();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMetricsLogger_noTron() {
|
||||
sendTronEvents = false;
|
||||
checkMetricsLogger();
|
||||
}
|
||||
|
||||
private void checkMetricsLogger() {
|
||||
final BatterySavingStatsTestable target = new BatterySavingStatsTestable();
|
||||
target.setSendTronLog(sendTronEvents);
|
||||
|
||||
target.advanceClock(1);
|
||||
target.drainBattery(1000);
|
||||
|
||||
Reference in New Issue
Block a user