Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b666de7b authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Dump battery history for checkin without locking

Bug: 266600420
Test: atest FrameworksServicesTests:BatteryStatsTests
Change-Id: Iabec133d48563ce826925e5dec2fbe516dc4564a
parent 13b8bf0e
Loading
Loading
Loading
Loading
+53 −46
Original line number Diff line number Diff line
@@ -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("):");
            }
            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) {
        synchronized (this) {
            prepareForDumpLocked();

            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++) {
+8 −10
Original line number Diff line number Diff line
@@ -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);
            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");
        }
    }