下面小編就為大家帶來一篇基於PHP-FPM進程池探秘。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
PHP 支援多進程而不支援多線程;PHP-FPM 在進程池中運行多個子進程並發處理所有串連請求。通過 ps 查看PHP-FPM進程池(pm.start_servers = 2)狀態如下:
root@d856fd02d2fe:~# ps aux -LUSER PID LWP %CPU NLWP %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 1 0.0 1 0.0 4504 692 ? Ss 13:10 0:00 /bin/sh /usr/local/php/bin/php-fpm startroot 7 7 0.0 1 0.4 176076 19304 ? Ss 13:10 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)www-data 8 8 0.0 1 0.2 176076 8132 ? S 13:10 0:00 php-fpm: pool wwwwww-data 9 9 0.0 1 0.2 176076 8132 ? S 13:10 0:00 php-fpm: pool wwwroot 10 10 0.0 1 0.0 18376 3476 ? Ss 14:11 0:00 bashroot 66 66 0.0 1 0.0 34420 2920 ? R+ 15:13 0:00 ps aux -L
從列表中可以看出,進程池www中有兩個尚處於空閑狀態的子進程PID 8和 PID 9。註:NLWP指輕量級進程數量,即線程數量。
PHP-FPM(FastCGI Process Manager)是什嗎?PHP-FPM為PHP-CGI提供進程管理方式,可以有效控制記憶體和進程,可以平滑重載PHP配置,其master process是常駐記憶體的。FastCGI是語言無關的、可伸縮架構的CGI開放擴充,其主要行為是將CGI解譯器進程保持在記憶體中更長時間,不是fork-and-execute,並因此獲得較高的效能。FastCGI支援分布式部署,可以部署在WEB伺服器以外的多個主機上。
探秘手段:類比多線程並發執行
1. 什麼是線程:線程有時又稱輕量級進程(Lightweight Process,LWP),通常由線程ID、當前指令指標(PC)、寄存器集合和堆棧組成,是進程中的一個實體,是被系統獨立調度的基本單位;線程自己不擁有系統資源,只擁有一點兒在運行中必不可少的資源,與同屬一個進程的其它線程共用進程所擁有的全部資源。 由於線程之間的相互制約,致使線程在運行中呈現出間斷性。線程也有就緒、阻塞和運行三種基本狀態。由於進程是資源擁有者,建立、撤消與切換開銷過大,在對稱式多處理機(SMP)上同時運行多個線程(Threads)才是更合適的選擇。線程的實體包括程式、資料和線程式控制制塊(Thread Control Block,TCB),TCB包括以下資訊:
(1)線程狀態;
(2)當線程不運行時,被儲存的現場資源;
(3)一組執行堆棧;
(4)存放每個線程的局部變數主存;
(5)訪問同一個進程中的主存和其它資源。
但使用多個進程會使得應用程式在出現進程池內的進程崩潰或被攻擊的情況下變得更加健壯。
2. 類比多線程:
<?php/** * PHP 只支援多進程不支援多線程。 * * PHP-FPM 在進程池中運行多個子進程並發處理所有串連, * 同一個子進程可先後處理多個串連請求,但同一時間 * 只能處理一個串連請求,未處理串連請求將進入隊列等待處理 * */class SimulatedThread{ //類比線程 private $thread; //主機名稱 private $host = 'tcp://172.17.0.5'; //連接埠號碼 private $port = 80; public function __construct() { //採用目前時間給線程編號 $this->thread = microtime(true); } /** * 通過socket發送一個新的HTTP串連請求到本機, * 此時當前類比線程既是服務端又是類比用戶端 * * 當前(程式)子進程sleep(1)後會延遲1s才繼續執行,但其持有的串連是繼續有效, * 不能處理新的串連請求,故這種做法會降低進程池處理並發串連請求的能力, * 類似延遲處理還有time_nanosleep()、time_sleep_until()、usleep()。 * 而且sleep(1)這種做法並不安全,nginx依然可能出現如下錯誤: * “epoll_wait() reported that client prematurely closed connection, * so upstream connection is closed too while connecting to upstream” * * @return void */ public function simulate() { $run = $_GET['run'] ?? 0; if ($run++ < 9) {//最多類比10個線程 $fp = fsockopen($this->host, $this->port); fputs($fp, "GET {$_SERVER['PHP_SELF']}?run={$run}\r\n\r\n"); sleep(1);//usleep(500) fclose($fp); } $this->log(); } /** * 日誌記錄當前類比線程已耗用時間 * * @return void */ private function log() { $fp = fopen('simulated.thread', 'a'); fputs($fp, "Log thread {$this->thread} at " . microtime(true) . "(s)\r\n"); fclose($fp); }}$thread = new SimulatedThread();$thread->simulate();echo "Started to simulate threads...";
探秘匯總:本人通過運行上述指令碼後,發現一些可預料但卻不是我曾想到的結果
1. PHP-FPM配置項pm.max_children = 5,simulated.thread記錄如下:
Log thread 1508054181.4236 at 1508054182.4244(s)Log thread 1508054181.4248 at 1508054182.4254(s)Log thread 1508054181.426 at 1508054182.428(s)Log thread 1508054181.6095 at 1508054182.6104(s)Log thread 1508054182.4254 at 1508054183.4262(s)Log thread 1508054183.4272 at 1508054183.4272(s)Log thread 1508054182.4269 at 1508054183.4275(s)Log thread 1508054182.4289 at 1508054183.43(s)Log thread 1508054182.6085 at 1508054183.6091(s)Log thread 1508054182.611 at 1508054183.6118(s)
最新產生的(類比)線程登記出現在紅色標示條目位置是因為進程池的並發串連處理能力上限為5,因此它只可能出現在第六條以後的位置。
Log thread 1508058075.042 at 1508058076.0428(s)Log thread 1508058075.0432 at 1508058076.0439(s)Log thread 1508058075.0443 at 1508058076.045(s)Log thread 1508058075.6623 at 1508058076.6634(s)Log thread 1508058076.0447 at 1508058077.0455(s)Log thread 1508058076.046 at 1508058077.0466(s)Log thread 1508058077.0465 at 1508058077.0466(s)Log thread 1508058076.0469 at 1508058077.0474(s)Log thread 1508058076.6647 at 1508058077.6659(s)Log thread 1508058076.6664 at 1508058077.6671(s)
有意思的是綠色條目代表的(類比)線程和紅色條目代表的(類比)線程的登記時間是一樣的,說明兩個(類比)線程是並發執行的。
2. PHP-FPM配置項pm.max_children = 10,simulated.thread記錄如下:
Log thread 1508061169.7956 at 1508061170.7963(s)Log thread 1508061169.7966 at 1508061170.7976(s)Log thread 1508061169.7978 at 1508061170.7988(s)Log thread 1508061170.2896 at 1508061171.2901(s)Log thread 1508061170.7972 at 1508061171.7978(s)Log thread 1508061171.7984 at 1508061171.7985(s)Log thread 1508061170.7982 at 1508061171.7986(s)Log thread 1508061170.7994 at 1508061171.8(s)Log thread 1508061171.2907 at 1508061172.2912(s)Log thread 1508061171.2912 at 1508061172.2915(s)
由於服務端並發串連處理能力上限達到10,因此最新產生的(類比)線程登記可出現在任何位置。
3. 執行usleep(500)延遲,simulated.thread記錄如下:
Log thread 1508059270.3195 at 1508059270.3206(s)Log thread 1508059270.3208 at 1508059270.3219(s)Log thread 1508059270.322 at 1508059270.323(s)Log thread 1508059270.323 at 1508059270.324(s)Log thread 1508059270.3244 at 1508059270.3261(s)Log thread 1508059270.3256 at 1508059270.3271(s)Log thread 1508059270.3275 at 1508059270.3286(s)Log thread 1508059270.3288 at 1508059270.3299(s)Log thread 1508059270.3299 at 1508059270.331(s)Log thread 1508059270.3313 at 1508059270.3314(s)
可見日誌記錄順序與(類比)線程產生的順序一致。usleep延遲的基本單位是微妙(us, 1 s = 1000000 us)。
從以上的記錄可以看出:
1)這些(類比)線程是第一次請求執行指令碼後就自動產生的,一個(類比)線程緊接著建立了另一個(類比)線程;
2)這些(類比)線程中有的是在同一個子進程空間中產生並啟動並執行;
3)前後相鄰(類比)線程產生時間間隔很小,幾乎是同時產生,或後一個(類比)線程在前一個(類比)線程尚未執行結束並退出之前產生;
4)多個(類比)線程之間可以並發執行。
所以,上述類比多線程並發的實現是成功的。PHP-FPM進程池中同一個子進程可先後處理多個串連請求,但同一時間只能處理一個串連請求,未處理串連請求將進入隊列等待處理。換句話,同一個子進程不具有並發處理串連請求的能力。
PHP-FPM Pool配置:它允許定義多個池,每個池可定義不同的配置項。以下只是列舉了我在探秘過程中還關注過的其他部分配置項
1、 listen:The address on which to accept FastCGI requests.它支援TCP Socket和unix socket兩種通訊協議。可設定listen = [::]:9000。
2、listen.allowed_clients:List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect. 該配置項為逗號分隔的列表,如listen.allowed_clients = 127.0.0.1,172.17.0.5。
3、pm:Choose how the process manager will control the number of child processes. 該配置項設定FPM管理進程池的方式,包括static、dynamic、ondemand三種。
4、pm.max_requests:The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries.設定每個子進程處理請求數的上限,對於處理第三方庫中的記憶體流失很有用。
5、pm.status_path:The URI to view the FPM status page.