查看Android cpu 相關數據的一些經常使用指令。貼圖數據來自模擬器。linux
在開發過程當中,咱們須要獲取設備的 CPU 信息,來查看性能。bash
//獲取 cpu 核心數
cat /sys/devices/system/cpu/possible
//查看Linux內核版本
uname -a 和 cat /proc/version複製代碼
經過 cat /proc/cpuinfo 命令性能
經過 /proc/stat 文件查看全部cpu活動信息ui
第一行是cpu總的使用狀況,各數據的含義:spa
這些數值的單位都是 jiffies,他是內核中的一個全局變量,用來記錄系統起來以來產生的節拍數。一個節拍大體能夠理解成爲操做系統進程調度的最小時間片,不一樣Linux系統內核這個值可能不一樣,一般在1-10ms之間。操作系統
cpu 17332 52007 24366 1083560 5182 16 1825 0 0 03d
user
(17332) 從系統啓動開始累積到當前時刻,處於用戶態的運行時間,不包含 nice 值爲負的進程。code
nice
(52007) 從系統啓動開始累積到當前時刻,nice 值爲負的進程所佔用的 CPU 時間。orm
system
(24366) 從系統啓動開始累積到當前時刻,處於核心態的運行時間。cdn
idle
(1083560) 從系統啓動開始累積到當前時刻,除 IO 等待時間之外的其餘等待時間。
iowait
(5182) 從系統啓動開始累積到當前時刻,IO 等待時間。(since 2.5.41)
irq
(16) 從系統啓動開始累積到當前時刻,硬中斷時間。(since 2.6.0-test4)
softirq
(1825) 從系統啓動開始累積到當前時刻,軟中斷時間。(since 2.6.0-test4)
stealstolen(0)
Stolen time, which is the time spent in other operating systems when running in a virtualized environment. 從系統啓動開始累積到當前時刻,在虛擬環境運行時花費在其餘操做系統的時間。(since 2.6.11)
guest(0)
Which is the time spent running a virtual CPU for guest operating systems under the control of the Linux kernel. 從系統啓動開始累積到當前時刻,在Linux內核控制下的操做系統虛擬cpu花費的時間。(since 2.6.24)
guest_nice(0)
Time spent running a niced guest (virtual CPU for guest operating systems under the control of the Linux kernel). 從系統啓動開始累積到當前時刻,在Linux內核控制下的操做系統虛擬cpu花費在nice進程上的時間。(since Linux 2.6.33)
由此能夠獲得cpu總的活動時間。
計算cpu使用率
經常使用cpu佔用率方法可描述爲: totalCPUrate = (非空閒cpu時間2-非空閒cpu時間1)/(cpu總時間2-cpu總時間1)x100% 換成變量描述爲: totalCPUrate =( (totalCPUTime2-idle2)-(totalCPUTime1-idle1))/(totalCPUTime2-totalCPUTime1)x100%
計算方法:
採樣兩個足夠短的時間間隔的cpu數據,分別記做t一、t2,其中t一、t2的結構均爲: (user、nice、system、idle、iowait、irq、softirq、stealstolen、guest、guest_nice)的10元組;(固然這裏依據Linux內核的不一樣有些數據可能沒有,就沒必要計入)
計算t一、t2總的cpu時間片totalCPUTime a) 把第一次的全部cpu10元組數據求和,獲得totalCPUTime1; b) 把第二次的全部cpu10元組數據求和,獲得totalCPUTime2;
計算空閒時間idle cpu空閒時間對應第四列的數據 a)得到第一次的idle數據,記爲idle1 b)得到第二次的idle數據,記爲idle2
計算cpu使用率 totalCPUrate = ((totalCPUTime2-idle2)-(totalCPUTime1-idle1))/(totalCPUTime2-totalCPUTime1)x100%