top數(shù)據(jù)工具化分析

背景

在做android性能分析時,經(jīng)常需要做整體的CPU占用性能分析。

本文主要通過awk分析top輸出的數(shù)據(jù),并結(jié)合excel進行圖表化輸出。

采集數(shù)據(jù)

可以通過adb shell連接設(shè)備,輸入top命令采集數(shù)據(jù),如:

top -d 3 > /sdcard/top.txt

如果需要采集保存數(shù)據(jù)的同時也實時查看占用的數(shù)據(jù),可以通過如下命令:

top -d 3 | tee /sdcard/top.txt

采集完數(shù)據(jù)后adb pull取出數(shù)據(jù)到電腦中。
單條數(shù)據(jù)格式示例:

Mem:   3659388k total,  2307824k used,  1351564k free,    22632k buffers
Swap:        0k total,        0k used,        0k free,   512440k cached
400%cpu 102%user   0%nice  29%sys 269%idle   0%iow   0%irq   0%sirq   0%host
  PID USER         PR  NI VIRT  RES  SHR S[%CPU] %MEM     TIME+ ARGS
 3885 system       20   0 2.1G 387M 106M S 35.6  10.8 233:52.04 speechclient.hmi
 1980 system       -2  -8 219M  36M  10M S 33.3   1.0 185:32.90 surfaceflinger
 4247 u0_a59       12  -8 1.6G 136M  87M S 24.3   3.7 149:07.13 pixieservice
 3946 u0_a82       10 -10 1.7G 373M  83M S 20.6  10.4 118:22.19 map
 4329 u0_a95       20   0 1.5G 110M  72M S  3.6   3.0  14:24.48 systemserver
 2121 system       -3 -10  72M 6.0M 4.1M S  3.6   0.1  21:17.53 android.hardware.graphics.composer@2.1-service
 2332 system       18  -2 2.4G 203M 139M S  2.6   5.6  20:42.89 system_server
 2103 audioserver  20   0  30M 2.7M 1.2M S  2.6   0.0  16:13.97 android.hardware.audio@2.0-service
18092 root         20   0  11M 4.0M 3.2M R  2.3   0.1   0:00.13 top -d 3
 3433 u0_a23       20   0 1.7G 172M 100M S  2.3   4.8  10:45.18 com.android.systemui
 2129 audioserver  20   0  87M 7.5M 2.9M S  2.3   0.2  18:16.44 audioserver
 1202 root         20   0    0    0    0 S  1.6   0.0   9:43.34 [galcore deamon ]
 2869 radio        20   0 1.5G  96M  81M S  1.0   2.6   1:57.21 com.android.phone
 2778 root         18  -2    0    0    0 S  0.6   0.0   4:39.71 [scheduler_threa]
 2083 root         20   0 142M  55M  10M S  0.6   1.5   4:31.19 evs_app
18045 root         20   0    0    0    0 D  0.3   0.0   0:00.61 [kworker/u8:2]
17990 root         20   0    0    0    0 D  0.3   0.0   0:01.35 [kworker/u8:1]
17289 root         20   0    0    0    0 I  0.3   0.0   0:04.30 [kworker/u8:5]
 4639 u0_a56       12  -8 1.5G  98M  82M S  0.3   2.7   0:14.97 naviselect
 2592 root         20   0  25M 1.3M 888K S  0.3   0.0   0:29.83 adbd --root_seclabel=u:r:su:s0
 2128 wifi         20   0  23M 8.4M 4.1M S  0.3   0.2   1:50.94 android.hardware.wifi@1.0-service
 2119 root         20   0  18M 4.3M 3.4M S  0.3   0.1   2:22.68 android.hardware.gnss@1.0-service-ubx

數(shù)據(jù)預(yù)處理

本文主要是通過awk進行數(shù)據(jù)的預(yù)處理,此處可以考慮使用VB、高級語言等替代實現(xiàn),此處以AWK進行介紹。

  • 創(chuàng)建一個awk腳本,示例內(nèi)容如下所示:
