top数据工具化分析
2020-10-09 本文已影响0人
retton
背景
在做android性能分析时,经常需要做整体的CPU占用性能分析。
本文主要通过awk分析top输出的数据,并结合excel进行图表化输出。
采集数据
可以通过adb shell连接设备,输入top命令采集数据,如:
top -d 3 > /sdcard/top.txt
如果需要采集保存数据的同时也实时查看占用的数据,可以通过如下命令:
top -d 3 | tee /sdcard/top.txt
采集完数据后adb pull取出数据到电脑中。
单条数据格式示例:
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
数据预处理
本文主要是通过awk进行数据的预处理,此处可以考虑使用VB、高级语言等替代实现,此处以AWK进行介绍。
- 创建一个awk脚本,示例内容如下所示:
#!/usr/bin/awk -f
# RS表示Record Seperate,记录分隔符,根据top输出的格式规范,此处已top头部的Tasks信息进行分割
BEGIN { RS="Tasks"; FS="\n"}
{
# cmds为定义的需要处理关心的进程命令列表,可以根据分析的需要进行删减
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)
}
}
# 过滤无效数据
if (foundCpu == 0) {
next
}
INDEX_CPU_PERCENT = 9
for (cmd in cmds) {
# 匹配对应cmd并累计对应的占用求和进行统计,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
- 执行
./perf.awk [FILE]
,可以在gitbash执行或者linux相关的sh环境下执行
示例,执行./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
数据展示
- 数据导入
通过excel导入数据,依次操作:
标题栏数据 -> 自文本 -> 选择对应output.txt并导入 -> 勾选分隔符号并下一步 -> 勾选Tab键和空格并完成 -> 确定
- 图表可视化,
- 选择需要关注的数据列,比如occupy(总占用)以及相关的占用进程
标题栏“插入”图表,选择组合图
-
配置对应数据列的图表类型,并确定
image.png -
生成图表,直观的查看各个样本点各个应用的占用情况
image.png