[Android systrace系列] systrace的信息从哪里来

--------------------------------------------------------------

这篇文章的小目标:

1. systrace是怎么抓出来的

2. 这些信息的来源是什么

------------------------------------

 

[Android systrace系列] systrace入门第一式 介绍了用命令行抓取systrace的方法。

打开systrace.py的目录,会发现一大堆python文件,让人以为抓取systrace的流程很复杂。

其实不然,systrace是附属于catapult这个项目(传送门),是一个抓trace的框架,而且不仅仅为Andorid服务。

 

打开/catapult/systrace/systrace/tracing_agents,我们就能发现很多抓trace的agent,这是抓不同类型信息对应的代码逻辑。

抓Android默认用到这三个agent,对应三类信息:

atrace_agent(atrace,负责收集用户空间打印的systrace log和kernel ftrace事件)

android_process_data_agent(ps命令,用来对应进程名与PID)

android_cgroup_agent(cgroup中cpuset子系统,市面销售的机器不一定能抓到)

 

atrace抓取的信息是Android systrace中最核心的部分。

通过atrace_agent.py的函数StartAgentTracing和StopAgentTracing,我们终于知道了systrace的主要部分是通过atrace命令抓取的。

实质上,只抓取atrace就可以生成systrace html文件了。

虽然使用systrace.py是抓取systrace最常规的方法,但如果要在测试脚本加入抓systrace的操作,我认为atrace命令是最直接的方法,也可以节省测试脚本的运行时间。

这也有助于了解systrace的细节,衍生出更多技巧。

 

0. 执行adb shell atrace --list_categories,选择你需要的log类别

1. adb shell atrace --async_start -b 8192 gfx input view webview wm am sm audio video camera hal res dalvik rs bionic power pm ss database network sched freq idle disk sync memreclaim binder_driver binder_lock gfx ion

2. 重现问题,现象出现后,执行 adb shell atrace --async_stop -z > a.out

3. 事后转换成html文件,systrace.py --from-file a.out -o a.html

 

 

现在我们阅读atrace的代码,寻找更多细节。

代码路径://android/frameworks/native/cmds/atrace/atrace.cpp

这个数组,就是之前提到的systrace信息的两大来源,用户空间打印的systrace log和kernel ftrace事件。

用户空间systrace logATRACE_TAG 开头的

kernel ftrace:events开头的

