preface
General Internet project is deployed on Linux server, if Linux server out of the question, so let’s learn at ordinary times high concurrency, there’s no point in stability, so the performance of the Linux grasp it is very important, of course, many students may think that these are operational classmates, but I don’t think so, Whether you are an architect or a CRUd boy, it is a very important basic quality to have overall control of the project, so I summarized this article, I hope it is useful for you, if you think my writing is good, remember to like it, click again. Let’s cut to the chase without further ado.
Factors affecting Linux server performance
cpu
CPU is fundamental to the stable operation of an operating system. The speed and performance of THE CPU largely determine the overall performance of the system. Therefore, the more cpus and higher frequency, the better the performance of the server.
memory
Memory size is also an important factor affecting Linux performance. If memory size is too small, system processes will be blocked and applications will become slow or even unresponsive. The memory is too large, resulting in resource waste.
Disk I/o
The DISK I/O performance directly affects the application performance. If the DISK I/O performance is not satisfied in an application with frequent read and write operations, the application will stall. Fortunately, today’s disks use many methods to improve I/O performance, such as the common disk RAID technology.
network
Linux applications are generally network-based. Therefore, network bandwidth is also an important factor affecting performance. A low-speed and unstable network blocks access to network applications, while a stable and high-speed network bandwidth ensures unimpeded running of applications. Fortunately, today’s networks tend to be gigabit or fiber-optic, and the impact of bandwidth issues on application performance is gradually decreasing.
Common Linux performance monitoring commands
Uptime (Overall System Performance Evaluation)
Note that the output value of load Average cannot be larger than the number of system cpus
So how do you check the number of cpus?
View system CPU information
Cat /proc/cpuinfo information
CPU cores refer to the number of CPU cores
You can also use the cat/proc/cpuinfo | grep “cores” | uniq direct view
[root@test ~]# cat /proc/cpuinfo |grep "cores"|uniqcpu cores : 2
Copy the code
CPU performance evaluation
Use the vmstat command to monitor the CPU
Vmstat 2 10 (Monitoring 10 times every 2 seconds)
procs
R indicates the number of processes running and waiting for the CPU time slice. If the value is greater than the number of cpus for a long time, you need to add system cpus
B indicates the number of processes waiting for resources
CPU
The US column shows the percentage of CPU time consumed by user processes. When us is high, it indicates that user processes consume a lot of CPU time. If it is higher than 50% for a long time, programs and algorithms need to be optimized
The SY column shows the percentage of CPU time consumed by the kernel process. A high SY value indicates that the kernel is consuming a lot of CPU resources
According to experience, the reference value of US + SY is 80%. If us+ SY is greater than 80%, CPU resources may be insufficient.
Monitor system CPU with SAR command
The SAR command adds overhead but has little impact
Yum install sysstat Install SAR command
Sar-u 3 5 (u displays the load status of all cpus in the system during the sampling time)%user: indicates the percentage of CPU time consumed by user processes
%nice: indicates the percentage of CPU consumed by a properly running process
%system: indicates the percentage of CPU time consumed by the system
% ioWAIT: indicates the percentage of CPU time occupied by AN I/O wait
%steal: there is a steal operation that PageIn forces for different pages in a tight environment
%idle: Indicates the percentage of idle CPU time
Memory performance evaluation
Monitor memory using the free instruction
Free -m # Check the memory usage in mThere is generally such an empirical formula:
If the available memory of applications/system physical memory is greater than 70%, the system memory is sufficient and does not affect system performance.
If the available memory of applications/physical memory of the system is less than 20%, the system memory is insufficient and needs to be increased.
20%< application available memory/system physical memory <70% : The system memory meets application requirements and does not affect system performance temporarily.
Vmstat is used to monitor memory
SWPD represents the amount of memory switched to memory swap (k). If the value of SWPD is not 0 or relatively large, but the value of SI and SO is 0 for a long time, there is no need to worry about this situation, which will not affect performance
Free indicates the amount of free physical memory
Buffer indicates the memory size of the buffers cache. The buffer is used only for reading and writing data of the device
Cache indicates the amount of memory used for page cached. If the cache value is large, the number of cached files is large. If the BI value in the I/O is small, the file system efficiency is high
Si represents disk call to memory, which is the amount of memory into memory swap
So refers to the amount of memory transferred from memory to disk, that is, the value of so SI is 0 under normal circumstances. If the value of SI SO is not 0 for a long time, the system memory is insufficient and needs to be added
Disk I/O performance evaluation
iostat
iostat -d 2 10
Blk_reads/s Number of data reads per second Blk_wrtn/s Number of data reads per second Blk_read Number of read data blocks Blk_wrtn Number of written data blocksCopy the code
There is no standard for these values and it is definitely abnormal if they are large for a long time
Evaluate disk performance using SAR
sar -d 2 5 Await average wait time per device I/O operation (ms)
SVCTM Average Service time of each DEVICE I/O operation (ms)
%util What percentage of a second is spent on I/O operations
criteria
Normally SVCTM should be less than await, the value of SVCTM depends on disk performance, CPU memory, etc
If the value of SVCTM is similar to the value of await, there is almost no I/O waiting and the disk is performing well. If the value of await is much higher than SVCTM, the I/O queue is waiting too long and the application on the system will be slow. In this case, the problem can be solved by replacing the disk with a faster disk
The smaller the %util is, the better. If %util is close to 100%, the disk is generating too many I/O requests and the I/O system is working at full load, which can be resolved by optimizing the program or switching to a faster disk
Network Performance evaluation
Run the ping command to check the network connectivity.
The netstat -I command is used to check the status of network interfaces.
Run the netstat -r command to check the system routing table.
The network running status of the system is displayed by sAR-n combination (sar-n DEV 5 3).
Common analysis:
Query the IP addresses with the most TCP connections:
sudo netstat -pant | grep ":22" | awk '{print $5}' | awk -F":" '{print $4}' |sort|uniq -c|sort -nr
Copy the code
Explanation:
Awk -f “:” ‘{print $4}
Sort -nr, -n in numerical order, -r in reverse order, from large to small.
Uniq -c deletes duplicate lines. -c adds the number of occurrences of each line.
The netstat command is a very useful tool for monitoring TCP/IP networks, showing routing tables, actual network connections, and status information for each network interface device.
netstat -pant
Parameter -p: displays the program id and program name of the Socket being used.
Parameter -a: displays the sockets of all connections.
Parameter -n: Uses the IP address directly instead of the DNS server.
Parameter -t: displays the connection status of TCP.
Extract the top 10 IP addresses that access the nginx server.
cat access.log | awk ‘{print $1}’ | sort | uniq -c | sort -nr | head -n10