Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Skills of using top Command in Linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Let's start by introducing the meaning of some fields in top:

VIRT:virtual memory usage virtual memory

1. The amount of virtual memory needed by the process, including libraries, code, data, etc.

2. If the process requests 100m of memory, but only 10m is actually used, it will grow by 100m instead of the actual usage

RES:resident memory usage resident memory

1. The amount of memory currently used by the process, excluding swap out

2. Include sharing of other processes

3. If you apply for 100m of memory and actually use 10m, it will only grow by 10m, which is opposite to VIRT

4. With regard to the memory occupied by the library, it only counts the memory occupied by the loaded library files.

SHR:shared memory shared memory

1. In addition to the shared memory of its own processes, it also includes the shared memory of other processes

2. Although the process only uses a few functions of the shared library, it contains the size of the entire shared library

3. Calculate the amount of physical memory occupied by a process: RES-SHR

4. After swap out, it will come down

DATA

1. Memory occupied by data. If top is not displayed, press the f key to display it.

2. The real data space required by the program is really to be used in operation.

During the operation of top, the display mode of the process can be controlled by the internal commands of top. The internal commands are as follows:

S-change the screen update frequency

L-turns off or turns on the representation of top information on the first line of the first part

T-turn off or turn on the representation of the first part, the second line Tasks and the third line Cpus information

M-turns off or turns on the presentation of the first part, line 4 Mem and line 5 Swap information

N-represents the list of processes in order of PID size

P-sorts the list of processes in the order of CPU occupancy

M-sorts the list of processes in order of memory footprint

H-Show help

N-sets the number of processes displayed in the process list

Q-exit top

S-change the screen update cycle

Serial number column name meaning

A PID process id

B PPID parent process id

C RUSER Real user name

D user id of the UID process owner

E user name of the owner of the USER process

F Group name of the owner of the GROUP process

G TTY the terminal name of the startup process. Processes that are not started from the terminal are displayed as?

H PR priority

I NI nice value. Negative values indicate high priority, while positive values indicate low priority.

The last CPU used by j P makes sense only in a multi-CPU environment

K% percentage of CPU time occupied from the last update of CPU to the present

Total CPU time used by TIME processes (in seconds)

Total CPU time used by the m TIME+ process (in 100s)

Percentage of physical memory used by n% MEM processes

O the total amount of virtual memory used by the VIRT process, in kb. VIRT=SWAP+RES

The size of the virtual memory used by the p SWAP process, in kb.

The amount of unswapped physical memory used by the Q RES process, in kb. RES=CODE+DATA

R the amount of physical memory occupied by CODE executable code, in kb

The amount of physical memory occupied by parts other than the executable code of s DATA (data segment + stack), in kb

T SHR shared memory size (in kb)

Number of page errors in u nFLT

V nDRT the number of pages that have been modified since it was last written.

W / S process status. (d = uninterruptible sleep state, R = running, S = sleep, T = tracking / stopping, Z = zombie process)

X COMMAND command name / command line

Y WCHAN displays the name of the system function in sleep if the process is sleeping

Z Flags task flag, refer to sched.h

By default, only the more important PID, USER, PR, NI, VIRT, RES, SHR, S,% CPU,% MEM, TIME+, COMMAND columns are displayed. You can change the display through the keyboard shortcuts below.

The f key allows you to select what is displayed. Press the f key to display the list of columns, press amurz to show or hide the corresponding columns, and finally press enter to determine.

Press o to change the order in which the columns are displayed. Press lowercase Amurz to move the corresponding column to the right, and uppercase Amurz to move the corresponding column to the left. Finally, press the enter key to confirm.

Press the uppercase F or O key, and then press amurz to sort the processes by the corresponding columns. The uppercase R key reverses the current sort.

How to use top:

Use format:

Top [-] [d] [p] [Q] [c] [C] [S] [s] [n]

Parameter description:

D: specifies the time interval between every two screen information refreshes. Of course, the user can use the s interactive command to change it.

P: monitor only the status of a process by specifying the monitoring process ID.

Q: this option will allow top to refresh without any delay. If the caller has superuser privileges, top will run at the highest possible priority.

S: specifies the accumulation mode.

S: make the top command run in safe mode. This removes the potential danger of interactive commands.

I: make top not show any idle or dead processes.

C: displays the entire command line instead of just the command name.

Description of common commands:

Ctrl+L: erase and rewrite the screen

K: terminates a process. The user will be prompted to enter the process PID that needs to be terminated and what kind of signal needs to be sent to the process. A general termination process can use a 15 signal; if it does not end properly, use signal 9 to force the process to end. The default value is signal 15. This command is blocked in safe mode.

I: ignore idle and dead processes. This is a switch command.

Q: exit the program

R: rearrange the priority of a process. The user is prompted to enter the process PID that needs to be changed and the process priority value to be set. Entering a positive value lowers the priority, otherwise it gives the process a higher priority. The default value is 10.

S: switch to cumulative mode.

S: change the delay time between refreshes. The user will be prompted to enter a new time in s. If there is a decimal, it is converted to m s. Enter a value of 0 and the system will continue to refresh, with a default value of 5 s. It should be noted that if you set too little time, it is likely to cause continuous refresh, so that it is too late to see the display at all, and the system load will be greatly increased.

F or F: add or remove items from the current display.

O or O: change the order in which items are displayed

L: toggle to display average load and startup time information.

M: toggles the display of memory information.

T: toggles the display of process and CPU status information.

C: toggles the display command name and the full command line.

M: sort according to the resident memory size.

P: sort according to the percentage of CPU usage.

T: sort by time / cumulative time.

W: writes the current settings to the ~ / .toprc file.

Improve your knowledge of top commands

Try to find out what programs your machine is running and which process is running out of memory and causing the system to be very slow-this is what the top command can do.

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

$top

Depending on which version of top you are running, you will see something like this:

Top-08:31:32 up 1 day, 4:09, 0 users, load average: 0.20,0.12, 0.10Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie%Cpu (s): 0.5 us, 0.3 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem: 4042284 total, 2523744 used, 1518540 free, 263776 buffersKiB Swap: 1048572 total, 0 used, 1048572 free. 1804264 cached MemPID USER PR NI VIRT RES SHR S% CPU% MEM TIME+ COMMAND 1 root 200 21964 3632 3124 S 0.0 0.1 bash 00.23 bash 193 root 20 123520 29636 8640 S 0.0 0.7 flask 195 root 20 023608 2724 2400 R 0.0 0.01 MEM TIME+ COMMAND 00.21 top

The version of top you are using may not look the same as this one, especially on the displayed columns.

How to read the output

You can judge what you are running based on the output, but you may be a little confused by trying to explain the results.

The first few rows contain a pile of statistics (details), followed by a table (column) that contains the result column. Let's start with the latter.

Column

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

The rightmost COMMAND column reports the process names (the command that starts them). In this example, the process names are bash (a command interpreter we are running top), flask (a web framework written by Python), and top itself.

Other columns provide useful information about the process:

PID: process ID, a unique identifier used to locate the process USER: user PR running the process: priority NI:Nice value of the task, a better expression 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 The unit is KiB (part of shared memory and virtual memory) S: process state, generally I for idle, R for running, S for hibernation, Z for zombie process T or t means stop (there are other rarer options)% CPU: CPU usage since the last screen update% MEM: RES resident memory usage since the last screen update TIME+: Total CPU usage time since the program started COMMAND: start command, as described earlier

It is not important to know exactly what the VIRT,RES and SHR values represent in daily operations. It is important to know that the process with the highest VIRT value is the one that uses the most memory. When you are using top to find out why your computer is running an VIRT card, the process with the largest number of cards is the culprit. If you want to know the exact meaning of shared memory and physical memory, please refer to the Linux Memory Types paragraph of the top manual.

Yes, I'm talking about kibibytes, not kilobytes. The 1024 value commonly referred to as kilobyte is actually kibibyte. The Greek word kilo means a thousand (for example, a kilometer is 1000 meters and a kilogram is 1000 grams). Kibi is a combination 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 tries to use the appropriate terminology here, so just understand it as it says.

Screen update description

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

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

Press h to invoke the help interface, which also shows the default delay (the interval between screen updates). The default value is 3 seconds, but you can change it by typing d (which probably means delay) or s (which may mean screen or seconds).

Details

There is 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 will find that these are very useful at a critical moment.

The first line contains general information about the system:

Top: we are running top! Hello! Top! XX:YY:XX: current time, update up every time the screen is updated (followed by X day, YY:ZZ): the uptime of the system, or how long load average has elapsed since the system started (followed by three numbers): the system load of the past one minute, five minutes, and 15 minutes, respectively.

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 above S (status) columns.

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

Us: user process sy: system process ni: nice idle time of user process id:CPU. When this value is high, the system is idle. Wa: wait time, or time spent waiting for wa O to complete hi: time spent on hardware interrupt si: time spent on software interrupt st: "time stolen by the hypervisor from this virtual machine"

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

Lines 4 (Kib Mem) and 5 (KiB Swap) provide information about memory and swap space. These values are:

Total memory capacity used memory buffer value of free memory swap space cache value

By default, they are displayed in KiB units, but press E (expand memory scale extend memory scaling) to rotate different units: KiB, MiB, GiB, TiB, PiB, EiB (kilobytes, megabytes, gigabytes, terabytes, petabytes and exabytes)

The top user manual has more information about options and configuration items. You can run man top to view the documents on your system. There are also many HTML versions of man manuals, but please note that these manuals may be targeted at different top versions.

Two substitutes for top

You don't always have to use top to check the 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 can show CPU and memory usage in a graphical interface.

This is what htop looks like in the same environment where we just ran top. The display is more concise, but the function is very rich.

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

The following is the meaning of different colors (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: the value at the end of the virtual task bar chart is the percentage of CPU used

Memory:

Green: used memory blue: buffered memory yellow: the value at the end of the cache memory bar chart shows the 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.

At the bottom of it is a set of activated shortcut key tips that can be used to filter the results or change the sort order. Try pressing some shortcut keys to see what they can do. But be careful when trying F9, it will call up a list of signals that will kill (that is, stop) a process. I suggest exploring these options outside the production environment.

Htop's author Hisham Muhammad (yes, htop's name comes from Hisham) gave a brief talk at FOSDEM 2018 in February. He explained that htop not only has a simple graphical interface, but also has a more modern statistical presentation of process information, which previous tools such as top do not have.

You can read more about htop on the manual page or on the htop website. (hint: the background of the website is a dynamic htop. )

Docker stats

If you are using Docker, you can run docker stats to generate a rich context interface for container state.

This may be more helpful than top, because it is not classified by process, but by container. This is especially useful when a container is slow to see which container consumes the most resources than the process of running top and then finding the container.

With the explanation of top and htop terms above, it should be easier for you to understand those in docker stats. However, the docker stats documentation provides a detailed description of each column.

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report