通过PID获取进程的CPU使用率(顶级源代码)

发布于 2024-10-18 21:48:30 字数 820 浏览 4 评论 0原文

如果我只知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情归归情 2024-10-25 21:48:30

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.

遗弃M 2024-10-25 21:48:30

从这些函数中获得的时间是该进程使用 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

青巷忧颜 2024-10-25 21:48:30

如果我只知道 PID,如何获取进程的 CPU 使用率? MAXOS X

在这种情况下,我尝试不限制 PID。但如果你知道 PID,那就更容易了。

ps -aeo pcpu,pid,user,args|tail -n +2|sort -r|head -n 1

输出:

2.8 207 user1 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome -psn_0_20485

要从 Java 程序中使用它,您可以尝试:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TopProcess {

    public static void main(String[] args) throws IOException {
        final String[] cmd = { "/bin/sh", "-c", "ps -aeo pcpu,pid,user,args|tail -n +2|sort -r|head -n 1" };
        String process;
        BufferedReader input = null;
        try{
            Process p = Runtime.getRuntime().exec(cmd); 
            input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((process = input.readLine()) != null) {
                System.out.println(process);
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        finally {
            input.close();
        }
    }
}

How can I get CPU usage for process if i know only PID? MAXOS X

In this case, I tried not to limit to a PID. But if you know the PID, it is even easier.

ps -aeo pcpu,pid,user,args|tail -n +2|sort -r|head -n 1

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:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TopProcess {

    public static void main(String[] args) throws IOException {
        final String[] cmd = { "/bin/sh", "-c", "ps -aeo pcpu,pid,user,args|tail -n +2|sort -r|head -n 1" };
        String process;
        BufferedReader input = null;
        try{
            Process p = Runtime.getRuntime().exec(cmd); 
            input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((process = input.readLine()) != null) {
                System.out.println(process);
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        finally {
            input.close();
        }
    }
}
夜夜流光相皎洁 2024-10-25 21:48:30

看看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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文