1.Unix Domain Socket Communication I have briefly introduced the Unix Domain Socket communication method before, see: Nginx+PHP-FPM domain Socket configuration method Unix Domain Socket because Without using the network, the performance of communication between Nginx and php-fpm can indeed be improved, but it will be unstable when the concurrency is high. Nginx will frequently report errors: Connect() to unix:/dev/shm/php-fcgi.sock failed (11: Resource temporarily unavailable) while connecting to upstream Yes Improve stability through the following two methods: 1) Increase the backlog in nginx and php-fpm The configuration method is: under the server of this domain name in the nginx configuration file, add default backlog= after listen 80 1024. At the same time, configure listen.backlog in php-fpm.conf to 1024, and the default is 128. 2) Increase the number of sock files and php-fpm instances Create a new sock file, and use the upstream module in Nginx to load balance requests to the two sets of php-fpm instances behind the two sock files superior. 2.php-fpm parameter tuning 2.1 Number of processesphp-fpm initial/idle/maximum number of worker processes pm.max_children = 300 pm .start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35
2.2 Maximum processing requests Number The maximum number of requests processed refers to A php-fpm worker process will be terminated after processing a few requests, and the master process will respawn a new one. The main purpose of this configuration is to avoid memory leaks caused by the php interpreter or third-party libraries referenced by the program.
pm.max_requests = 10240 2.3 Maximum execution time The maximum execution time can be configured in both php.ini and php-fpm.conf. The configuration items are max_execution_time and request_terminate_timeout respectively. For its role and impact, please refer to: Detailed explanation of 502 and 504 errors in Nginx 3. High CPU usage troubleshooting method of php-fpm 3.1CPU usage monitoring method 1) top command After directly executing the top command , enter 1 to see the CPU usage of each core. And the sampling time can be shortened by top -d 0.1. The sar below seems to be shortest only 1 second. 2) sar command Installation of sar and iostat commands: sysstat.x86_64: The sar and iostat system monitoring commands yum install -y sysstat.x86_64 Execute sar -P ALL 1 100. -P ALL means monitoring all cores, 1 means collecting every 1 second, 100 means collecting 100 times. The output results are as follows: pcpu%user%nice%System%Iowait%STEAL%Idle all 85.54 0.00 5.69 0.00 0.00 8.76 0.00 25.25 0.00 0.00 0.00 0.00 0.00 2 0.00 3.92 0.00 0.00 6.86 3 91.00 0.00 2.00 0.00 0.00 7.00 4 75.00 0.00 9.00 0.00 0.00 16.00 5 94.95 0.00 5.05 0.00 0.00 0.00 6 95.00 0.00 4.00 0.00 0.00 1.00 7 87.88 0.00 4.04 0.00 0.00 8.08 8 93.94 0.00 3.03 0.00 0.00 3.03 9 88.00 0.00 3.00 0.00 0.00 9.00 10 89.11 0.00 2.97 0.00 0.00 7.92 11 82.35 0.00 3.92 0.00 0.00 13.73 12 73.27 0.00 7.92 0.00 0 .00 18.81 13 81.44 0.00 4.12 0.00 0.00 14.43 14 77.23 0.00 6.93 0.0 0 0.00 15.84 15 78.79 0.00 4.04 0.00 0.00 17.17 3.2 Turn on the slow logConfigure the output php-fpm slow log, the threshold is 2 seconds: request_slowlog_timeout = 2 slowlog = log/$pool.log. slowUse the sort/uniq command to analyze and summarize the php-fpm slow log: [root@b28-12 log]# grep -v "^$" www.log.slow.tmp | cut -d " " -f 3,2 | sort | uniq -c | sort -k1,1nr | head -n 50 5181 run() /www/test.net/framework/web/filters/CFilter.php:41 5156 filter() /www/test.net/framework/web/filters/CFilterChain.php:131 2670 = /www/test.net/index.php 2636 run() /www/test.net/application/controllers/survey/index .php:665 2630 action() /www/test.net/application/controllers/survey/index.php:18 2625 run() /www/test.net/framework/web/actions/CAction.php:75 2605 runWithParams() /www/test.net/framework/web/CController.php:309 2604 runAction() /www/test.net/framework/web/filters/CFilterChain.php:134 2538 run() / www/test.net/framework/web/CController.php:292 2484 runActionWithFilters() /www/test.net/framework/web/CController.php:266 2251 run() /www/test.net/framework/ web/CWebApplication.php:276 1799 translate() /www/test.net/application/libraries/Limesurvey_lang.php:118 1786 load_tables() /www/test.net/application/third_party/php-gettext/gettext.php:254 1447 runController() /www/test.net/framework/web/CWebApplication.php:135 Parameter explanation: sort: Sort words Uniq -c: Display unique lines, and add the number of times this line appears in the file at the beginning of each line Sort -k1,1nr: Sort by the first field, value, and Reverse order Head -10: Get the first 10 rows of data 3.3 Use strace to track the process 1) Use nohup to convert strace to background execution until the php-fpm process on attach dies: nohup strace -T -p 13167 > 13167-strace.log & Parameter description: -c Count the execution of each system call Time, number of times, number of errors, etc. 2) You can also use the -c parameter to let strace help summarize, which is very convenient and powerful! [root@b28-12 log]# strace -cp 9907 Process 9907 attached - interrupt to quit Process 9907 detached % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 56.61 0.016612 5 3121 read 11.11 0.003259 1 2517 715 stat 8.04 0.002358 7 349 brk 6.02 0.001767 1 1315 poll 4.28 0.001255 6 228 recvfrom 2.71 0.000796 1 671 open 2.54 0.000745 0 2453 fcntl 2.37 0.000696 1 1141 write 1.69 0.000497 1 593 13 access 1.37 0.000403 0 1816 lseek 0.89 0.000262 1 451 22 sendto 0.56 0.000163 1 276 208 lstat 0.49 0.000145 0 384 getcwd 0.31 0.000090 0 1222 fstat 0.28 0.000082 0 173 munmap 0.26 0.000077 0 174 mmap 0.24 0.000069 2 41 socket 0.23 0.000068 0 725 close 0.00 0.000000 0 13 rt_sigaction 0.00 0.000000 0 13 rt_sigprocmask 0.00 0.000000 0 1 rt_sigreturn 0.00 0.000000 0 78 setitimer 0.00 0.000000 0 26 26 connect 0.00 0.000000 0 15 2 accept 0.00 0.000000 0 39 recvmsg 0.00 0.000000 0 26 shutdown 0.00 0.000000 0 13 bind 0.00 0.000000 0 13 getsockname 0.00 0.000000 0 65 setsockopt 0.00 0.00 0000 0 13 getsockopt 0.00 0.000000 0 8 getdents 0.00 0.000000 ,,,,,,,,,,,,,,,,,,,,,,,,, , ---- ----------- ----------- --------- --------- ------ ---------- 100.00 0.029344 18000 986 total ps: You can use strace to learn the interpretation and execution process of the PHP interpreter 3.4 Accelerate PHP interpretation and execution If there is indeed no problem with your program, just execute it There are too many operations and there is no way to optimize. Then consider using a PHP accelerator such as APC or xcache to reduce the time it takes for the CPU to interpret PHP files. These PHP accelerators will generate intermediate code opcode when the PHP file is first interpreted, so subsequent execution will be much faster and reduce some CPU operations. Let’s take xcache as an example to see how to install and configure it. The command to install xcache is as follows. There are many parameters in ./configure and I don’t know what they are used for. There is no explanation on the official website, so I only turned on --enable-xcache: tar zxvf xcache-3.0.3.tar.gz / usr/local/php/bin/phpize ./configure --with-php-c/local/php/bin/php-config --enable-xcache make The configuration in make install php.ini is as follows, the most important The two parameters are marked in red. It is generally recommended that xcache.size be determined according to the number of PHP files. xcache.count is the same as the number of CPU cores: [xcache.admin]xcache.admin.enable_auth = Offxcache.admin.user = "xcache"FAQ An error will be reported when starting php-fpm: Cannot open or create file set by xcache.mmap_path, check the path permission or check xcache.size/var_size against system limitation This is because /tmp/xcache is a file and cannot be created into a directory. After restarting the php-fpm service, use the top command to observe that the VIRT (including the swap area) of each worker process is the size of xcache.size, but the REQ has become very small. Using the above configuration has shortened the peak time of CPU usage, but all cores will still reach more than 90% at the peak time. I don’t know if there is no correct configuration. In addition, when concurrency is high, the configuration method of /dev/zero often causes Nginx 502 errors. /tmp/xcache and enabling readonly_protection are very stable. 4.php program performance monitoring The common method is to turn on the performance monitoring function of xdebug and analyze the xdebug output results through WinCacheGrind software. For the installation of xdebug and the method of debugging with IDE, please refer to: Vim+ .auto_profile = on xdebug.collect_params = onxdebug.collect_return = onxdebug.profiler_enable = on xdebug.trace_output_dir = "/tmp"xdebug.profiler_output_dir = "/tmp" will output All performance data for executing PHP functions, but the files generated will also be larger. You can turn off some options such as collect_params, collect_return, to reduce the amount of output data. Or turn off automatic output and monitor the specified function by calling the xdebug function at the beginning and end of the function you want to monitor.-i outputs the entry pointer of the system call. - q disables the output of detachment messages. -r prints the relative time for each system call. -t prepends the time information to each line in the output. -tt prepends each line in the output Time information, microsecond level. -ttt microsecond level output, time expressed in seconds. -T displays the time consumed by each call. -v outputs all system calls. Some calls are related to environment variables, status, Input and output calls are not output by default due to frequent use. -V outputs strace version information. -x outputs non-standard strings in hexadecimal form -xx All strings are output in hexadecimal form. -a column Set the output position of the return value. The default is 40. -e execve only records system calls such as execve -p main process number
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. The above has introduced a summary of Nginx+PHP-FPM optimization skills, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials. |