TOP in Linux – interactive process monitor

TOP is one of the most famous programs in both the world of Linux and Unix as such. Most Linux distributions use the rewritten and improved version that is part of procps-ng. In connection with this fact, I would like to emphasize that the majority of the content presented in this article will not work […]

TOP is one of the most famous programs in both the world of Linux and Unix as such. Most Linux distributions use the rewritten and improved version that is part of procps-ng. In connection with this fact, I would like to emphasize that the majority of the content presented in this article will not work on another implementation.

The top itself presents a collection of useful statistics on the current load / behavior / health of the system and the processes in it. Due to the fact that the command top at first glance it can be a bit overwhelming, I allowed myself to bring it closer to readers.

Because the package procps-ng (or procps in SUSE systems) is part of the group core, i.e. the smallest possible installation of Linux Enterprise systems (Rocky Linux, RHEL, AlmaLinux, CentOS) and generally rpm systems (such as Fedora, OpenSuse, SUSE), it does not require additional installation.

What is what at TOP?

To call our process monitor, simply enter its name – does not require any additional flags or options.

After calling:

top

we will see more or less the following way out:

top

Let me spread the command output here top on prime factors.

top

The first line is the name of the program under which it was made top. In our case it is standard top. Then it displays information about the time of uninterrupted computer work (uptime), the number of users (active – sessions count as in the program who) and average load average. This line is not by accident the same as the first line of well-known recommendation w.

top

The next section is a section about tasks (tasks) or threads (threads) managed by the kernel and their status. As you can easily see, the top does not report all states to us, because the number „total ” is not equal to the total remaining columns. This is due to the fact that there is not even a state of „idle ”, i.e. processes that literally do nothing. To see what states we currently have in the system, it is enough to make a simple single-line ps ax -o state | sort | uniq, and for counting, instead of ordinary uniq, you can use unic -c. One of the most interesting columns in the top output is a column with a counted number of processes zombies. Simply put, these are processes that have finished their performance and are waiting for wait() from a parent. However, we rarely see them, because programs with children usually have well-written end-of-work service. However, in the case of orphans (trials that have lost their parent) systemd or init automatically invoke wait() on adopted processes. For the zombie process to arise and reside in the process table, there must be a number of events and often programming errors.

top

The next line contains CPU states (percentage per state), which may be as follows:

  •  us (er): time to perform user processes with zero nice value (most processes);
  • sy (stem): time to perform testicular processes;
  •  nice: time of processes with a value of nice > 0;
  •  id (le): time of idle (inactivity) processes;
  •  wa (it for IO): waiting time for operation I / O;
  •  hi [Hardware Interrups]: time spent handling hardware interrupts;
  •  si [Software Interrups]: time spent operating program interrupts;
  •  st (olen): time stolen by hypervisor (for virtual machines).

In the alternative mode of displaying the use of CPU with the help of grap we have the following format:

[us + ni] / [sy] [sum of all except id (le)] [Graf]

 

top

After information about the processor, it displays information about memory.

We have here the amount of total, free (free) (untouched by the kernel), used (used), memory used for buffers / cache (buff / cache) and available (avail Mem). The most important value is usually avail Mem. In case swap did not exist, and avail Mem „would reach ” zero, it would be called Killer OOM (Out Of Memory).

The second line contains swap information (total, free, used). Note that we have here used.. This dot is not accidental. Indicates the end of the swap section – avail Mem does not apply to the swap and does not include it in its value. This is shown in the example below.

top

The last output section of the `top` command is „task field ” (loose English translation „task area ”).

top

In the standard configuration we have:

  •  PID – Process ID.
  •  User – User, to which the process belongs.
  •  PR – Priority. There are two types of priorities: Normal (number) and RT (Real Time – performed in real time – rare).
  •  NI – Nice value. Note that in most cases priority = 20 + NI.
  •  VIRT – Virtual Image Size, i.e. the size of the virtual process image. This is the sum of memory mapped by the process (e.g. graphics card memory), memory used by the process, files mapped by the process and memory shared with other processes. In many cases, VIRT is even misleading.
  •  RES – Resident Memory – unswappable memory that the process uses (physical memory).
  •  SHR – Shared Memory – memory that is / can be shared by the process.
  •  S – Condition of the process (Running, Sleeping, Zombie, etc …).
  •  % CPU – CPU usage as a percentage. In multiprocessor environments (speaking of the processor as the processor core, not necessarily many systems) it can be more than 100%. For example, saturation of two processors (cores or processor threads, depending on the type of processor, architecture, etc.) will give us a 200% result.
  •  % MEM – Percentage share in available physical memory.
  •  TIME + – The total time the processor has used for the task since it started.+ means greater accuracy. In our case, hundredths of a second.
  •  COMMAND – Command.

Top customization

Before we start „bury ” in standard settings top, I suggest you make a symbolic link and then call it.

$ sudo ln -s /usr/bin/top /usr/bin/my_top
$ my_top

Now the first exit line will start from my_top - . This is due to the fact that toplike any Linux program, it has access to the information with which it was called. The top tool uses this fact to enable us to create and maintain multiple configurations. In my humble opinion, this is quite a clever mechanism.

