通过PID获取进程的CPU使用率(顶级源代码)
如果我只知道 PID,如何获取进程的 CPU 使用率? MAXOS X
我浏览了 MACOSX 的顶级实用程序源代码, 我在 libtop.c 中发现了几个重要的函数,
/* Iterate through all processes and update their statistics. */
libtop_p_proc_table_read(boolean_t reg)
libtop_p_task_update()
/* Get CPU usage statistics. */
libtop_pinfo_update_cpu_usage()
问题是我不明白它们如何从如此巨大的数量中获取%CPU使用率 MACOS 特定的“mach 内核”系统调用。 有人有解决方案吗?
我是他们获得 system_time、user_time、total_time 的来源。
我的total_time是从进程开始的时间还是什么? 或者total_time 可能等于1 秒。
例如我的结果:对于 Opera 浏览器:
pid:1214 用户:653.517582sec 系统:193.597306sec 总计:847.114888sec
来自顶级实用程序的正确信息:
PID COMMAND %CPU TIME
1214- Opera 8.0 14:04.52
我不明白如何将结果转换为 8.0%。 这里的总时间是用户时间和系统时间的总和。 总时间正确:847 秒约为 14 分 04 秒 为了获得CPU使用百分比,我需要诸如所有进程的空闲时间之类的东西。
我已经花了一整天但没有任何预付款。
How can I get CPU usage for process if i know only PID?
MAXOS X
I looked through top utility source codes for MACOSX and
I have found several important functions in libtop.c
/* Iterate through all processes and update their statistics. */
libtop_p_proc_table_read(boolean_t reg)
libtop_p_task_update()
/* Get CPU usage statistics. */
libtop_pinfo_update_cpu_usage()
The problem is that I don't understand how they get %CPU usage from this huge amout of
MACOS specific "mach kernel" system calls.
Does anybody have solution for this?
I the source they get system_time, user_time , total_time.
I total_time is time from process start or what?
Or may be total_time is equal 1 sec.
For example my results: for Opera Browser:
pid:1214 user:653.517582sec system:193.597306sec total:847.114888sec
Correct info from top utility:
PID COMMAND %CPU TIME
1214- Opera 8.0 14:04.52
I dont understand how to convert my results to 8.0%.
Total time here is a sum of user time and system time.
Total time is correct: 847sec is approx 14min 04 sec
To get cpu usage percent i need something like idle time for all process.
I have already spend the whole day but without any advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 https://chromium.googlesource 中的 GetCPUUsage() .com/chromium/src/+/master/base/process/process_metrics_mac.cc 了解如何在 Chrome 的任务管理器中实现这一点。
Look at GetCPUUsage() in https://chromium.googlesource.com/chromium/src/+/master/base/process/process_metrics_mac.cc for how this is implemented in chrome's task manager.
从这些函数中获得的时间是该进程使用 cpu 的时间。
你应该考虑到cpu一次只能被一个进程使用(忽略多核cpu)。
因此,要获得百分比值,您必须第一次询问,然后等待,最后询问第二次。
%CPU = (TIME2 - TIME1)/Waited_time;
如果您等待一秒钟,您已经拥有了良好的 %CPU
如果您有多个 CPU,则可能需要除以 cpu/每个 cpu 核心的数量
The time that you get from theses function, are the times that this process used the cpu.
You should consider that the cpu can only be used by one process at a time (ignore multi core cpu).
So to get a value in percent, you have to ask a first time, then wait, and finally ask a second time.
and %CPU = (TIME2 - TIME1)/Waited_time;
if you wait exactly a second you already have the good %CPU
If you have more than one CPU, you may need the divide by the number of cpu / core per cpu
在这种情况下,我尝试不限制 PID。但如果你知道 PID,那就更容易了。
输出:
2.8 207 user1 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome -psn_0_20485
要从 Java 程序中使用它,您可以尝试:
In this case, I tried not to limit to a PID. But if you know the PID, it is even easier.
Output:
2.8 207 user1 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome -psn_0_20485
To use it from a Java program here is what you can try:
看看top.c如何实现按CPU使用率排序。 Total_time - p_total_time 给出进程在最后一个采样间隔内使用的 CPU 时间。将其除以每个过程的差异之和,即可得到百分比。
顺便说一句,当在 Mac 上使用 Mac OS X 阅读 Mac OS X 文档和 Mac OS X 源代码时,您可能已经注意到它们始终拼写为“Mac”和“Mac OS X”。您可能认为他们只是将自己的操作系统名称拼错了几百万次,但事实上,尽管听起来很奇怪,“Mac OS”才是正确的拼写,而不是“MACOS”。 Mac 不是缩写词。
Look at how top.c implements sorting by CPU usage. total_time - p_total_time gives the CPU time used by the process over the last sampling interval. Divide this by the sum of this difference for each process and you've got your percent.
BTW, while using Mac OS X on your Mac to read Mac OS X documentation and Mac OS X source code, you may have noticed that they consistently spell it "Mac" and "Mac OS X". You probably thought they just misspelled the name of their own OS a couple million times, but in fact, as strange as it may sound, "Mac OS" is the correct spelling, not "MACOS". Mac is not an acronym.