In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you what to do if there are not enough PHP-FPM processes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Feel that the number of PHP-FPM processes is not enough?
As a phper, the most frequently used architecture is LNMP. Every time the traffic comes, our service will change from hundreds of milliseconds to a few seconds. At this time, we speculate that mysql has slow sql,redis, not enough key,php-fpm processes, and so on. The above situation can be checked through some logs of the business. What we are mainly proving this time is the practice of insufficient number of php-fpm processes.
Reproduce the scene
1. Adjust the number of PHP-FPM processes on my local to 2
# vim / etc/php-fpm.d/www.confpm = staticpm.max_children = 2
two。 Use ab to stress test the interface
$ab-c 40-n 3000 http://127.0.0.1/group/check_groupsServer Software: nginx/1.16.0Server Hostname: miner_platform.cnServer Port: 80Document Path: / group/check_groupsDocument Length: 44 bytesConcurrency Level: 40Time taken for tests: 29.384 secondsComplete requests: 3000Failed requests: 0Write errors: 0Total transferred: 699000 bytesHTML transferred: 132000 bytesRequests per second: 102.10 [# / sec] (mean) Time per request: 391.788 [ms] (mean) Time per request: 9.795 [ms] (mean) Across all concurrent requests) Transfer rate: 23.23 [Kbytes/sec] receivedConnection Times (ms) min mean [+ /-sd] median maxConnect: 0 0.2 3Processing: 306 344 80.6 318 3558Waiting: 306 343 80.5318 3555Total: 307 344 80.6 318 3558Percentage of the requests served within a certain time (ms) 50% 318 66% 322 75% 333 80% 369 90% 428 95% 461 98% 508 99% 553 100% 3558 (longest request)
Try to solve the problem
1. PHP-FPM STATUS
We found that there are interfaces from 318ms to 3.558s, so how do we know that there are not enough php-fpm processes to cause this problem? To put it another way, is there any way to let us know that php-fpm can't handle it? At this point we need to open the php-fpm built-in status.
For more information, please see: https://www.yisu.com/php-weizijiaocheng-485633.html
Curl http://127.0.0.1/status.phppool: wwwprocess manager: staticstart time: 29/Nov/2021:18:27:38 + 0800start since: 6493accepted conn: 3136listen queue: 38max listen queue: 39listen queue len: 128idle processes: 0active processes: 2total processes: 2max active processes: 2max children reached: 0slow requests: 0
For more detailed fields, please see the link above. For detailed instructions, we will mainly talk about the following parameters.
Listen queue: this is the number of accept queues in which our php-fpm is on the server side at the moment.
Max listen queue: the maximum number of waiting connections since the php-fpm process was started (to put it bluntly, the maximum persistence of listen queue mentioned above)
Listen queue len: students who have experience in socket network programming know that. Int listen (int sockfd, int backlog); yes, you can set this parameter, but it has something to do with the system setting.
2. Netstat to view link status
We have come to the conclusion that when the php-fpm process cannot handle it, the request will be placed in the accept queue, and once we know this, we don't even need to go through status.
The first line represents the listening socket, and the Recv-Q represents the accept queue length.
$netstat-antp | grep php-fpmtcp 38 0 127.0.0.1 grep php-fpmtcp 9000 0.0.0.0 LISTEN 97/php-fpm: master tcp 8 0 127.0.1 LISTEN 97/php-fpm 9000 127.0.1 master tcp 55540 ESTABLISHED 964/php-fpm: pool w tcp 8 0127.0.1 LISTEN 97/php-fpm 9000 127. 0.0.1:55536 ESTABLISHED 965/php-fpm: pool w
To sum up, we know that when the number of PHP-FPM processes is not enough, the accept queue length of connections requested by nginx clients becomes larger. Is that it? No, we also need to analyze why we can get this phenomenon.
Principle analysis
A brief introduction to the working process of PHP-FPM
First of all, we need to talk briefly about the working process of php-fpm. Let's simply model its pseudocode (here just to describe the whole socket process)
/ / 1. Create socket$socket = socket_create (AF_INET, SOCK_STREAM, 0); / / 2. Bind socketsocket_bind ($socket, "0.0.0.0", 9000); / / 3. Monitor socketsocket_listen ($socket, 5); for ($iPhp socket)
We know about how php-fpm works, and at this point we need to know roughly how nginx interacts with php-fpm through a request.
$curl http://miner_platform.cn/group/check_groups{"code":10006,"message":"sign\u65e0\u6548."}
1.nginx system call
All the points that need to be paid attention to are commented in this. What is fetched is the nginx work process
$strace-f-s 64400-p 958 strace: Process 958 attached epoll_wait (8, [{EPOLLIN, {u32mm 1226150064, u64mm 94773974503600}}], 512,-1) = 1 accept4 (6, {sa_family=AF_INET, sin_port=htons (46616), sin_addr=inet_addr ("127.0.0.1")}, [112-> 16], SOCK_NONBLOCK) = 3 epoll_ctl (8, EPOLL_CTL_ADD, 3, {EPOLLIN | EPOLLRDHUP | EPOLLET, {u32mm 1226159737, u64mm 94773974513273}) = 0 epoll_wait (8, [{EPOLLIN ) = 1 recvfrom (3, "GET / group/check_groups HTTP/1.1\ r\ nUser-Agent: curl/7.29.0\ r\ nHost: miner_platform.cn\ r\ nAccept: * / *\ r\ n\ r\ n", 1024,0, NULL, NULL) = 99 stat ("/ data/miner_platform/src/public/group/check_groups") 0x7ffcb593d1b0) =-1 ENOENT (No such file or directory) stat ("/ data/miner_platform/src/public/group/check_groups", 0x7ffcb593d1b0) =-1 ENOENT (No such file or directory) epoll_ctl (8, EPOLL_CTL_MOD, 3, {EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET, {u32 / data 94773974513273}) = 0 lstat ("/ data", {st_mode=S_IFDIR | 0777, st_size=4096,...}) = 0 lstat ("/ data/miner_platform", {st_mode=S_IFDIR | 0777, st_size=4096) ) = 0 lstat ("/ data/miner_platform/src", {st_mode=S_IFDIR | 0777, st_size=4096,...}) = 0 lstat ("/ data/miner_platform/src/public", {st_mode=S_IFDIR | 0777, st_size=4096,...}) = 0 getsockname (3, {sa_family=AF_INET, sin_port=htons (80), sin_addr=inet_addr ("127.0.0.1")}, [112-> 16]) = 0 / / 1. Create socket socket (AF_INET, SOCK_STREAM, IPPROTO_IP) = 11 ioctl (11, FIONBIO, [1]) = 0 epoll_ctl (8, EPOLL_CTL_ADD, 11, {EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET, {u32 = 1226163953, u64 = 94773974517489}) = 0 / 2. Connect 127.0.0.1 sin_port=htons 9000 connect (11, {sa_family=AF_INET, sin_port=htons (9000), sin_addr=inet_addr ("127.0.0.1")}, 16) =-1 EINPROGRESS (Operation now in progress) epoll_wait (8, [{EPOLLOUT, {u32 * 1226159737, u64 * * 94773974513273}}, {EPOLLOUT, {u32 * 1226163953, u64 * 94773974517489}}, 51260000) = 2 getsockopt (11, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 / 3. Write this request writev (11) in accordance with the FASTCGI protocol [{iov_base= "\ 1\ 1\ 0\ 1\ 0\ 0\ 0\ 1\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 4\ 0\ 1\ 2!\ 7\ 0\ 17) SCRIPT_FILENAME/data/miner_platform/src/public/index.php\ f\ 0QUERY_STRING\ 16\ 3REQUEST_METHODGET\ f\ 0CONTENT_TYPE\ 16\ 0CONTENT_LENGTH\ v\ nSCRIPT_NAME/index.php\ v\ 23REQUEST_URI/group/check_groups \ f\ nDOCUMENT_URI/index.php\ r\ 37DOCUMENT_ROOT/data/miner_platform/src/public\ 17\ 10SERVER_PROTOCOLHTTP/1.1\ 16\ 4REQUEST_SCHEMEhttp\ 21\ 7GATEWAY_INTERFACECGI/1.1\ 17\ fSERVER_SOFTWAREnginx/1.16.0\ v\ tREMOTE_ADDR127.0.0.1\ v\ 5REMOTE_PORT46616\ v\ tSERVER_ADDR127.0.0.1\ v\ 2SERVER_PORT80\ v\ 21SERVER_NAMEminer_platform.cn\ 17\ 3REDIRECT_STATUS200\ 17\ vHTTP _ USER_AGENTcurl/7.29.0\ t\ 21HTTP_HOSTminer_platform.cn\ v\ 3HTTPACCEPTERGUP *\ 0\ 0\ 0\ 0\ 0\ 0\ 1\ 4\ 0\ 1\ 0\ 0\ 1\ 5\ 0\ 1\ 0\ 0\ 0\ 0 " Iov_len=592}], 1) = 592 epoll_wait (8, [{EPOLLIN | EPOLLOUT, {u32: 1226163953, u64: 94773974517489}}], 512, 60000) = 1 / 4. Accept the PHP-FPM response result recvfrom (11, "\ 1\ 6\ 0\ 1\ 0\ 257\ 1\ 0X-Powered-By: PHP/7.2.16\ r\ nCache-Control: no-cache, private\ r\ nDate: Wed, 01 Dec 2021 12:24:52 GMT\ r\ nContent-Type: application/json\ r\ n\ n {\" code\ ": 10006 \ "message\":\ "sign\\ u65e0\\ u6548.\"}\ 0\ 1\ 3\ 0\ 1\ 0\ 10\ 0\ 0\ 0\ 0\ 0\ 0}\ 0, 4096, 0, NULL, NULL) = 200epoll_wait (8, [{EPOLLIN | EPOLLOUT | EPOLLRDHUP, {u32mm 1226163953, u6494773974517489}], 51260000) = 1 readv (11, [{iov_base= ", iov_len=3896}], 1) = 0 / 5. Close this socket connection close (11) = 0 / / 6. Respond to browser writev (3, [{iov_base= "HTTP/1.1 200 OK\ r\ nServer: nginx/1.16.0\ r\ nContent-Type: application/json\ r\ nTransfer-Encoding: chunked\ r\ nConnection: keep-alive\ r\ nX-Powered-By: PHP/7.2.16\ r\ nCache-Control: no-cache, private\ r\ nDate: Wed, 01 Dec 2021 12:24:52 GMT\ r\ n\ r\ n", iov_len=222} {iov_base= "2c\ r\ n", iov_len=4}, {iov_base= "{\" code\ ": 10006,\" message\ ":\" sign\\ u65e0\\ u6548.\ "}", iov_len=44}, {iov_base= "\ r\ n", iov_len=2}, {iov_base= "0\ r\ n\ n", iov_len=5}], 5) = 277 write (5) "127.0.0.1-- [01/Dec/2021:20:24:52 + 0800]\" GET / group/check_groups HTTP/1.1\ "20055\" -\ "\" curl/7.29.0\ "\" 1.029 127.0.1 curl/7.29.0 9000 200 1.030 ",) = 138setsockopt (3, SOL_TCP, TCP_NODELAY, [1], 4) = 0 epoll_wait (8, [{EPOLLIN | EPOLLOUT | EPOLLRDHUP, {u32mm 1226159737) ], 512, 65000) = 1 recvfrom (3, ", 1024, 0, NULL, NULL) = 0 close (3) = 0 epoll_wait (8)
2.php-fpm system call
Crawled the php-fpm work process
/ / 1. Accept received the data 965 accept (9, {sa_family=AF_INET, sin_port=htons (45512), sin_addr=inet_addr ("127.0.0.1")}, [112-> 16]) from the nginx (127.0.0.1 sa_family=AF_INET 45512) client, omitting a lot of / / 2. Respond to client 965 write (4, "\ 1\ 6\ 0\ 1\ 0\ 257\ 1\ 0X-Powered-By: PHP/7.2.16\ r\ nCache-Control: no-cache, private\ r\ nDate: Wed, 01 Dec 2021 12:37:18 GMT\ r\ nContent-Type: application/json\ r\ n\ r\ n {\" code\ ": 10006 \ "message\":\ "sign\\ u65e0\\ u6548.\"}\ 0\ 1\ 3\ 0\ 1\ 0\ 10\ 0\ 0\ 0\ 0\ 0\ 0 p\ 0\ 0 ", 200) = 200max / 3. Do not write data for this socket 965 shutdown (4, SHUT_WR) = 0 shutdown / 4. Accept nginx (127.0.0.1 NULL 45512) client data 965 recvfrom (4, "\ 1\ 5\ 0\ 1\ 0\ 0\ 0\ 0\ 0", 8, 0, NULL, NULL) = 8 recvfrom / 5. Accept nginx (127.0.0.1 NULL 45512) client data 965 recvfrom (4, ", 8, 0, NULL, NULL) = 0 max / 6. Close the connection 965 close (4) = 0965 lstat ("/ data/miner_platform/src/vendor/composer/../../app/Http/Middleware/BusinessHeaderCheck.php", {st_mode=S_IFREG | 0777, st_size=989,...}) = 0965 stat ("/ data/miner_platform/src/app/Http/Middleware/BusinessHeaderCheck.php", {st_mode=S_IFREG | 0777, st_size=989) ) = 0965 chdir ("/") = 0965 times ({tms_utime=3583, tms_stime=1977, tms_cutime=0, tms_cstime=0}) = 4315309933965 setitimer (ITIMER_PROF, {it_interval= {tv_sec=0, tv_usec=0}, it_value= {tv_sec=0, tv_usec=0}}, NULL) = 0965 fcntl (3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0) L_len=0}) = 0965 setitimer (ITIMER_PROF, {it_interval= {tv_sec=0, tv_usec=0}, it_value= {tv_sec=0, tv_usec=0}}, NULL) = 0965 accept (9
TCP three-way handshake
We have already known a request above, and the process is the same when the request concurrency is high. At this time, we draw out that the following figure is the same as the process we described above, only to refine the process of three handshakes. This is when we introduce sync queue and accept queue.
We call listen (executed by the php-fpm master process above), and at the same time the kernel creates two queues, sync queue and accept queue
The second step of the three-way handshake when the Server (referring to the php-fpm master process) sends the SYN+ACK message, this information is put into the sync queue
When the three-way handshake is complete, the connection queue that is not applied (refers to the php-fpm work process) is called by accept to retrieve it. At this time, the socket is in the ESTABLISHED state. Each time the application calls the accept () function, it removes the connection from the queue header. If the queue is empty, accept () usually blocks. A fully connected queue is also known as accept queue.
Thank you for reading! This is the end of the article on "what to do if there are not enough PHP-FPM processes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.