[Android systrace系列] systrace的信息從哪裏來

--------------------------------------------------------------html

這篇文章的小目標:python

1. systrace是怎麼抓出來的linux

2. 這些信息的來源是什麼android

------------------------------------git

 

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

打開systrace.py的目錄,會發現一大堆python文件,讓人覺得抓取systrace的流程很複雜。web

其實否則,systrace是附屬於catapult這個項目(傳送門),是一個抓trace的框架,並且不單單爲Andorid服務。shell

 

打開/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

相關文章
相關標籤/搜索