Analysis and location of common commands and server exceptions in Linux

Time:2021-4-8

1、 Introduction

2、 Common commands

#CentOS installation system status command, the following performance indicators command can not try to install
yum install -y sysstat

Machine performance index command

1. Top command

#View system performance command
top -H

#- h can view all threads started by a process, P specifies the process to be viewed
top -Hp 6106

Analysis and location of common commands and server exceptions in Linux

  • Load average: three groups of numbers are respectively expressed as 1 minute, 5 minutes and 15 minutes of CPU load. Take a single core machine as an example. Load = 0.5 means that the CPU still has half of the resources to process other thread requests. Load = 1 means that all the resources of the CPU are processing requests, and there are no remaining resources to use. Load = 2 means that the CPU is overloaded, and another double of the threads are waiting to be processed. In multi-core processors, your load average should not be higher than the total number of processor cores.

Note: this command is used to view the whole system performance from a global perspective.

2. Uptime command

#A simplified version of the top command
[[email protected] home]# uptime
 22:43:42 up  1:06,  2 users,  load average: 0.97, 0.74, 0.40

CPU performance index command

1. Vmstat command

#Samples are taken every two seconds for three times
[[email protected] home]# vmstat -n 2 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 427172   2116 342488    0    0    23     4  494  564  3  7 91  0  0
 1  0      0 427172   2116 342520    0    0     0    65 4586 4940 20 26 54  0  0
 2  0      0 427172   2116 342524    0    0     0     0 4589 4940 20 25 54  0  0
  • procs
project meaning
r In principle, the number of processes running and waiting for CPU time slice should not exceed 2 for CPU with 1 core, and the running queue of the whole system should not exceed 2 times of the total number of cores
b The number of processes waiting for resources, such as waiting for disk I / O, network I / O, etc
  • cpu
project meaning
us The percentage of CPU time consumed by the user process is high, which means that the user process consumes more CPU time. If it is more than 50% for a long time, the program needs to be optimized
sy The percentage of CPU time consumed by kernel process, US + sy reference value is 80%, exceeding this threshold indicates that the CPU load is too large
id Percentage of idle CPUs
wa The percentage of CPU time the system waits for Io
st Percentage of CPU time stolen from a virtual machine

Note: this command is mainly used to view some indicators of CPU (including but not limited to).

2. Mpstat (multiprocessor Statistics) command

#Check the information of all CPU cores. Sample every 2 seconds for three times. The - P {all} indicates which CPU to monitor, and the value of CPU is in [0, number of CPUs - 1]
mpstat -P ALL 2 3

Analysis and location of common commands and server exceptions in Linux

Note: view all CPU core information, its report and some statistical information of CPU, which are stored in / proc / STAT file. In the multi CPUs system, it can not only view the average status information of all CPUs, but also view the information of specific CPUs.

3. Pidstat command

#Check the CPU information, sample once every second, a total of 5 times, - P specifies the specific process number
pidstat -u 1 5 -p 1796

#Check the memory information, sample once per second, 5 times in total, - P specifies the specific process number
pidstat -r 1 5 -p 1796

#Check the disk information, sample once every second, 5 times in total, - P specifies the specific process number
pidstat -d 1 5 -p 1796

Analysis and location of common commands and server exceptions in Linux

Analysis and location of common commands and server exceptions in Linux

Analysis and location of common commands and server exceptions in Linux

Description: view a specific process using CPU information.

Memory index command

1. Free command

#Default unit: KB
free

#Specified unit MB
free -m

#Specified unit GB
free -g

Analysis and location of common commands and server exceptions in Linux

Disk index command

1. DF command

#View free disk space
df -h

Analysis and location of common commands and server exceptions in Linux

IO index command

1. Iostat command (disk IO)

#Check the disk IO status, sampling once in 2 seconds, three times in total
iostat -xdk 2 3

Analysis and location of common commands and server exceptions in Linux

project meaning
rkB/s Data read per second KB
wkB/s Write data per second KB
svctm Average service time of IO requests, in milliseconds
await The average waiting time of IO requests, in milliseconds. The smaller the value, the better
util What percentage of a second is spent on IO operation? When it is close to 100%, it means that the disk bandwidth is full and needs to be optimized or added

Note: the long-term large data read-write of RKB / s and WKB / S is certainly not a normal phenomenon; the value of svctm is very close to await, which means that there is almost no IO waiting and the disk performance is good. If the await value is much higher than the value of svctm, it means that the IO waiting time is too long and needs to be optimized.

1. Ifstat command (network IO)

#View network IO status
ifstat 1

3、 Server exception analysis and location

1. Analysis and orientation of Java program’s high CPU occupation

Make a simple java program of CPU intensive computing, and then run it on the server. This example is used to simulate the scene of high CPU consumption in the production environment.
Analysis and location of common commands and server exceptions in Linux

#Compile
javac WhileTrueDemo.java -d

#Run the sample program, resulting in CPU intensive computing
nohup java com.jayway.WhileTrueDemo &

Specific steps

  • First, use the machine performance command top to find the process with the best CPU proportion

Analysis and location of common commands and server exceptions in Linux

Note: through the top command, we can see that the CPU occupation of the process with PID 6106 is too large.

  • Using PS and JPS to further locate
#Use the PS command to further view the Java program
[[email protected] home]# ps -ef | grep java | grep -v grep
root       6106   1460 99 22:09 pts/0    00:52:15 java com.jayway.WhileTrueDemo

#Using java commands to view
[[email protected] home]# jps -l
6106 com.jayway.WhileTrueDemo
6238 sun.tools.jps.Jps
  • Navigate to a specific thread or code
#-M shows all threads
#-How long does the PID process use CPU
#-O this parameter is followed by a user-defined format
ps -mp 6106 -o THREAD,tid,time

Analysis and location of common commands and server exceptions in Linux

Note: as can be seen from the figure above, 6107 thread takes up the most CPU and takes up the most CPU time.

  • Convert the required thread ID to hexadecimal English lowercase format
#The result is: 17dB
printf "%x\n" 6107
  • Java program uses jstack command to view Java stack information and locate specific problems
#The process number is 6106, and the problem thread number is 17dB
jstack 6106 | grep 17db -C10

Analysis and location of common commands and server exceptions in Linux

Note: as you can see from the figure above, the eighth line of the whiletruedemo file is the root of the problem.

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]