当前位置: 首页 > 工具软件 > php-affinity > 使用案例 >

PHP CPU亲和性

谢骏奇
2023-12-01

大佬地址:
PHP插件 https://github.com/huyanping/php-affinity
CPU亲和性理论 https://www.ibm.com/developerworks/cn/linux/l-affinity.html

Install

wget https://github.com/huyanping/php-affinity/archive/0.1.tar.gz
tar zxvf 0.1.tar.gz
cd affinity
phpize
./configure --with-php-config=/path/to/php-config
make && make test && make install

原理代码

/**
 * set CPU affinity
 *
 * @param $cpu_id
 * @return bool
 */
function setaffinity($cpu_id){
    $num = getcpucores();
    if($cpu_id >= $num){
        return false;
    }
    $set = system_call($cpu_id);
    if($set === -1){
        return false;
    }

    return true;
}

/**
 * get CPU affinity
 *
 * @return bool
 */
function getaffinity(){
    $cpu_id = system_call();
    if($cpu_id === -1){
        return false;
    }
    return $cpu_id;
}


/**
 * get number of CPU
 *
 * @return bool
 */
function getcpucores(){
    $nums = system_call();
    if($nums === -1){
        return false;
    }
    return $nums;
}
 类似资料: