htop is a sophisticated terminal-based system monitoring tool that displays statistics on various system processes and their resource usage in a human-readable, color-coded, and simple-to-use style. htop is a substitute for, or rather a complement to, the well-known, tried-and-true "top," adding features like mouse support, direct process termination, whilst providing a more human-readable output. It allows admins to monitor system processes as well as their entire command lines to determine the cause of load by each process
htop is easy to install because it is available in most major distributions' standard repositories, its as simple to install as:
On any Debian based derivative:
sudo apt-get install htop –y
On any Redhat based derivative:
sudo yum install htop –y
How does one run htop?
Simple, just type in htop and hit enter.
The user interface explained
There are four sections to the user interface.
- You receive bar-like interactive gauges that you may customise in section (1).
- A fast overview of running tasks, load average, and uptime in section (2).
- A thorough view of running processes in section (3).
- A quick list of the most frequent keyboard shortcuts, which you can access with a mouse in section (4).
What does it all mean?
Section 1: Processors and Memory
CPU and memory resources are displayed here. In this example we can see that this is a 4 core server by looking at the numbers 0 through 3. A five-core will have five bars, a six-core will have 6 bars, and so forth. One CPU is represented by each number/bar. On the right-hand side of each bar is a percentage that indicates how much CPU is being used.
Colour coding is used to categorise CPU utilisation by processes:
- Blue: The % of CPU used by low priority processes
- Green: The % of CPU used for user processes
- Red: The % of CPU used by system/kernel processes
- Yellow/Orange: The % of CPU used by IRQ time
- Magenta: The % of CPU used by Soft IRQ time
- Grey: The % of CPU by IO Wait time
- Cyan: The % of CPU by Steal
What we want to identify is how busy the CPU/s are. The bars in this scenario are generally empty, indicating that the CPU is mostly idle. Generally, the more CPU you utilise, the busier your system becomes, affecting the overall performance of your server.
High CPU usage is not really indicative of an issue, yet continuously high CPU usage is something that merits attention.
RAM AND SWAP UTILISATION
This section displays information on the system's memory use. We have RAM and Swap - Swap is a component of your hard drive that your server uses similarly to RAM. You can clearly see that only 383MB of RAM is being utilised in this scenario.
RAM usage is displayed by colors:
Green: Used memory pages
Blue: Buffer pages
Yellow: Cache pages
If you run out of RAM, you can fall back on swap space. High swap and RAM use will have an impact on your system's capacity to handle tasks effectively, leading to poor overall performance. Per this scenario, you can see I have 0 swap space set up denoted by 0K/0K.
Section 2 – Processes & Load Average
Processes are essentially tasks, as you can see there are 33 processes in total, with 1 presently running. 18 "thr" here stands for "threads." So we have 33 processes that are divided into 18 threads.
Load average / (proc_q_length) , is the average system load calculated over one, five, and fifteen minutes, denoted by the numbers ‘0.14’, ‘0.07’ and ‘0.04’ in this example. A general rule of thumb on measuring, if your server is under high load is to use a formula like ‘4 x CPUCores’, if the number returned exceeds what you’re seeing as per the load average, then this merits an investigation. High CPU load is a more indicative of an issue present on a server then high CPU usage. Other tools such as 'sysstat' do a great job of collating these stats.
Server uptime is also presented in this section and displays how long the server has been up.
Section 3 – Process Information
If you're noticing a lot of resource utilisation, we need to look at the processes to determine what's causing it. The active process list is shown below:
Per the man page for htop, the columns displayed by default should be read as follows:
- PID: The process ID.
- USER: The username of the process owner or the user ID if the name can’t be determined.
- PRI: The kernels internal priority for the process, usually just its NI value plus twenty. Different for real-time processes.
- NI: The nice value of a process, from 19 (low priority) to -20 (high priority). A high value means the process is being nice, letting others have a higher relative priority. Only root can lower the value.
- VIRT: Size in memory of the total program size.
- RES: The resident set size, i.e. the size of the text and data sections, plus stack usage.
- SHR: The size of the process’s shared pages.
- S: The state of the process.
- S: for sleeping (idle)
- R: for running
- D: for disk sleep (uninterruptible)
- Z: for zombie (waiting for parent to read its exit status)
- T: for traced or suspended (e.g by SIGTSTP)
- W: for paging
- CPU%: The percentage of the CPU time that the process is currently using.
- MEM%: The percentage of memory the process is currently using (based on the process’s resident memory size, see M_RESIDENT below).
- TIME+: The time, measured in clock ticks that the process has spent in user and system time (see UTIME, STIME above).
- Command: Full path of the command.
SECTION 4 - Shortcuts for the keyboard
Aside from its configurability, the main strength of htop rests in its many features, which are available via shortkeys. The shortcuts at the bottom are all self-explanatory, configurable and mouse clickable.
There is no doubt that htop is one of the finest process managers. This article should have helped you grasp the basics of htop, for further in depth detail and information on htop, its configurability, flags etc I’ll refer you to the man (manual) page and the developers website: https://htop.dev/