The top Command in Linux

Nilesh Katuwal Feb 02, 2024
  1. Breakdown of top Command in Linux
  2. Information About CPU
  3. Using top Command in Linux
The top Command in Linux

The top command in Linux is intended to assist users in determining which processes are active and which applications are consuming more memory or processing power than they should.

The top command is simple to use, but you should know the specifics. The load average is a problematic metric to evaluate a server’s performance.

Breakdown of top Command in Linux

Entering top in terminal:

$ top

Output:

top - 00:04:54 up  3:30,  1 user,  load average: 1.84, 3.02, 2.50
Tasks: 233 total,   2 running, 231 sleeping,   0 stopped,   0 zombie
%Cpu(s): 38.5 us,  9.5 sy,  0.0 ni, 51.6 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   3840.9 total,    277.3 free,   2286.3 used,   1277.3 buff/cache
MiB Swap:   2048.0 total,   1447.0 free,    601.0 used.    777.7 avail Mem 
  • The top - 00:04:54 up 3:30, 1 user,: The command displays the current system time, followed by the uptime, which is 3 hours and 30 minutes in this case, and finally, the number of users logged in to the system, which is 1 in this case. Via SSH, this can be done locally on the box, idle, on the screen, and so on.
  • The load average: 1.84, 3.02, 2.50: This section displays the load average, which can be a perplexing concept, especially when dealing with virtual machines and the cloud. The first number is the current load average, the second number is the five-minute average, and the third number is the 15-minute load average.
  • The Tasks: 233 total, 2 running, 231 sleeping, 0 stopped, 0 zombie: The number of applications appears when you type ps aux or a variation of its use

The number of ongoing processes indicates how much your CPU is currently being used. Because non-multithreaded applications can only use one CPU at a time, it’s common to see two methods occupying 50% of your CPU with a load average of 2 on a quad-core server.

The number of sleeping processes indicates operating but not actively used; this typically includes many background jobs, system software, printer drivers, and other such items.

Unless you delivered a SIGSTOP or kill-STOP to a process for troubleshooting, the number of stopped processes should be 0. A value different from 0 may cause concern on production systems.

Processes involving zombies indicate that a multi-threaded application created a child process that suddenly destroyed or terminated, leaving a zombie process.

If something terrible happens, Apache will most likely respond with a vengeance. In most cases, this should also be 0.

Information About CPU

%Cpu(s): 38.5 us, 9.5 sy, 0.0 ni, 51.6 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st

  • The percentage of time that each processor uses up to 100%. As a result, on a quad-core with one process taking 100% of one CPU, this will indicate 25% use. On an 8-core system, 12.5 percent means 1 core is pinned out.
  • The term percentage system CPU utilization refers to how much of the system’s CPU is used. Higher numbers here could suggest a problem with kernel configurations, a driver issue, or any number of other reasons.
  • The percentage ni denotes the percentage of CPU used by userland processes that have been influenced by the nice or renice commands, i.e., their priorities have been modified from the scheduler’s default to higher or lower. A positive number indicates a lower priority, while a negative number indicates a higher priority. The default value is 0, which suggests the scheduler makes the decision.
  • The result of subtracting the previous three figures from 100.0 percent is a percent id, which gauges idle processing power.
  • The wa is the % of I/O waiting by CPU. When a process or program needs data, it checks the processor caches first, then memory, and ultimately disk. It usually has to wait for the IO thread to finish reading the information into RAM before working on it again when it hits the disk. The slower the disk, the higher the IO Wait percentage for each process. If the wait time is consistently more significant than 100/(number of CPUs * number of processes), we may have a storage issue that needs to be monitored. Check this number first if you detect a high load average. If this value is high, the processes on your disk are bottlenecked.
  • The si is the time spent servicing software interrupts. Rather than a piece of hardware or a device (driver) requesting an interrupt on the motherboard’s interrupt line, the Linux kernel includes a capability in version 2.4 that allows the software to request an interrupt and the kernel to handle it via its interrupt handler. It means that an application can ask for priority status, the kernel can acknowledge receipt of the request, and the software will patiently wait for the interrupt to be addressed.
  • The st refers to time stolen by virtual machines. We don’t use VMs to limit the CPU, so letting a VM or two use 8 CPUs on occasion won’t harm the pool as a whole. If the number of physicals (or logical CPUs in the case of hyperthreaded xeons) CPU utilized by VMs’ virtual CPUs is greater than the number of physicals, steal time will increase.

Using top Command in Linux

To show top command syntax:

$ top -h

Output:

procps-ng 3.3.16
Usage:
  top -hv | -bcEHiOSs1 -d secs -n max -u|U user -p pid(s) -o field -w [cols]

You can use the top command in a secure mode.

$ top -s

Related Article - Linux Command