一、獲取應用包名,ex:shell
adb shell dumpsys window | findstr mCurrentFocus mCurrentFocus=Window{b1af8e1 u0 com.xw.samlpe.bubbleseekbar/com.xw.samlpe.bubbleseekbar.MainAbilityShellActivity}
二、獲取應用的pid,全部線程的tid值,得到具體線程的狀態----得到線程運行時間processCPUTime(即utime,stime,cutime,cstime的和)ide
1 adb shell ps -ef |findstr com.xw.samlpe.bubbleseekbar #獲取pid線程
2 adb shell ps -T -p pid值 #全部線程的tid進程
3 adb shell cat /proc/pid值/task/tid值/stat #具體線程的狀態 it
備註:proc/pid/task/tid/stat文件介紹以下:io
1 C:\Users\dWX1068106>adb shell cat /proc/2275/task/2471/stat 2 2471 (Thread-3) S 626 972 0 0 -1 1077952576 11015 0 0 0 364 207 0 0 10 -10 39 0 534300 6705520640 40954 18446744073709551615 1 1 0 0 0 0 4612 4097 1073775864 0 0 0 -1 3 0 0 0 0 0 0 0 0 0 0 0 0 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
每一個參數意思爲:
參數 解釋
pid=2741 進程(包括輕量級進程,即線程)號
com=(Thread-3) 應用程序或命令的名class
utime=364 該任務在用戶態運行的時間,單位爲jiffies(通常地等於10ms),下標爲13
stime=207 該任務在覈心態運行的時間,單位爲jiffies,下標爲14
cutime=0 累計的該任務的全部的waited-for進程曾經在用戶態運行的時間,單位爲jiffies
cstime=0 累計的該任務的全部的waited-for進程曾經在覈心態運行的時間,單位爲jiffies程序
三、得到/proc/stat中 cpu運行時間 totalCpuTime即(CPU指標:user,nice, system, idle, iowait, irq, softirq的和)im
四、totalCpuTime,processCPUTime均取0.3s的差值,來判斷cpu_rate(咱們須要的cpu使用率),cpu
cpu = 100 * (processCpuTime差值) / (totalCpuTime差值) if cpu < 0: cpu = 0 elif cpu > 100: cpu = 100 return cpu