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

Commit 30996daa authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add Log.wtf to help diagnose out-of-order battery history events

Bug: 407828438
Test: atest PowerStatsTests
Flag: com.android.server.power.optimization.report_out_of_order_battery_history_events
Change-Id: I267f4590c7e4c0a6bbef7567e6fc1095bfb7382d
parent 6d6e8186
Loading
Loading
Loading
Loading
+24 −10
Original line number Original line Diff line number Diff line
@@ -1928,6 +1928,12 @@ public class BatteryStatsHistory {
                // Negative or excessively large time deltas are unexpected.
                // Negative or excessively large time deltas are unexpected.
                debugLog(cur, last, "Unexpected time delta: " + deltaTime, true);
                debugLog(cur, last, "Unexpected time delta: " + deltaTime, true);
            }
            }
            if (com.android.server.power.optimization.Flags
                    .reportOutOfOrderBatteryHistoryEvents()) {
                // Generate verbose syslog to help root-cause the issue
                debugLog(Log.ERROR, cur, last, "Unexpected time delta: " + deltaTime, false);
                Slog.wtfStack(TAG, "Out-of-order battery history event. Check logcat for details");
            }
        } else if (deltaTime >= BatteryStatsHistory.DELTA_TIME_ABS) {
        } else if (deltaTime >= BatteryStatsHistory.DELTA_TIME_ABS) {
            deltaTimeToken = BatteryStatsHistory.DELTA_TIME_INT;
            deltaTimeToken = BatteryStatsHistory.DELTA_TIME_INT;
        } else {
        } else {
@@ -2482,15 +2488,26 @@ public class BatteryStatsHistory {
            return;
            return;
        }
        }


        PrintWriter logWriter;
        try {
        try {
            logWriter = new PrintWriter(
            PrintWriter logWriter = new PrintWriter(
                    new FileOutputStream(DEBUG_LOG_FILE, /* append */ true));
                    new FileOutputStream(DEBUG_LOG_FILE, /* append */ true));
            debugLog(logWriter, item, last, message, includeStack);
            logWriter.println();
            logWriter.close();
        } catch (IOException e) {
        } catch (IOException e) {
            Slog.e(TAG, "Cannot create debug log file");
            Slog.e(TAG, "Cannot create debug log file");
            logWriter = new PrintWriter(new LogWriter(Log.DEBUG, TAG));
            debugLog(Log.DEBUG, item, last, message, includeStack);
        }
    }
    }


    private void debugLog(int priority, HistoryItem item, HistoryItem last, String message,
            boolean includeStack) {
        debugLog(new PrintWriter(new LogWriter(priority, TAG), /* autoflash */ true),
                item, last, message, includeStack);
    }

    private void debugLog(PrintWriter writer, HistoryItem item, HistoryItem last, String message,
            boolean includeStack) {
        if (mDebugLogBaseTimeUtc == 0) {
        if (mDebugLogBaseTimeUtc == 0) {
            mDebugLogBaseTimeUtc = mClock.currentTimeMillis();
            mDebugLogBaseTimeUtc = mClock.currentTimeMillis();
            mDebugLogBaseMonotonicTime = mMonotonicClock.monotonicTime();
            mDebugLogBaseMonotonicTime = mMonotonicClock.monotonicTime();
@@ -2500,21 +2517,18 @@ public class BatteryStatsHistory {


        long currentTimeSaved = last.currentTime;
        long currentTimeSaved = last.currentTime;
        last.currentTime = mDebugLogBaseTimeUtc + (last.time - mDebugLogBaseMonotonicTime);
        last.currentTime = mDebugLogBaseTimeUtc + (last.time - mDebugLogBaseMonotonicTime);
        historyPrinter.printNextItem(logWriter, last, 0, false, true);
        historyPrinter.printNextItem(writer, last, 0, false, true);
        last.currentTime = currentTimeSaved;
        last.currentTime = currentTimeSaved;


        currentTimeSaved = item.currentTime;
        currentTimeSaved = item.currentTime;
        item.currentTime = mDebugLogBaseTimeUtc + (item.time - mDebugLogBaseMonotonicTime);
        item.currentTime = mDebugLogBaseTimeUtc + (item.time - mDebugLogBaseMonotonicTime);
        historyPrinter.printNextItem(logWriter, item, 0, false, true);
        historyPrinter.printNextItem(writer, item, 0, false, true);
        item.currentTime = currentTimeSaved;
        item.currentTime = currentTimeSaved;


        logWriter.println("    " + message);
        writer.println("    " + message);


        if (includeStack) {
        if (includeStack) {
            new Exception("Stack trace").printStackTrace(logWriter);
            new Exception("Stack trace").printStackTrace(writer);
        }
        }

        logWriter.println();
        logWriter.close();
    }
    }
}
}
+10 −0
Original line number Original line Diff line number Diff line
@@ -54,3 +54,13 @@ flag {
     bug: "403599628"
     bug: "403599628"
     is_fixed_read_only: true
     is_fixed_read_only: true
}
}

flag {
    name: "report_out_of_order_battery_history_events"
    namespace: "backstage_power"
    description: "Generate a Log.wtf when an out-of-order event is written to battery history"
    bug: "407828438"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}