Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to get the CPU percentage for processes on a busy server out of ps, but it's giving me nonsensical numbers. Here's a truncated example:

$ ps -eo pcpu,pid | sort -n

%CPU   PID
...
36.5 26403
38.6 28295
39.0 11741
42.6 11736
58.6 15960
61.4 21989
63.0 21939
65.5 18017
66.0 20708
67.3 22040
73.2 31950
80.0 11740
83.6 22091
 109 16648
171851 22888
171851 22938
171856 23191
171858 23241
171866 24681
171875 26996
171895 30277
172376 26084
172449 25530

top reports a CPU% of 0.0 for the later pids. What's going on here? How can I convince ps to give me the right CPU% number here? As an alternative, is is possible to tell top -b -n1 to give me the full ARGV for the processes it's reporting? The man page is unilluminating.

EDITED TO ADD:

If I do a ps -eo pcpu,pid --sort -pcpu | head, I still get bad numbers:

%CPU   PID
343702 23762
343653 22570
 109 16648
83.6 22091
73.3 31950
67.3 22040
66.0 20708

EDITED FURTHER TO ADD:

The systems I've seen this on so far are both real hardware: x86_64 Debian Squeeze systems, one with the 2.6.32-5-amd64 kernel, and the other with 2.6.39-bpo.2-amd64. The former has 32 cores, the latter 12. One's an Opteron with 192GB RAM, the other a Xeon with 72GB.

The more I look at this, the more it looks like a simple bug in ps that shows high numbers for processes with very little activity, but I don't want to rely on that until I know more.

share|improve this question
    
Could you describe more about the environment your system is in? ps reads from the /proc filesystem and in chroots this could be problematic since that is shared with installations outside the chroot. Also, OpenVZ or container-based virtualization might be culprit here. And details about your OS, kernel and architecture are welcome too. –  gertvdijk Sep 13 '12 at 18:12
    
I've added a little more info. There's no chroots or virtualisation involved. –  regularfry Sep 14 '12 at 12:58

1 Answer 1

How can I convince ps to give me the right CPU% number here?

How about this:

ps -eo pcpu,user,pid,args --sort -pcpu | head

is is possible to tell top -b -n1 to give me the full ARGV for the processes it's reporting?

   -c : Command line/Program name toggle
        Starts top with the last remembered 'c' state reversed.  Thus, if top was displaying command lines,
        now that field will show program names, and visa versa.  See the 'c' interactive command for  addi‐
        tional information.
share|improve this answer
    
This also fails. I've edited the question to show the result. –  regularfry Sep 13 '12 at 15:52
    
Append args to see what does it say. –  quanta Sep 13 '12 at 16:00
    
re -c: this truncates the command line to the width of the terminal. Setting COLUMNS=$BIGNUM looks like the only option at the moment. –  regularfry Sep 13 '12 at 16:00
    
Well, this is fun. Turns out that even setting COLUMNS to a high number truncates the command line before the output I need is shown. –  regularfry Sep 13 '12 at 16:18
    
Please show us the output of ps -eo pcpu,user,pid,args --sort -pcpu | head? –  quanta Sep 13 '12 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.