#!/usr/bin/awk -f
# RS表示Record Seperate,記錄分隔符,根據(jù)top輸出的格式規(guī)范,此處已top頭部的Tasks信息進行分割
BEGIN { RS="Tasks"; FS="\n"} 
{
    # cmds為定義的需要處理關(guān)心的進程命令列表,可以根據(jù)分析的需要進行刪減
    cmds["pixie"] = "autofly.pixieservice"
    cmds["surfacef"] = "surfaceflinger"
    cmds["vr"] = "client.hmi"
    cmds["bl"] = "autofly.map"
    cmds["sysS"] = "system_server"
    cmds["audioS"] = "audioserver"
    cmds["audioHal"] = "android.hardware.audio@2.0-service"
    cmds["gnssHal"] = "android.hardware.gnss@1.0-service-ubx"
    cmds["media"] = "autofly.mediax"
    cmds["ota"] = "autofly.downloads"
    cmds["aiot"] = "autofly.aiot"
    cmds["xS"] = "autofly.systemserver"
    cmds["noticec"] = "autofly.noticecenter"
    cmds["launch"] = "autofly.launcher"
    cmds["notify"] = "autofly.notification"
    cmds["account"] = "autofly.accountcenter"
    cmds["weexjsb"] = "libweexjsb.so"
    cmds["setting"] = "autofly.setting"
    cmds["miniprog"] = "autofly.miniprogram"
    cmds["platform"] = "autofly.platformservice"
    cmds["media.c"] = "media.codec hw/android.hardware.media.omx@1.0-service"
    cmds["kworker"]="kworker"
    cmds["sysui"]="com.android.systemui"
    cmds["composer"]="android.hardware.graphics.composer@2.1-service"

    if (NR == 1) {
        # 輸出標題行
        printf("total user nice sys idle iow irq sirq host occupy ")
        for (c in cmds) {
            printf("%s ", c)
        }
        printf("\r\n")
    }

    foundCpu = 0
    for (i = 1; i <= NF; ++i) {
        ret = match($i, "[0-9]+%cpu") 
        if (ret > 0) {
            foundCpu = 1
            split($i, array, " ")
            # array element as 400%cpu 112%user   4%nice  33%sys 249%idle   0%iow   0%irq   1%sirq   0%host
            total = 0
            idle = 0
            for (str in array) {
                split(array[str], percent, "%")
                if (percent[2] == "cpu") {
                    total = percent[1]
                } else if (percent[2] == "idle") {
                    idle = percent[1]
                }
                printf("%5.1f ", percent[1]) 
            }

            #計算總占用
            printf("%5.1f ", total - idle)
        }
    }

    # 過濾無效數(shù)據(jù)
    if (foundCpu == 0) {
        next
    }

    INDEX_CPU_PERCENT = 9
    for (cmd in cmds) {
        # 匹配對應(yīng)cmd并累計對應(yīng)的占用求和進行統(tǒng)計,cmd可以為正則表達式
        occupy = 0.0f
        for (i = 1; i <= NF; ++i) {
            ret = match($i, cmds[cmd])
            if (ret > 0) {
                split($i, array, " ")
                occupy += array[INDEX_CPU_PERCENT]
            }
        }
        printf("%5.1f ", occupy)
    }
    printf("\r\n")
}

腳本保存為perf.awk

  • 執(zhí)行./perf.awk [FILE],可以在gitbash執(zhí)行或者linux相關(guān)的sh環(huán)境下執(zhí)行
    示例,執(zhí)行./perf.awk top_4.txt(也可以通過重定向保存到文件中./perf.awk top_4.txt > output.txt)輸出如下:
