82

I am using freescale IMX6 quad processor. I want to know if the top command lists the CPU usage of all 4 cores or of a single core. I am seeing an application's CPU usage being the same with 4 cores and with a single core. I was guessing the CPU usage by the application will increase on a single core and decrease on 4 cores but it has not changed.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
user3818847
  • 1,171
  • 22
    try pressing 1 while top is running – Dani_l Jul 23 '14 at 10:57
  • 1
    Could you please elaborate. As in what happens if I press 1. Because I am getting this inconsistent result since 2 days. – user3818847 Jul 23 '14 at 10:59
  • you may find this link useful: http://unix.stackexchange.com/questions/41311/cpu-and-core-usage-stats – TPS Jul 23 '14 at 11:05
  • 1
    Which application is that? Why do you think your application should use multiple cores if available? There are many applications out there that work on a single CPU/core and for which nobody bothered to take the time to parallize them. – Anthon Jul 23 '14 at 11:05
  • @Anthon well that's the question, if top gives you a percentage of 4core or a percentage of a core, because if the percentage comes from 4 cores, then the value (even with a non threaded app should be 4 time fewer – Kiwy Jul 23 '14 at 11:27
  • 4
    no. if multiple cores, they accumulate to over 100%. 4 cores can get as high as 800% with hyperthreading on each core – Dani_l Jul 23 '14 at 11:35
  • 1
    I like to use htop for this. – Richard Apr 25 '17 at 01:34
  • pressing 1 does not help is I grep a specific process. i.e top | grep followed by 1 does not show cpu core info – Naveen Jan 02 '19 at 18:23
  • Then top | awk '/(Cpu|<proc-Name>)/' will show you Cpu and that one process. Then press 1 to list Cpus separately. But top is not designed for piping. You can specify list of pids (by number) on command line. But Cpu is still total of all processes, not just the ones you list. – Paul_Pedant Jul 18 '20 at 21:54
  • related post https://stackoverflow.com/questions/3342889/how-do-i-measure-separate-cpu-core-usage-for-a-process – red0ct May 31 '23 at 15:21

3 Answers3

111

I'm not entirely sure what you're asking here. Yes, top shows CPU usage as a percentage of a single CPU by default. That's why you can have percentages that are >100. On a system with 4 cores, you can see up to 400% CPU usage.

You can change this behavior by pressing I (that's Shift + i and toggles "Irix mode") while top is running. That will cause it to show the pecentage of available CPU power being used. As explained in man top:

    1. %CPU  --  CPU Usage
       The task's share of the elapsed CPU time since the last screen
       update, expressed as a percentage of total  CPU  time.   In  a
       true  SMP environment, if 'Irix mode' is Off, top will operate
       in 'Solaris mode' where a task's cpu usage will be divided  by
       the  total  number  of  CPUs.  You toggle 'Irix/Solaris' modes
       with the 'I' interactive command.

Alternatively, you can press 1 which will show you a breakdown of CPU usage per CPU:

top - 13:12:58 up 21:11, 17 users,  load average: 0.69, 0.50, 0.43
Tasks: 248 total,   3 running, 244 sleeping,   0 stopped,   1 zombie
%Cpu0  : 33.3 us, 33.3 sy,  0.0 ni, 33.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  : 16.7 us,  0.0 sy,  0.0 ni, 83.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 60.0 us,  0.0 sy,  0.0 ni, 40.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   8186416 total,  6267232 used,  1919184 free,   298832 buffers
KiB Swap:  8191996 total,        0 used,  8191996 free,  2833308 cached
terdon
  • 242,166
  • 2
    with hyperthread I believe you can see up to 800% as /proc/cpuinfo will show each thread as a cpu – Dani_l Jul 23 '14 at 11:37
  • 2
    @Dani_l yes, whether the "core" is physical or virtual is irrelevant, it is treated as a "CPU" by top. The output I show is from my laptop which has a single physical CPU with two cores, each of which has a 2nd logical core. The result is that top sees 4 cores. – terdon Jul 23 '14 at 11:44
  • Sorry for the nitpicking, in my dayjob we have to distinguish between sockets, cores and threads when reserving resources. I guess the habit stuck. – Dani_l Jul 23 '14 at 11:48
  • so is it correct to assume that if 'nvproc --all' returns 20 that the CPU % reported by htop per-process should be divided by 20 to get the actual percentage of total CPU capability available? e.g. htop reporting 54.2% for a process in this context would actually represent 2.71% total CPU usage by that process? – CCJ Dec 10 '20 at 01:57
  • 1
    @CCJ htop should show a breakdown by CPU anyway (this was about top), but yes, in the percentage column you should divide by the number of available cores. – terdon Dec 10 '20 at 14:24
16

If you're wanting to open top immediately displaying separate CPUs without needing to press 1, you can use the -1 option.

e.g.:

top -1

...
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 44.7 us, 55.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 46.7 us, 53.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
...     

Note: This works on Debian, but the variant of top installed may vary depending on distro.

6

If you want top command to display per CPU usage everytime you run top.

  • run top command
  • press 1, this will display per CPU usage
  • Type W and press Enter to save configuration to file
  • Next time running top will display per CPU usage.
  • This way we can configure top to our custom requirement

(Above steps works with top version procps-ng 3.3.12)