Merge "Revert "Breaking history writing out of BatteryStatsImpl""
This commit is contained in:
committed by
Android (Google) Code Review
commit
23d6326b82
@@ -2316,6 +2316,11 @@ public abstract class BatteryStats {
|
||||
|
||||
public abstract void finishIteratingHistoryLocked();
|
||||
|
||||
/**
|
||||
* Return the base time offset for the battery history.
|
||||
*/
|
||||
public abstract long getHistoryBaseTime();
|
||||
|
||||
/**
|
||||
* Returns the number of times the device has been started.
|
||||
*/
|
||||
@@ -7601,6 +7606,8 @@ public abstract class BatteryStats {
|
||||
CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
|
||||
getEndPlatformVersion());
|
||||
|
||||
long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
|
||||
|
||||
if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
|
||||
if (startIteratingHistoryLocked()) {
|
||||
try {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -36,6 +36,7 @@ public class BatteryStatsHistoryIterator {
|
||||
|
||||
public BatteryStatsHistoryIterator(@NonNull BatteryStatsHistory history) {
|
||||
mBatteryStatsHistory = history;
|
||||
mBatteryStatsHistory.startIteratingHistory();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,11 +231,4 @@ public class BatteryStatsHistoryIterator {
|
||||
out.batteryTemperature = (short) ((batteryLevelInt & 0x01ff8000) >>> 15);
|
||||
out.batteryVoltage = (char) ((batteryLevelInt & 0x00007ffe) >>> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called when iteration is complete.
|
||||
*/
|
||||
public void close() {
|
||||
mBatteryStatsHistory.finishIteratingHistory();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -657,7 +657,7 @@ public class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStat
|
||||
|
||||
// Now that we have finally received all the data, we can tell mStats about it.
|
||||
synchronized (mStats) {
|
||||
mStats.recordHistoryEventLocked(
|
||||
mStats.addHistoryEventLocked(
|
||||
elapsedRealtime,
|
||||
uptime,
|
||||
BatteryStats.HistoryItem.EVENT_COLLECT_EXTERNAL_STATS,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,7 @@ import android.os.BatteryConsumer;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.os.BatteryUsageStatsQuery;
|
||||
import android.os.Parcel;
|
||||
import android.os.Process;
|
||||
import android.os.SystemClock;
|
||||
import android.os.UidBatteryConsumer;
|
||||
@@ -31,8 +32,10 @@ import android.util.SparseArray;
|
||||
|
||||
import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.internal.os.BatteryStatsHistory;
|
||||
import com.android.internal.os.PowerProfile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -217,7 +220,18 @@ public class BatteryUsageStatsProvider {
|
||||
}
|
||||
|
||||
BatteryStatsImpl batteryStatsImpl = (BatteryStatsImpl) mStats;
|
||||
batteryUsageStatsBuilder.setBatteryHistory(batteryStatsImpl.copyHistory());
|
||||
|
||||
// Make a copy of battery history to avoid concurrent modification.
|
||||
Parcel historyBuffer = Parcel.obtain();
|
||||
historyBuffer.appendFrom(batteryStatsImpl.mHistoryBuffer, 0,
|
||||
batteryStatsImpl.mHistoryBuffer.dataSize());
|
||||
|
||||
final File systemDir =
|
||||
batteryStatsImpl.mBatteryStatsHistory.getHistoryDirectory().getParentFile();
|
||||
final BatteryStatsHistory batteryStatsHistory =
|
||||
new BatteryStatsHistory(historyBuffer, systemDir, null);
|
||||
|
||||
batteryUsageStatsBuilder.setBatteryHistory(batteryStatsHistory);
|
||||
}
|
||||
|
||||
BatteryUsageStats stats = batteryUsageStatsBuilder.build();
|
||||
|
||||
@@ -28,7 +28,6 @@ import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHistory;
|
||||
import com.android.internal.os.Clock;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -50,14 +49,13 @@ public class BatteryStatsHistoryTest {
|
||||
private final Parcel mHistoryBuffer = Parcel.obtain();
|
||||
private File mSystemDir;
|
||||
private File mHistoryDir;
|
||||
private final Clock mClock = new MockClock();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Context context = InstrumentationRegistry.getContext();
|
||||
mSystemDir = context.getDataDir();
|
||||
mHistoryDir = new File(mSystemDir, "battery-history");
|
||||
mHistoryDir = new File(mSystemDir, BatteryStatsHistory.HISTORY_DIR);
|
||||
String[] files = mHistoryDir.list();
|
||||
if (files != null) {
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
@@ -69,8 +67,8 @@ public class BatteryStatsHistoryTest {
|
||||
|
||||
@Test
|
||||
public void testConstruct() {
|
||||
BatteryStatsHistory history = new BatteryStatsHistory(mHistoryBuffer, mSystemDir, 32, 1024,
|
||||
null, mClock);
|
||||
BatteryStatsHistory history =
|
||||
new BatteryStatsHistory(mHistoryBuffer, mSystemDir, () -> 32);
|
||||
createActiveFile(history);
|
||||
verifyFileNumbers(history, Arrays.asList(0));
|
||||
verifyActiveFile(history, "0.bin");
|
||||
@@ -78,8 +76,8 @@ public class BatteryStatsHistoryTest {
|
||||
|
||||
@Test
|
||||
public void testStartNextFile() {
|
||||
BatteryStatsHistory history = new BatteryStatsHistory(mHistoryBuffer, mSystemDir, 32, 1024,
|
||||
null, mClock);
|
||||
BatteryStatsHistory history =
|
||||
new BatteryStatsHistory(mHistoryBuffer, mSystemDir, () -> 32);
|
||||
List<Integer> fileList = new ArrayList<>();
|
||||
fileList.add(0);
|
||||
createActiveFile(history);
|
||||
@@ -116,13 +114,13 @@ public class BatteryStatsHistoryTest {
|
||||
assertEquals(0, history.getHistoryUsedSize());
|
||||
|
||||
// create a new BatteryStatsHistory object, it will pick up existing history files.
|
||||
BatteryStatsHistory history2 = new BatteryStatsHistory(mHistoryBuffer, mSystemDir, 32, 1024,
|
||||
null, mClock);
|
||||
// verify constructor can pick up all files from file system.
|
||||
BatteryStatsHistory history2 =
|
||||
new BatteryStatsHistory(mHistoryBuffer, mSystemDir, () -> 32);
|
||||
// verify construct can pick up all files from file system.
|
||||
verifyFileNumbers(history2, fileList);
|
||||
verifyActiveFile(history2, "33.bin");
|
||||
|
||||
history2.reset();
|
||||
history2.resetAllFiles();
|
||||
createActiveFile(history2);
|
||||
// verify all existing files are deleted.
|
||||
for (int i = 2; i < 33; ++i) {
|
||||
|
||||
@@ -63,7 +63,6 @@ public class MockBatteryStatsImpl extends BatteryStatsImpl {
|
||||
MockBatteryStatsImpl(Clock clock, File historyDirectory) {
|
||||
super(clock, historyDirectory);
|
||||
initTimersAndCounters();
|
||||
setMaxHistoryBuffer(128 * 1024);
|
||||
|
||||
setExternalStatsSyncLocked(mExternalStatsSync);
|
||||
informThatAllExternalStatsAreFlushed();
|
||||
@@ -105,6 +104,12 @@ public class MockBatteryStatsImpl extends BatteryStatsImpl {
|
||||
return mForceOnBattery ? true : super.isOnBattery();
|
||||
}
|
||||
|
||||
public void forceRecordAllHistory() {
|
||||
mHaveBatteryLevel = true;
|
||||
mRecordingHistory = true;
|
||||
mRecordAllHistory = true;
|
||||
}
|
||||
|
||||
public TimeBase getOnBatteryBackgroundTimeBase(int uid) {
|
||||
return getUidStatsLocked(uid).mOnBatteryBackgroundTimeBase;
|
||||
}
|
||||
@@ -196,14 +201,12 @@ public class MockBatteryStatsImpl extends BatteryStatsImpl {
|
||||
@GuardedBy("this")
|
||||
public MockBatteryStatsImpl setMaxHistoryFiles(int maxHistoryFiles) {
|
||||
mConstants.MAX_HISTORY_FILES = maxHistoryFiles;
|
||||
mConstants.onChange();
|
||||
return this;
|
||||
}
|
||||
|
||||
@GuardedBy("this")
|
||||
public MockBatteryStatsImpl setMaxHistoryBuffer(int maxHistoryBuffer) {
|
||||
mConstants.MAX_HISTORY_BUFFER = maxHistoryBuffer;
|
||||
mConstants.onChange();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user