If CPU is running 100% usage, other processes should be put inside a run queue. Which command can I use to get the size of the run-queue? vmstat seems to return a related value of the CPU like below:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 875128 576328 2147136    0    0     1     4    3   11  0  0 99  0  0

According to the manual, "r: The number of runnable processes (running or waiting for run time).", the column r indicates the number of processes including both running and wait processes. How can I just get the number the waiting processes?


The number of runnable processes is given by procs_running in /proc/stat:

awk '/procs_running/ { print $2 }' /proc/stat

Subtract the number of CPU threads available, stopping at 0, and you’ll get the number of scheduling units (processes or threads) waiting to be scheduled. You can determine the number of CPU threads available from /proc/stat too, using the cpu? lines. Overall:

awk '/cpu[^ ]/ { nb = substr($1, 4); if (nb > nbcpus) nbcpus = nb };
/procs_running/ { runqueue = $2 - nbcpus; if (runqueue < 0) runqueue = 0; print runqueue }' /proc/stat
| improve this answer | |
  • Is lscpu command used to get the number of available CPU threads? The calculation would be Thread(s) per core * Core(s) per socket * Socket(s)? – Joey Yi Zhao Feb 12 '18 at 8:28
  • You can look at the CPU(s) line from lscpu, or calculate the value from /proc/stat; see my update. – Stephen Kitt Feb 12 '18 at 8:48
  • According to this article: access.redhat.com/sites/default/files/attachments/… the definition of runnable is When a process is in a Runnable state, it means it has all the resources it needs to run, except that the CPU is not available. So in this case, I don't need to substract the number of CPU available threads, right? – Joey Yi Zhao Feb 13 '18 at 0:07
  • No, runnable in /proc/stat doesn’t distinguish between running and runnable, it looks at the task state; note that in the article you link to, both states are the same. – Stephen Kitt Feb 14 '18 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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