static const TracingCategory k_categories[] = {
    { "gfx",        "Graphics",         ATRACE_TAG_GRAPHICS, {
        { OPT,      "events/mdss/enable" },
        { OPT,      "events/sde/enable" },
    } },
    { "input",      "Input",            ATRACE_TAG_INPUT, { } },
    { "view",       "View System",      ATRACE_TAG_VIEW, { } },
    { "webview",    "WebView",          ATRACE_TAG_WEBVIEW, { } },
    { "wm",         "Window Manager",   ATRACE_TAG_WINDOW_MANAGER, { } },
    { "am",         "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } },
    { "sm",         "Sync Manager",     ATRACE_TAG_SYNC_MANAGER, { } },
    { "audio",      "Audio",            ATRACE_TAG_AUDIO, { } },
    { "video",      "Video",            ATRACE_TAG_VIDEO, { } },
    { "camera",     "Camera",           ATRACE_TAG_CAMERA, { } },
    { "hal",        "Hardware Modules", ATRACE_TAG_HAL, { } },
    { "res",        "Resource Loading", ATRACE_TAG_RESOURCES, { } },
    { "dalvik",     "Dalvik VM",        ATRACE_TAG_DALVIK, { } },
    { "rs",         "RenderScript",     ATRACE_TAG_RS, { } },
    { "bionic",     "Bionic C Library", ATRACE_TAG_BIONIC, { } },
    { "power",      "Power Management", ATRACE_TAG_POWER, { } },
    { "pm",         "Package Manager",  ATRACE_TAG_PACKAGE_MANAGER, { } },
    { "ss",         "System Server",    ATRACE_TAG_SYSTEM_SERVER, { } },
    { "database",   "Database",         ATRACE_TAG_DATABASE, { } },
    { "network",    "Network",          ATRACE_TAG_NETWORK, { } },
    { "adb",        "ADB",              ATRACE_TAG_ADB, { } },
    { "vibrator",   "Vibrator",         ATRACE_TAG_VIBRATOR, { } },
    { "aidl",       "AIDL calls",       ATRACE_TAG_AIDL, { } },
    { k_coreServiceCategory, "Core services", 0, { } },
    { k_pdxServiceCategory, "PDX services", 0, { } },
    { "sched",      "CPU Scheduling",   0, {
        { REQ,      "events/sched/sched_switch/enable" },
        { REQ,      "events/sched/sched_wakeup/enable" },
        { OPT,      "events/sched/sched_waking/enable" },
        { OPT,      "events/sched/sched_blocked_reason/enable" },
        { OPT,      "events/sched/sched_cpu_hotplug/enable" },
        { OPT,      "events/sched/sched_pi_setprio/enable" },
        { OPT,      "events/cgroup/enable" },
    } },
    { "irq",        "IRQ Events",   0, {
        { REQ,      "events/irq/enable" },
        { OPT,      "events/ipi/enable" },
    } },
    { "irqoff",     "IRQ-disabled code section tracing", 0, {
        { REQ,      "events/preemptirq/irq_enable/enable" },
        { REQ,      "events/preemptirq/irq_disable/enable" },
    } },
    { "preemptoff", "Preempt-disabled code section tracing", 0, {
        { REQ,      "events/preemptirq/preempt_enable/enable" },
        { REQ,      "events/preemptirq/preempt_disable/enable" },
    } },
    { "i2c",        "I2C Events",   0, {
        { REQ,      "events/i2c/enable" },
        { REQ,      "events/i2c/i2c_read/enable" },
        { REQ,      "events/i2c/i2c_write/enable" },
        { REQ,      "events/i2c/i2c_result/enable" },
        { REQ,      "events/i2c/i2c_reply/enable" },
        { OPT,      "events/i2c/smbus_read/enable" },
        { OPT,      "events/i2c/smbus_write/enable" },
        { OPT,      "events/i2c/smbus_result/enable" },
        { OPT,      "events/i2c/smbus_reply/enable" },
    } },
    { "freq",       "CPU Frequency",    0, {
        { REQ,      "events/power/cpu_frequency/enable" },
        { OPT,      "events/power/clock_set_rate/enable" },
        { OPT,      "events/power/clock_disable/enable" },
        { OPT,      "events/power/clock_enable/enable" },
        { OPT,      "events/clk/clk_set_rate/enable" },
        { OPT,      "events/clk/clk_disable/enable" },
        { OPT,      "events/clk/clk_enable/enable" },
        { OPT,      "events/power/cpu_frequency_limits/enable" },
    } },
    { "membus",     "Memory Bus Utilization", 0, {
        { REQ,      "events/memory_bus/enable" },
    } },
    { "idle",       "CPU Idle",         0, {
        { REQ,      "events/power/cpu_idle/enable" },
    } },
    { "disk",       "Disk I/O",         0, {
        { OPT,      "events/f2fs/f2fs_sync_file_enter/enable" },
        { OPT,      "events/f2fs/f2fs_sync_file_exit/enable" },
        { OPT,      "events/f2fs/f2fs_write_begin/enable" },
        { OPT,      "events/f2fs/f2fs_write_end/enable" },
        { OPT,      "events/ext4/ext4_da_write_begin/enable" },
        { OPT,      "events/ext4/ext4_da_write_end/enable" },
        { OPT,      "events/ext4/ext4_sync_file_enter/enable" },
        { OPT,      "events/ext4/ext4_sync_file_exit/enable" },
        { REQ,      "events/block/block_rq_issue/enable" },
        { REQ,      "events/block/block_rq_complete/enable" },
    } },
    { "mmc",        "eMMC commands",    0, {
        { REQ,      "events/mmc/enable" },
    } },
    { "load",       "CPU Load",         0, {
        { REQ,      "events/cpufreq_interactive/enable" },
    } },
    { "sync",       "Synchronization",  0, {
        // before linux kernel 4.9
        { OPT,      "events/sync/enable" },
        // starting in linux kernel 4.9
        { OPT,      "events/fence/enable" },
    } },
    { "workq",      "Kernel Workqueues", 0, {
        { REQ,      "events/workqueue/enable" },
    } },
    { "memreclaim", "Kernel Memory Reclaim", 0, {
        { REQ,      "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
        { REQ,      "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
        { REQ,      "events/vmscan/mm_vmscan_kswapd_wake/enable" },
        { REQ,      "events/vmscan/mm_vmscan_kswapd_sleep/enable" },
        { OPT,      "events/lowmemorykiller/enable" },
    } },
    { "regulators",  "Voltage and Current Regulators", 0, {
        { REQ,      "events/regulator/enable" },
    } },
    { "binder_driver", "Binder Kernel driver", 0, {
        { REQ,      "events/binder/binder_transaction/enable" },
        { REQ,      "events/binder/binder_transaction_received/enable" },
        { OPT,      "events/binder/binder_set_priority/enable" },
    } },
    { "binder_lock", "Binder global lock trace", 0, {
        { OPT,      "events/binder/binder_lock/enable" },
        { OPT,      "events/binder/binder_locked/enable" },
        { OPT,      "events/binder/binder_unlock/enable" },
    } },
    { "pagecache",  "Page cache", 0, {
        { REQ,      "events/filemap/enable" },
    } },
};

 

各个ATRACE_TAG_XXX的功能是掩码,在 //android/system/core/include/cutils/trace.h 的函数

static inline void atrace_begin(uint64_t tag, const char* name) 中控制是否写入相应TAG的log。

 

systrace在各层的调用接口,以及最终systrace的log格式如下表:

(看不清楚的可以右击图片,在新标签页打开图片)

 

跟踪上表的代码很容易会发现,用户空间的systrace log,最终通过libcutils的接口,写入到

手机的 /sys/kernel/debug/tracing/trace_marker,就是写入到kernel ftrace的buffer中。

用户空间打印的systrace log 和 kernel ftrace事件在这里胜利会师了。

 

用纯文本打开systrace html 文件,除了上述两类log,开头还有一大段Javascript。

通过Javascript的解析处理,呈现出systrace可视化的分析界面。

 

 

转载请注明出处:https://www.cnblogs.com/zzcperf/p/14052681.html

posted @ 2020-11-29 21:37  zzc1024  阅读(1774)  评论(0编辑  收藏  举报