獲取進(jìn)程線程信息
當(dāng)發(fā)生 ANR 的時候,Android 系統(tǒng)會打印 CPU 相關(guān)的信息到日志中,使用的是ProcessCpuTracker
這樣好像并沒有權(quán)限可以拿到其他應(yīng)用進(jìn)程的 CPU 信息
當(dāng)發(fā)現(xiàn)應(yīng)用的某個進(jìn)程 CPU 使用率比較高的時候,可以通過下面幾個文件檢查該進(jìn)程下各個線程的 CPU 使用率,繼而統(tǒng)計出該進(jìn)程各個線程的時間占比。
/proc/[pid]/stat // 進(jìn)程 CPU 使用情況
/proc/[pid]/task/[tid]/stat // 進(jìn)程下面各個線程的 CPU 使用情況
/proc/[pid]/sched // 進(jìn)程 CPU 調(diào)度相關(guān)
/proc/loadavg // 系統(tǒng)平均負(fù)載癌刽,uptime 命令對應(yīng)文件
已將這部分邏輯加入網(wǎng)管監(jiān)控中。代碼cn.mwee.android.skynet.appruninfo.util.ProcessCpuTracker
如果需要獲取線程信息打開開關(guān) 會獲取以下信息](http://www.samirchen.com/linux-cpu-performance/)
CPU usage from 180344ms to 243ms ago (2019-02-21 20:54:48.521 to 2019-02-21 20:57:48.622): 0.5% 14771/com.mwee.android.pos.dinner:bizcenter(S): 0.3% user + 0.1% kernel / faults: 1367 minor thread stats: 0.1% 14979/thread_runtime_(R): 0.1% user + 0% kernel / faults: 162 minor 0% 15278/MYD_NetPool_114(S): 0% user + 0% kernel / faults: 193 minor 0% 14902/LogFileThread(S): 0% user + 0% kernel / faults: 296 minor 0% 14781/HeapTaskDaemon(S): 0% user + 0% kernel / faults: 168 minor 0% 14947/MYD_NetPool_114(S): 0% user + 0% kernel / faults: 64 minor 0% 8321/Smack Packet Re(S): 0% user + 0% kernel / faults: 291 minor 0% 14944/MYD_SLooper_114(S): 0% user + 0% kernel / faults: 57 minor 0% 8165/Smack Packet Re(S): 0% user + 0% kernel / faults: 294 minor 0% 14935/MWLog(S): 0% user + 0% kernel / faults: 13 minor 0% 14808/LogFileThread(S): 0% user + 0% kernel / faults: 69 minor 0% 14981/MYD_NetPool_114(S): 0% user + 0% kernel / faults: 13 minor 0% 14983/MYD_NetPool_114(S): 0% user + 0% kernel / faults: 13 minor 0% 15093/MYD_NetPool_114(S): 0% user + 0% kernel / faults: 3 minor 0% 15326/Smack Packet Re(S): 0% user + 0% kernel 0% 24139/Smack Listener (S): 0% user + 0% kernel 0% 24138/Smack Packet Re(S): 0% user + 0% kernel 0% 14771/inner:bizcenter(S): 0% user + 0% kernel / faults: 19 minor 0% 15061/Thread-403(S): 0% user + 0% kernel / faults: 4 minor 0% 14779/FinalizerDaemon(S): 0% user + 0% kernel 0% 14972/Binder_4(S): 0% user + 0% kernel / faults: 8 minor 0% 15009/MYD_SNetPool_11(S): 0% user + 0% kernel 0% 15156/MYD_xmpplink_al(S): 0% user + 0% kernel / faults: 6 minor 0% 14657/Binder_7(S): 0% user + 0% kernel 0% 14987/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14938/WifiManager(S): 0% user + 0% kernel 0% 14789/Binder_2(S): 0% user + 0% kernel / faults: 2 minor 0% 14951/AlpAckLoop(S): 0% user + 0% kernel 0% 19106/Smack Listener (S): 0% user + 0% kernel / faults: 4 minor 0% 19105/Smack Packet Re(S): 0% user + 0% kernel / faults: 1 minor 0% 19104/Smack Packet Wr(S): 0% user + 0% kernel / faults: 6 minor 0% 14778/ReferenceQueueD(S): 0% user + 0% kernel 0% 5790/Binder_6(S): 0% user + 0% kernel 0% 26903/Binder_5(S): 0% user + 0% kernel 0% 14945/ClientHeartBeat(S): 0% user + 0% kernel 0% 14949/ALPServerLoop(S): 0% user + 0% kernel / faults: 4 minor 0% 14783/Binder_1(S): 0% user + 0% kernel 0% 14777/JDWP(S): 0% user + 0% kernel 0% 14960/Binder_3(S): 0% user + 0% kernel 0% 14939/Thread-379(S): 0% user + 0% kernel 0% 14941/Thread-381(S): 0% user + 0% kernel 0% 14940/Thread-380(S): 0% user + 0% kernel 0% 10446/OkHttp Connecti(S): 0% user + 0% kernel 0% 14885/Okio Watchdog(S): 0% user + 0% kernel 0% 14976/thread_runtime_(S): 0% user + 0% kernel 0% 14780/FinalizerWatchd(S): 0% user + 0% kernel 0% 14889/pool-1-thread-1(S): 0% user + 0% kernel 0% 15095/MYD_NetPool_114(S): 0% user + 0% kernel 0% 24137/Smack Packet Wr(S): 0% user + 0% kernel / faults: 1 minor 0% 9534/Smack Listener (S): 0% user + 0% kernel 0% 9511/Smack Packet Re(S): 0% user + 0% kernel 0% 9510/Smack Packet Wr(S): 0% user + 0% kernel / faults: 1 minor 0% 15163/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14776/Signal Catcher(S): 0% user + 0% kernel 0% 15010/MYD_SNetPool_11(S): 0% user + 0% kernel 0% 14989/MYD_SNetPool_11(S): 0% user + 0% kernel 0% 14999/MYD_SNetPool_11(S): 0% user + 0% kernel 0% 14914/BuglyThread-3(S): 0% user + 0% kernel 0% 15094/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14912/BuglyThread-1(S): 0% user + 0% kernel 0% 14818/BuglyThread-1(S): 0% user + 0% kernel 0% 14913/BuglyThread-2(S): 0% user + 0% kernel 0% 15325/Smack Packet Wr(S): 0% user + 0% kernel / faults: 1 minor 0% 15328/Smack Listener (S): 0% user + 0% kernel 0% 14888/OkHttp Connecti(S): 0% user + 0% kernel 0% 14858/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14942/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14859/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14860/MYD_NetPool_114(S): 0% user + 0% kernel 0% 14819/BuglyThread-2(S): 0% user + 0% kernel 0% 14820/BuglyThread-3(S): 0% user + 0% kernel 0% 1495
上面是美易點業(yè)務(wù)中心的線程信息 尝丐。上面可以發(fā)現(xiàn)有幾個重復(fù)的線程显拜,bugly線程都重復(fù)了一次。這是由于不同classloader下 分別init了一次bugly爹袁。Android性能優(yōu)化-線程性能優(yōu)化
線程不是免費的:它們占用內(nèi)存远荠。每個線程最少消耗64k內(nèi)存。如果設(shè)備上安裝了許多應(yīng)用失息,該值就會快速添加譬淳,特別是在調(diào)用棧顯著增長的情況下