top customized!
There are many interactive process viewer for Linux. But only one is preinstalled by default — top!
TL;DR
This tutorial is slated to discuss the process viewer — top. More specifically, top is a process viewer about information aggregation and the separation of relevant information from irrelevant data.
NOTE: Techniques for performance analysis are not included in this article.
What is top?
top is a process viewer that runs interactively from a Shell. top is a data visualizer that is focused on CPU- and Memory consumption through processes, subsequently displaying the data in a customizable view — such that it can be used to manipulate those processes. In comparison to htop and nmon, top always stands out as its modus operandi is out of the box; hence, what some may term as boring:
But with a little hands-on, top can look modern and friendly
Why top?
Other process viewers like htop and nmon can be configured, are better looking, are in some parts more powerful, and readily available for most distributions. In addition to the fact that these process viewers are third-party software, you need to install them on every distribution. top, on the other hand, is already preinstalled — on whatever Linux distribution you are using. Given its mainstream adoption, top is considered a robust and powerful tool — one that has been included in the toolbox of every Linux-Sysadmin — for decades.
First steps
You can call top with the following command:
$ top
In this tutorial, top will be configured step by step. For further information, I recommend the excellent man-page.
Saving your changes and manual refresh
By pressing W, your configuration will be written to $HOME/.toprc or $HOME/.config/procps/toprc
The default refresh rate is set to 3 seconds, but you can also manually refresh by hitting space.
Summary Area
The UI is split into two Areas: The Summary Area on top and the Task Area below. Let’s delve into the Summary Area:
The summary is already jam-packed with plenty of information, but there is some stuff that I would like to change:
- %CPU(s): Since my VM has only two cores, I don’t want to have here a summary of CPU consumption. Instead, I want to see the usage of both cores. Therefore I press 1.
- MiB Mem/MiB Swap: With 12GB memory, I would like to see the size in GiB. Therefore I press E.
- I want to see the scroll coordinates in the Task Area. I activate it by pressing C.
- the default refresh rate of 3 seconds is too long; therefore, I press S and set the rate to 0.75
I save the configuration by pressing W such that the next time I reinitiate top; my Summary Area will look like this:
Task Area
The Task Area is where processes and the data are shown. This section is where such operations as; aggregation, filtering, sorting, and changing the style (forest vs. list), are performed. Each Column is a Metric that can be changed. A short reference with the most important keys is attached at the end of this article.
Alternative Display Mode
Jump to the A-mode by pressing A:
The A-mode provides four windows — each of them configurable for different needs. I decided to design the windows according to the following topics:
- Window 1: CPU and time
- Window 2: Memory and swap-space
- Window 3: Processes filtered by user
- Window 4: Processes sorted by status
Navigation and renaming of the first window
To select the first window, I press G and then 1. As a little help, the name of the selected window is visible at the top-left corner in the Summary Area. “1:Def” is displayed as the window name. I renamed it to CPU by pressing G, again:
Selecting fields and moving them in the right order
The actual fields do not fit what I want to see — and that is everything about CPU and time consumption. So I had to change the fields. I navigated to the field menu by pressing F. The menu provided me with the following fields:
I selected the desired fields and sorted them in the right order:
The configuration can be saved and applied to the window CPU by pressing Q.
Filtering and sorting data
To display only the essential information, I sorted the data by consumed CPU and applied the following filters:
- I toggle off 0-values
- Since the selected columns leave a lot of space, I want to see the full path to the running processes and the arguments they are called with
- I reduce the number of displayed processes to eight
- only processes that consume more than 5% CPU should be displayed
Therefore I hit the following keys:
P, 0, c
n -> 8
o -> %CPU>5,0
Hint: when you filter with > (greater than) and < (less than) like in “%CPU>5,0,” it is essential to use the comma and a fraction of 1. Otherwise, the filtration function would not work.
Since there are only five processes that consume more than 5% CPU, there are only five processes shown instead of 8:
But the view is not static: I can move the columns left and right with the arrow keys, and sort them dynamically. If I would like to sort the values in TIME+ — I press the right arrow key until TIME+ hits the left border; this temporarily disables the %CPU filter and shows me the longest-running processes:
Adding colors
Something is still missing: top gives me three different ways to lead my eyes to the right information:
- adding colors to the different parts
- highlighting the background and font
- highlighting rows and columns
Color-mapping
To call the menu for color-mapping, press Z from the main window. If A-mode is active, the color palette for each window can be defined. I am still in the window CPU, hence I chose the following combination:
- To have a better idea of which window is active, I always choose the same color for the Summary Data (S), Task Information (T), and Column Header (H). In this case, I chose yellow (3).
- For Messages/Prompts (M), I chose white.
- Pressing Return saves the configuration and closes the dialog.
Now it starts to look a bit better:
Highlighting
But there are more options to improve the view. Using highlighting often leads the eyes to the relevant data. Therefore I have to remove my filter “%CPU>5,0”. Use “+” to remove the filter for the active window only; then use “=” for all filters in all windows.
I want to highlight the active sorted column — in this case, %CPU. The highlight for that column should be a background highlight. Therefore I press the following keys:
+, x, b
Now I can instantly get insight into all of the relevant information in that window:
The same process can be done for all windows.
Result
See for yourself:
Process Handling
Setting priorities
top is more than just a process viewer. I can change the priority of a process, directly impacting the number of CPU cycles he takes. As you can see in the picture above, I am running five yes-processes. I want to prioritize (renice) the process 5560. Therefore I type R (for renice) and set the nice-values (the priority) to 19:
The priority is now 39, which means the process has the lowest priority. By pressing the up and down arrow keys, I can shift a process to the top of the displayed processes. That PID of that process will be preset when I press R, so I don’t even have to manually enter the PID. As you can see, I have done this for four of the five yes-processes, and the CPU usage of the changed processes is now less than 2%, while the fifth yes-process is consuming about 96% CPU. Have a look:
Terminating processes
Processes can be terminated from the top interface. Therefore, if you press K, the PID of the process on top of the list is selected by default. Since I sorted by CPU consumption, the most CPU-hungry process is becomes automatically selected. A SIGTERM (kill -15) signal is sent to the process by convention, but a SIGKILL (kill -9) or any other signal can be sent to terminate the process.
Conclusion
Suppose you made it here, congratulations! top is a stable and well-established tool used to analyze performance and find memory or CPU bottlenecks. With a little bit of knowledge — and a little bit of work, top can be transformed into a decent-looking modern tool. I hope I gave you some ideas and saved you some time.
Short reference
Summary Area
- l: toggle on/off Load-Average/Uptime
- 1: show single CPU vs. multiple CPU
- t: show CPU as numerical values (us, sy, ni, id, wa, hi, si, sd) or as bars
- E: cycle through the available memory scaling in KiB, MiB, GiB, TiB, PiB, EiB
- m: show memory usage as numerical values (total/free/used) or as bars
- C: show scroll coordinates
- B: bold fonts enable/disable
- d, s: set refresh intervals like “1” or “0.25”
Task Area
Sorting:
- P: sorting by CPU usage
- M: sorting by memory usage
- e: cycle through the available memory scaling in KiB, MiB, GiB, TiB, PiB
- T: sorting the processes by running time
- N: sorting the processes by PID
- R: reverse sorting
Filtering:
- u, U: filter processes by user
- i: toggle on/ off idle processes
- 0: toggle on/off 0-values
- o: filtering a specific column for a value, non-case-sensitive
- same like o, but case sensitive
- =: reset all filter
- +: reset the filter in the active window (only A-Mode)
In A-Mode:
- g -> 1–4: select a specific window
- w: switch through the windows
Different display modes for processes
- c: display commands with full path info and arguments
- V: forest-view mode