diff --git a/services/core/java/com/android/server/am/LmkdStatsReporter.java b/services/core/java/com/android/server/am/LmkdStatsReporter.java index 91588913c33c6..4380b42ee54c5 100644 --- a/services/core/java/com/android/server/am/LmkdStatsReporter.java +++ b/services/core/java/com/android/server/am/LmkdStatsReporter.java @@ -50,7 +50,8 @@ public final class LmkdStatsReporter { * Logs the event when LMKD kills a process to reduce memory pressure. * Code: LMK_KILL_OCCURRED = 51 */ - public static void logKillOccurred(DataInputStream inputData) { + public static void logKillOccurred(DataInputStream inputData, int totalForegroundServices, + int procsWithForegroundServices) { try { final long pgFault = inputData.readLong(); final long pgMajFault = inputData.readLong(); @@ -67,11 +68,10 @@ public final class LmkdStatsReporter { final int thrashing = inputData.readInt(); final int maxThrashing = inputData.readInt(); final String procName = inputData.readUTF(); - FrameworkStatsLog.write(FrameworkStatsLog.LMK_KILL_OCCURRED, uid, procName, oomScore, pgFault, pgMajFault, rssInBytes, cacheInBytes, swapInBytes, processStartTimeNS, minOomScore, freeMemKb, freeSwapKb, mapKillReason(killReason), thrashing, - maxThrashing); + maxThrashing, totalForegroundServices, procsWithForegroundServices); } catch (IOException e) { Slog.e(TAG, "Invalid buffer data. Failed to log LMK_KILL_OCCURRED"); return; diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java index 42792bf64f443..ccbca76d18683 100644 --- a/services/core/java/com/android/server/am/ProcessList.java +++ b/services/core/java/com/android/server/am/ProcessList.java @@ -814,7 +814,12 @@ public final class ProcessList { < LmkdStatsReporter.KILL_OCCURRED_MSG_SIZE) { return false; } - LmkdStatsReporter.logKillOccurred(inputData); + Pair temp = getNumForegroundServices(); + final int totalForegroundServices = temp.first; + final int procsWithForegroundServices = temp.second; + LmkdStatsReporter.logKillOccurred(inputData, + totalForegroundServices, + procsWithForegroundServices); return true; case LMK_STATE_CHANGED: if (receivedLen @@ -5123,6 +5128,26 @@ public final class ProcessList { } } + /** + * Get the number of foreground services in all processes and number of processes that have + * foreground service within. + */ + Pair getNumForegroundServices() { + int numForegroundServices = 0; + int procs = 0; + synchronized (mService) { + for (int i = 0, size = mLruProcesses.size(); i < size; i++) { + ProcessRecord pr = mLruProcesses.get(i); + int numFgs = pr.mServices.getNumForegroundServices(); + if (numFgs > 0) { + numForegroundServices += numFgs; + procs++; + } + } + } + return new Pair<>(numForegroundServices, procs); + } + private final class ImperceptibleKillRunner extends IUidObserver.Stub { private static final String EXTRA_PID = "pid"; private static final String EXTRA_UID = "uid"; diff --git a/services/core/java/com/android/server/am/ProcessServiceRecord.java b/services/core/java/com/android/server/am/ProcessServiceRecord.java index 9951e983a752a..67eb675503ad3 100644 --- a/services/core/java/com/android/server/am/ProcessServiceRecord.java +++ b/services/core/java/com/android/server/am/ProcessServiceRecord.java @@ -180,6 +180,16 @@ final class ProcessServiceRecord { mRepFgServiceTypes = foregroundServiceTypes; } + int getNumForegroundServices() { + int count = 0; + for (int i = 0, serviceCount = mServices.size(); i < serviceCount; i++) { + if (mServices.valueAt(i).isForeground) { + count++; + } + } + return count; + } + void updateHasTopStartedAlmostPerceptibleServices() { mHasTopStartedAlmostPerceptibleServices = false; mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = 0;