How to get %CPU usage of a process
Hi there,
I need to get %CPU usage of process , I tried following method on Redhat 7.3 Linux 2.4.18-3 1. read /proc/<PID>/stat (utime) entry and save in val1 2. read same /proc/<PID>/stat (utime) entry after 5 seconds and store in val2 3. calculate by using (val2 - val1) / 500 (since jiffies is 1/100 of second). Does this make any sense. But when I look at command output of ps -auxw, %CPU usage is about 50-60%(for eg) for daemons (i.e., <PID> above method). This is a different value than I calculated using above method. Please let me know 1. if above method is not correct, let me know correct method to calculate %CPU usage of process. 2. Is there any IOCTL calls to process management in Linux where i can get automated value of %CPU usage. Thanks in Advance. -KOTRESH.D |
help
To wiev your usage of procesor use /sbin/top utility.
Or simply use ps Or simply install Linux Slackware :-) |
typing the "top" command should give you all of this information.
this is from the man pages: NAME top - show system usage statistics SYNOPSIS top [-u] [-w] [-k] [-s interval] [-e | -d | -a] [-l samples] [number] DESCRIPTION top displays an ongoing sample of system usage statistics. It operates in various modes, but by default shows cpu utilization and memory usage for each process in the system. |
Hello..
Thanks for your replies. But I need to get the value of %CPU usage of process specified by PID programmatically not from command line. My above method is in reference to linux mailing lists and has drawbacks of sleeping for 5 seconds. I have other idea for this using popen() and fread(). for eg command name --> xxx, its PID --> yyy. FILE * fp;char buf[1024]; char * cmd =" ps -C xxx -o yyy=-eo\"%C %x %t %a\""; fp = popen(cmd,"r"); fread(buf,1,sizeof(buf),fp); Now buf has instant value of %CPU usage.Is this OK.. But I feel using command strings in application programs is not good practice. Using API s or IOCTL is considered as good practice. Is there any other idea??. TIA. Kotresh. |
Hi Kotresh,
I'm also working on to find %CPU usage of a particular process. My problem is just a little more complex, i'm trying to find Peak CPU usage and average CPU usage of a particular process. I also want to do it programmatically and not from command line. So did u ever figured out how to do it, if yes, could you share the solution with me. Thanks, Romy |
The third column of the output below is the CPU utilization (expressed as a percent):
Code:
$ ps aux | grep " <pid> " | grep -v grep Example: My X server, pid 5562, is currently using 1.2% of the CPU cycles (and 2.5% of memory - the fourth column - if you care about that): Code:
$ ps aux | grep " 5562 " | grep -v grep |
CPU Utilization - C program ?
Hello,
have you done something with C programming %CPU utilization? I need to know how to do it, "top" source code is so big for me... For the moment i'm using ps to get it, output it and parse it later to extract cpu and memory utilization, but ps is not so accurate as i hoped. I mean there are BIG differences between output of ps and top in cpu utilization...Don't know what to do. Thanks and regards, Andrei |
in FEDORACORE they have the option called "SYSTEM MONITER" : i dont acatly remember where though
|
Quote:
that's the problem, i need something that should run in the background on all UNIX platforms ( AIX, HPUX, Solaris, MacOSX and Linux ), at least one version of file ( script or c code ) for each of these platforms. I have examples were ps -eo comm,pcpu | grep program_name is returning something like : program_name 1 ( that means 1% CPU utilization, isn't so? ) and when top -b -n 1 | grep program_name it returns ~60% in CPU utilization..... That's why i think ( looked over top3.5 source code ) that "top" is more accurate then "ps". Please if somebody has an opinion ( with examples ofcourse :), post it! Thanks and regards to all, AJ |
Quote:
|
script
Hi all.
I've writted a little script to control the cpu usage by a process. Here's the code: ----------------------------------------- Code:
#!/bin/bash I hope it may be useful. I would like to receive your comments. P.S.: Sorry, I haven't translated it to english yet. Regards. _slap |
script translation
Well, here's the translation to english.
Code:
#!/bin/bash |
You all can use
top -p <processid1> -p <processid1> to monitor specific processes ;) |
thanx guys for the help
:) |
All times are GMT -5. The time now is 03:58 AM. |