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

How to use the top command on Linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use the top command on the Linux system. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In Linux systems, top commands are often used to monitor the system status of linux, such as cpu, memory usage, similar to Windows's task manager.

Detailed explanation of top parameters

The first line, the task queue information, the same as the execution result of the uptime command

System time: 07:27:05

Run time: up 1:57 min

Currently logged in user: 3 user

Load balance (uptime) load average: 0.00,0.00,0.00

The three numbers after average are 1 minute, 5 minutes, and 15 minutes of load, respectively.

Load average data is the number of active processes checked every 5 seconds and then calculated according to a specific algorithm. If this number is divided by the number of logical CPU, a result higher than 5 indicates that the system is overloaded.

The second line, Tasks-tasks (processes)

Total process: 150 total, run: 1 running, hibernation: 149 sleeping, stop: 0 stopped, zombie process: 0 zombie

The third line, cpu status information

0.0%us [user space]-percentage of CPU occupied by user space.

0.3%sy [sysctl]-percentage of CPU occupied by kernel space.

0.0%ni []-percentage of CPU occupied by processes that have changed priority

99.7%id [idolt]-percentage of idle CPU

0.0%wa [wait]-percentage of CPU occupied by IO waiting

0.0%hi [Hardware IRQ]-percentage of CPU occupied by hard interrupts

0.0%si [Software Interrupts]-percentage of CPU occupied by soft interrupt

Fourth line, memory status

1003020k total, 234464k used, 777824k free, 24084k buffers [amount of cached memory]

Fifth line, swap exchanges partition information

2031612k total, 536k used, 2031076k free, 505864k cached [total buffered switching areas]

Note:

Available memory = free + buffer + cached

For memory monitoring, we should always monitor the used of the fifth line of swap swap partition in top. If this value is constantly changing, it means that the kernel is constantly exchanging data between memory and swap, which is really out of memory.

The total memory in use (used) in the fourth line refers to the amount of memory now controlled by the system kernel.

The total amount of free memory (free) in the fourth line is the amount that the kernel has not yet come under its control.

Not all the memory managed by the kernel is in use, and it also includes memory that has been used in the past and can be reused now, and the kernel does not return the reusable memory to free, so there will be less and less free memory on linux, but don't worry about it.

Line six, blank line

Line 7 is as follows: status monitoring of each process (task)

PID-process id USER-process owner PR-process priority NI-nice value. Negative values indicate high priority, and positive values represent low priority VIRT-the total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES RES-the amount of physical memory used by the process that has not been swapped out, in kb. RES=CODE+DATA SHR-shared memory size in kb S-process status. D = uninterruptible sleep state R = run S = sleep T = track / stop Z = zombie process% CPU-percentage of CPU time last updated to the present% MEM-percentage of physical memory used by the process TIME+-total CPU time used by the process, unit 1 stroke 100 seconds COMMAND-process name (command name / command line)

Detailed explanation

VIRT:virtual memory usage virtual memory 1, the amount of virtual memory that the process "needs", including libraries, code, data, etc. 2. If the process requests 100m of memory but only uses 10m, 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, but does not include swap out 2, including the sharing of other processes 3, if the request for 100m memory, the actual use of 10m, it only increases by 10m, contrary 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, not only the shared memory of its own processes, but also the shared memory of other processes. Although the process only uses a few shared library functions, it contains the size of the entire shared library. 3. The formula for calculating the physical memory occupied by a process: RES-SHR 4, swap out, it will fall

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-turn off or turn on the representation of the first line of top information t-close or turn on the representation of the first part, the second line Tasks and the third line Cpus information, m-close or open the first part, the fourth line Mem and the fifth line Swap information N-in the order of the size of PID, the list of processes P-has a large CPU occupancy rate. Sort the process list in small order M-sort the process list in the order of memory usage h-display help n-set the number of processes displayed in the process list Q-exit top s-change the screen update cycle

The top usage method uses the 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.

Common command description 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.

View the multicore CPU commands mpstat-P ALL and sar-P ALL

Description: sar-P ALL > aaa.txt redirects output to file aaa.txt

1 top command, and then press the number "1" to monitor the status of each logical CPU:

Looking at the figure above, the server has eight logical CPU, which is actually a physical CPU.

If you do not press 1, the average of all cpu is displayed in the top view.

2 command: mpstat-P ALL

3 command: sar-P ALL

More output can be grep or redirected to the file to view

When process field sorting enters top by default, each process is sorted according to the usage of CPU. In [top View 01], the java process with process ID of 14210 ranks first (cpu occupies 100%), and the java process with process ID of 14183 ranks second (cpu occupies 12%). You can change the sort field through keyboard commands. For example, you want to monitor which process takes up the most MEM. My general usage is as follows:

\ 1. When you tap the keyboard "b" (turn the highlighting effect on / off), the view of top changes as follows:

We find that the "top" process with a process id of 12363 is highlighted, and the top process is the only runing process shown in the second line of the view. You can turn off or turn on the highlighting effect of the running process by hitting the "y" key. \ 2. When you tap the keyboard "x" (turns on / off the highlighting effect of sorting), the view of top changes as follows: as you can see, the default sorting column of top is "% CPU". \ 3. Through the effect picture of "shift + >" or "shift +":

Views are now sorted by% MEM.

Change the process display field\ 1. Press the "f" key and top goes to another view, where you can orchestrate the display fields in the base view:

Here is a list of all the process fields that can be displayed in the top base view. Fields with "" and marked with uppercase letters can be displayed, and fields without "" and lowercase letters are not displayed. If you want to display the "CODE" and "DATA" fields in the base view, you can click the "r" and "s" keys:

\ 2. Enter returns to the base view, and you can see that two more fields, "CODE" and "DATA", are added:

Linux to view the number of physical CPU, number of cores, Number of logical CPU # total number of cores = number of physical CPU # total number of physical CPU = number of physical CPU X number of cores per physical CPU X number of super threads # View the number of physical CPU cat / proc/cpuinfo | grep "physical id" | sort | uniq | wc-l # View the number of core in each physical CPU (that is, the number of cores) cat / proc/cpuinfo | grep "cpu cores" | uniq # View logical CPU Number of cat / proc/cpuinfo | grep "processor" | wc-l Thank you for reading! This is the end of this article on "how to use top commands in Linux system". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Development

Wechat

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

12
Report