Linux top command view memory and multi-core CPU usage Storytelling

Source: Internet
Author: User
Tags cpu usage java web

View multi-core CPU commands

Mpstat-p All and sar-p all
Description: Sar-p all > Aaa.txt redirect output to file Aaa.txt

Top command

Often used to monitor Linux system conditions, such as CPU, memory usage, the programmer basically know this command, but it is strange to use it very few people, such as the top monitor view of memory value of the meaning of a lot of distortion.
This article covers the meaning of various data in the top view and the sorting of the fields of each process (task) in the view through top monitoring of a running Web server.

Top Enter view

First line:
10:08:45-Current system time
Ten days, 3:05-the system has been running for 10 day, 3 hours, 5 minutes (not restarted during this period)
1 users-currently has 1 users logged into the system
The three numbers behind the load average:0.00, 0.00, 0.00-load Average are 1-minute, 5-minute, 15-minute loads respectively.

The load average data is the number of active processes that are checked every 5 seconds and then calculated by a particular algorithm. If this number is divided by the number of logical CPUs, the result above 5 indicates that the system is overloaded.

Second line:
tasks-task (process), the system now has a total of 135 processes, of which there are 1 in the running, 134 in hibernation (sleep), 0 in the stoped state, and 0 in the zombie State (zombie).

Third line: CPU status
0.3% us-The percentage of CPU consumed by user space.
0.0% sy-The percentage of CPU consumed by the kernel space.
0.0% ni-The percentage of CPU that has changed the priority of the process
99.7% id-Idle CPU percentage
0.0% Wa-io waiting for CPU percentage
0.0% hi-Hard Interrupt (Hardware IRQ)% of CPU occupied
0.0% si-Soft Interrupt (software interrupts)% of CPU occupied

In this case the CPU usage ratio is different from the Windows concept, and if you don't understand the user space and kernel space, it needs to be recharged.

Line four: Memory status
3808060k total-Total Physical memory (4GB)
3660048k used-Total Memory in use (3.6GB)
148012k free-Total free memory (148M)
359760k buffers-Cache Memory (359M)

Line five: Swap swap partition
4184924k total-Swap Area total (4G)
Total swap area used by 0k used-(0M)
4184924k free-Total Idle swap area (4G)
2483956k cached-Buffer Swap area total (2483M)

The total amount of memory in use in row four (used) refers to the amount of memory that is now controlled by the system kernel, and the total amount of free memory that the kernel has not included in its control range. The memory that is included in kernel management is not always in use, but also includes the memory that has been used in the past that can now be reused, and the kernel does not return these reusable memory to free, so there is less memory on Linux, but don't worry about it.

If you are accustomed to calculating the number of available memory, here is an approximate formula: The fourth line of Free + fourth row of buffers + fifth row of cached, press this formula for this server's available memory: 148m+259m+2483m = 2990M.

For memory monitoring, in the top we have to monitor the fifth line swap partition used, if this value is constantly changing, indicating that the kernel is constantly in memory and swap data exchange, which is really not enough memory.

Line Six is a blank line

Line seventh below: status monitoring of each process (Task)
pid-Process ID
user-Process Owner
pr-Process Priority
The Ni-nice value. Negative values indicate high priority, positive values indicate low priority
The total amount of virtual memory used by the virt-process, in kilobytes. Virt=swap+res
The size, in kilobytes, of the physical memory used by the res-process and not swapped out. Res=code+data
shr-shared memory size, in kilobytes
S-process state. d= non-disruptive sleep status r= run s= sleep t= track/stop z= zombie Process
%cpu-percentage of CPU time that was last updated to current
Percentage of physical memory used by the%mem-process
Total CPU time used by the time+-process, Unit 1/100 sec
command-Process Name (command name/command line)

Multi-u multi-core CPU monitoring

In the top basic view, press the keypad number "1" to monitor the status of each logical CPU:

Observe that the server has 4 logical CPUs, which are actually 1 physical CPUs.

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

Process Field Sort

By default, when Top is reached, the processes are sorted by CPU consumption, the Java process with process ID 14210 in top view 01 is ranked first (CPU occupied 100%), and the Java process with process ID 14183 is ranked second (CPU consumption 12%). You can change the sort field by keyboard instructions, for example, to monitor which process consumes the most mem, I use the following method:

1. Tap the keyboard "B" (Turn on/off the highlight effect) and the top view changes as follows:

We found that the "top" process with process ID 12363 was highlighted, and the top process was the only running state (runing) that was shown in the second row of the view, and you could turn off or turn on the highlight of the running process by tapping the "Y" key.
2. Tap the keyboard "X" (The highlight effect on/off the row sequence), and the top view changes as follows:
As you can see, the top default sort column is "%cpu".
3. Use SHIFT + > or SHIFT + < To change the column to the right or left by pressing SHIFT + >:

The view is now sorted according to%mem.

Change the Process display field

1. Tap the "F" key and top into another view, where you can arrange the display fields in the base view:

Here is a list of all the process fields that can be displayed in the top Basic view, fields with "*" and uppercase letters are visible, fields without "*" and lowercase are not displayed. If you want to display the "CODE" and "DATA" two fields in the base view, you can tap the "R" and "s" Keys:

2. "Enter" to return to the basic view, you can see more "CODE" and "DATA" two fields:

Additions to the top command

The top command is the preferred command for system monitoring on Linux, but sometimes it does not reach our requirements, such as the current server, where top monitoring has a lot of limitations. This server runs the WebSphere cluster, there are two node services, is "Top view 01" in the eldest, the old 22 Java processes, the top command is the smallest monitoring unit of the process, so I do not see the number of Java threads and customer connections, And these two indicators are very important indicators of Java Web Services, usually I use PS and netstate two commands to supplement the top of the shortcomings.

To monitor the number of Java threads:

ps-elf | grep java | Wc-l

Monitor network client connections:

Netstat-n | grep TCP | grep Listening Port | Wc-l

The above two commands, can change the parameters of grep, to achieve more detailed monitoring requirements.

Under the guidance of the idea of "everything is a file" in the Linux system, the running state of all processes can be obtained by file. system root/proc, each digital subdirectory name is the PID of the running process, into any process directory, through which files or directories to observe the process of the various operational indicators, such as the task directory is used to describe the thread of the process, Therefore, you can also get the number of threads running in a process by using the following method (PID refers to the process ID):

Ls/proc/pid/task | Wc-l
There is also a command pmap in Linux to output the state of the process memory that can be used to analyze the thread stack:

Pmap PID

Everyone is familiar with Linux under the top command to see all processes of memory, CPU and other information. In addition, there are other commands that can be used to obtain more detailed information, such as process-related


Through top or Ps-ef | grep ' process name ' gets the PID of the process. This command can provide information such as the status of the process, the number of file handles, memory usage, and so on.
Vmstat-s-S M
You can view the reports that contain each item in memory, and you can specify which units to view by using-S M or-s K, which defaults to KB. With the Watch command, you can see the report of dynamic changes.

Also available Cat/proc/meminfo

To see the configuration information for the CPU available


It can display information such as CPU core number, clock frequency, CPU model, etc.

To view CPU fluctuations, especially on multi-core machines, you can use the

Mpstat-p all 10

The command can sample CPU usage at intervals of 10 seconds, each of which is displayed, for example, the idle condition of each core.
Just view the mean, available
Iostat-p All
This command can view information such as all device usage, read and write bytes, and so on.

In addition, Htop, there is time to use.

Linux view number of physical CPUs, number of cores, logical CPUs

# total number of cores = number of physical CPUs X number of cores per physical CPU

# Total Logical CPUs = number of physical CPUs x number of cores per physical CPU X number of hyper-threads

# View the number of physical CPUs

cat/proc/cpuinfo| grep "Physical ID" | Sort| uniq| Wc-l

# See the number of cores in each physical CPU (that is, the number of nuclei)

cat/proc/cpuinfo| grep "CPU Cores" | Uniq

# View the number of logical CPUs

cat/proc/cpuinfo| grep "Processor" | Wc-l

View CPU information (model number)
Cat/proc/cpuinfo | grep name | Cut-f2-d: | Uniq-c

Linux top command view memory and multi-core CPU usage Storytelling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.