ps and top Commands are giving Different %CPU Utilization for the Same Job
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ps and top Commands are giving Different %CPU Utilization for the Same Job
Hi Guys,
I am getting two different %CPU values for a process / PID when I issue top and ps commands:
Command: top -p 11859
Output:
Code:
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND
11859 xyzprod 25 0 246M 246M 24084 S 0.0 4.9 0:02 fiaccad
Command: ps -up 11859
Output:
Code:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
xyzprod 11859 104 4.9 561088 252780 ? S 09:05 109:02 /app/fiaccad
As you can see above the "top" displays 0.0 in the %CPU column whereas "ps" reports "104" in its %CPU column for the same process / command running.
Our monitoring tool (may be, Netcool) is generating alerts whose alarm value (as seen in the ps' output above) matches with the output of the "ps" command but not with that of "top" command.
So, which one is correct? Which output shall I consider / disregard? Besdies why the difference is there?
Please Note: I checked the same job on some other boxes where it is running and found the same behaviour with respect to "top" and "ps" commands' output.
that is interesting, I can't give you the 100% answer as I don't remember the info of the top of my head and a google search didn't help. I remember dealing with something similar but different in regards to utilization being read incorrectly. After dealing with Red Hat support it came down to the way the ps and top read the /proc/pid/ files to determine the utilization, they determine it in a different way.
I hope it gets you in the right direction, sorry I couldn't be of more help but I'll keep doing some googling to see if I can track it down.
CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.
Top is not necessarily more accurate. Depends on what you are looking for regarding process information. PS is a good way to tell what a process has been doing over its life. Top is the point in time good for looking short term, or at short running processes.
PS is good for looking at longer running processes and getting an idea of how the process is impacting the server long term.
Just depends on the view of the processes you need to have.
I must clear my point here. Netcool, for example, sends me an alert saying that "Job xyz PID 1234 is utilizing 85% of CPU" and I look up the PID 1234 by running "top" and do not see it on the top of the top's output. (I hit C to sort on CPU Utilisation.) I run "top -p 1234" and see "%CPU 0.0". ps tells me "90%".
I observe the trends shown by top and see CPU Utilisation upto 80% and 95% (fluctuating around those figures).
Now put yourself in my situation. Would you go ahead and close the ticket by logging the output from top or would you go ahead and ask the job's owner to kill the job or otherwise look into it?
As pointed out by Kustom42 with respect to ps: "CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process." In that case, a particular job should not be the culprit which is escalating the CPU utilisation because ps is not telling us that the job in question is eating up 85% of CPU's brain at this moment. Though it has done so during the las couple of minutes or hours as the case may be. Whereas top reports that the job is innocent. As pointed out by grim76: "Top is not necessarily more accurate."
Well, that post does not lead us to any conclusion. I have seen your contribution to it, though.
We want to see the Current Status of CPU Utilisation By a Given Process. We need an exact answer to that problem. I have run the top command in batch mode:
Code:
top -b -n 1
and the output is "%CPU 0.0" for that particular PID. In batch mode, the top command should not be showing the commulative differences.
top -b -n 1 will measure the current load and it may depend even on the fact: which task is currently on a CPU (the top itself). In this case a sleeping process may have 0.0 load.
ps will give you something like an average, it measured from the start of the process. The higher average means the real higher load. Also, on a system with 8 CPUs (for example) the 104% load is not a blocking issue.
So if I were you I should run top several times, and check the max, min and average values for the given process.
ps will give you something like an average, it measured from the start of the process. The higher average means the real higher load. Also, on a system with 8 CPUs (for example) the 104% load is not a blocking issue.
So if I were you I should run top several times, and check the max, min and average values for the given process.
Agreed!
I just noticed one more interesting thing that when I run the top command as:
Code:
top -p 1234
(where 1234 is the PID of the process) top shows "5.4" (for example) for the same process / PID whereas running top without any options, top is showing "24.5". Interestingly this time I do not see any output by running ps as:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.