Linux 如何获得比 TOP 命令更高的“CPU%”精度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15148193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get higher precision of "CPU%" than that from TOP command?
提问by HymanWM
When I use TOP command, I could get the following info:
当我使用 TOP 命令时,我可以获得以下信息:
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
We can see the CPU%
is round to integer, is there any way I could get a process's CPU%
with higher precision?
我们可以看到CPU%
舍入到整数,有什么办法可以获得CPU%
更高精度的进程?
-- Clarifications on the bounty --Alex
-- 关于赏金的说明 --亚历克斯
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. topcommand 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:
该问题是指 Android 系统,最好是非 root 设备。虽然 Android 为 Java 应用程序提供了先进的分析技术,但用于本机代码 (C++) 的工具是有限的。Android 上的top命令允许显示系统中运行的所有线程的统计信息,包括 Java 线程和 C++ 线程。我正在寻找有助于以下任务的答案:
My app uses 2% CPU when it is inactive in background, while it should be below 0.1%. If I run top -t
, I get 0% for all 15 threads that belong to my process (some threads are Java threads, e.g. the Main, UI thread; others are pthreads that never attach to JVM). How can I guess which thread eats the battery?
我的应用程序在后台处于非活动状态时使用 2% 的 CPU,而它应该低于 0.1%。如果我运行top -t
,我会得到属于我的进程的所有 15 个线程的 0%(一些线程是 Java 线程,例如 Main、UI 线程;其他线程是从不附加到 JVM 的 pthread)。我怎么能猜出哪个线程吃电池?
I would be glad to get even more details about this unexpected activity, and Android provides great helpers like TraceViewfor Java threads. Any insight regarding tools for native code will be highly appreciated.
我很高兴获得关于这个意外活动的更多细节,Android为 Java 线程提供了像TraceView这样的好帮手。任何有关本机代码工具的见解都将受到高度赞赏。
采纳答案by Gene
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.
您没有在帖子中提到它,但在评论中您说您确实需要每个线程而不是每个进程的 CPU 利用率。
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.
如果您找不到足够准确的工具,您可以/proc/[pid]/task/[ThreadName]
按照的手册页/proc
中所述直接查看。这给出了自执行开始以来在“时钟滴答”中消耗的总 CPU 时间。获得比这更好的分辨率可能很困难或不可能。
Edit
编辑
From the OP's comment, a command that lists the relevant information is:
根据 OP 的评论,列出相关信息的命令是:
adb shell cat /proc/${pid}/task/*/stat | awk -F\ '{print , }'
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.
这只是cat
S上的正确/proc
文件到调试主机,它运行一个小awk
程序打印列pid
和user time
。如果不可用,您也可以轻松使用cut -d " " -f1,14
或类似的东西perl
来获取列awk
。
回答by Alberto Megía
Try this:
尝试这个:
ps -eo pcpu,pid,user,args | sort -r -k1 | less
%CPU PID USER COMMAND
9.0 2721 user bash
1.4 956 root ...
0.5 2212 user ...
EDIT:
编辑:
You can use adb shell and busybox (http://www.busybox.net/downloads/BusyBox.html)
您可以使用 adb shell 和 busybox ( http://www.busybox.net/downloads/BusyBox.html)
adb shell busybox top
adb shell busybox top
c:\ adb push busybox /system/bin
c:\ adb shell
# busybox top
CPU: 2.3% usr 3.1% sys 3.9% nic 90.5% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 1.06 1.66 10.63 1/589 8048
←[7m PID PPID USER STAT VSZ %MEM CPU %CPU COMMAND←[0m
31619 2180 10112 S 217m 67.0 0 3.8 com.mgeek.android.DolphinBrowser.B
2232 2180 1000 S 551m169.6 0 2.6 system_server
8038 8037 0 R 2068 0.6 0 0.8 busybox top
2178 1 0 S 11092 3.3 0 0.6 /system/bin/drexe
6812 2180 10104 S 199m 61.2 0 0.5 android.tether
2291 2180 1001 S 324m 99.8 0 0.3 com.android.phone
2308 2180 10006 S 325m100.0 0 0.1 com.sec.android.app.dialertab
2177 1 1001 S 9624 2.8 0 0.1 /system/bin/rild
5 2 0 SW< 0 0.0 0 0.1 [events/0]
30087 2180 10022 S 358m110.4 0 0.0 com.samsung.vvm
2304 2180 10006 S 311m 96.0 0 0.0 com.sec.android.app.twlauncher
16110 2180 10006 S 296m 91.3 0 0.0 android.process.acore
2445 2180 10006 S 272m 83.8 0 0.0 com.sec.android.provider.logsprovi
8064 2180 10002 S 238m 73.4 0 0.0 com.google.process.gapps
31537 2180 10037 S 227m 69.9 0 0.0 com.google.android.gm
2288 2180 10048 S 221m 68.1 0 0.0 com.swype.android.inputmethod
2285 2180 10013 S 215m 66.3 0 0.0 com.tat.livewallpaper.aurora
30664 2180 10011 S 213m 65.8 0 0.0 com.android.email
31191 2180 10099 S 209m 64.4 0 0.0 com.sirma.mobile.bible.android
2377 2180 10087 S 207m 63.9 0 0.0 android.tts
(Taken from here)
(取自这里)
回答by g00dy
Got this information from another thread:
从另一个线程获取此信息:
3) Getting CPU info
3)获取CPU信息
~$ adb shell dumpsys cpuinfo
~$ adb shell dumpsys cpuinfo
Output:
输出:
Load: 0.08 / 0.4 / 0.64 CPU usage from 42816ms to 34683ms ago: system_server: 1% = 1% user + 0% kernel / faults: 16 minor kdebuglog.sh: 0% = 0% user + 0% kernel / faults: 160 minor tiwlan_wq: 0% = 0% user + 0% kernel usb_mass_storag: 0% = 0% user + 0% kernel pvr_workqueue: 0% = 0% user + 0% kernel +sleep: 0% = 0% user + 0% kernel +sleep: 0% = 0% user + 0% kernel TOTAL: 6% = 1% user + 3% kernel + 0% irq
负载:0.08 / 0.4 / 0.64 CPU 使用率从 42816 毫秒到 34683 毫秒前:system_server:1% = 1% 用户 + 0% 内核/故障:16 次要 kdebuglog.sh:0% = 0% 用户 + 0% 内核/故障:160次要 tiwlan_wq: 0% = 0% 用户 + 0% 内核 usb_mass_storag: 0% = 0% 用户 + 0% 内核 pvr_workqueue: 0% = 0% 用户 + 0% 内核 +sleep: 0% = 0% 用户 + 0% 内核+sleep:0% = 0% 用户 + 0% 内核总计:6% = 1% 用户 + 3% 内核 + 0% irq
EDIT:
编辑:
You can also try this command: echo $(adb shell ps | grep com.android.phone | awk '{ system("adb shell cat /proc/" $2 "/stat");}' | awk '{print $14+$15;}')
你也可以试试这个命令: echo $(adb shell ps | grep com.android.phone | awk '{ system("adb shell cat /proc/" $2 "/stat");}' | awk '{print $14+$15;}')
Also:
还:
using top : This will show you the cpu stats
top -b -n 1 |grep ^Cpu
使用 top :这将显示 CPU 统计信息
top -b -n 1 |grep ^Cpu
using ps: This will show you the % cpu usage for each process.
ps -eo pcpu,pid,user,args | sort -r -k1 | less
using ps: 这将显示每个进程的 cpu 使用百分比。
ps -eo pcpu,pid,user,args | sort -r -k1 | less
EDIT2:
编辑2:
In realtion to your comments and the bounty description (How can I guess which thread eats the battery?) I found an interesting page:
关于你的评论和赏金描述(我怎么能猜出哪个线程吃电池?)我发现了一个有趣的页面:
http://ziyang.eecs.umich.edu/projects/powertutor/
http://ziyang.eecs.umich.edu/projects/powertutor/
As stated there:
正如那里所说:
You can use PowerTutor to monitor the power consumption of any application.
您可以使用 PowerTutor 监控任何应用程序的功耗。
Try this for an instance and see if it meets your requirements.
以一个实例试试这个,看看它是否满足您的要求。
FINAL EDIT:
最终编辑:
Check out the Systrace documentation on the developer.android.com site:
查看 developer.android.com 站点上的 Systrace 文档:
http://developer.android.com/tools/debugging/systrace.htmlhttp://developer.android.com/tools/help/systrace.html
http://developer.android.com/tools/debugging/systrace.html http://developer.android.com/tools/help/systrace.html
I'm sorry if you already tried that, but that's one concrete method to measure the performance.
如果您已经尝试过,我很抱歉,但这是衡量性能的一种具体方法。
回答by Ken Wolf
Use DDMSand method profiling to get a TraceView.
使用DDMS和方法分析来获取TraceView。
Basically:
基本上:
- Launch your app in debug mode
- In DDMS, in the Devices tab, click "Start method profiling"
- Do stuff on your device to recreate the conditions you want to monitor
- Click "Stop method profiling"
- You'll get a fairly detailed graph with each thread's execution that you can drill down into
- 在调试模式下启动您的应用
- 在 DDMS 中,在“设备”选项卡中,单击“启动方法分析”
- 在你的设备上做一些事情来重新创建你想要监控的条件
- 单击“停止方法分析”
- 您将获得每个线程执行的相当详细的图表,您可以深入查看
More details here: http://developer.android.com/tools/debugging/debugging-tracing.html
更多细节在这里:http: //developer.android.com/tools/debugging/debugging-tracing.html
Disclaimer: I've only done this with a simple test app so I don't know how much mileage you'll get out of it. It does seem to give a bit more precision than what has been described so far, and does not require root.
免责声明:我只用一个简单的测试应用程序完成了这项工作,所以我不知道你会从中获得多少里程。它似乎比目前所描述的更精确,并且不需要 root。