如何进行APP卡顿分析?
1.如何定义发生了卡顿?
当出现 App的FPS平均值小于30,最小值小于24
,说明安卓应用发生了卡顿。
2.卡顿分析方法
造成卡顿的原因有很多,但是最终都会反映到cpu时间
上。
CPU时间包含了用户时间和系统时间。
用户时间:执行用户态应用程序代码所消耗的时间。
系统时间:执行内核态系统调用所消耗的时间,包括I/O,锁,中断和其他系统调用所消耗的时间。
3.使用top命令查看应用进程CPU消耗情况
3.1 adb shell top
Tasks: 646 total, 1 running, 639 sleeping, 0 stopped, 0 zombie
Tasks: 647 total, 2 running, 639 sleeping, 0 stopped, 0 zombie
Mem: 3828172k total, 3760404k used, 67768k free, 169336k buffers
Swap: 524284k total, 9480k used, 514804k free, 1745900k cached
800%cpu 195%user 1%nice 45%sys 551%idle 1%iow 3%irq 3%sirq 0%host
PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS
22034 u0_a188 10 -10 2.2G 349M 134M S 156 9.3 21:53.26 caocaokeji.research.geniustransport
20191 u0_a191 20 0 1.9G 197M 66M S 16.3 5.2 2:05.48 com.geespace.lanucher
793 system -2 -8 73M 25M 18M S 11.0 0.6 7:26.11 surfaceflinger
1268 system 18 -2 4.7G 264M 210M S 9.0 7.0 78:22.53 system_server
767 system -3 -8 46M 18M 4.8M S 5.0 0.4 3:17.86 android.hardware.graphics.composer@2.1-service
23557 root 20 0 10M 2.8M 1.5M R 2.3 0.0 0:01.59 top
2540 root RT 0 0 0 0 S 2.0 0.0 1:27.26 [irq/41-1008000.]
1930 u0_a31 20 0 4.3G 153M 78M S 2.0 4.1 11:26.07 com.android.systemui
855 audioserver 20 0 81M 13M 6.5M S 1.6 0.3 33:30.65 audioserver
775 wifi 20 0 17M 4.1M 3.0M S 1.6 0.1 5:01.33 android.hardware.wifi@1.0-service
759 audioserver 20 0 32M 11M 4.7M S 1.6 0.2 42:14.29 android.hardware.audio@2.0-service
22808 root 20 0 0 0 0 S 1.3 0.0 0:04.71 [kworker/u16:4]
864 media 20 0 138M 13M 9.0M S 1.3 0.3 30:00.13 mediaserver
21876 root 20 0 0 0 0 S 1.0 0.0 0:08.38 [kworker/u16:9]
21426 root RT 0 0 0 0 D 1.0 0.0 0:09.98 [mdss_fb0]
19370 root 20 0 0 0 0 S 1.0 0.0 1:32.65 [kworker/u16:0]
3294 u0_a82 20 0 1.8G 60M 46M S 1.0 1.6 5:49.53 com.baidu.BaiduMap:MapCoreService
2323 root 18 -2 0 0 0 S 1.0 0.0 0:27.53 [cds_mc_thread]
549 root 20 0 7.9M 1.4M 724K S 1.0 0.0 1:48.16 ueventd
20107 root 20 0 0 0 0 S 0.6 0.0 0:02.20 [kworker/1:0]
588 logd 30 10 20M 5.3M 1.4M S 0.6 0.1 5:55.43 logd
443 root RT 0 0 0 0 S 0.6 0.0 2:55.47 [cfinteractive]
79 root 20 0 0 0 0 S 0.6 0.0 3:53.48 [smem_native_rpm]
22718 u0_a191 20 0 1.7G 45M 35M S 0.3 1.2 1:12.71 com.geespace.lanucher:remote
3024 u0_a82 12 -8 1.8G 104M 75M S 0.3 2.7 4:52.75 com.baidu.BaiduMap
2702 u0_a3 20 0 4.3G 120M 70M S 0.3 3.2 1:17.70 ch.deletescape.lawnchair.ci
2166 radio 20 0 4.2G 68M 52M S 0.3 1.8 6:29.89 com.android.phone
1119 root 20 0 10M 1.9M 1.3M S 0.3 0.0 4:17.32 msm_irqbalance -f /system/vendor/etc/msm_irqbalance.conf
853 root 20 0 10M 1.8M 1.3M S 0.3 0.0 0:09.53 hvdcp_opti
833 root 20 0 35M 1.1M 636K S 0.3 0.0 7:57.53 adbd --root_seclabel=u:r:su:s0
785 root 20 0 16M 3.0M 2.3M S 0.3 0.0 0:26.58 healthd
779 gps 20 0 42M 8.9M 6.5M S 0.3 0.2 0:26.07 vendor.qti.gnss@1.0-service
771 system 20 0 15M 3.2M 2.4M S 0.3 0.0 0:26.50 android.hardware.power@1.1-service-qti
769 system 20 0 12M 2.4M 1.8M S 0.3 0.0 0:08.34 android.hardware.light@2.0-service.xiaomi_clover
451 root -2 0 0 0 0 D 0.3 0.0 6:53.51 [mmc-cmdqd/0]
291 root -3 0 0 0 0 S 0.3 0.0 1:20.95 [kgsl_worker_thr]
15 root 20 0 0 0 0 S 0.3 0.0 3:27.16 [ksoftirqd/1]
10 root 20 0 0 0 0 S 0.3 0.0 1:09.00 [rcuop/0]
23584 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kworker/0:2]
23538 root 20 0 0 0 0 S 0.0 0.0 0:00.54 [kworker/u16:6]
23255 root 20 0 0 0 0 S 0.0 0.0 0:00.40 [kworker/1:1]
23185 root 20 0 0 0 0 S 0.0 0.0 0:00.03 [kworker/0:0]
22922 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kworker/3:2]
22623 root 20 0 0 0 0 S 0.0 0.0 0:00.29 [kworker/0:1]
22371 root 20 0 0 0 0 S 0.0 0.0 0:00.77 [kworker/1:2]
22030 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kworker/6:2]
21512 root 20 0 8.7M 1.8M 1.3M S 0.0 0.0 0:00.03 sh -
21511 root 20 0 0 0 0 S 0.0 0.0 0:00.14 [tty_worker_thre]
21510 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [tty_worker_thre]
注意:
%CPU显示的是进程占用一个核的百分比,而不是整个cpu(N核)的百分比,有时候可能大于100,那是因为该进程启用了多线程占用了多个核心,所以有时候我们看该值得时候会超过100%,但不会超过总核数*100
3.2 获取指定进程的CPU,内存消耗,并设置刷新间隔
clover:/ # top -d 1 | grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 171M 96M S 41.9 4.5 64:11.96 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.02 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 168M 96M S 3.0 4.4 64:12.09 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.02 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 29.0 4.5 64:12.12 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.02 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 16.0 4.5 64:12.41 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.02 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 168M 96M S 4.0 4.4 64:12.57 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.02 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 169M 96M S 25.0 4.5 64:12.61 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 1.0 0.0 0:00.02 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 169M 96M S 3.0 4.5 64:12.86 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.03 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 171M 96M S 25.0 4.5 64:12.89 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.03 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 44.0 4.5 64:13.14 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.03 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 168M 96M S 24.0 4.5 64:13.58 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.03 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 171M 96M S 5.0 4.5 64:13.82 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.03 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 44.0 4.5 64:13.87 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 1.0 0.0 0:00.03 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 171M 96M S 10.0 4.5 64:14.31 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 25.0 4.5 64:14.41 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 29.0 4.5 64:14.66 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 170M 96M S 26.0 4.5 64:14.95 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 169M 96M S 3.0 4.5 64:15.21 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 169M 96M S 3.0 4.5 64:15.21 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 0.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
22034 u0_a188 20 0 2.3G 171M 96M S 50.0 4.5 64:15.24 caocaokeji.research.geniustransport
27769 root 20 0 8.5M 1.5M 1.0M S 1.0 0.0 0:00.04 grep caocaokeji.research.geniustransport
4.使用ps命令
除了top命令可以比较全面的查看整体CPU信息之外,如果我们只想查看当前指定进程已经消耗的CPU时间占系统总时间的百分比或其他状态信息的话,可以使用ps命令
4.1 查看指定进程的状态信息 ps -p pId
clover:/ # ps -p 22034
USER PID PPID VSZ RSS WCHAN ADDR S NAME
u0_a188 22034 647 2417296 360192 SyS_epoll+ ecee7494 S caocaokeji.research.geniustransport
输出参数的含义如下:
-USER:用户名
- PID:进程ID
- PPID:父进程ID
- VSZ:虚拟内存大小(K为单位 )
- RSS:常驻内存大小(正在使用的页)
-WCHAN:进程在内核态中的运行时间 - Instruction pointer:指令指针
-NAME:进程名字
最后输出参数S表示的是进程当前的状态,总共10种可能状态,如下所示:
R (running) S (sleeping) D (device I/O) T (stopped) t (traced)
Z (zombie) X (deader) x (dead) K (wakekill) W (waking)
4.2 查看指定进程已经消耗的CPU时间占系统总时间的百分比
clover:/ # ps -o pcpu -p 22034
%CPU
142
5.使用dumpsys cpuinfo命令
clover:/ # dumpsys cpuinfo
Load: 8.4 / 8.63 / 7.78
CPU usage from 320316ms to 20269ms ago (2020-08-20 09:16:16.957 to 2020-08-20 09:21:17.004):
144% 22034/caocaokeji.research.geniustransport: 132% user + 12% kernel / faults: 369335 minor
12% 20191/com.geespace.lanucher: 9.6% user + 2.6% kernel / faults: 4289 minor
11% 793/surfaceflinger: 6% user + 5.4% kernel
8.7% 1268/system_server: 6.4% user + 2.2% kernel / faults: 104549 minor
5.1% 767/android.hardware.graphics.composer@2.1-service: 3.2% user + 1.8% kernel / faults: 109 minor
2.2% 855/audioserver: 1.2% user + 0.9% kernel / faults: 292 minor
2.1% 2540/irq/41-1008000.: 0% user + 2.1% kernel
1.9% 759/android.hardware.audio@2.0-service: 0.5% user + 1.4% kernel
1.3% 864/mediaserver: 0.9% user + 0.4% kernel
1.3% 3294/com.baidu.BaiduMap:MapCoreService: 1.1% user + 0.2% kernel / faults: 6769 minor
1% 22808/kworker/u16:4: 0% user + 1% kernel
1% 21426/mdss_fb0: 0% user + 1% kernel
0.8% 21876/kworker/u16:9: 0% user + 0.8% kernel
0.8% 588/logd: 0.4% user + 0.3% kernel / faults: 9 minor
0.7% 19370/kworker/u16:0: 0% user + 0.7% kernel
0.7% 775/android.hardware.wifi@1.0-service: 0.4% user + 0.2% kernel / faults: 2 minor
0.6% 1930/com.android.systemui: 0.4% user + 0.1% kernel / faults: 781 minor
0.5% 443/cfinteractive: 0% user + 0.5% kernel
0.5% 79/smem_native_rpm: 0% user + 0.5% kernel
0.5% 291/kgsl_worker_thr: 0% user + 0.5% kernel
0.4% 779/vendor.qti.gnss@1.0-service: 0.2% user + 0.2% kernel / faults: 39 minor
0.4% 22718/com.geespace.lanucher:remote: 0.3% user + 0% kernel / faults: 1192 minor
0.3% 2166/com.android.phone: 0.2% user + 0.1% kernel / faults: 256 minor
0.3% 2887/org.mokee.mkparts: 0.2% user + 0% kernel / faults: 12237 minor
0.3% 771/android.hardware.power@1.1-service-qti: 0.1% user + 0.1% kernel
0.3% 2323/cds_mc_thread: 0% user + 0.3% kernel
0.2% 7/rcu_preempt: 0% user + 0.2% kernel
0.2% 76/lpass_smem_glin: 0% user + 0.2% kernel
0.2% 75/smem_native_lpa: 0% user + 0.2% kernel
0.2% 451/mmc-cmdqd/0: 0% user + 0.2% kernel
0.2% 589/servicemanager: 0% user + 0.1% kernel
0.1% 20107/kworker/1:0: 0% user + 0.1% kernel
0.1% 1119/msm_irqbalance: 0% user + 0.1% kernel
0% 549/ueventd: 0% user + 0% kernel
0.1% 3024/com.baidu.BaiduMap: 0% user + 0% kernel / faults: 22 minor
0.1% 494/core_ctl/4: 0% user + 0.1% kernel
0.1% 21450/kworker/u16:8: 0% user + 0.1% kernel
0.1% 2409/wpa_supplicant: 0% user + 0% kernel / faults: 62 minor
0.1% 20464/com.baidu.BaiduMap:SandBoxProcess: 0% user + 0% kernel / faults: 543 minor
0.1% 457/irq/93-spdm_bw_: 0% user + 0.1% kernel
0.1% 869/rild: 0% user + 0% kernel / faults: 107 minor
0.1% 926/lowi-server: 0% user + 0% kernel / faults: 225 minor
0% 10/rcuop/0: 0% user + 0% kernel
0% 866/wificond: 0% user + 0% kernel / faults: 676 minor
0% 39/rcuop/4: 0% user + 0% kernel
0% 645/netd: 0% user + 0% kernel / faults: 1675 minor
0% 53/rcuop/6: 0% user + 0% kernel
0% 25/rcuop/2: 0% user + 0% kernel
0% 833/adbd: 0% user + 0% kernel / faults: 399 minor
0% 49/migration/6: 0% user + 0% kernel
0% 56/migration/7: 0% user + 0% kernel
0% 35/migration/4: 0% user + 0% kernel
0% 42/migration/5: 0% user + 0% kernel
0% 94/system: 0% user + 0% kernel
0% 5572/kworker/3:1: 0% user + 0% kernel
0% 15/ksoftirqd/1: 0% user + 0% kernel
0% 785/healthd: 0% user + 0% kernel
0% 846/cnd: 0% user + 0% kernel / faults: 386 minor
0% 23255/kworker/1:1: 0% user + 0% kernel
0% 18/rcuop/1: 0% user + 0% kernel
0% 590/hwservicemanager: 0% user + 0% kernel
0% 23185/kworker/0:0: 0% user + 0% kernel
0% 46/rcuop/5: 0% user + 0% kernel
0% 787/rmt_storage: 0% user + 0% kernel
0% 568/jbd2/mmcblk0p64: 0% user + 0% kernel
0% 853/hvdcp_opti: 0% user + 0% kernel
0% 60/rcuop/7: 0% user + 0% kernel
0% 770/android.hardware.memtrack@1.0-service: 0% user + 0% kernel
0% 21183/com.android.bluetooth: 0% user + 0% kernel / faults: 55 minor
0% 22623/kworker/0:1: 0% user + 0% kernel
0% 3/ksoftirqd/0: 0% user + 0% kernel
0% 2702/ch.deletescape.lawnchair.ci: 0% user + 0% kernel / faults: 23 minor
0% 357/goodix_charger_: 0% user + 0% kernel
0% 20156/kworker/2:2: 0% user + 0% kernel
0% 32/rcuop/3: 0% user + 0% kernel
0% 445/irq/65-c0c4000.: 0% user + 0% kernel
0% 486/irq/712-ima-rdy: 0% user + 0% kernel
0% 758/android.hidl.allocator@1.0-service: 0% user + 0% kernel
0% 766/android.hardware.graphics.allocator@2.0-service: 0% user + 0% kernel / faults: 50 minor
0% 769/android.hardware.light@2.0-service.xiaomi_clover: 0% user + 0% kernel
0% 2136/.dataservices: 0% user + 0% kernel / faults: 22 minor
0% 13/migration/0: 0% user + 0% kernel
0% 72/msm_watchdog: 0% user + 0% kernel
0% 74/mpss_smem_glink: 0% user + 0% kernel
0% 112/kworker/u17:0: 0% user + 0% kernel
0% 115/kworker/u17:1: 0% user + 0% kernel
0% 760/android.hardware.bluetooth@1.0-service-qti: 0% user + 0% kernel
0% 6589/com.android.htmlviewer: 0% user + 0% kernel / faults: 63 minor
0% 21512/sh: 0% user + 0% kernel / faults: 126 minor
0% 8/rcu_sched: 0% user + 0% kernel
0% 11/rcuos/0: 0% user + 0% kernel
0% 14/migration/1: 0% user + 0% kernel
0% 21/migration/2: 0% user + 0% kernel
0% 26/rcuos/2: 0% user + 0% kernel
0% 28/migration/3: 0% user + 0% kernel
0% 73/smem_native_mps: 0% user + 0% kernel
0% 215/hwrng: 0% user + 0% kernel
0% 781/vendor.qti.hardware.perf@1.0-service: 0% user + 0% kernel
0% 792/lmkd: 0% user + 0% kernel
0% 848/thermal-engine: 0% user + 0% kernel
0% 867/media.codec: 0% user + 0% kernel
0% 1027/netmgrd: 0% user + 0% kernel / faults: 112 minor
0% 2150/com.qualcomm.qti.telephonyservice: 0% user + 0% kernel
0% 2601/org.mokee.audiofx: 0% user + 0% kernel
0% 3805/com.ximalaya.ting.android.car:player: 0% user + 0% kernel
0% 5514/kworker/5:0: 0% user + 0% kernel
0% 5603/kworker/0:2H: 0% user + 0% kernel
0% 6989/com.simplemobiletools.gallery.mokee: 0% user + 0% kernel
0% 7066/kworker/7:0: 0% user + 0% kernel
0% 21511/tty_worker_thre: 0% user + 0% kernel
+0% 23538/kworker/u16:6: 0% user + 0% kernel
+0% 23584/kworker/0:2: 0% user + 0% kernel
+0% 23659/kworker/1:3: 0% user + 0% kernel
27% TOTAL: 21% user + 4.6% kernel + 0% iowait + 0.5% irq + 0.3% softirq
从上述信息可知,第一行显示的是cpu load( 负载平均值)
:Load:8.4 / 8.63 / 7.78 这三个数字表示平均一分钟,五分钟,十五分钟的平均值
,而较低的数字则更好。数字越大表示有问题或者机器过载,需要注意的是,这里Load需要除以核心数,比如我这里的系统核心数为8核,所以最终每一个单核CPU的Load为1.05/1.07/0.97,如果Load超过1,则表示出现了问题。最后我们可以看到系统占用的CPU总时间27%,这个值是根据前面所有值相加/系统CPU数的处理结果。
6.通过读取/proc/stat 文件获取总的CPU时间\
采样两个足够短的时间的CPU快照,即需要两次去读取/proc/stat文件 获取两个时间点对应的数据 。
130|clover:/ # cat proc/stat
cpu 3626483 693386 1416709 14088503 44871 377376 385365 0 0 0
cpu0 1226189 176988 480426 9019164 42751 98563 184262 0 0 0
cpu1 1614623 444863 563194 502901 1256 111786 139918 0 0 0
cpu2 461899 50058 277172 682663 301 148077 46009 0 0 0
cpu3 52417 2863 44521 789359 334 11536 8142 0 0 0
cpu4 77202 6421 14942 767903 75 1302 2071 0 0 0
cpu5 70742 5369 13494 771004 66 1121 421 0 0 0
cpu6 65125 3795 12495 775102 44 1633 942 0 0 0
cpu7 58286 3029 10465 780407 44 3358 3600 0 0 0
intr 95120329 0 0 0 14418476 0 15280514 4 4 4 0 44 277254 0 44 10 0 0 0 0 0 0 0 12093 0 0 0 0 0 0 0 0 0 0 0 0 11 11 0 0 0 0 629207 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 868079 43469 57876 8904 10 148 113934 10791355 96 5375844 12 306876 1128852 79519 0 0 0 0 0 264126 157 1789660 0 0 0 0 42675 0 0 4461 0 209008 223183 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1076 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7499 0 0 0 185 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 62161 357591 5468 0 0 0 0 0 4 0 4 0 6 0 11 11 726 0 0 2141 0 205494 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1913 0 831 0 0 0 0 0 0 0 0 0 0 0 0 0 12 9 9 0 12 12 186 12 0 0 0 0 0 0 0 0 1779 0 42 0 0 2 11 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1894667 0 0 0 0 29 0 0 1 16 0 0 0 0 26 146350 2 0 15
ctxt 264760673
btime 1597630092
processes 59580
procs_running 4
procs_blocked 1
softirq 50019209 16269109 13533246 78059 576245 480303 28287 3304286 10580430 0 5169244
#第二次采样
cpu 3628111 693703 1417479 14117231 44877 377518 385495 0 0 0
cpu0 1226424 177119 480551 9022389 42755 98600 184304 0 0 0
cpu1 1615677 445031 563682 505426 1256 111827 139991 0 0 0
cpu2 462182 50074 277309 686061 302 148130 46014 0 0 0
cpu3 52473 2863 44541 793196 335 11547 8152 0 0 0
cpu4 77202 6421 14942 771839 75 1302 2071 0 0 0
cpu5 70742 5369 13494 774941 66 1121 421 0 0 0
cpu6 65125 3797 12495 779036 44 1633 942 0 0 0
cpu7 58286 3029 10465 784343 44 3358 3600 0 0 0
intr 95158823 0 0 0 14424566 0 15287215 4 4 4 0 44 277254 0 44 10 0 0 0 0 0 0 0 12097 0 0 0 0 0 0 0 0 0 0 0 0 11 11 0 0 0 0 629824 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 868311 43484 57876 8904 10 148 114102 10795482 96 5378146 12 307006 1129289 79558 0 0 0 0 0 264253 157 1790408 0 0 0 0 42690 0 0 4461 0 209063 223183 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1076 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7499 0 0 0 185 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 62204 357705 5468 0 0 0 0 0 4 0 4 0 6 0 11 11 726 0 0 2141 0 205635 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1913 0 831 0 0 0 0 0 0 0 0 0 0 0 0 0 12 9 9 0 12 12 186 12 0 0 0 0 0 0 0 0 1780 0 42 0 0 2 11 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1896111 0 0 0 0 29 0 0 1 16 0 0 0 0 26 146388 2 0 15
ctxt 264867319
btime 1597630092
processes 59599
procs_running 1
procs_blocked 0
softirq 50040839 16275675 13538661 78071 576502 480389 28287 3305593 10586006 0 5171655
因为我的手机是8核,所以这里的cpu个数是8个,从cpu0到cpu7,第一行的cpu即是8个cpu的指标数据汇总
,因为要计算系统cpu的使用率,当然应该以cpu为基准,两次采样的CPU指标数据如下:
cpu1 3626483 693386 1416709 14088503 44871 377376 385365 0 0 0
cpu2 3628111 693703 1417479 14117231 44877 377518 385495 0 0 0
其对应的各项指标如下:
CPU(user, nice, system, idle, iowait, irq, softirq, stealstolen, guest)
- user:表示从
系统启动开始至今处于用户态的运行时间
,注意不包含nice值为负的进程 - nice:表示从系统启动开始至今nice值为负的进程所占用的CPU时间
- system:表示从系统启动开始至今处于内核态的运行时间
- idle:表示从系统启动开始至今除IO等待时间以外的其他等待时间
- iowait:表示从系统启动开始至今的IO等待时间
- irq:表示从系统启动开始至今的硬中断时间
- softirq:表示从系统启动开始至今的软中断时间
- stealstolen:表示当虚拟化环境种运行时在其他操作系统中花费的时间,在android系统下此值为0.
- guest:表示当在Linux内核的控制下为其他操作系统运行虚拟CPU所花费的时间 在android系统下此值为0。
此外,这些数值的单位都是jiffies。jiffies 是内核中的一个全局变量,用来记录系统启动以来产生的节拍数,在linux中,一个节拍大致可以理解为操作系统进程调度的最小时间片,不同的linux系统内核中的这个值可能不同,通常在1ms到10ms之间
。
了解了/proc/stat 命令下各项参数的含义之后,我们就可以由前后两次时间点的CPU数据能计算得到cpu1和cpu2的活动时间,如下所示:
totalCPUTime=user+nice+system+idle+iowait+irq+softirq+stealstolen+guest
cpu1=3626483+693386+1416709+14088503+44871 +377376 +385365 +0 +0+ 0=20632693
cpu2=3628111+693703+1417479+14117231+44877 +377518 +385495 +0 +0+ 0=20664414
由此可以得出CPU时间,如下所示:
totalCPUTime=cpu2-cpu1=31721jiffies
最后我们可以计算出系统CPU的使用率:
先计算得到CPU的空闲时间
idleCPUTime=idle2-idle1=28728jiffies
最后得到系统CPU的使用率
totalCPUUse=(totalCPUTime-idleCPUTime)/totalCPUTime=(31721-28728)/31721=9%
可以看到,前后两次时间点的CPU使用率大概为9%。说明我们系统的CPU处于空闲状态,如果CPU使用率一直大于60%,则表示系统处于繁忙状态,此时就需要进一步分析用户时间和系统时间的比例,看看到底是系统占用了CPU还是应用进程占用了CPU。