1. Socket communication in Unix domain Before a brief introduction of Unix Domain Socket communication method, see: Nginx + PHP-FPM Domain Socket configuration method
Because the Unix domain Socket does not go through the network, it can indeed improve the communication performance between Nginx and php-fpm, but in High concurrency is unstable. Nginx will frequently report errors: Connect () to unix:/dev/shm/php-fcgi.sock failed (11: Resource temporarily unavailable) while connecting to upstream
The following two methods can be used to improve stability: 1) increase the backlog in nginx and php-fpm. Configuration method: add default backlog = 1024 after listen 80 under the server of this domain name in the nginx configuration file.
Also configure listen. backlog in the php-fpm.conf to 1024, default to 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 balance the request load to the two php-fpm instances behind the two sock files. 2. php-fpm parameter optimization 2.1 processes
Php-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 max number of requests processed
The maximum number of requests processed is the number of requests processed by a php-fpm worker process that is terminated. Respawn a new one. The main purpose of this configuration is to avoid third-party libraries referenced by the php interpreter or program. Memory leakage. Pm. max_requests = 10240 2.3 Maximum execution time The maximum execution time can be configured in both php. ini and php-fpm.conf, with the configuration items max_execution_time and request_terminate_timeout, respectively. For more information about the roles and impacts of these errors, see Section 502 and 504 errors in Nginx. 3. php-fpm high CPU usage troubleshooting method 3.1CPU usage monitoring method 1) top command Run the top command and enter 1 to view the CPU usage of each core. In addition, the top-d 0.1 can shorten the sampling time. The sar below seems to be at least 1 second. 2) sar commands Install the 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 indicates that ALL the cores are monitored. 1 indicates that the data is collected every 1 second, and 100 indicates that the data is collected 100 times. The output result is as follows: CPU % user % nice % system % iowait % steal % idle All 85.54 0.00 5.69 0.00 0.00 0 74.75 0.00 25.25 0.00 0.00 1 98.00 0.00 2.00 0.00 0.00 0.00 2 89.22 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.00 0.00 15.84 15 78.79 0.00 4.04 0.00 0.00 17.17 3.2 enable slow log Configure and output the php-fpm slow log with a threshold of 2 seconds:
Request_slowlog_timeout = 2
Slowlog = log/$ pool. log. slow
Use the sort/uniq command to analyze and summarize slow php-fpm logs: [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 description: Sort: sorts words. Uniq-c: displays a unique row and adds the number of times this row appears in the file to the beginning of each row. Sort-k1, 1nr: sort by the first field, numerical value, and backward Head-10: get the first 10 rows of data 3.3 Use strace to track processes 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: calculate the execution time, number of times, and number of errors of each system call. -D: output strace debugging information about standard errors. -F tracks the sub-processes generated by the fork call. -O filename, the trace results of all processes are output to the corresponding filename -F attempts to trace vfork calls. in-f, vfork is not tracked. -H outputs brief help information. -I output the entry pointer of the system call. -Q: Do not output the message about the disconnection. -R prints the relative time, which is called by every system. -T add time information before each row in the output. -Tt adds time information before each row in the output, in microseconds. -Ttt microsecond-level output, expressed in seconds. -T shows the time consumed by each call. -V outputs all system calls. some calls about environment variables, status, input and output are not output by default due to frequent calls. -V outputs the version information of strace. -X outputs non-standard strings in hexadecimal format -Xx all strings are output in hexadecimal format. -A column Set the output position of the returned value. The default value is 40. -E execve only records system calls such as execve. -P main process number 2) you can also use the-c parameter to make strace help summary, 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 callerrors 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.000000 0 13 getsockopt 0.00 0.000000 0 8 getdents 0.00 0.000000 0 26 chdir 0.00 0.000000 0 1 futex -------------------------------------------------------------- 100.00 0.029344 18000 986 total Ps: You can use strace to learn the explain execution process of the php interpreter. 3.4 accelerate PHP interpretation If your program does not have any problems, you just need to execute too many operations and cannot optimize it. You can use PHP accelerators such as APC or xcache to reduce the time consumed by the CPU to interpret PHP files. These PHP accelerators generate intermediate code opcode when interpreting the PHP file for the first time, so the subsequent execution will be much faster and some CPU operations will be reduced. The following uses xcache as an example, See how to install and configure. The xcache installation command is as follows./configure has many parameters that do not know what to use. it is not described on the official website. Therefore, only enable-xcache is enabled: 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 Make install The configuration in php. ini is as follows. The most important parameter is the red parameter. generally, xcache. size is recommended based on the number of php files. xcache. count is the same as the number of CPU cores:
[Xcache. admin]
Xcache. admin. enable_auth = Off
Xcache. admin. user = "xcache"
Xcache. admin. pass = ""
[Xcache]
Xcache. shm_scheme = "mmap"
Xcache. size = 1024 M
Xcache. count = 16
Xcache. slots = 8 K
Xcache. ttl = 0
Xcache. gc_interval = 0
Xcache. var_size = 16 M
Xcache. var_count = 1
Xcache. var_slots = 8 K
Xcache. var_ttl = 0
Xcache. var_maxttl = 0
Xcache. var_gc_interval = 300
Xcache. test = Off
Xcache. readonly_protection = Off
; Xcache. readonly_protection = On
Xcache. mmap_path = "/dev/zero"
; Xcache. mmap_path = "/tmp/xcache"
Xcache. coredump_directory = ""
Xcache. cacher = On
Xcache. stat = On
Xcache. optimizer = Off
[Xcache. coverager]
; Xcache. coverager = On
; Xcache. coveragedump_directory = ""
A common problem is that an error is reported when php-fpm is started: 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 create a directory. After restarting the php-fpm service, use the top command to check whether the VIRT of each worker process (including the swap area) is xcache. size, but the REQ becomes small. The above configuration is used to shorten the peak time of CPU usage, but the peak time is still more than 90% for all cores. In addition, the/dev/zero configuration method often causes Nginx 502 errors during high concurrency. /Tmp/xcache and readonly_protection are stable. 4. php program performance monitoring The common method is to enable the xdebug performance monitoring function and analyze the xdebug output result through WinCacheGrind software. For how to install xdebug and debug it with IDE, see Vim + XDebug to Debug PHP. These items configured in php. ini are output performance information:
Xdebug. auto_trace = on
Xdebug. auto_profile = on
Xdebug. collect_params = on
Xdebug. collect_return = on
Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "/tmp"
Xdebug. profiler_output_dir = "/tmp"
In this way, XDebug outputs the performance data of all php functions, but the generated files are also relatively large. You can disable some options such as collect_params and collect_return, To reduce the output data volume. Or disable automatic output. you can call the xdebug function at the beginning and end of the function to monitor the specified function. The output file name is cachegrind.out.1277560600and trace.3495983249.txt. you can use WinCacheGrind on Windows to perform graphical analysis. The usage of WinCacheGrind is described on the Internet. -C: calculate the execution time, number of times, and number of errors of each system call. -D: output strace debugging information about standard errors. -F tracks the sub-processes generated by the fork call. -O filename, the trace results of all processes are output to the corresponding filename -F attempts to trace vfork calls. in-f, vfork is not tracked. -H outputs brief help information. -I output the entry pointer of the system call. -Q: Do not output the message about the disconnection. -R prints the relative time, which is called by every system. -T add time information before each row in the output. -Tt adds time information before each row in the output, in microseconds. -Ttt microsecond-level output, expressed in seconds. -T shows the time consumed by each call. -V outputs all system calls. some calls about environment variables, status, input and output are not output by default due to frequent calls. -V outputs the version information of strace. -X outputs non-standard strings in hexadecimal format -Xx all strings are output in hexadecimal format. -A column Set the output position of the returned value. The default value is 40. -E execve only records system calls such as execve.
-P main process number |