Remove dependency of battery history on com.android.internal.os.BatteryStatsImpl

Bug: 235635119
Test: atest FrameworksCoreTests:com.android.internal.os.BatteryStatsTests
Change-Id: I6b573f90591aa0e404e7963c004de4dc2ac2452b
This commit is contained in:
Dmitri Plotnikov
2022-06-10 16:05:56 -07:00
parent a76d24a017
commit 4596e5fccf
5 changed files with 136 additions and 119 deletions

View File

@@ -16,7 +16,6 @@
package com.android.internal.os;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.BatteryStats;
import android.os.Parcel;
@@ -36,6 +35,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
/**
* BatteryStatsHistory encapsulates battery history files.
@@ -60,12 +60,51 @@ import java.util.Set;
public class BatteryStatsHistory {
private static final boolean DEBUG = false;
private static final String TAG = "BatteryStatsHistory";
// Current on-disk Parcel version. Must be updated when the format of the parcelable changes
public static final int VERSION = 208;
public static final String HISTORY_DIR = "battery-history";
public static final String FILE_SUFFIX = ".bin";
private static final int MIN_FREE_SPACE = 100 * 1024 * 1024;
// Part of initial delta int that specifies the time delta.
public static final int DELTA_TIME_MASK = 0x7ffff;
public static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
public static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
public static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
// Flag in delta int: a new battery level int follows.
public static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
// Flag in delta int: a new full state and battery status int follows.
public static final int DELTA_STATE_FLAG = 0x00100000;
// Flag in delta int: a new full state2 int follows.
public static final int DELTA_STATE2_FLAG = 0x00200000;
// Flag in delta int: contains a wakelock or wakeReason tag.
public static final int DELTA_WAKELOCK_FLAG = 0x00400000;
// Flag in delta int: contains an event description.
public static final int DELTA_EVENT_FLAG = 0x00800000;
// Flag in delta int: contains the battery charge count in uAh.
public static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
// These upper bits are the frequently changing state bits.
public static final int DELTA_STATE_MASK = 0xfe000000;
// These are the pieces of battery state that are packed in to the upper bits of
// the state int that have been packed in to the first delta int. They must fit
// in STATE_BATTERY_MASK.
public static final int STATE_BATTERY_MASK = 0xff000000;
public static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
public static final int STATE_BATTERY_STATUS_SHIFT = 29;
public static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
public static final int STATE_BATTERY_HEALTH_SHIFT = 26;
public static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
public static final int STATE_BATTERY_PLUG_SHIFT = 24;
// We use the low bit of the battery state int to indicate that we have full details
// from a battery level change.
public static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
// Flag in history tag index: indicates that this is the first occurrence of this tag,
// therefore the tag value is written in the parcel
public static final int TAG_FIRST_OCCURRENCE_FLAG = 0x8000;
@Nullable
private final BatteryStatsImpl mStats;
private final Supplier<Integer> mMaxHistoryFiles;
private final Parcel mHistoryBuffer;
private final File mHistoryDir;
/**
@@ -107,15 +146,17 @@ public class BatteryStatsHistory {
/**
* Constructor
* @param stats BatteryStatsImpl object.
* @param systemDir typically /data/system
* @param historyBuffer The in-memory history buffer.
*
* @param historyBuffer The in-memory history buffer.
* @param systemDir typically /data/system
* @param maxHistoryFiles the largest number of history buffer files to keep
*/
public BatteryStatsHistory(@NonNull BatteryStatsImpl stats, File systemDir,
Parcel historyBuffer) {
mStats = stats;
public BatteryStatsHistory(Parcel historyBuffer, File systemDir,
Supplier<Integer> maxHistoryFiles) {
mHistoryBuffer = historyBuffer;
mHistoryDir = new File(systemDir, HISTORY_DIR);
mMaxHistoryFiles = maxHistoryFiles;
mHistoryDir.mkdirs();
if (!mHistoryDir.exists()) {
Slog.wtf(TAG, "HistoryDir does not exist:" + mHistoryDir.getPath());
@@ -157,9 +198,9 @@ public class BatteryStatsHistory {
* @param historyBuffer the history buffer
*/
public BatteryStatsHistory(Parcel historyBuffer) {
mStats = null;
mHistoryDir = null;
mHistoryBuffer = historyBuffer;
mMaxHistoryFiles = null;
}
public File getHistoryDirectory() {
@@ -193,8 +234,8 @@ public class BatteryStatsHistory {
* create next history file.
*/
public void startNextFile() {
if (mStats == null) {
Slog.wtf(TAG, "mStats should not be null when writing history");
if (mMaxHistoryFiles == null) {
Slog.wtf(TAG, "mMaxHistoryFiles should not be zero when writing history");
return;
}
@@ -221,8 +262,9 @@ public class BatteryStatsHistory {
}
// if there are more history files than allowed, delete oldest history files.
// MAX_HISTORY_FILES can be updated by GService config at run time.
while (mFileNumbers.size() > mStats.mConstants.MAX_HISTORY_FILES) {
// mMaxHistoryFiles comes from Constants.MAX_HISTORY_FILES and can be updated by GService
// config at run time.
while (mFileNumbers.size() > mMaxHistoryFiles.get()) {
int oldest = mFileNumbers.get(0);
getFile(oldest).delete();
mFileNumbers.remove(0);
@@ -377,7 +419,7 @@ public class BatteryStatsHistory {
private boolean skipHead(Parcel p) {
p.setDataPosition(0);
final int version = p.readInt();
if (version != BatteryStatsImpl.VERSION) {
if (version != VERSION) {
return false;
}
// skip historyBaseTime field.

View File

@@ -62,7 +62,7 @@ public class BatteryStatsHistoryIterator {
void readHistoryDelta(Parcel src, BatteryStats.HistoryItem cur) {
int firstToken = src.readInt();
int deltaTimeToken = firstToken & BatteryStatsImpl.DELTA_TIME_MASK;
int deltaTimeToken = firstToken & BatteryStatsHistory.DELTA_TIME_MASK;
cur.cmd = BatteryStats.HistoryItem.CMD_UPDATE;
cur.numReadInts = 1;
if (DEBUG) {
@@ -70,13 +70,13 @@ public class BatteryStatsHistoryIterator {
+ " deltaTimeToken=" + deltaTimeToken);
}
if (deltaTimeToken < BatteryStatsImpl.DELTA_TIME_ABS) {
if (deltaTimeToken < BatteryStatsHistory.DELTA_TIME_ABS) {
cur.time += deltaTimeToken;
} else if (deltaTimeToken == BatteryStatsImpl.DELTA_TIME_ABS) {
} else if (deltaTimeToken == BatteryStatsHistory.DELTA_TIME_ABS) {
cur.readFromParcel(src);
if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
return;
} else if (deltaTimeToken == BatteryStatsImpl.DELTA_TIME_INT) {
} else if (deltaTimeToken == BatteryStatsHistory.DELTA_TIME_INT) {
int delta = src.readInt();
cur.time += delta;
cur.numReadInts += 1;
@@ -89,7 +89,7 @@ public class BatteryStatsHistoryIterator {
}
final int batteryLevelInt;
if ((firstToken & BatteryStatsImpl.DELTA_BATTERY_LEVEL_FLAG) != 0) {
if ((firstToken & BatteryStatsHistory.DELTA_BATTERY_LEVEL_FLAG) != 0) {
batteryLevelInt = src.readInt();
readBatteryLevelInt(batteryLevelInt, cur);
cur.numReadInts += 1;
@@ -104,16 +104,16 @@ public class BatteryStatsHistoryIterator {
batteryLevelInt = 0;
}
if ((firstToken & BatteryStatsImpl.DELTA_STATE_FLAG) != 0) {
if ((firstToken & BatteryStatsHistory.DELTA_STATE_FLAG) != 0) {
int stateInt = src.readInt();
cur.states = (firstToken & BatteryStatsImpl.DELTA_STATE_MASK) | (stateInt
& (~BatteryStatsImpl.STATE_BATTERY_MASK));
cur.batteryStatus = (byte) ((stateInt >> BatteryStatsImpl.STATE_BATTERY_STATUS_SHIFT)
& BatteryStatsImpl.STATE_BATTERY_STATUS_MASK);
cur.batteryHealth = (byte) ((stateInt >> BatteryStatsImpl.STATE_BATTERY_HEALTH_SHIFT)
& BatteryStatsImpl.STATE_BATTERY_HEALTH_MASK);
cur.batteryPlugType = (byte) ((stateInt >> BatteryStatsImpl.STATE_BATTERY_PLUG_SHIFT)
& BatteryStatsImpl.STATE_BATTERY_PLUG_MASK);
cur.states = (firstToken & BatteryStatsHistory.DELTA_STATE_MASK) | (stateInt
& (~BatteryStatsHistory.STATE_BATTERY_MASK));
cur.batteryStatus = (byte) ((stateInt >> BatteryStatsHistory.STATE_BATTERY_STATUS_SHIFT)
& BatteryStatsHistory.STATE_BATTERY_STATUS_MASK);
cur.batteryHealth = (byte) ((stateInt >> BatteryStatsHistory.STATE_BATTERY_HEALTH_SHIFT)
& BatteryStatsHistory.STATE_BATTERY_HEALTH_MASK);
cur.batteryPlugType = (byte) ((stateInt >> BatteryStatsHistory.STATE_BATTERY_PLUG_SHIFT)
& BatteryStatsHistory.STATE_BATTERY_PLUG_MASK);
switch (cur.batteryPlugType) {
case 1:
cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
@@ -135,11 +135,11 @@ public class BatteryStatsHistoryIterator {
+ " states=0x" + Integer.toHexString(cur.states));
}
} else {
cur.states = (firstToken & BatteryStatsImpl.DELTA_STATE_MASK) | (cur.states
& (~BatteryStatsImpl.STATE_BATTERY_MASK));
cur.states = (firstToken & BatteryStatsHistory.DELTA_STATE_MASK) | (cur.states
& (~BatteryStatsHistory.STATE_BATTERY_MASK));
}
if ((firstToken & BatteryStatsImpl.DELTA_STATE2_FLAG) != 0) {
if ((firstToken & BatteryStatsHistory.DELTA_STATE2_FLAG) != 0) {
cur.states2 = src.readInt();
if (DEBUG) {
Slog.i(TAG, "READ DELTA: states2=0x"
@@ -147,7 +147,7 @@ public class BatteryStatsHistoryIterator {
}
}
if ((firstToken & BatteryStatsImpl.DELTA_WAKELOCK_FLAG) != 0) {
if ((firstToken & BatteryStatsHistory.DELTA_WAKELOCK_FLAG) != 0) {
final int indexes = src.readInt();
final int wakeLockIndex = indexes & 0xffff;
final int wakeReasonIndex = (indexes >> 16) & 0xffff;
@@ -167,7 +167,7 @@ public class BatteryStatsHistoryIterator {
cur.wakeReasonTag = null;
}
if ((firstToken & BatteryStatsImpl.DELTA_EVENT_FLAG) != 0) {
if ((firstToken & BatteryStatsHistory.DELTA_EVENT_FLAG) != 0) {
cur.eventTag = cur.localEventTag;
final int codeAndIndex = src.readInt();
cur.eventCode = (codeAndIndex & 0xffff);
@@ -187,14 +187,14 @@ public class BatteryStatsHistoryIterator {
cur.eventCode = BatteryStats.HistoryItem.EVENT_NONE;
}
if ((batteryLevelInt & BatteryStatsImpl.BATTERY_DELTA_LEVEL_FLAG) != 0) {
if ((batteryLevelInt & BatteryStatsHistory.BATTERY_DELTA_LEVEL_FLAG) != 0) {
cur.stepDetails = mReadHistoryStepDetails;
cur.stepDetails.readFromParcel(src);
} else {
cur.stepDetails = null;
}
if ((firstToken & BatteryStatsImpl.DELTA_BATTERY_CHARGE_FLAG) != 0) {
if ((firstToken & BatteryStatsHistory.DELTA_BATTERY_CHARGE_FLAG) != 0) {
cur.batteryChargeUah = src.readInt();
}
cur.modemRailChargeMah = src.readDouble();
@@ -206,10 +206,10 @@ public class BatteryStatsHistoryIterator {
return false;
}
if ((index & BatteryStatsImpl.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
if ((index & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
BatteryStats.HistoryTag tag = new BatteryStats.HistoryTag();
tag.readFromParcel(src);
tag.poolIdx = index & ~BatteryStatsImpl.TAG_FIRST_OCCURRENCE_FLAG;
tag.poolIdx = index & ~BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG;
mHistoryTags.put(tag.poolIdx, tag);
outTag.setTo(tag);

View File

@@ -166,8 +166,8 @@ public class BatteryStatsImpl extends BatteryStats {
// In-memory Parcel magic number, used to detect attempts to unmarshall bad data
private static final int MAGIC = 0xBA757475; // 'BATSTATS'
// Current on-disk Parcel version
static final int VERSION = 208;
// Current on-disk Parcel version. Must be updated when the format of the parcelable changes
public static final int VERSION = 208;
// The maximum number of names wakelocks we will keep track of
// per uid; once the limit is reached, we batch the remaining wakelocks
@@ -1625,7 +1625,8 @@ public class BatteryStatsImpl extends BatteryStats {
mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer);
} else {
mStatsFile = new AtomicFile(new File(historyDirectory, "batterystats.bin"));
mBatteryStatsHistory = new BatteryStatsHistory(this, historyDirectory, mHistoryBuffer);
mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer, historyDirectory,
this::getMaxHistoryFiles);
}
mHandler = null;
mPlatformIdleStateCallback = null;
@@ -3980,8 +3981,8 @@ public class BatteryStatsImpl extends BatteryStats {
int idx;
if (idxObj != null) {
idx = idxObj;
if ((idx & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
mHistoryTagPool.put(tag, idx & ~TAG_FIRST_OCCURRENCE_FLAG);
if ((idx & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
mHistoryTagPool.put(tag, idx & ~BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG);
}
return idx;
} else if (mNextHistoryTagIdx < HISTORY_TAG_INDEX_LIMIT) {
@@ -3996,10 +3997,10 @@ public class BatteryStatsImpl extends BatteryStats {
if (mHistoryTags != null) {
mHistoryTags.put(idx, key);
}
return idx | TAG_FIRST_OCCURRENCE_FLAG;
return idx | BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG;
} else {
// Tag pool overflow: include the tag itself in the parcel
return HISTORY_TAG_INDEX_LIMIT | TAG_FIRST_OCCURRENCE_FLAG;
return HISTORY_TAG_INDEX_LIMIT | BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG;
}
}
@@ -4084,49 +4085,10 @@ public class BatteryStatsImpl extends BatteryStats {
in coulombs follows.
*/
// Part of initial delta int that specifies the time delta.
static final int DELTA_TIME_MASK = 0x7ffff;
static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
// Flag in delta int: a new battery level int follows.
static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
// Flag in delta int: a new full state and battery status int follows.
static final int DELTA_STATE_FLAG = 0x00100000;
// Flag in delta int: a new full state2 int follows.
static final int DELTA_STATE2_FLAG = 0x00200000;
// Flag in delta int: contains a wakelock or wakeReason tag.
static final int DELTA_WAKELOCK_FLAG = 0x00400000;
// Flag in delta int: contains an event description.
static final int DELTA_EVENT_FLAG = 0x00800000;
// Flag in delta int: contains the battery charge count in uAh.
static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
// These upper bits are the frequently changing state bits.
static final int DELTA_STATE_MASK = 0xfe000000;
// Flag in history tag index: indicates that this is the first occurrence of this tag,
// therefore the tag value is written in the parcel
static final int TAG_FIRST_OCCURRENCE_FLAG = 0x8000;
// These are the pieces of battery state that are packed in to the upper bits of
// the state int that have been packed in to the first delta int. They must fit
// in STATE_BATTERY_MASK.
static final int STATE_BATTERY_MASK = 0xff000000;
static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
static final int STATE_BATTERY_STATUS_SHIFT = 29;
static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
static final int STATE_BATTERY_HEALTH_SHIFT = 26;
static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
static final int STATE_BATTERY_PLUG_SHIFT = 24;
// We use the low bit of the battery state int to indicate that we have full details
// from a battery level change.
static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
@GuardedBy("this")
public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
dest.writeInt(DELTA_TIME_ABS);
dest.writeInt(BatteryStatsHistory.DELTA_TIME_ABS);
cur.writeToParcel(dest, 0);
return;
}
@@ -4137,48 +4099,48 @@ public class BatteryStatsImpl extends BatteryStats {
int deltaTimeToken;
if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
deltaTimeToken = DELTA_TIME_LONG;
} else if (deltaTime >= DELTA_TIME_ABS) {
deltaTimeToken = DELTA_TIME_INT;
deltaTimeToken = BatteryStatsHistory.DELTA_TIME_LONG;
} else if (deltaTime >= BatteryStatsHistory.DELTA_TIME_ABS) {
deltaTimeToken = BatteryStatsHistory.DELTA_TIME_INT;
} else {
deltaTimeToken = (int)deltaTime;
}
int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
int firstToken = deltaTimeToken | (cur.states & BatteryStatsHistory.DELTA_STATE_MASK);
final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
? BATTERY_DELTA_LEVEL_FLAG : 0;
? BatteryStatsHistory.BATTERY_DELTA_LEVEL_FLAG : 0;
final boolean computeStepDetails = includeStepDetails != 0
|| mLastHistoryStepDetails == null;
final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
if (batteryLevelIntChanged) {
firstToken |= DELTA_BATTERY_LEVEL_FLAG;
firstToken |= BatteryStatsHistory.DELTA_BATTERY_LEVEL_FLAG;
}
final int stateInt = buildStateInt(cur);
final boolean stateIntChanged = stateInt != lastStateInt;
if (stateIntChanged) {
firstToken |= DELTA_STATE_FLAG;
firstToken |= BatteryStatsHistory.DELTA_STATE_FLAG;
}
final boolean state2IntChanged = cur.states2 != last.states2;
if (state2IntChanged) {
firstToken |= DELTA_STATE2_FLAG;
firstToken |= BatteryStatsHistory.DELTA_STATE2_FLAG;
}
if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
firstToken |= DELTA_WAKELOCK_FLAG;
firstToken |= BatteryStatsHistory.DELTA_WAKELOCK_FLAG;
}
if (cur.eventCode != HistoryItem.EVENT_NONE) {
firstToken |= DELTA_EVENT_FLAG;
firstToken |= BatteryStatsHistory.DELTA_EVENT_FLAG;
}
final boolean batteryChargeChanged = cur.batteryChargeUah != last.batteryChargeUah;
if (batteryChargeChanged) {
firstToken |= DELTA_BATTERY_CHARGE_FLAG;
firstToken |= BatteryStatsHistory.DELTA_BATTERY_CHARGE_FLAG;
}
dest.writeInt(firstToken);
if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
+ " deltaTime=" + deltaTime);
if (deltaTimeToken >= DELTA_TIME_INT) {
if (deltaTimeToken == DELTA_TIME_INT) {
if (deltaTimeToken >= BatteryStatsHistory.DELTA_TIME_INT) {
if (deltaTimeToken == BatteryStatsHistory.DELTA_TIME_INT) {
if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
dest.writeInt((int)deltaTime);
} else {
@@ -4226,11 +4188,13 @@ public class BatteryStatsImpl extends BatteryStats {
wakeReasonIndex = 0xffff;
}
dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
if (cur.wakelockTag != null && (wakeLockIndex & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
if (cur.wakelockTag != null
&& (wakeLockIndex & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
cur.wakelockTag.writeToParcel(dest, 0);
cur.tagsFirstOccurrence = true;
}
if (cur.wakeReasonTag != null && (wakeReasonIndex & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
if (cur.wakeReasonTag != null
&& (wakeReasonIndex & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
cur.wakeReasonTag.writeToParcel(dest, 0);
cur.tagsFirstOccurrence = true;
}
@@ -4239,7 +4203,7 @@ public class BatteryStatsImpl extends BatteryStats {
final int index = writeHistoryTag(cur.eventTag);
final int codeAndIndex = (cur.eventCode & 0xffff) | (index << 16);
dest.writeInt(codeAndIndex);
if ((index & TAG_FIRST_OCCURRENCE_FLAG) != 0) {
if ((index & BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG) != 0) {
cur.eventTag.writeToParcel(dest, 0);
cur.tagsFirstOccurrence = true;
}
@@ -4298,10 +4262,13 @@ public class BatteryStatsImpl extends BatteryStats {
} else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
plugType = 3;
}
return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
| ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
| ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
| (h.states&(~STATE_BATTERY_MASK));
return ((h.batteryStatus & BatteryStatsHistory.STATE_BATTERY_STATUS_MASK)
<< BatteryStatsHistory.STATE_BATTERY_STATUS_SHIFT)
| ((h.batteryHealth & BatteryStatsHistory.STATE_BATTERY_HEALTH_MASK)
<< BatteryStatsHistory.STATE_BATTERY_HEALTH_SHIFT)
| ((plugType & BatteryStatsHistory.STATE_BATTERY_PLUG_MASK)
<< BatteryStatsHistory.STATE_BATTERY_PLUG_SHIFT)
| (h.states & (~BatteryStatsHistory.STATE_BATTERY_MASK));
}
private void computeHistoryStepDetails(final HistoryStepDetails out,
@@ -4501,7 +4468,7 @@ public class BatteryStatsImpl extends BatteryStats {
// Mark every entry in the pool with a flag indicating that the tag
// has not yet been encountered while writing the current history buffer.
for (Map.Entry<HistoryTag, Integer> entry: mHistoryTagPool.entrySet()) {
entry.setValue(entry.getValue() | TAG_FIRST_OCCURRENCE_FLAG);
entry.setValue(entry.getValue() | BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG);
}
startRecordingHistory(elapsedRealtimeMs, uptimeMs, false);
HistoryItem newItem = new HistoryItem();
@@ -12363,7 +12330,8 @@ public class BatteryStatsImpl extends BatteryStats {
mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer);
} else {
mStatsFile = new AtomicFile(new File(systemDir, "batterystats.bin"));
mBatteryStatsHistory = new BatteryStatsHistory(this, systemDir, mHistoryBuffer);
mBatteryStatsHistory = new BatteryStatsHistory(mHistoryBuffer, systemDir,
this::getMaxHistoryFiles);
}
mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
@@ -12388,6 +12356,12 @@ public class BatteryStatsImpl extends BatteryStats {
FrameworkStatsLog.write(FrameworkStatsLog.DEVICE_IDLE_MODE_STATE_CHANGED, mDeviceIdleMode);
}
private int getMaxHistoryFiles() {
synchronized (this) {
return mConstants.MAX_HISTORY_FILES;
}
}
@VisibleForTesting
protected void initTimersAndCounters() {
mScreenOnTimer = new StopwatchTimer(mClock, null, -1, null, mOnBatteryTimeBase);
@@ -12926,7 +12900,8 @@ public class BatteryStatsImpl extends BatteryStats {
mHistoryTags = new SparseArray<>(mHistoryTagPool.size());
for (Map.Entry<HistoryTag, Integer> entry: mHistoryTagPool.entrySet()) {
mHistoryTags.put(entry.getValue() & ~TAG_FIRST_OCCURRENCE_FLAG, entry.getKey());
mHistoryTags.put(entry.getValue() & ~BatteryStatsHistory.TAG_FIRST_OCCURRENCE_FLAG,
entry.getKey());
}
}
@@ -17089,9 +17064,9 @@ public class BatteryStatsImpl extends BatteryStats {
@GuardedBy("this")
void readHistoryBuffer(Parcel in) throws ParcelFormatException {
final int version = in.readInt();
if (version != VERSION) {
if (version != BatteryStatsHistory.VERSION) {
Slog.w("BatteryStats", "readHistoryBuffer: version got " + version
+ ", expected " + VERSION + "; erasing old stats");
+ ", expected " + BatteryStatsHistory.VERSION + "; erasing old stats");
return;
}
@@ -17152,7 +17127,7 @@ public class BatteryStatsImpl extends BatteryStats {
TimeUtils.formatDuration(mLastHistoryElapsedRealtimeMs, sb);
Slog.i(TAG, sb.toString());
}
out.writeInt(VERSION);
out.writeInt(BatteryStatsHistory.VERSION);
out.writeLong(mHistoryBaseTimeMs + mLastHistoryElapsedRealtimeMs);
if (!inclData) {
out.writeInt(0);

View File

@@ -227,7 +227,7 @@ public class BatteryUsageStatsProvider {
final File systemDir =
batteryStatsImpl.mBatteryStatsHistory.getHistoryDirectory().getParentFile();
final BatteryStatsHistory batteryStatsHistory =
new BatteryStatsHistory(batteryStatsImpl, systemDir, historyBuffer);
new BatteryStatsHistory(historyBuffer, systemDir, null);
batteryUsageStatsBuilder.setBatteryHistory(batteryStatsHistory);
}

View File

@@ -27,17 +27,17 @@ import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
/**
* Test BatteryStatsHistory.
*/
@@ -74,7 +74,7 @@ public class BatteryStatsHistoryTest {
@Test
public void testConstruct() {
BatteryStatsHistory history =
new BatteryStatsHistory(mBatteryStatsImpl, mSystemDir, mHistoryBuffer);
new BatteryStatsHistory(mHistoryBuffer, mSystemDir, () -> 32);
createActiveFile(history);
verifyFileNumbers(history, Arrays.asList(0));
verifyActiveFile(history, "0.bin");
@@ -83,7 +83,7 @@ public class BatteryStatsHistoryTest {
@Test
public void testStartNextFile() {
BatteryStatsHistory history =
new BatteryStatsHistory(mBatteryStatsImpl, mSystemDir, mHistoryBuffer);
new BatteryStatsHistory(mHistoryBuffer, mSystemDir, () -> 32);
List<Integer> fileList = new ArrayList<>();
fileList.add(0);
@@ -122,7 +122,7 @@ public class BatteryStatsHistoryTest {
// create a new BatteryStatsHistory object, it will pick up existing history files.
BatteryStatsHistory history2 =
new BatteryStatsHistory(mBatteryStatsImpl, mSystemDir, mHistoryBuffer);
new BatteryStatsHistory(mHistoryBuffer, mSystemDir, () -> 32);
// verify construct can pick up all files from file system.
verifyFileNumbers(history2, fileList);
verifyActiveFile(history2, "33.bin");