Suggestions for using the top command in Linux

Improve your knowledge of the top command with this tutorial. Trying to figure out what programs are running on your machine, and which processes are running out of memory and causing the system to be very, very slow — that’s what the top command does.

By Katie Mclaughlin
Linux China|

The 2018-08-21 GMT

collection
share

Technical Sharon | invite you with gome on August 25 / AWS/around three experts to discuss small program electrical contractor in actual combat

Improve your knowledge of the top command with this tutorial.

Trying to figure out what programs are running on your machine, and which processes are running out of memory and causing the system to be very, very slow — that’s what the top command does.

Top is a very useful program that acts like a Windows task manager or an activity monitor for MacOS. Running top on a * NIx machine will show you in real time what processes are running on the system.

$ top Copy the code

Depending on the top version you’re running, you’ll see something like this:

Top-08:31:32 UP 1 day, 4:09, 0 Users, Load Average: 0.20, 0.12, 0.10 Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.5us, 0.3SY, 0.0Ni, 99.2id, 0.0wa, 0.0hi, 0.0Si, 0.0st KiB Mem: 4042284 total, 2523744 used, 1518540 free, 263776 buffers KiB Swap: 1048572 total, 0 used, 1048572 Free.1804264 cached Mem PID USER PR NI VIRT RES SHR S %CPU % Mem TIME+ COMMAND 1 root 20 0 21964 3632 3124 S 0.0 Flask 32 32 root 20 0 23608 2724 2400 R 0.0 0.1 0:00. 21 topCopy the code

The version of top you are using may look different from this, especially in the displayed columns.

How do I read the output

You can judge what you’re running from the output, but trying to interpret the results can be confusing.

The first few rows contain a bunch of statistics (details), followed by a table (columns) containing the result columns. Let’s start with the latter.

column

These are the processes that the system is running. By default, the CPU usage is sorted in descending order. This means that programs at the top of the list are using more CPU resources and placing a heavier burden on your system. These programs are literally the most resource-consuming (top) processes for resource usage. I have to say, top is a great name.

The COMMAND column on the far right reports the process names (the commands that started them). In this case, the process names are bash (a command interpreter where we’re running Top), Flask (a Python-written Web framework), and Top itself.

The other columns provide useful information about the process:

  • PID: process ID, a unique identifier used to locate the process
  • USER: User who runs the process
  • PR: Task priority
  • NI: Nice value, a better representation of priority
  • VIRT: Size of virtual memory in KiB (kibibytes)
  • RES: Resident memory size in KiB (part of physical memory and virtual memory)
  • SHR: Shared memory size in KiB (part of shared memory and virtual memory)
  • S: Process status, mediumIStands for idle,RStands for run,SStands for dormancy,ZRepresents zombie processes,TtStands for stop (there are other, rarer options)
  • %CPU: CPU usage since the last screen update
  • %MEM: since the last screen updateRESResident memory usage
  • TIME+: Total CPU usage since the program started
  • COMMAND: Starts the command, as described earlier

Knowing exactly what the VIRT, RES, and SHR values represent is not important in day-to-day operations. It is important to know that the process with the highest VIRT value is the one that uses the most memory. When you’re using top to figure out why your computer is running matchcards, the process with the highest VIRT value is the culprit. If you want to know exactly what shared Memory and physical Memory mean, check out the Linux Memory Types section in the Top manual.

And yes, I said kibibytes not kilobytes. The 1024 value commonly called kilobyte is actually Kibibyte. The Greek kilo (χίλιοι) means one thousand (for example, a kilometer is one thousand meters, and a kilogram is one thousand grams). Kibi is a blend of kilo and binary, meaning 1024 bytes (or 2^10). However, because the word is hard to say, many people say kilobyte when they say 1024 bytes. Top is trying to use proper terminology here, so take it as it says.

Screen Update Description

Real-time screen updates are one of the really cool things that Linux programs can do. This means that programs can update what they display in real time, so it looks dynamic, even if they’re using text. Very cool! In our example, the update interval is important because some statistics (%CPU and %MEM) are based on the last screen update.

Because we are running in a persistent program, we can enter commands to modify the configuration in real time (rather than stopping the application and running it again with a different command-line option).

