抓取进程和系统cpu loading信息,debug相关

本文介绍如何利用top命令的参数查看系统CPU负载,并通过ftrace收集数据。进一步,通过脚本(如Perl)解析trace数据,转换为波形图或HTML文件以进行可视化分析。
1、通过top命令抓取系统的cpu loading情况

1)查看一下top命令支持的参数

C:\Users\hxiong>adb root

C:\Users\hxiong>adb shell
generic_x86_64:/ # top -h
Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [
-t ] [ -h ]
    -m num  Maximum number of processes to display.
    -n num  Updates to show before exiting.
    -d num  Seconds to wait between updates.
    -s col  Column to sort by (cpu,vss,rss,thr).
    -H      Show threads instead of processes.
    -h      Display this help screen.
generic_x86_64:/ #

2)通过top命令显示前10个进程的cpu loading 情况,每隔5秒更新一次信息

generic_x86_64:/ # top -m 10 -d 5
User 11%, System 11%, IOW 0%, IRQ 0%
User 1 + Nice 0 + Sys 1 + Idle 7 + IOW 0 + IRQ 0 + SIRQ 0 = 9

  PID USER     PR  NI CPU% S  #THR     VSS     RSS PCY Name
 3305 root     20   0  11% R     1   9868K   2040K  fg top
    2 root     20   0   0% S     1      0K      0K  fg kthreadd
    3 root     20   0   0% S     1      0K      0K  fg ksoftirqd/0
    5 root      0 -20   0% S     1      0K      0K  fg kworker/0:0H
    6 root     20   0   0% S     1      0K      0K  fg kworker/u8:0
    7 root     RT   0   0% S     1      0K      0K  fg migration/0
    8 root     20   0   0% S     1      0K      0K  fg rcu_preempt
    9 root     20   0   0% S     1      0K      0K  fg rcu_bh
   10 root     20   0   0% S     1      0K      0K  fg rcu_sched
   11 root     RT   0   0% S     1      0K      0K  fg migration/1
^C
130|generic_x86_64:/ #

也可以写成如下脚步,将信息保存到文件
cpuloadinginfo.bat

@echo off
adb root
adb wait-for-device

set cpuInfoDir=%cd%\cpuInfoLog

rem make dir
if exist %cpuInfoDir% rd /s/q %cpuInfoDir%
md %cpuInfoDir%

rem the file which saved cpu loading info
set cpuInfoFile=%cpuInfoDir%\cpuloading.txt

echo %cpuInfoFile%

adb shell top -m 10 -d 5 > %cpuInfoFile%

pause

3)通过top命令显示前20个线程的cpu loading 情况,每隔5秒更新一次信息

130|generic_x86_64:/ # top -m 20 -d 5 -t
User 0%, System 60%, IOW 0%, IRQ 0%
User 0 + Nice 0 + Sys 3 + Idle 2 + IOW 0 + IRQ 0 + SIRQ 0 = 5

  PID   TID USER     PR  NI CPU% S     VSS     RSS PCY Thread          Proc
 1292  3486 camerase 12  -8  40% S  29952K   8720K  fg Camera_startThr /system/bin/cameraserver
 3491  3491 root     20   0  40% R   9868K   2300K  fg top             top
 3391  3391 u0_a44   10 -10  20% S 1562024K  61068K  fg .android.camera com.android.camera
    5     5 root      0 -20   0% S      0K      0K  fg kworker/0:0H
    6     6 root     20   0   0% S      0K      0K  fg kworker/u8:0
    7     7 root     RT   0   0% S      0K      0K  fg migration/0
    8     8 root     20   0   0% S      0K      0K  fg rcu_preempt
    9     9 root     20   0   0% S      0K      0K  fg rcu_bh
   10    10 root     20   0   0% S      0K      0K  fg rcu_sched
   11    11 root     RT   0   0% S      0K      0K  fg migration/1
   12    12 root     20   0   0% S      0K      0K  fg ksoftirqd/1
   13    13 root     20   0   0% S      0K      0K  fg kworker/1:0
   14    14 root      0 -20   0% S      0K      0K  fg kworker/1:0H
   15    15 root     RT   0   0% S      0K      0K  fg migration/2
   16    16 root     20   0   0% S      0K      0K  fg ksoftirqd/2
   17    17 root     20   0   0% S      0K      0K  fg kworker/2:0
   18    18 root      0 -20   0% S      0K      0K  fg kworker/2:0H
   19    19 root     RT   0   0% S      0K      0K  fg migration/3
   20    20 root     20   0   0% S      0K      0K  fg ksoftirqd/3
   21    21 root     20   0   0% S      0K      0K  fg kworker/3:0
^C
130|generic_x86_64:/ #

2、通过ftrace抓系统cpu loading信息
1)查看ftrace的文件节点

C:\Users\hxiong>adb root

C:\Users\hxiong>adb shell
generic_x86_64:/ # cd /sys/kernel/debug/tracing
generic_x86_64:/sys/kernel/debug/tracing # ls -l
total 0
-r--r--r--  1 root root  0 2017-07-01 15:28 README
-r--r--r--  1 root root  0 2017-07-01 15:28 available_events
-r--r--r--  1 root root  0 2017-07-01 15:28 available_tracers
-rw-rw-r--  1 root shell 0 2017-07-01 15:28 buffer_size_kb
-r--r--r--  1 root root  0 2017-07-01 15:28 buffer_total_size_kb
-rw-r--r--  1 root root  0 2017-07-01 15:28 current_tracer
drwxr-xr-x 40 root root  0 2017-07-01 15:28 events
-rw-r--r--  1 root root  0 2017-07-01 15:28 free_buffer
drwxr-xr-x  2 root root  0 2017-07-01 15:28 instances
drwxr-xr-x  2 root root  0 2017-07-01 15:28 options
drwxr-xr-x  6 root root  0 2017-07-01 15:28 per_cpu
-r--r--r--  1 root root  0 2017-07-01 15:28 printk_formats
-r--r--r--  1 root root  0 2017-07-01 15:28 saved_cmdlines
-r--r--r--  1 root root  0 2017-07-01 15:28 saved_tgids
-rw-r--r--  1 root root  0 2017-07-01 15:28 set_event
-rw-r--r--  1 root root  0 2017-07-01 15:28 snapshot
-rw-rw----  1 root shell 0 2017-07-01 15:28 trace
-rw-rw-r--  1 root shell 0 2017-07-01 15:28 trace_clock
--w--w--w-  1 root root  0 2017-07-01 15:28 trace_marker
-rw-r--r--  1 root root  0 2017-07-01 15:28 trace_options
-r--r--r--  1 root root  0 2017-07-01 15:28 trace_pipe
-rw-r--r--  1 root root  0 2017-07-01 15:28 tracing_cpumask
-rw-r--r--  1 root root  0 2017-07-01 15:28 tracing_max_latency
-rw-rw-r--  1 root shell 0 2017-07-01 15:28 tracing_on
-rw-r--r--  1 root root  0 2017-07-01 15:28 tracing_thresh
generic_x86_64:/sys/kernel/debug/tracing #

2)配置好相关设置,就可以开始抓trace信息了
可以使用如下脚步(trace.bat),结束之后会得到一个sys_ftrace_data 文件。

@echo off
adb root
adb wait-for-device

rem set size
adb shell "echo 16384 > /sys/kernel/debug/tracing/buffer_size_kb"
rem set or use debug method
adb shell "echo nop > /sys/kernel/debug/tracing/current_tracer"
rem set debug event
adb shell "echo 'sched_switch sched_wakeup sched_wakeup_new' > /sys/kernel/debug/tracing/set_event"
rem enable debug
adb shell "echo 1 > /sys/kernel/debug/tracing/tracing_enabled >nul 2>&1"
rem stop debug
adb shell "echo 0 > /sys/kernel/debug/tracing/tracing_on"
rem clear debug data 
adb shell "echo > /sys/kernel/debug/tracing/trace"

rem wait user to start
echo press any key to start ...
pause

rem start debug
adb shell "echo 1 > /sys/kernel/debug/tracing/tracing_on"

rem wait user to stop
echo press any key to stop ...
pause

rem stop debug
adb shell "echo 0 > /sys/kernel/debug/tracing/tracing_on"

echo pull debug data start ...
adb pull /sys/kernel/debug/tracing/trace sys_ftrace_data
echo pull debug data end

pause

3)如果发现系统的中断比较多(觉得有异常的话),可以设置你要trace的events

generic_x86_64:/sys/kernel/debug/tracing/events/irq # ls -l
total 0
-rw-r--r-- 1 root root 0 2017-07-01 15:28 enable
-rw-r--r-- 1 root root 0 2017-07-01 15:28 filter
drwxr-xr-x 2 root root 0 2017-07-01 15:28 irq_handler_entry
drwxr-xr-x 2 root root 0 2017-07-01 15:28 irq_handler_exit
drwxr-xr-x 2 root root 0 2017-07-01 15:28 softirq_entry
drwxr-xr-x 2 root root 0 2017-07-01 15:28 softirq_exit
drwxr-xr-x 2 root root 0 2017-07-01 15:28 softirq_raise
generic_x86_64:/sys/kernel/debug/tracing/events/irq #

将上述脚本中的下面一行
adb shell "echo 'sched_switch sched_wakeup sched_wakeup_new' > /sys/kernel/debug/tracing/set_event"
改成
adb shell "echo 'irq_handler_entry irq_handler_exit softirq_entry softirq_exit' > /sys/kernel/debug/tracing/set_event"


4)抓到的trace数据怎么看,这个通过需要脚本把对这些数据进行分析(解析),然后生成对应的波形图文件。excl文件,或者html文件,也就是转成直观的图形或数据来看,公司里面有人专门写这样的脚本(用的是perl 脚本),当然也可以用其他脚本或编程语言。要怎么写脚本,我就不多说了,网上找一下吧,因为公司有现成,所以我也没写过。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值