Log app start memory state in background

Bug: 73379331
Test: refactoring, unit tests pass
adb logcat -b events | grep "319,"
adb logcat -b stats | grep "55,"

Change-Id: Id959b1b6ce547b9155c72e6734a32b54a2d3a64a
diff --git a/services/core/java/com/android/server/am/ActivityMetricsLogger.java b/services/core/java/com/android/server/am/ActivityMetricsLogger.java
index 978e344..5d5ed55 100644
--- a/services/core/java/com/android/server/am/ActivityMetricsLogger.java
+++ b/services/core/java/com/android/server/am/ActivityMetricsLogger.java
@@ -51,6 +51,7 @@
 import android.util.StatsLog;
 
 import com.android.internal.logging.MetricsLogger;
+import com.android.internal.os.BackgroundThread;
 import com.android.internal.os.SomeArgs;
 import com.android.server.LocalServices;
 
@@ -74,8 +75,6 @@
     private static final long INVALID_START_TIME = -1;
 
     private static final int MSG_CHECK_VISIBILITY = 0;
-    private static final int MSG_LOG_APP_TRANSITION = 1;
-    private static final int MSG_LOG_APP_START_MEMORY_STATE_CAPTURE = 2;
 
     // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
     // time we log.
@@ -116,13 +115,6 @@
                     final SomeArgs args = (SomeArgs) msg.obj;
                     checkVisibility((TaskRecord) args.arg1, (ActivityRecord) args.arg2);
                     break;
-                case MSG_LOG_APP_TRANSITION:
-                    logAppTransition(msg.arg1, msg.arg2,
-                            (WindowingModeTransitionInfoSnapshot) msg.obj);
-                    break;
-                case MSG_LOG_APP_START_MEMORY_STATE_CAPTURE:
-                    logAppStartMemoryStateCapture((WindowingModeTransitionInfo) msg.obj);
-                    break;
             }
         }
     }
@@ -141,11 +133,13 @@
 
     private final class WindowingModeTransitionInfoSnapshot {
         final private ApplicationInfo applicationInfo;
+        final private ProcessRecord processRecord;
         final private String packageName;
         final private String launchedActivityName;
         final private String launchedActivityLaunchedFromPackage;
         final private String launchedActivityLaunchToken;
         final private String launchedActivityAppRecordRequiredAbi;
+        final private String processName;
         final private int reason;
         final private int startingWindowDelayMs;
         final private int bindApplicationDelayMs;
@@ -166,6 +160,8 @@
             bindApplicationDelayMs = info.bindApplicationDelayMs;
             windowsDrawnDelayMs = info.windowsDrawnDelayMs;
             type = getTransitionType(info);
+            processRecord = findProcessForActivity(info.launchedActivity);
+            processName = info.launchedActivity.processName;
         }
     }
 
@@ -505,15 +501,16 @@
             // This will avoid any races with other operations that modify the ActivityRecord.
             final WindowingModeTransitionInfoSnapshot infoSnapshot =
                     new WindowingModeTransitionInfoSnapshot(info);
-            mHandler.obtainMessage(MSG_LOG_APP_TRANSITION, mCurrentTransitionDeviceUptime,
-                    mCurrentTransitionDelayMs, infoSnapshot).sendToTarget();
+            final int currentTransitionDeviceUptime = mCurrentTransitionDeviceUptime;
+            final int currentTransitionDelayMs = mCurrentTransitionDelayMs;
+            BackgroundThread.getHandler().post(() -> logAppTransition(
+                    currentTransitionDeviceUptime, currentTransitionDelayMs, infoSnapshot));
 
             info.launchedActivity.info.launchToken = null;
-            mHandler.obtainMessage(MSG_LOG_APP_START_MEMORY_STATE_CAPTURE, info).sendToTarget();
         }
     }
 
-    // This gets called on the handler without holding the activity manager lock.
+    // This gets called on a background thread without holding the activity manager lock.
     private void logAppTransition(int currentTransitionDeviceUptime, int currentTransitionDelayMs,
             WindowingModeTransitionInfoSnapshot info) {
         final LogMaker builder = new LogMaker(APP_TRANSITION);
@@ -572,6 +569,7 @@
                 launchToken,
                 packageOptimizationInfo.getCompilationReason(),
                 packageOptimizationInfo.getCompilationFilter());
+        logAppStartMemoryStateCapture(info);
     }
 
     private int convertAppStartTransitionType(int tronType) {
@@ -629,15 +627,14 @@
         return -1;
     }
 
-    private void logAppStartMemoryStateCapture(WindowingModeTransitionInfo info) {
-        final ProcessRecord processRecord = findProcessForActivity(info.launchedActivity);
-        if (processRecord == null) {
+    private void logAppStartMemoryStateCapture(WindowingModeTransitionInfoSnapshot info) {
+        if (info.processRecord == null) {
             if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture processRecord null");
             return;
         }
 
-        final int pid = processRecord.pid;
-        final int uid = info.launchedActivity.appInfo.uid;
+        final int pid = info.processRecord.pid;
+        final int uid = info.applicationInfo.uid;
         final MemoryStat memoryStat = readMemoryStatFromMemcg(uid, pid);
         if (memoryStat == null) {
             if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture memoryStat null");
@@ -647,8 +644,8 @@
         StatsLog.write(
                 StatsLog.APP_START_MEMORY_STATE_CAPTURED,
                 uid,
-                info.launchedActivity.processName,
-                info.launchedActivity.info.name,
+                info.processName,
+                info.launchedActivityName,
                 memoryStat.pgfault,
                 memoryStat.pgmajfault,
                 memoryStat.rssInBytes,