Php-affinity by setting CPU affinity, you can bind a process to a CPU core. In general, we may want to modify the CPU affinity of a process in the following scenarios:
Frequent parallel operations
Thread Scalability Testing (simulates linear scaling by increasing the number of CPU cores)
Run time-sensitive, decisive process
Open source software, such as Nginx,swoole, provides a way to set CPU affinity. Nginx officially recommends that the number of workers equal to the number of CPU cores is also to bind a process to a CPU core while avoiding the overhead of process switching.
PHP itself does not provide a function to set CPU affinity, which we can implement in two ways:
Php-affinity is a php extension written using C, project address: https://github.com/huyanping/php-affinity
/** * Set CPU affinity * * @param $cpu _id * @return BOOL */functionsetaffinity ($cpu _id) {$num = getc Pucores (); if ($cpu _id>= $num) {returnfalse; } $set = System_call ($cpu _id); if ($set = = =-1) {returnfalse; } returntrue; }/** * Get CPU affinity * * @return BOOL */functiongetaffinity () {$cpu _id= Syst Em_call (); if ($cpu _id===-1) {returnfalse; } return$cpu_id; }/** * Get number of C PU * * @return BOOL */Functiongetcpucores ( ) {$nums = System_call (); if ($nums = = =-1) {returnfalse; } return$nums; }
The following three functions are available:
setaffinity– Setting CPU Affinity
getaffinity– Getting CPU Affinity
getcpucores– getting the number of CPU cores
The API is as follows:
Reference documents:
Https://zh.wikipedia.org/wiki/%E5%A4%84%E7%90%86%E5%99%A8%E4%BA%B2%E5%92%8C%E6%80%A7
Http://www.ibm.com/developerworks/cn/linux/l-affinity.html
Http://www.cnblogs.com/LubinLew/p/cpu_affinity.html
Project home:http://www.open-open.com/lib/view/home/1448371981438