It is my (very basic) understanding that the CPU consumption in the header is percentage of total CPU (all CPUs/cores), whereas the %CPU
in the table is (by default) percentage per CPU/core.
This means that the sum of the percentages in the table divided by the number of CPUs/cores in your system should roughly match the CPU consumption in the header.
Further, the man page states:
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 a process is multi-threaded and top is not operating in
Threads mode, amounts greater than 100% may be reported. You toggle Threads mode with
the `H' interactive command.
Also for multi-processor environments, 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.
You can see in the last paragraph that if you press I
(Shift+i), you switch to a mode where the CPU time for each process is divided by the number of CPUs/cores. In this mode, the total of the table should match the number in the header.
Examples from my own system with 4 cores
Irix mode on (default) - sum in the table is roughly 4 times total in header.
![enter image description here](https://i.stack.imgur.com/HcIdP.png)
Irix mode off (Solaris mode) - sum in the table is roughly the same as in header.
![enter image description here](https://i.stack.imgur.com/YkUeY.png)
Also, see @terdon's answer as to why the numbers aren't exactly the same - and add to that rounding of fractions etc.
Disabling Irix mode in batch operation
To disable Irix mode in batch operation, do the following:
- Run
top
in normal mode
- Disable Irix mode with: Shift+i
- Save a configuration file for the current user: Shift+w
The configuration file will be located at $HOME/.config/procps
Now, when you run top for this user (including batch mode), Irix mode will be disabled.
To make this the default for the root
user, switch to root (with sudo -s
) and run the 3 steps above.
Since top is a relatively simple program, it's not possible to run with a custom config file. If you require custom config or CPU metrics, consider using another method than top output.