7

When reading /proc/stat, I get these return values:

cpu  20582190 643 1606363 658948861 509691 24 112555 0 0 0                      
cpu0 3408982 106 264219 81480207 19354 0 35 0 0 0                               
cpu1 3395441 116 265930 81509149 11129 0 30 0 0 0                               
cpu2 3411003 197 214515 81133228 418090 0 1911 0 0 0                            
cpu3 3478358 168 257604 81417703 30421 0 29 0 0 0                               
cpu4 1840706 20 155376 83328751 1564 0 7 0 0 0                                  
cpu5 1416488 15 171101 83410586 1645 13 108729 0 0 0                            
cpu6 1773002 7 133686 83346305 25666 10 1803 0 0 0                              
cpu7 1858207 10 143928 83322929 1819 0 8 0 0 0

Some sources state to read only the first four values to calculate CPU usage, while some sources say to read all the values.

Do I read only the first four values to calculate CPU utilization; the values user, nice, system, and idle? Or do I need all the values? Or not all, but more than four? Would I need iowait, irq, or softirq?

cpu  20582190 643 1606363

Versus the entire line.

cpu  20582190 643 1606363 658948861 509691 24 112555 0 0 0

Edits: Some sources also state that iowait is added into idle.

When calculating a specific process' CPU usage, does the method differ?

2
  • It depends on what you understand "CPU usage" to mean. You obviously know what all the columns are, so I'm guessing you've read the man page and know what they mean, too. What are you having trouble with? Commented Feb 24, 2013 at 2:13
  • I was originally confused on why some people said to only use the first four columns in calculating CPU utilization (percentage of thread usage 0-100%). Although to my current understanding, all columns are should be used, but I don't know if there's a situation like that one column is already added into another. I heard iowait is apparently pre-added to idle, but I am unsure. Commented Feb 24, 2013 at 2:30

1 Answer 1

3

The man page states that it varies with architecture, and also gives a couple of examples describing how they are different:

In Linux 2.6 this line includes three additional columns: ...

Since Linux 2.6.11, there is an eighth column, ...

Since Linux 2.6.24, there is a ninth column, ...

When "some people said to only use..." they were probably not taking these into account.

Regarding whether the calculation differs across CPUs: You will find lines related to "cpu", "cpu0", "cpu1", ... in /proc/stat. The "cpu" fields are all aggregates (not averages) of corresponding fields for the individual CPUs. You can check that for yourself with a simple awk one-liner.

cpu 84282 747 20805 1615949 44349 0 308 0 0 0

cpu0 26754 343 9611 375347 27092 0 301 0 0 0

cpu1 12707 56 2581 422198 5036 0 1 0 0 0

cpu2 33356 173 6160 394561 7508 0 4 0 0 0

cpu3 11464 174 2452 423841 4712 0 1 0 0 0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.