Improve logic around battery history time base

Bug: 280079024
Test: atest FrameworksCoreTests FrameworksServicesTests
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:707dcfb6b4e715e7e1d8e4d4b3e98e837b4d203d)
Merged-In: Iea5e3305f1bdc3e3fa6cb2a0d05082206aeae03c
Change-Id: Iea5e3305f1bdc3e3fa6cb2a0d05082206aeae03c
This commit is contained in:
Dmitri Plotnikov
2023-05-03 19:15:33 -07:00
committed by Cherrypicker Worker
parent a034b4741d
commit 5f550dc616
3 changed files with 24 additions and 15 deletions

View File

@@ -1468,6 +1468,11 @@ public class BatteryStatsHistory {
mHistoryLastLastWritten.setTo(mHistoryLastWritten);
final boolean hasTags = mHistoryLastWritten.tagsFirstOccurrence || cur.tagsFirstOccurrence;
mHistoryLastWritten.setTo(mHistoryBaseTimeMs + elapsedRealtimeMs, cmd, cur);
if (mHistoryLastWritten.time < mHistoryLastLastWritten.time - 60000) {
Slog.wtf(TAG, "Significantly earlier event written to battery history:"
+ " time=" + mHistoryLastWritten.time
+ " previous=" + mHistoryLastLastWritten.time);
}
mHistoryLastWritten.tagsFirstOccurrence = hasTags;
writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
mLastHistoryElapsedRealtimeMs = elapsedRealtimeMs;
@@ -1908,12 +1913,6 @@ public class BatteryStatsHistory {
in.setDataPosition(curPos + bufSize);
}
if (DEBUG) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** OLD mHistoryBaseTimeMs: ");
TimeUtils.formatDuration(mHistoryBaseTimeMs, sb);
Slog.i(TAG, sb.toString());
}
mHistoryBaseTimeMs = historyBaseTime;
if (DEBUG) {
StringBuilder sb = new StringBuilder(128);
@@ -1922,11 +1921,10 @@ public class BatteryStatsHistory {
Slog.i(TAG, sb.toString());
}
// We are just arbitrarily going to insert 1 minute from the sample of
// the last run until samples in this run.
if (mHistoryBaseTimeMs > 0) {
long oldnow = mClock.elapsedRealtime();
mHistoryBaseTimeMs = mHistoryBaseTimeMs - oldnow + 1;
long elapsedRealtimeMs = mClock.elapsedRealtime();
mLastHistoryElapsedRealtimeMs = elapsedRealtimeMs;
mHistoryBaseTimeMs = mHistoryBaseTimeMs - elapsedRealtimeMs + 1;
if (DEBUG) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** ADJUSTED mHistoryBaseTimeMs: ");

View File

@@ -2497,13 +2497,10 @@ public class ActivityManagerService extends IActivityManager.Stub
final File systemDir = SystemServiceManager.ensureSystemDir();
// TODO: Move creation of battery stats service outside of activity manager service.
mBatteryStatsService = new BatteryStatsService(systemContext, systemDir,
BackgroundThread.get().getHandler());
mBatteryStatsService.getActiveStatistics().readLocked();
mBatteryStatsService.scheduleWriteToDisk();
mBatteryStatsService = BatteryStatsService.create(systemContext, systemDir,
BackgroundThread.getHandler(), this);
mOnBattery = DEBUG_POWER ? true
: mBatteryStatsService.getActiveStatistics().getIsOnBattery();
mBatteryStatsService.getActiveStatistics().setCallback(this);
mOomAdjProfiler.batteryPowerChanged(mOnBattery);
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));

View File

@@ -399,6 +399,20 @@ public final class BatteryStatsService extends IBatteryStats.Stub
mCpuWakeupStats = new CpuWakeupStats(context, R.xml.irq_device_map, mHandler);
}
/**
* Creates an instance of BatteryStatsService and restores data from stored state.
*/
public static BatteryStatsService create(Context context, File systemDir, Handler handler,
BatteryStatsImpl.BatteryCallback callback) {
BatteryStatsService service = new BatteryStatsService(context, systemDir, handler);
service.mStats.setCallback(callback);
synchronized (service.mStats) {
service.mStats.readLocked();
}
service.scheduleWriteToDisk();
return service;
}
public void publish() {
LocalServices.addService(BatteryStatsInternal.class, new LocalService());
ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());