PHP extension: php-affinity

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

  • Call the system command to modify the cpu affinity of the current process

  • Compile php extensions to encapsulate system calls.

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

    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.