Dump battery history for checkin without locking

Bug: 266600420
Test: atest FrameworksServicesTests:BatteryStatsTests
Change-Id: Iabec133d48563ce826925e5dec2fbe516dc4564a
This commit is contained in:
Dmitri Plotnikov
2023-02-01 17:58:57 -08:00
parent 13b8bf0eed
commit b666de7b5e
2 changed files with 61 additions and 56 deletions

View File

@@ -4356,13 +4356,6 @@ public abstract class BatteryStats {
}
}
/**
* Temporary for settings.
*/
public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
dumpCheckinLocked(context, pw, which, reqUid, checkWifiOnly(context));
}
private static final String[] CHECKIN_POWER_COMPONENT_LABELS =
new String[BatteryConsumer.POWER_COMPONENT_COUNT];
static {
@@ -7477,22 +7470,8 @@ public abstract class BatteryStats {
public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
private void dumpHistory(PrintWriter pw, int flags, long histStart, boolean checkin) {
if (!checkin) {
synchronized (this) {
final long historyTotalSize = getHistoryTotalSize();
final long historyUsedSize = getHistoryUsedSize();
pw.print("Battery History (");
pw.print((100 * historyUsedSize) / historyTotalSize);
pw.print("% used, ");
printSizeValue(pw, historyUsedSize);
pw.print(" used of ");
printSizeValue(pw, historyTotalSize);
pw.print(", ");
pw.print(getHistoryStringPoolSize());
pw.print(" strings using ");
printSizeValue(pw, getHistoryStringPoolBytes());
pw.println("):");
}
synchronized (this) {
dumpHistoryTagPoolLocked(pw, checkin);
}
final HistoryPrinter hprinter = new HistoryPrinter();
@@ -7586,6 +7565,43 @@ public abstract class BatteryStats {
}
}
private void dumpHistoryTagPoolLocked(PrintWriter pw, boolean checkin) {
if (checkin) {
for (int i = 0; i < getHistoryStringPoolSize(); i++) {
pw.print(BATTERY_STATS_CHECKIN_VERSION);
pw.print(',');
pw.print(HISTORY_STRING_POOL);
pw.print(',');
pw.print(i);
pw.print(",");
pw.print(getHistoryTagPoolUid(i));
pw.print(",\"");
String str = getHistoryTagPoolString(i);
if (str != null) {
str = str.replace("\\", "\\\\");
str = str.replace("\"", "\\\"");
pw.print(str);
}
pw.print("\"");
pw.println();
}
} else {
final long historyTotalSize = getHistoryTotalSize();
final long historyUsedSize = getHistoryUsedSize();
pw.print("Battery History (");
pw.print((100 * historyUsedSize) / historyTotalSize);
pw.print("% used, ");
printSizeValue(pw, historyUsedSize);
pw.print(" used of ");
printSizeValue(pw, historyTotalSize);
pw.print(", ");
pw.print(getHistoryStringPoolSize());
pw.print(" strings using ");
printSizeValue(pw, getHistoryStringPoolBytes());
pw.println("):");
}
}
private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
if (steps == null) {
@@ -7802,33 +7818,17 @@ public abstract class BatteryStats {
// This is called from BatteryStatsService.
@SuppressWarnings("unused")
public void dumpCheckinLocked(Context context, PrintWriter pw,
public void dumpCheckin(Context context, PrintWriter pw,
List<ApplicationInfo> apps, int flags, long histStart) {
prepareForDumpLocked();
synchronized (this) {
prepareForDumpLocked();
dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
getEndPlatformVersion());
dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
getEndPlatformVersion());
}
if ((flags & (DUMP_INCLUDE_HISTORY | DUMP_HISTORY_ONLY)) != 0) {
for (int i = 0; i < getHistoryStringPoolSize(); i++) {
pw.print(BATTERY_STATS_CHECKIN_VERSION);
pw.print(',');
pw.print(HISTORY_STRING_POOL);
pw.print(',');
pw.print(i);
pw.print(",");
pw.print(getHistoryTagPoolUid(i));
pw.print(",\"");
String str = getHistoryTagPoolString(i);
if (str != null) {
str = str.replace("\\", "\\\\");
str = str.replace("\"", "\\\"");
pw.print(str);
}
pw.print("\"");
pw.println();
}
dumpHistory(pw, flags, histStart, true);
}
@@ -7836,6 +7836,13 @@ public abstract class BatteryStats {
return;
}
synchronized (this) {
dumpCheckinLocked(context, pw, apps, flags);
}
}
private void dumpCheckinLocked(Context context, PrintWriter pw, List<ApplicationInfo> apps,
int flags) {
if (apps != null) {
SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
for (int i=0; i<apps.size(); i++) {

View File

@@ -2880,7 +2880,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
checkinStats.setPowerProfileLocked(mPowerProfile);
checkinStats.readSummaryFromParcel(in);
in.recycle();
checkinStats.dumpCheckinLocked(mContext, pw, apps, flags,
checkinStats.dumpCheckin(mContext, pw, apps, flags,
historyStart);
mStats.mCheckinFile.delete();
return;
@@ -2892,17 +2892,15 @@ public final class BatteryStatsService extends IBatteryStats.Stub
}
}
}
if (DBG) Slog.d(TAG, "begin dumpCheckinLocked from UID " + Binder.getCallingUid());
if (DBG) Slog.d(TAG, "begin dumpCheckin from UID " + Binder.getCallingUid());
awaitCompletion();
synchronized (mStats) {
mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
if (writeData) {
mStats.writeAsyncLocked();
}
mStats.dumpCheckin(mContext, pw, apps, flags, historyStart);
if (writeData) {
mStats.writeAsyncLocked();
}
if (DBG) Slog.d(TAG, "end dumpCheckinLocked");
if (DBG) Slog.d(TAG, "end dumpCheckin");
} else {
if (DBG) Slog.d(TAG, "begin dumpLocked from UID " + Binder.getCallingUid());
if (DBG) Slog.d(TAG, "begin dump from UID " + Binder.getCallingUid());
awaitCompletion();
mStats.dump(mContext, pw, flags, reqUid, historyStart);
@@ -2912,7 +2910,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
pw.println();
mCpuWakeupStats.dump(new IndentingPrintWriter(pw, " "), SystemClock.elapsedRealtime());
if (DBG) Slog.d(TAG, "end dumpLocked");
if (DBG) Slog.d(TAG, "end dump");
}
}