开发者

get CPU usage for process by PID (top source code)

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 funct开发者_Go百科ions 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.


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.


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


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();
        }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