1

Is there a command which can show CPU usage information (both for overall cpu and for individual cores), in a static way (i.e. just once) similar to how free shows memory usage information?

When we run top, the cpu information at the top part after hitting 1 to show individual core usage as well as overall cpu usage may be what I want. But top by default dynamically update the information, and output more information (memory and per process information) than I need.

Thanks.

3

There is a command to show processor related runtime statistics on the fly, called mpstat. On most distributions it is part of the sysstat package.

Whilst it has an output that could be a more direct answer to your question mpstat -P ALL -I CPU, I prefer the output of mpstat -P ALL, which is much more intelligible.

As an example, the output in a machine with 40 CPUs:

Linux 3.0.101-63-default    02/16/19    _x86_64_

09:52:11     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
09:52:11     all    0.04    0.00    0.04    0.02    0.00    0.00    0.00    0.00   99.90
09:52:11       0    0.13    0.00    0.13    0.57    0.00    0.00    0.00    0.00   99.16
09:52:11       1    0.24    0.00    0.49    0.07    0.00    0.00    0.00    0.00   99.21
09:52:11       2    0.04    0.00    0.03    0.00    0.00    0.00    0.00    0.00   99.94
09:52:11       3    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:52:11       4    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.97
09:52:11       5    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11       6    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11       7    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.97
09:52:11       8    0.01    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11       9    0.01    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11      10    0.12    0.00    0.10    0.19    0.00    0.00    0.00    0.00   99.59
09:52:11      11    0.09    0.00    0.06    0.01    0.00    0.00    0.00    0.00   99.84
09:52:11      12    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.94
09:52:11      13    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.94
09:52:11      14    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:52:11      15    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:52:11      16    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.95
09:52:11      17    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:52:11      18    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      19    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:52:11      20    0.06    0.00    0.06    0.01    0.00    0.00    0.00    0.00   99.87
09:52:11      21    0.19    0.00    0.27    0.01    0.00    0.00    0.00    0.00   99.54
09:52:11      22    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.97
09:52:11      23    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      24    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.97
09:52:11      25    0.02    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.97
09:52:11      26    0.01    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11      27    0.01    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11      28    0.01    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11      29    0.01    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
09:52:11      30    0.04    0.00    0.04    0.00    0.00    0.00    0.00    0.00   99.92
09:52:11      31    0.03    0.00    0.03    0.00    0.00    0.00    0.00    0.00   99.94
09:52:11      32    0.02    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      33    0.02    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      34    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      35    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      36    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      37    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      38    0.02    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.96
09:52:11      39    0.03    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.96

From man mpstat

The mpstat command writes to standard output activities for each available processor, processor 0 being the first one. Global average activities among all proces-sors are also reported. The mpstat command can be used both on SMP and UP machines, but in the latter, only global average activities will be printed. If no activity has been selected, then the default report is the CPU utilization report.

If you are interested on the topic, I recommend the book:

Systems Performance: Enterprise and the Cloud, Brendan Gregg

PS Whilst mpstat is quite useful for the short term, the companion sar utility that also gets installed with sysstat, ends up being more useful, for establishing patterns and a baseline over the long term.

corollary: Don't go cron with mpstat, use sar.

2
top -bn 1

After you set up your top rc;

top
1
W
q

grep what you want

top -bn 1 | grep -P "^(%|top)"

For output like

top - 11:40:33 up 24 days, 32 min,  1 user,  load average: 0.00, 0.00, 0.00
 %Cpu0  :  0.0 us,  1.2 sy,  0.0 ni, 98.1 id,  0.1 wa,  0.3 hi,  0.2 si,  0.0 st
 %Cpu1  :  0.0 us,  0.9 sy,  0.0 ni, 98.9 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
 %Cpu2  :  0.0 us,  0.8 sy,  0.0 ni, 99.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 %Cpu3  :  0.0 us,  0.8 sy,  0.0 ni, 99.1 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
 %Cpu4  :  2.0 us,  2.0 sy,  0.2 ni, 93.0 id,  2.3 wa,  0.2 hi,  0.3 si,  0.0 st
 %Cpu5  :  0.9 us,  3.0 sy,  0.1 ni, 94.8 id,  0.8 wa,  0.3 hi,  0.1 si,  0.0 st
 %Cpu6  :  0.2 us,  2.6 sy,  0.0 ni, 96.4 id,  0.4 wa,  0.2 hi,  0.1 si,  0.0 st
 %Cpu7  :  0.1 us,  1.0 sy,  0.1 ni, 98.4 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
  • Thanks. (1) How can I just get the CPU usage part, not memory part, not per process part? (2) can I also get both per core usage part, and overall cpu usage part? – Tim Feb 16 at 15:10
  • Thanks. On Lubuntu 18.04, top -bn 1 doesn't show individual cores – Tim Feb 16 at 16:45
  • @Tim then you missed the '1 W' part. – user1133275 Feb 16 at 18:12

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.