1.如何定義發(fā)生了卡頓?
當出現(xiàn) App的FPS平均值小于30舀锨,最小值小于24
,說明安卓應用發(fā)生了卡頓。
2.卡頓分析方法
造成卡頓的原因有很多,但是最終都會反映到cpu時間
上鳞绕。
CPU時間包含了用戶時間和系統(tǒng)時間。
用戶時間:執(zhí)行用戶態(tài)應用程序代碼所消耗的時間尸曼。
系統(tǒng)時間:執(zhí)行內(nèi)核態(tài)系統(tǒng)調(diào)用所消耗的時間们何,包括I/O,鎖控轿,中斷和其他系統(tǒng)調(dià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%在抛,但不會超過總核數(shù)*100
3.2 獲取指定進程的CPU钟病,內(nèi)存消耗,并設(shè)置刷新間隔
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信息之外刚梭,如果我們只想查看當前指定進程已經(jīng)消耗的CPU時間占系統(tǒng)總時間的百分比或其他狀態(tài)信息的話肠阱,可以使用ps命令
4.1 查看指定進程的狀態(tài)信息 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
輸出參數(shù)的含義如下:
-USER:用戶名
- PID:進程ID
- PPID:父進程ID
- VSZ:虛擬內(nèi)存大小(K為單位 )
- RSS:常駐內(nèi)存大小(正在使用的頁)
-WCHAN:進程在內(nèi)核態(tài)中的運行時間 - Instruction pointer:指令指針
-NAME:進程名字
最后輸出參數(shù)S表示的是進程當前的狀態(tài),總共10種可能狀態(tài),如下所示:
R (running) S (sleeping) D (device I/O) T (stopped) t (traced)
Z (zombie) X (deader) x (dead) K (wakekill) W (waking)
4.2 查看指定進程已經(jīng)消耗的CPU時間占系統(tǒng)總時間的百分比
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 這三個數(shù)字表示平均一分鐘朴读,五分鐘屹徘,十五分鐘的平均值
,而較低的數(shù)字則更好衅金。數(shù)字越大表示有問題或者機器過載噪伊,需要注意的是,這里Load需要除以核心數(shù)氮唯,比如我這里的系統(tǒng)核心數(shù)為8核鉴吹,所以最終每一個單核CPU的Load為1.05/1.07/0.97,如果Load超過1惩琉,則表示出現(xiàn)了問題豆励。最后我們可以看到系統(tǒng)占用的CPU總時間27%,這個值是根據(jù)前面所有值相加/系統(tǒng)CPU數(shù)的處理結(jié)果琳水。
6.通過讀取/proc/stat 文件獲取總的CPU時間\
采樣兩個足夠短的時間的CPU快照,即需要兩次去讀取/proc/stat文件 獲取兩個時間點對應的數(shù)據(jù) 般堆。
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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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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 16275675 13538661 78071 576502 480389 28287 3305593 10586006 0 5171655
因為我的手機是8核在孝,所以這里的cpu個數(shù)是8個,從cpu0到cpu7淮摔,第一行的cpu即是8個cpu的指標數(shù)據(jù)匯總
私沮,因為要計算系統(tǒng)cpu的使用率,當然應該以cpu為基準和橙,兩次采樣的CPU指標數(shù)據(jù)如下:
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:表示從
系統(tǒng)啟動開始至今處于用戶態(tài)的運行時間
仔燕,注意不包含nice值為負的進程 - nice:表示從系統(tǒng)啟動開始至今nice值為負的進程所占用的CPU時間
- system:表示從系統(tǒng)啟動開始至今處于內(nèi)核態(tài)的運行時間
- idle:表示從系統(tǒng)啟動開始至今除IO等待時間以外的其他等待時間
- iowait:表示從系統(tǒng)啟動開始至今的IO等待時間
- irq:表示從系統(tǒng)啟動開始至今的硬中斷時間
- softirq:表示從系統(tǒng)啟動開始至今的軟中斷時間
- stealstolen:表示當虛擬化環(huán)境種運行時在其他操作系統(tǒng)中花費的時間造垛,在android系統(tǒng)下此值為0.
- guest:表示當在Linux內(nèi)核的控制下為其他操作系統(tǒng)運行虛擬CPU所花費的時間 在android系統(tǒng)下此值為0。
此外晰搀,這些數(shù)值的單位都是jiffies五辽。jiffies 是內(nèi)核中的一個全局變量,用來記錄系統(tǒng)啟動以來產(chǎn)生的節(jié)拍數(shù)外恕,在linux中杆逗,一個節(jié)拍大致可以理解為操作系統(tǒng)進程調(diào)度的最小時間片,不同的linux系統(tǒng)內(nèi)核中的這個值可能不同鳞疲,通常在1ms到10ms之間
罪郊。
了解了/proc/stat 命令下各項參數(shù)的含義之后,我們就可以由前后兩次時間點的CPU數(shù)據(jù)能計算得到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
最后我們可以計算出系統(tǒng)CPU的使用率:
先計算得到CPU的空閑時間
idleCPUTime=idle2-idle1=28728jiffies
最后得到系統(tǒng)CPU的使用率
totalCPUUse=(totalCPUTime-idleCPUTime)/totalCPUTime=(31721-28728)/31721=9%
可以看到,前后兩次時間點的CPU使用率大概為9%腺毫。說明我們系統(tǒng)的CPU處于空閑狀態(tài)癣疟,如果CPU使用率一直大于60%,則表示系統(tǒng)處于繁忙狀態(tài)拴曲,此時就需要進一步分析用戶時間和系統(tǒng)時間的比例争舞,看看到底是系統(tǒng)占用了CPU還是應用進程占用了CPU。