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

Commit 176736ca authored by Mohamad Mahmoud's avatar Mohamad Mahmoud
Browse files

Introduce LongMethodTracer: signal-based method tracing utility

Adds a new utility (LongMethodTracer) to trigger method tracing in native processes via signal, as part of the pre-ANR tracing mechanism.

- Adds LongMethodTracer class in Java, providing APIs to trigger tracing for one or multiple processes.
- Implements JNI bindings for LongMethodTracer, handling signal delivery using sigqueue with packed payloads.
- Introduces a type-tagging convention in sigval to differentiate tracing requests (LONG_METHOD_TRACING_TYPE_ID).

This utility abstracts the signal-based tracing trigger, enabling lightweight integration for pre-ANR diagnostics.

Test: ran multiple scenarios and  verified signal delivery and tracing manually
Bug: 413119509
Flag: com.android.server.utils.long_method_trace
Design Document: go/long-method-tracing-for-anrs

Change-Id: I545e2724cec3db5bed1fc884700ac2338e1ccec9
parent 6f1f1631
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.utils;

import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;

import android.os.Trace;
import android.util.Slog;

import com.android.internal.annotations.Keep;

/**
 * Triggers long method tracing in a process for a fixed duration.
 *
 * <p>This uses a native signal-based mechanism to request tracing in the target process. The actual
 * signal and delivery mechanism are abstracted away.
 *
 * <p>The collected tracing information currently appears in the ANR report if the traced process
 * encounters an ANR during or after the tracing window.
 *
 * @hide
 */
public class LongMethodTracer {
    private static final String TAG = "LongMethodTracer";

    /**
     * Requests method tracing in the given process.
     *
     * @param pid The target process ID to trace.
     * @param durationMs The tracing duration in milliseconds.
     * @return true if the request was successfully sent; false otherwise.
     */
    @Keep
    public static boolean trigger(int pid, int durationMs) {
        if (!Flags.longMethodTrace()) {
            return false;
        }

        if (pid <= 0) {
            throw new IllegalArgumentException("Invalid PID: " + pid);
        }
        if (durationMs <= 0) {
            throw new IllegalArgumentException("Duration must be positive: " + durationMs);
        }

        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER,
                "LongMethodTracer#trigger()");
        Slog.i(TAG, "Triggering long method tracing for pid:" + pid);

        boolean result = nativeTrigger(pid, durationMs);
        if (!result) {
            Slog.w(TAG, "Failed to trigger long method tracing for pid " + pid);
        }
        Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
        return result;
    }

    private static native boolean nativeTrigger(int pid, int durationMs);

}
+3 −0
Original line number Diff line number Diff line
@@ -16,3 +16,6 @@ per-file LazyJniRegistrar.java = file:/PERFORMANCE_OWNERS
per-file AnrTimer*.java = file:/PERFORMANCE_OWNERS

per-file flags.aconfig = file:/PERFORMANCE_OWNERS

# Long Method Tracing util
per-file LongMethodTracer.java = benmiles@google.com, ilkos@google.com, mohamadmahmoud@google.com
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
@@ -15,3 +15,11 @@ flag {
     description: "Use AnrTimer to signal ANRs in JobScheduler"
     bug: "408440679"
}

flag {
     name: "long_method_trace"
     namespace: "stability"
     is_fixed_read_only: true
     description: "Enable long method tracing when true"
     bug: "419753987"
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ cc_library_static {
        "com_android_server_am_LowMemDetector.cpp",
        "com_android_server_pm_PackageManagerShellCommandDataLoader.cpp",
        "com_android_server_sensor_SensorService.cpp",
        "com_android_server_utils_LongMethodTracer.cpp",
        "com_android_server_wm_TaskFpsCallbackController.cpp",
        "onload.cpp",
        ":lib_cachedAppOptimizer_native",
+3 −0
Original line number Diff line number Diff line
@@ -41,3 +41,6 @@ per-file *AnrTimer* = file:/PERFORMANCE_OWNERS

# HintManagerService
per-file com_android_server_hint_HintManagerService.cpp = file:/ADPF_OWNERS

# Long Method Tracer
per-file com_android_server_utils_LongMethodTracer.cpp = benmiles@google.com, ilkos@google.com, mohamadmahmoud@google.com
 No newline at end of file
Loading