PHP Extension: php-affinity

Source: Internet
Author: User
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:

    • Invoking system commands to modify the current process CPU affinity

    • Write PHP extensions to encapsulate system calls.

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.