The first thing you should practically always set is to scale your memory. In contrast to htoptop does not have intelligent scaling. It is set to a rigid value (unit). What’s worse, by default they are KiB (kilobytes, in theory correctly called kikibytes [use 2 ^ 10 instead of 10 ^ 3, but this vocabulary was not accepted and only the creators of memory carriers and Apple use „distorted ” nomenclature]). Use the key E we can scale the memory up to exbibytes (more often called exabytes [interestingly, according to Polish Wikipedia, in this case we replace ks with x]). If you reach the end of scaling „we loop ” and get the initial value. In order to scale the memory for the task area, we use a small one e. Here, however, maximum scaling ends on petabytes.

After such an operation, we can save our configuration. A keyboard shortcut is used for this W. Depending on the program name, the path to the configuration file is ${HOME}/.${NAZWA}rc or in newer versions of the procps-ng package ${HOME}/.config/procps/.${NAZWA}rc.

Then I suggest you go to the refresh settings. By default, it is set to 3 seconds. Use the key d (d like delay) or s (like a second) you can set the desired value. In my case it is usually 5 seconds (for this purpose I enter 5 and press enter).

Another variable that I like to have in my top is column change. First column VIRT must go! Secondly, you can add a column at your discretion. In my case, let me add nTH talking about the number of threads in a given process. To enter half (field) edit mode, we’ll use the key f.

In this mode, we navigate using the arrows w górę and w dół. We can turn the fields on / off with space. If we want to change the order of the fields, press the right arrow (we enter the field transfer mode). Then using the arrows góradół we move the field to the selected place. To exit the field transfer mode and confirm its new position, use the key enter. I changed the order of the fields % MEM and set nTH in the right place in my opinion.

Finally, after changing, the window should look something like this.

top

In field editing mode, there is also significant functionality, which is setting the field after which the top will sort processes. A key is used for this s. This is the easiest way to set the sorting field.

The penultimate thing I would like to pay attention to when choosing fields is the fact that top in alternative mode it has 4 windows (groups) – 1: Def, 2: Mem, 3: Job, 4: Usr. The default window that appears to us is the 1: Def window. It is shared by both the „normal ” mode of operation and the „alternative ” mode of operation. The key that changes the edited window is the key w.

With the fields set as we want, we can leave the field editing using q or ESC. The other way is a little safer because double pressing q will exit the program :) while ESC will come out of editing the window and then force you to refresh the program status.

Once we have the configuration set, we can confidently save our settings by using the key again W.

The last change in the configuration that I would like to propose is the change of colors. I realize that not everyone (guy) distinguishes between more than 3 colors, no less without panic, the top allows us to choose from the list of stunning 8! To enter the color mapping mode, we use the key Z. However, before this operation, turn on the color mode using the switch z.

At the beginning with the help (SMHT) select sections, and then choose one of the numbers (0-7). In this case, we also have the option of changing the window whose colors we edit (serves this w or a).

To exit and save the changes, press the key enter.
The settings presented below give the following configuration S2 M1 H4 T7.

top

The final effect of our configuration can be found below.

top

Some additional tips on how to use TOP

I will get the first and most important advice directly from the `top` manual.

When operating top, the two most important keys are help (’h’ or ’?’) and quit (’q’) key.

Due to the extensive options, it makes no sense to remember all possible keys. Nevertheless, let me mention a few of them.

Sorting and filters

P – sorts processes after using the processor.
M – sorts processes after using memory.
n – sorts by PID number.
< and > – changes the field after which sorting takes place.
u – filters by username, unfortunately you should enter its full name.
R – reverses the sorting order.

Useful

k – is used to send SIGKILL.
r – is used to change the value of the nice parameter.

„alternative mode ” *

A – is used to enter / exit the above alternative mode.
w or a – changes the window in alternative mode.

Change of summaries

1,2,3 – changes processor summary mode. These modes can have „sub modes ”. In this case, call the key again.
t – changes the processor summary display mode.
m – changes the memory summary mode.
l – enables / displays a summary banner (first line).
H – changes task mode (processes) to threads (threads).

Other

c – enables / disables displaying entire command paths.
G – selection of one of 4 groups (1: Def, 2: Mem, 3: Job, 4: Usr) in a given window. In alternative mode, we have 4 windows that have successively listed groups. Here it should be noted that top is not consistent in naming. Once we talk about groups, once about windows. In normal operation mode, we select a group of fields that are displayed. In alternative mode, the command sets a group of fields that will be in the window.

Summary and video

I would like to thank you for spending your time with this text. Every day, EuroLinux provides support for users of operating systems and databases. We provide content and services that are easier to absorb and more difficult to substance. Today we have prepared an instructional video for this material, which is below. His topics are very similar to those discussed in this article. I think it is a valuable complement to this article. I wish you fruitful watching (the video is available in Polish language only).

https://www.youtube.com/watch?v=MrfJryQ7C9Y

blank Authors

The blog articles are written by people from the EuroLinux team. We owe 80% of the content to our developers, the rest is prepared by the sales or marketing department. We make every effort to ensure that the content is the best in terms of content and language, but we are not infallible. If you see anything that needs to be corrected or clarified, we'd love to hear from you.