Android14如何抓取开机Trace

0 阅读2分钟

方法写在external/perfetto/perfetto.rc中:

#############################################################################
#  perfetto_trace_on_boot - Starts a perfetto trace on boot
#############################################################################
#
# There are two separate actions (a trigger action and a start action) to make
# sure that perfetto_trace_on_boot is started only once on boot (otherwise,
# whenever persist.debug.perfetto.boottrace=1 is set, perfetto_trace_on_boot
# would start immediately).
#
# persist.debug.perfetto.boottrace=1 can be manually set after boot (to record
# a trace on the next reboot) and we don't want to immediately start a trace
# when setting the debug property. So we turn "ro.persistent_properties.ready"
# into a trigger, and then check whether we should start tracing when the
# trigger fires.
on perfetto_maybe_trace_on_boot && property:persist.debug.perfetto.boottrace=1 && property:persist.traced.enable=1
    setprop persist.debug.perfetto.boottrace ""
    rm /data/misc/perfetto-traces/boottrace.perfetto-trace
    # Set by traced after listen()ing on the consumer socket. Without this,
    # perfetto could try to connect to traced before traced is ready to listen.
    wait_for_prop sys.trace.traced_started 1
    start perfetto_trace_on_boot

on property:ro.persistent_properties.ready=true
    trigger perfetto_maybe_trace_on_boot

service perfetto_trace_on_boot /system/bin/perfetto -c /data/misc/perfetto-configs/boottrace.pbtxt --txt -o /data/misc/perfetto-traces/boottrace.perfetto-trace
    disabled
    gentle_kill
    oneshot
    user shell
    group nobody

操作步骤:

  1. 把perfetto配置文件boottrace.pbtxt放到/data/misc/perfetto-configs/boottrace.pbtxt中。
  2. 确保getprop persist.traced.enable返回1。
  3. 执行setprop persist.debug.perfetto.boottrace 1
  4. 重启后,当次开机的trace就在/data/misc/perfetto-traces/boottrace.perfetto-trace

注意事项:

  1. 尽量只抓必要的信息,不然抓取的数据量超过文件大小上限会出现大量log未闭合或目标过程被冲掉的情况。
  2. 抓出来的boottrace.perfetto-trace可以在 ui.perfetto.dev/ 上查看。
  3. boottrace.pbtxt示例:
buffers: {
    size_kb: 260096
    fill_policy: RING_BUFFER
}
buffers: {
    size_kb: 2048
    fill_policy: RING_BUFFER
}
data_sources: {
    config {
        name: "android.packages_list"
        target_buffer: 1
    }
}
data_sources: {
    config {
        name: "linux.process_stats"
        target_buffer: 1
        process_stats_config {
            scan_all_processes_on_start: true
        }
    }
}
data_sources: {
    config {
        name: "android.log"
        android_log_config {
            log_ids: LID_EVENTS
            log_ids: LID_CRASH
            log_ids: LID_KERNEL
            log_ids: LID_DEFAULT
            log_ids: LID_RADIO
            log_ids: LID_SECURITY
            log_ids: LID_STATS
            log_ids: LID_SYSTEM
        }
    }
}
data_sources: {
    config {
        name: "linux.ftrace"
        ftrace_config {
            ftrace_events: "sched/sched_switch"
            ftrace_events: "power/suspend_resume"
            ftrace_events: "sched/sched_wakeup"
            ftrace_events: "sched/sched_wakeup_new"
            ftrace_events: "sched/sched_waking"
            ftrace_events: "sched/sched_process_exit"
            ftrace_events: "sched/sched_process_free"
            ftrace_events: "task/task_newtask"
            ftrace_events: "task/task_rename"
            ftrace_events: "ftrace/print"
            atrace_categories: "am"
            atrace_categories: "adb"
            atrace_categories: "aidl"
            atrace_categories: "dalvik"
            atrace_categories: "audio"
            atrace_categories: "binder_lock"
            atrace_categories: "binder_driver"
            atrace_categories: "bionic"
            atrace_categories: "camera"
            atrace_categories: "database"
            atrace_categories: "gfx"
            atrace_categories: "hal"
            atrace_categories: "input"
            atrace_categories: "network"
            atrace_categories: "nnapi"
            atrace_categories: "pm"
            atrace_categories: "power"
            atrace_categories: "rs"
            atrace_categories: "res"
            atrace_categories: "rro"
            atrace_categories: "sm"
            atrace_categories: "ss"
            atrace_categories: "vibrator"
            atrace_categories: "video"
            atrace_categories: "view"
            atrace_categories: "webview"
            atrace_categories: "wm"
            atrace_apps: "*"
        }
    }
}
duration_ms: 40000
write_into_file: true
file_write_period_ms: 2500
max_file_size_bytes: 500000000
flush_period_ms: 30000
incremental_state_config {
    clear_period_ms: 5000
}