Have you ever wished that Unix system monitoring was better? Htop is one of the best tools. What does Htop do? Is it easy to understand the output? This article explains all that and more.
Why do we need HTOP?
This tool is designed for Unix systems and is an interactive system monitor, process viewer, and manager. In addition to providing many of the same capabilities as top, it offers far greater flexibility over how you can view system processes.
Top provides only a list of the top resource-consuming processes; htop provides the complete list. With Htop, you can view process trees and view resource-usage statistics based on colours.
The three main roles of Htop are:
- Monitoring the system
- Activity in real-time
- Viewing and managing processes
How to Install HTOP?
The first step to obtaining a better Terminal experience is installing Htop. This can be done on any distribution that uses the terminal, like Linux or macOS!
It may be easier to install Htop on multiple Linux distributions at once using Snap since it simplifies the installation process.
The installation method will vary depending upon your specific flavour of operating system; however, there’s one thing they all have in common – it doesn’t take long at all (maybe 5 minutes?). So let’s get started!
sudo apt-get install htop
- Linux (Fedora)
sudo dnf install htop
- Linux (CentOS)
sudo yum install epel-release sudo yum install htop
brew install htop
Once the utility is installed, you can enter htop by simply typing:
How to understand the output from htop?
Let’s take a quick look at the output you get when you first open htop, and what each of the options means.
The length of time the system has been running is indicated by this parameter. The length of time the system has been running can also be viewed by running the uptime command. The file /proc/uptime contains this information.
As well as uptime, there are three other numbers that represent the average load. You can find them in /proc/loadavg. Strace output shows that this file was opened as well.
PID (Process ID)
The process ID (also known as PID for short) is assigned to every new process when it is started.
Running a program in the background (&) from bash displays the job number in square brackets as well as the PID of the job.
Whenever you launch a new process, the process that initiated it is called the parent process. Your new process is now a child of the parent process. A tree is formed by these relationships. Htop shows the process hierarchy if you hit F5. Using f with ps or pstree is also an option.
The user who initiates a process is the owner of that process. Each user is assigned a numeric ID. You can use the id command combined with the username to find the UID and GID that are assigned to a user.
The ID command pulls the information contained in the /etc/passwd and /etc/group files.
Process State (S)
Despite being referred to as only an “S” in htop, it stands for process state. It can contain the following values:
- The process is currently running or is in a state where it will start shortly.
- S: the process is in a state of interrupted sleep (waiting for something to happen).
- D: the process is in uninterruptible sleep (cannot be woken from sleep).
- Z: the process is dead (a.k.a. “zombie”). It has been terminated and will not be repeated by its parent.
- T: A job control signal terminates the process.
- t: The debugger stops the process during tracing.
- X: The process has stopped. It’s unlikely you’ll see this in htop.
The Linux operating system is multitasking, so you can run several processes simultaneously even with a single CPU. If you are connected to your server via SSH, you can view htop’s output while your blog is being served over the internet.
If a single CPU can only handle one instruction at a time, how is that possible? This is done through timesharing. While the other processes wait to run, one process starts running, then it’s suspended as the others take turns. This is referred to as the time slice.
As long as your system is not under high load, you won’t notice the time slice that much. (A study of how long time slices are in Linux would be really interesting.)
Thus, the average load is the number of processes that are running on average. In the case of a single core and a load average of 1.0, 100% of the CPU has been utilized. You may experience slowdowns or delays when the load average is higher than 1.0. This means that more processes are trying to run than the CPU is able to handle. Whenever the load is below 1.0, it is indicating that the CPU sometimes idles.
Additionally, this should help explain why sometimes a process running for 10 seconds has a running time that’s higher or lower than the exact number of seconds.
Process Niceness and Priority
If more tasks need to be processed than the number of CPU cores available, you must decide which tasks will be processed next and which ones will have to wait. This is the responsibility of the task scheduler.
The Linux kernel’s scheduler controls the picking of the next process from a run queue, depending on the algorithm used.
Schedulers are generally beyond our influence, but we can tell them which processes are more important to us, so they might take that into account.
Processes are sorted according to their Niceness (NI), with -20 being the highest priority and 19 being the lowest. Having a nice process result in a less nice process can be confusing. As a result, a nicer process yields more results.
Processes are perceived as being the only ones in memory. Virtual memory is used to achieve this.
A process can’t access physical memory directly. However, it has its own virtual address space which is translated into physical memory by the kernel or can be mapped to disk. Therefore, it can appear that processes are using more memory than the amount you have installed.
I am trying to make the point here that determining how much memory a process uses is not very straightforward. What about shared libraries and disk-mapped memory? However, you can estimate memory usage using the kernel and Htop.
These are the colours that represent memory usage:
- Green: Memory is being used
- Blue: Buffered memory
- Orange: Cached memory
This comprehensive blog should have helped you gain a better understanding of htop and enable you to better use the program. Comment below with any questions or suggestions you may have. Feel free to let us know if you enjoyed reading this article and believe you are able to create engaging content for our readers. Our Write for Us page explains how you can contribute to Apache-IoT.