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

Commit 8347354e authored by Ng Zhi An's avatar Ng Zhi An
Browse files

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
parent 3aef2cd1
Loading
Loading
Loading
Loading
+17 −20
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import android.util.SparseIntArray;
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 @@ class ActivityMetricsLogger {
    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 @@ class ActivityMetricsLogger {
                    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 @@ class ActivityMetricsLogger {

    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 @@ class ActivityMetricsLogger {
            bindApplicationDelayMs = info.bindApplicationDelayMs;
            windowsDrawnDelayMs = info.windowsDrawnDelayMs;
            type = getTransitionType(info);
            processRecord = findProcessForActivity(info.launchedActivity);
            processName = info.launchedActivity.processName;
        }
    }

@@ -505,15 +501,16 @@ class ActivityMetricsLogger {
            // 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 @@ class ActivityMetricsLogger {
                launchToken,
                packageOptimizationInfo.getCompilationReason(),
                packageOptimizationInfo.getCompilationFilter());
        logAppStartMemoryStateCapture(info);
    }

    private int convertAppStartTransitionType(int tronType) {
@@ -629,15 +627,14 @@ class ActivityMetricsLogger {
        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 @@ class ActivityMetricsLogger {
        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,