PHP extension: php-affinity Php-affinity can bind processes to a cpu core by setting cpu affinity. In the following scenarios, we may want to modify the cpu affinity of processes:
Frequent parallel operations
Thread scalability test (simulate linear expansion by increasing the number of cpu cores)
Time-sensitive and decisive processes
It provides methods for setting cpu affinity for open-source software such as nginx and swoole. Nginx officially recommends that the number of workers is equal to the number of cpu cores to bind a process to a cpu core and avoid overhead caused by process switching.
Php does not provide a function for setting cpu affinity. we can achieve this in two ways:
Php-affinity is a php extension written in c, Project address: https://github.com/huyanping/php-affinity
/** * set CPU affinity * * @param $cpu_id * @return bool */ functionsetaffinity($cpu_id){ $num= getcpucores(); if($cpu_id>= $num){ returnfalse; } $set= system_call($cpu_id); if($set=== -1){ returnfalse; } returntrue; } /** * get CPU affinity * * @return bool */ functiongetaffinity(){ $cpu_id= system_call(); if($cpu_id=== -1){ returnfalse; } return$cpu_id; } /** * get number of CPU * * @return bool */ functiongetcpucores(){ $nums= system_call(); if($nums=== -1){ returnfalse; } return$nums; }
Three functions are provided:
Setaffinity-set cpu affinity
Getaffinity-obtain cpu affinity
Getcpucores-obtain the number of cpu cores
The API is as follows:
References:
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 homepage:Http://www.open-open.com/lib/view/home/1448371981438