total user nice sys idle iow irq sirq host occupy sysui aiot surfacef media.c ota notify setting audioHal launch media audioS composer platform miniprog noticec xS weexjsb gnssHal kworker account bl vr sysS pixie
400.0  94.0   0.0  26.0 281.0   0.0   0.0   0.0   0.0 119.0   0.0   0.0  12.9   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   3.2   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   9.6  41.9   3.2  22.5
400.0 112.0   4.0  33.0 249.0   0.0   0.0   1.0   0.0 151.0   0.6   0.0  24.3   0.0   0.0   0.0  19.3   3.0   0.0   0.0   5.3   3.0   0.3   0.3   0.0   6.3   0.0   0.3   2.2   0.0  12.3  40.0   9.0  23.6
400.0 227.0  51.0  67.0  55.0   0.0   0.0   1.0   0.0 345.0   9.0   0.0  36.0   0.0   0.0   0.0   1.0   2.6   2.3 146.0   4.9   4.6   0.0   0.0   0.0   5.6   0.0   0.3   1.8   0.0  14.3  40.3  31.0  25.6
400.0 149.0  31.0  50.0 170.0   0.0   0.0   0.0   0.0 230.0   7.3   0.0  31.3   0.0   0.0   0.3   0.3   2.3   9.3   0.0   4.6   4.3   0.6   0.0   0.0   1.6   0.0   0.6   2.7   0.0  12.6  49.6  43.0  23.0
400.0 190.0  15.0  96.0  97.0   1.0   0.0   1.0   0.0 303.0  15.3   0.0  55.0   0.0   0.0   0.0   0.0   3.0  14.3   0.0   6.0   7.3   0.0   0.0   0.0   7.0   0.0   0.6   3.3   0.0  40.6  45.6  40.3  26.0
400.0 155.0   2.0  88.0 151.0   3.0   0.0   1.0   0.0 249.0   5.0   0.0  37.0   0.0   0.0   0.0   0.0   2.6   0.0   0.0   4.9   4.0   0.0   0.0   0.0   0.3   0.0   0.0   5.8   0.0  49.3  48.6  20.0  24.6
400.0 158.0   2.0  51.0 185.0   2.0   0.0   2.0   0.0 215.0   1.0   0.0  41.6   0.0   0.0   0.0   0.0   2.6   0.3   0.0   4.9   4.3   0.0   0.0   0.0   5.6   0.0   0.6   3.6   0.0  47.3  47.3  21.0  26.6
400.0 145.0  11.0  46.0 198.0   0.0   0.0   0.0   0.0 202.0   2.6   0.0  42.6   0.0   0.0   0.3   0.0   2.6   0.0   0.3   4.9   5.6   0.0   0.0   0.0   2.6   0.0   0.6   1.8   0.0  45.3  44.0  18.0  26.3
400.0 135.0   5.0  38.0 222.0   0.0   0.0   1.0   0.0 178.0   0.6   0.0  40.0   0.0   0.0   0.0   0.0   2.6   0.0   0.0   4.9   4.0   0.0   0.3   0.0   6.6   0.0   0.0   1.8   0.0  41.0  40.0  11.6  26.3
400.0 184.0   1.0  54.0 158.0   1.0   0.0   2.0   0.0 242.0   5.6   0.0  54.3   0.0   0.0   0.0   0.0   3.0   6.0   0.0   5.6   7.3   0.0   0.0   0.0   5.6   0.0   1.0   3.1   0.0  42.3  45.6  24.6  28.0
400.0 164.0  21.0  59.0 154.0   2.0   0.0   1.0   0.0 246.0  10.0   0.0  39.0   0.0   0.0   0.0   0.0   2.3   0.0   0.0   4.6   4.3   0.3   0.0   0.0   0.3   0.0   0.3   3.6   7.6  15.3  38.3  41.3  23.3
400.0 120.0   5.0  38.0 235.0   1.0   0.0   1.0   0.0 165.0   2.3   0.0  29.6   0.0   0.0   0.3   0.0   2.6   0.0   0.0   4.6   3.0   0.0   0.0   0.0   7.0   0.0   0.3   2.7  19.3  11.0  39.0  20.0  24.0
400.0 124.0   5.0  33.0 237.0   0.0   0.0   0.0   0.0 163.0   7.6   0.0  29.0   0.0   0.0   0.0   0.0   2.3   0.3   0.0   4.6   3.0   0.0   0.0   0.0   2.3   0.0   0.3   1.5  17.6  12.3  35.0  25.3  22.6
400.0 123.0   8.0  39.0 230.0   0.0   0.0   0.0   0.0 170.0   5.0   0.0  25.6   0.0   0.0   0.0   0.0   2.0   0.0   0.0   4.3   2.6   0.0   0.0   0.0   3.6   0.0   0.6   1.8  15.0  12.0  45.6  26.6  22.6
400.0 134.0   7.0  37.0 220.0   1.0   0.0   1.0   0.0 180.0   4.6   0.0  25.6   0.0   0.0   0.0   0.0   2.6   0.0   0.6   4.9   2.3   0.0   0.0   0.0   5.6   0.0   0.3   2.4  28.3  11.6  45.6  21.3  23.0
400.0 107.0  10.0  36.0 245.0   1.0   0.0   1.0   0.0 155.0   6.3   0.0  32.0   0.0   0.0   0.0   0.0   2.0   0.0   0.0   4.0   3.6   0.0   0.0   0.0   0.6   0.0   0.3   1.8  17.3  12.6  35.0  19.0  23.0
400.0 104.0   5.0  31.0 261.0   0.0   0.0   0.0   0.0 139.0   0.3   0.0  19.0   0.0   0.0   0.0   0.0   2.3   0.0   0.0   4.3   2.3   0.0   0.0   0.0   6.0   0.0   0.3   1.5   6.0  14.6  43.3  19.0  22.6
400.0  80.0   1.0  22.0 296.0   0.0   0.0   0.0   0.0 104.0   2.0   0.0  19.6   0.0   0.0   0.0   0.0   2.0   0.0   0.0   3.6   2.0   0.0   0.0   0.0   2.6   0.0   0.3   0.9   2.3  12.6  31.3   4.0  22.3
400.0  78.0   2.0  20.0 300.0   0.0   0.0   0.0   0.0 100.0   0.0   0.0  18.0   0.0   0.0   0.0   0.0   2.3   0.0   0.0   4.6   2.3   0.0   0.0   0.0   2.0   0.0   0.3   1.5   5.0  14.3  29.0   5.6  23.0
400.0  85.0   7.0  18.0 290.0   0.0   0.0   0.0   0.0 110.0   3.3   0.0  21.0   0.0   0.0   0.0   0.0   2.0   0.0   0.0   4.0   2.0   0.0   0.0   0.0   6.0   0.0   0.3   0.3   0.0  12.0  42.3   2.3  22.6
400.0  73.0   0.0  20.0 307.0   0.0   0.0   0.0   0.0  93.0   2.0   0.0  18.6   0.0   0.0   0.0   0.0   2.6   0.0   0.0   4.6   2.3   0.0   0.0   0.0   0.0   0.0   0.0   0.9   0.0  11.6  35.6   1.6  22.0
400.0  74.0   0.0  18.0 307.0   0.0   0.0   0.0   0.0  93.0   0.0   0.0  16.0   0.0   0.0   0.3   0.0   2.3   0.0   0.0   4.3   2.0   0.0   0.3   0.0   4.0   0.0   0.6   0.6   0.0  12.0  35.3   2.0  21.3

數(shù)據(jù)展示

  • 數(shù)據(jù)導(dǎo)入
    通過excel導(dǎo)入數(shù)據(jù),依次操作:
    標題欄數(shù)據(jù) -> 自文本 -> 選擇對應(yīng)output.txt并導(dǎo)入 -> 勾選分隔符號并下一步 -> 勾選Tab鍵和空格并完成 -> 確定
  • 圖表可視化,
    • 選擇需要關(guān)注的數(shù)據(jù)列,比如occupy(總占用)以及相關(guān)的占用進程
    • 標題欄“插入”圖表,選擇組合圖
    • 配置對應(yīng)數(shù)據(jù)列的圖表類型,并確定


      image.png
    • 生成圖表,直觀的查看各個樣本點各個應(yīng)用的占用情況


      image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容