如何获得更高的"CPU%"精度?比从TOP命令得到的还要多? [英] How to get higher precision of "CPU%" than that from TOP command?
问题描述
当我使用TOP命令时,可以获得以下信息:
When I use TOP command, I could get the following info:
shell@android:/ $ top -n 1
User 31%, System 10%, IOW 0%, IRQ 0%
User 346 + Nice 10 + Sys 120 + Idle 637 + IOW 6 + IRQ 0 + SIRQ 2 = 1121
PID PR CPU% S #THR VSS RSS PCY UID Name
481 1 26% S 89 762832K 81688K fg system system_server
1699 0 5% S 27 676472K 39092K fg u0_a72 wm.cs.systemmonitor
11243 0 3% S 28 673140K 29796K bg u0_a111 com.weather.Weather
13327 2 1% S 23 680472K 35844K bg u0_a83 com.rhmsoft.fm
659 0 1% S 17 663044K 33136K bg u0_a13 android.process.media
20260 1 0% R 1 1208K 508K shell top
我们可以看到CPU%
是四舍五入到整数,有什么办法可以使进程的CPU%
精度更高?
We can see the CPU%
is round to integer, is there any way I could get a process's CPU%
with higher precision?
-关于赏金的说明- Alex
-- Clarifications on the bounty -- Alex
该问题针对的是Android系统,最好是针对非root用户的设备.尽管Android为Java应用程序提供了高级分析技术,但用于本机代码(C ++)的工具却受到限制. Android上的 top 命令允许显示系统中运行的所有线程(Java线程和C ++线程)的统计信息.我正在寻找对以下任务有帮助的答案:
The question refers to Android system, and preferably to a non-rooted device. While Android provides advanced profiling techniques for Java applications, tools for native code (C++) are limited. top command on Android allows to show the statistics for all threads running in the system, both Java threads and C++ threads. I am looking for an answer that will help with the following quest:
我的应用在后台处于非活动状态时使用2%的CPU,但应低于0.1%.如果我运行 top -t
,则属于我的进程的所有15个线程都将获得0%(某些线程是Java线程,例如Main,UI线程;其他线程是从不附加到JVM的p线程) .我怎么猜哪个线程会吃掉电池?
我很乐意获得有关此意外活动的更多详细信息,Android提供了诸如 TraceView (用于Java线程).对于本机代码工具的任何见解将受到高度赞赏.
I would be glad to get even more details about this unexpected activity, and Android provides great helpers like TraceView for Java threads. Any insight regarding tools for native code will be highly appreciated.
推荐答案
您在文章中没有提到它,但是在注释中您说您确实需要每个线程而不是每个进程的CPU利用率.
You didn't mention it in your post, but in the comment you said that you really need CPU utilization per thread, not per process.
If you can't find a tool that's accurate enough, you can look directly in /proc/[pid]/task/[ThreadName]
as described in the man page for /proc
. This gives total CPU time consumed in "clock ticks" since execution began. Getting better resolution than this is probably difficult or impossible.
修改
在OP的注释中,列出相关信息的命令是:
From the OP's comment, a command that lists the relevant information is:
adb shell cat /proc/${pid}/task/*/stat | awk -F\ '{print $1, $14}'
这只是将正确的/proc
文件提供给调试主机,调试主机运行一个很小的awk
程序以打印pid
和user time
的列.如果awk
不可用,您还可以轻松地使用cut -d " " -f1,14
或perl
中的类似内容来获取列.
This just cat
s the correct /proc
files to the debugging host, which runs a tiny awk
program to print the columns for pid
and user time
. You could also easily use cut -d " " -f1,14
or something similar in perl
to get the columns if awk
isn't available.
这篇关于如何获得更高的"CPU%"精度?比从TOP命令得到的还要多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!