"Recommended reprint" who graft my system resources php-fpm
The testers told us that games on the Facebook platform were significantly slower than the games on other platforms. I asked, is not because of fanqiang network reason. They said: No, other games are smoother. Using HttpWatch to view the HTTP request, it was found that the api.php request took 6 seconds and most of the time was red (HttpWatch red indicates the server operation time). Normally 2 seconds, now 6 seconds, the server processing time differs so much in different servers. Intuition tells: server resources.
Server overview
- Operating system: Centos5.3-64-2.6.18-164.el5
- Application Services Nginx0.6, PHP-FPM, MySQL5.1, PHP5.2, Memcached
- Memcached only cached game system settings and template parameters
Fault detection
1. Other first, top look at the CPU, RAM, swap which is more tense.
By analysis, you can see a total of 602 processes, of which 601 processes hibernate. This seems to be something wrong, the kernel process is about 80, plus memcached, Nginx, mysqld, will not exceed 90. In addition to these, only php-fpm management of the php-cgi, is it ...?
CPU display, CPU pressure is not big, can say no pressure. We looked at the memory usage profile again, found that 4G of memory, the consumption of the remaining few (free+buffers), more than 95% of the memory has been allocated. The use of interactive space, we temporarily do not care. The command top also lists the most resource-intensive processes, and the longest-running (time+) mysqld (about 2 hours) consumes less resources. In addition, looking at php-cgi, a single php-cgi occupies a lot of memory. So, you can be bold to guess: server memory resources are relatively tense, and not by a process to occupy a large amount of memory, it is possible that some suspended processes occupy memory is not released. Verify our ideas by further monitoring memory usage with free.
2. Instruction free, learn about RAM Resource usage. Of course, you can also view the file/proc/meminfo
Let's take a look at the MEM statistics, total represents the amount of physical memory, about 4G. Used, which indicates that memory has been allocated, is not represented as being used, including (buffer&cached). Free refers to unallocated memory, and buffers and cached represent memory that has been allocated but not yet in use. The second line (Buffers/cache), used means that the memory is actually used, is obtained by the first line (used-buffer-cached), and free indicates that memory that has not been used is obtained by the first line (free+buffer+cached). Swap lines represent memory exchange usage, with little (infrequently) swpd, which does not affect server performance because the system needs to swap the V-type memory pages out or adjust the size of buffer and cached. However, frequent swpd may mean that the server is running out of physical memory, less than the specified swap rating, and needs to be swapped out of memory pages.
When we view the free results, we mainly look at the second line. A glance can see 4G of memory, of which 3898M memory is used, and 49M of memory is not, are running out. This also confirms our first step of guessing that memory is exhausted. Here, we further suspect that the memory space is severely insufficient, the process will be blocked, the system will continue to exchange the unused data out so, the data will be used to read into the SI. We can further verify through the vmstat that this conjecture of ours.
3. Instruction Vmstat Monitoring Memory usage
As a memory monitor, we are more concerned about SWPD, free, si, and so. When the general system is not busy, we see that the value of SWPD,SO will not last very high, often 0. Here, we see that the SWPD value is 1.5G, and the free value is small, again indicating that there is not enough physical memory. Where SI reports the total amount of memory moved from swap to physical memory per second, so reports the total amount of memory moved from physical memory to swap area per second. Of course, si is sometimes large, and do not excessive anxiety, often encountered a program requires large memory to read and write media files, Si value will become larger. So, it is usually a memory shortage of a signal, if the long time this value has remained large, it is likely that the memory is not enough, small fluctuations can be ignored. Next, you can find the culprit that consumes memory through PS.
4. Instruction PS Identify the culprit that consumes memory
[Email protected] ~]# ps-a–sort-rss-o comm,pmem,pcpu |uniq-c |head-15
1 COMMAND%MEM%cpu
1 mysqld 0.6 0.0
503 php-cgi 0.3 0.0
5 php-cgi 0.2 0.0
1 php-cgi 0.1 0.0
1 php-cgi 0.0 0.0
1 memcached 0.0 0.0
1 sshd 0.0 0.0
1 nginx 0.0 0.0
1 sshd 0.0 0.0
1 nginx 0.0 0.0
2 bash 0.0 0.0
3 nginx 0.0 0.0
1 sshd 0.0 0.0
1 nginx 0.0 0.0
Instruction PS is more commonly used, but also relatively simple. With the results reported above, we can hit php-cgi this process at a glance. Although a single php-cgi memory is not too large, but the 503 php-cgi process, it is a bit scary. It takes up almost all of the memory (503*0.3%). We can guess that php-cgi is managed by PHP-FPM and is not properly configured to php-fpm one of the parameters, resulting in an excessive number of php-cgi processes being opened.
5. Set PHP-FPM Process Quantity management
By re-setting the Max_children value of php-conf.conf to 150, the system memory is restored to normal usage. Free, si, so, b all indicate that the memory system resources are normal and there is no pressure.
The memory freed by the php-cgi process is not immediately reclaimed by the system, and a php-cgi probably consumes 20MB of memory (depending on the PHP extensions you load). Therefore, it is necessary to limit the number of php-cgi processes you start. So, how much is this number appropriate, you can count the number of php-cgi at the peak of the server by top. can also be as PHP-FPM suggested, through NETSTAT-NP | grep 127.0.0.1:9000 to collect data by setting Max_children to make the number of waits as small as possible
6. How much memory a php-cgi occupies
A php-cgi process, about how much memory to occupy, about 20MB (specifically to see how much of your PHP load module). You can see where memory is being consumed by the PMAP directive. Therefore, try not to load unnecessary PHP extension modules, you can reduce unnecessary memory waste.
[[email protected] etc]# Pmap $ (pgrep php-cgi |head-1)
6746:/usr/local/php/bin/php-cgi–fpm–fpm-config/usr/local/php/etc/php-fpm.conf
0000000000400000 6680K r-x–/usr/local/php/bin/php-cgi
0000000000c86000 268K rw-/usr/local/php/bin/php-cgi
0000000000cc9000 56K rw-[Anon]
0000000005012000 2240K rw-[Anon]
0000003efd200000 112K r-x–/lib64/ld-2.5.so
.......
00002ac28a7a5000 2048k-–/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so
00002ac28a9a5000 4K rw-/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so
00002ac28a9a6000 84K r-x–/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so
00002ac28a9bb000 2048k-–/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so
00002ac28abbb000 8K rw-/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so
00002ac28abbd000 32K rw-[Anon]
00002ac28abd4000 40K r-x–/lib64/libnss_files-2.5.so
00002ac28abde000 2044k-–/lib64/libnss_files-2.5.so
00002ac28addd000 4K r--/lib64/libnss_files-2.5.so
00002ac28adde000 4K rw-/lib64/libnss_files-2.5.so
00007fffa717e000 84K rw-[Stack]
ffffffffff600000 8192k-–[Anon]
Total 154172K
?