perfetto 抓取trace指导

1. 准备阶段

1.1 Android 官网下载trace脚本

curl -O https://raw.githubusercontent.com/google/perfetto/master/tools/record_android_trace
 
# 文件目录在这里可以看
https://cs.android.com/android/platform/superproject/+/main:external/perfetto/tools/record_android_trace
 

更多的可以参考 Quickstart: Record traces on Android

1.2 开启开关

如果遇到报错信息,尝试如下解决开启有的⼿机 traced 功 默认是 闭状态的

adb shell setprop persist.traced.enable 1

如果需要抓取详细trace

adb root;
adb shell "echo 0 > sys/kernel/tracing/tracing_on";
adb shell setprop persist.sys.perfdebug.monitor.enable true;
adb shell setprop persist.sys.perfdebug.monitor.catalog all;
adb shell setprop persist.sys.hwui.skia_atrace_enabled true;
adb shell setprop persist.sys.perfdebug.monitor.catalog  all

[帮助⽂档] perfetto.dev/docs/quick… 相关的配置⽂件可在ui.perfetto.dev/#…上⽣成⼀份配置⽂件如下

adb shell perfetto \
  -c - --txt \
  -o /data/misc/perfetto-traces/trace \
<<EOF
 
buffers: {
    size_kb: 63488
    fill_policy: DISCARD
}
buffers: {
    size_kb: 2048
    fill_policy: DISCARD
}
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 {
        }
    }
}
data_sources: {
    config {
        name: "android.surfaceflinger.frametimeline"
    }
}
data_sources: {
    config {
        name: "android.game_interventions"
    }
}
data_sources: {
    config {
        name: "android.network_packets"
        network_packet_trace_config {
            poll_ms: 250
        }
    }
}
data_sources: {
    config {
        name: "android.packages_list"
    }
}
data_sources: {
    config {
        name: "linux.sys_stats"
        sys_stats_config {
            stat_period_ms: 1000
            stat_counters: STAT_CPU_TIMES
            stat_counters: STAT_FORK_COUNT
            cpufreq_period_ms: 1000
        }
    }
}
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: "power/cpu_frequency"
            ftrace_events: "power/cpu_idle"
            ftrace_events: "raw_syscalls/sys_enter"
            ftrace_events: "raw_syscalls/sys_exit"
            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_apps: "*"
        }
    }
}
duration_ms: 10000
 
EOF
 

注意: atrace_apps 这参数需要注意的是,你需要分析那个应用的trace就配置应用的包名,如果配置的是通配符*,那么就会出现所有应用log都抓取的情况,通常会应用IO限制导致抓取的trace不完整而无法分析

1.3 导 出配 件 perfetto.pbtxt,并push到手机指定目录,重启设备生效

 adb push perfetto.pbtxt /data/local/tmp/perfetto.pbtxt

1.4 编写脚本文件perfetto

adb root;
adb shell "echo 0 > sys/kernel/tracing/tracing_on";
adb shell setprop persist.sys.perfdebug.monitor.enable true;
adb shell setprop persist.sys.perfdebug.monitor.catalog all;
adb shell setprop persist.sys.hwui.skia_atrace_enabled true;
adb shell setprop persist.sys.perfdebug.monitor.catalog  all
cat<<EOF>config.pbtx
duration_ms: 40000
flush_period_ms: 30000
incremental_state_config {
    clear_period_ms: 5000
}
 
buffers: {
    size_kb: 126488
    fill_policy: RING_BUFFER
}
buffers: {
    size_kb: 2048
    fill_policy: RING_BUFFER
}
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: "power/cpu_frequency"
            ftrace_events: "power/cpu_idle"
            ftrace_events: "power/gpu_frequency"
            ftrace_events: "gpu_mem/gpu_mem_total"
            ftrace_events: "sched/sched_process_exit"
            ftrace_events: "sched/sched_process_free"
            ftrace_events: "task/task_newtask"
            ftrace_events: "task/task_rename"
            atrace_categories: "am"
            atrace_categories: "aidl"
            atrace_categories: "binder_lock"
            atrace_categories: "binder_driver"
            atrace_categories: "camera"
            atrace_categories: "database"
            atrace_categories: "gfx"
            atrace_categories: "hal"
            atrace_categories: "input"
            atrace_categories: "pm"
            atrace_categories: "rs"
            atrace_categories: "res"
            atrace_categories: "rro"
            atrace_categories: "sm"
            atrace_categories: "ss"
            atrace_categories: "video"
            atrace_categories: "view"
            atrace_categories: "wm"
            atrace_apps: "com.miui.home"
            atrace_apps: "com.mi.globallauncher"
        }
    }
}
 
data_sources: {
    config {
        name: "android.log"
        android_log_config {
        }
    }
}
 
data_sources: {
    config {
        name: "linux.process_stats"
        target_buffer: 1
        process_stats_config {
            scan_all_processes_on_start: true
        }
    }
}
EOF
if [ ! -d "result" ];then
  mkdir result
fi
 
./record_android_trace -c config.pbtx -o ./trace/result/perfetto_$(date +%Y%m%d%H%M%S)
 

目录结构如下

1.5 直接运⾏命令抓取trace

./record_android_trace -c config.pbtx -o ./trace/result/perfetto_$(date +%Y%m%d%H%M%S)

1.6 CTRL + C 停止抓取