一:抓取(最好自己写1个python脚本):
adb shell setprop persist.traced.enable 1
cat /data/local/tmp/config.pbtx | perfetto -c - --txt -o /data/misc/perfetto-traces/trace.perfetto-trace//建议
adb shell perfetto -o /sdcard/trace_file.perfetto-trace -t 10s -a com.example.myapplication sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory //不建议
还可以adb shell进取后用atrace。//不建议
直接在Perfetto UI也可以直接抓取。//建议
adb pull /data/misc/perfetto-traces/trace.perfetto-trace
config.pbtx 可以在Perfetto UI生成。
pull出的trace.perfetto-trace拖到Perfetto UI查看即可。
二:埋点
import android.os.Trace;
Trace.beginSection("mytrace");
//需要监控的代码段
Trace.endSection();//成对出现
config.pbtx:模板
buffers: {
size_kb: 707200
fill_policy: RING_BUFFER
}
buffers: {
size_kb: 707200
fill_policy: RING_BUFFER
}
data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
proc_stats_poll_ms: 1000
}
}
}
data_sources: {
config {
name: "android.log"
android_log_config {
}
}
}
data_sources: {
config {
name: "android.surfaceflinger.frametimeline"
}
}
data_sources: {
config {
name: "linux.sys_stats"
sys_stats_config {
meminfo_period_ms: 1000
vmstat_period_ms: 1000
stat_period_ms: 1000
stat_counters: STAT_CPU_TIMES
stat_counters: STAT_FORK_COUNT
}
}
}
data_sources: {
config {
name: "android.heapprofd"
target_buffer: 0
heapprofd_config {
sampling_interval_bytes: 4096
shmem_size_bytes: 8388608
block_client: true
}
}
}
data_sources: {
config {
name: "android.java_hprof"
target_buffer: 0
java_hprof_config {
}
}
}
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: "sched/sched_process_exit"
ftrace_events: "sched/sched_process_free"
ftrace_events: "task/task_newtask"
ftrace_events: "task/task_rename"
ftrace_events: "lowmemorykiller/lowmemory_kill"
ftrace_events: "oom/oom_score_adj_update"
ftrace_events: "ftrace/print"
ftrace_events: "binder/*"
atrace_categories: "input"
atrace_categories: "gfx"
atrace_categories: "view"
atrace_categories: "webview"
atrace_categories: "camera"
atrace_categories: "dalvik"
atrace_categories: "power"
atrace_categories: "wm"
atrace_categories: "am"
atrace_categories: "ss"
atrace_categories: "sched"
atrace_categories: "freq"
atrace_categories: "binder_driver"
atrace_categories: "aidl"
atrace_categories: "binder_lock"
atrace_categories: "hal"
atrace_apps: "*"
}
}
}
duration_ms: 20000
flush_period_ms: 20000
incremental_state_config {
clear_period_ms: 5000
}
write_into_file: true
或者
buffers: {
size_kb: 63488
fill_policy: DISCARD
}
buffers: {
size_kb: 2048
fill_policy: DISCARD
}
data_sources: {
config {
name: "android.gpu.memory"
}
}
data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
proc_stats_poll_ms: 1000
}
}
}
data_sources: {
config {
name: "android.log"
android_log_config {
}
}
}
data_sources: {
config {
name: "android.surfaceflinger.frametimeline"
}
}
data_sources: {
config {
name: "linux.sys_stats"
sys_stats_config {
meminfo_period_ms: 1000
vmstat_period_ms: 1000
stat_period_ms: 1000
stat_counters: STAT_CPU_TIMES
stat_counters: STAT_FORK_COUNT
}
}
}
data_sources: {
config {
name: "android.java_hprof"
target_buffer: 0
java_hprof_config {
}
}
}
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: "gpu_mem/gpu_mem_total"
ftrace_events: "raw_syscalls/sys_enter"
ftrace_events: "raw_syscalls/sys_exit"
ftrace_events: "regulator/regulator_set_voltage"
ftrace_events: "regulator/regulator_set_voltage_complete"
ftrace_events: "power/clock_enable"
ftrace_events: "power/clock_disable"
ftrace_events: "power/clock_set_rate"
ftrace_events: "mm_event/mm_event_record"
ftrace_events: "kmem/rss_stat"
ftrace_events: "ion/ion_stat"
ftrace_events: "dmabuf_heap/dma_heap_stat"
ftrace_events: "kmem/ion_heap_grow"
ftrace_events: "kmem/ion_heap_shrink"
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