Pressing H invokes the help screen, which also displays the default delay (the interval between screen updates). This defaults to 3 seconds, but you can change it by typing D (roughly delay) or s (maybe screen or seconds).

details

There’s a lot of useful information on the process list. Some of the details seem a little strange and confusing. But once you take the time to go through them one by one, you’ll find that they can be very useful in a pinch.

The first line contains general information about the system:

  • top: We are runningtop! Hello!top!
  • XX:YY:XX: Current time, updated each time the screen is updated
  • up(The next isX day, YY:ZZ) : SystematicuptimeOr how much time has passed since the system started
  • load average(followed by three numbers) : for the past one minute, five minutes and 15 minutesSystem load

The second line (Task) shows information about the running Task without explanation. It shows the total number of processes and the number of running, dormant, stopped, and zombie processes. This is actually the sum of the S columns above.

The third line (%Cpu(s)) shows Cpu usage by type. Data is the value between screen flushes. These values are:

  • us: User process
  • sy: System process
  • ni:niceThe user process
  • id: Indicates the idle time of the CPU. A high value indicates that the system is idle
  • wa: Wait time, or the time spent waiting for I/O to complete
  • hi: Time spent on hardware outages
  • si: Time spent on software outages
  • st: “Time stolen from this VIRTUAL machine by the hypervisor”

You can expand or collapse the Task and %Cpu(s) rows by clicking t (toggle).

The fourth line (Kib Mem) and the fifth line (Kib Swap) provide information about memory and Swap space. These values are:

  • Total memory capacity
  • Have used the memory
  • Free memory
  • The cache value of memory
  • The cached value of swap space

By default they are presented in KiB units, but pressing E (extend Memory Scaling) rotates different units: KiB, MiB, GiB, TiB, PiB, EiB (kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes)

The top user manual has more options and configuration items. You can run Man Top to view documents on your system. There are also many HTML versions of the MAN manual, but be aware that these manuals may be for different top versions.

Two alternatives to top

You don’t always have to use top to check system status. You can use other tools to help troubleshoot problems depending on your situation, especially if you want a more graphical or professional interface.

htop

Htop is a lot like Top, but it brings something very useful: it shows CPU and memory usage graphically.

This is what HTOP looks like in the same environment where we just ran TOP. The display is simpler, but the functionality is rich.

Task statistics, load, uptime, and process lists are still there, but it has nice, colorful, dynamic per-core CPU usage, as well as graphical memory usage.

Here’s what the different colors mean (you can also get help with this information by pressing H).

CPU task priority or type:

  • Blue: Low priority
  • Green: Normal priority
  • Red: kernel task
  • Blue: virtual task
  • The value at the end of the bar chart is the percentage of CPU used

Memory:

  • Green: Used memory
  • Blue: buffered memory
  • Yellow: Cache memory
  • The values at the end of the bar chart show used memory and total memory

If colors don’t work for you, you can run htop -c to disable them; Then HTOP will use different symbols to show CPU and memory types.

It has a set of active keyboard shortcuts at the bottom that can be used to manipulate the filtering results or change the sort order. Try pressing some shortcut keys and see what they can do. Be careful when trying F9 though, it brings up a list of signals that kill (that is, stop) a process. I recommend exploring these options outside of the production environment.

Htop author Hisham Muhammad (yes, the name hTOP comes from Hisham) gave a short talk at FOSDEM 2018 in February. He explained that hTOP not only has a clean graphical interface, but also a more modern way to display statistics of process information, which was not available in previous tools such as TOP.

You can read more about HTOP on the manual page or the HTOP website. (Hint: the site background is a dynamic HTOP.)

docker stats

If you are using Docker, you can run Docker Stats to generate a context-rich interface for the container state.

This is probably more helpful than TOP, because it is sorted not by process but by container. This is especially useful because when a container is running slowly, it is faster to see which container consumes the most resources than the process that runs top and then finds the container.

With the top and htop terms explained above, you should be able to understand those in Docker Stats more easily. However, the Docker Stats documentation provides a detailed description of each column.

Original: https://linux.cn/article-9937-1.html