Nginx采用多进程Master/Worker结构,Worker进程数为CPU个数时工作效率最高,Nginx通过affinity为每个Worker进程绑定一个CPU,避免进程切换带来的消耗,同时能够保证缓存命中率。
Nginx配置文件conf/nginx.conf中关于Worker进程个数,和affinity的配置命令:
worker_processes ;
worker_cpu_affinity ;
应用程序可以主动调用接口设定:
Linux进程:
int sched_setaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
int sched_getaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
例子:
#define _GNU_SOURCE #include <sched.h>
#include <unistd.h> void do_something(void); int
main(void)
{
pid_t pid;
cpu_set_t mask; if((pid = fork()) == ){
do_something();
}
CPU_ZERO(&mask);
CPU_SET(,&mask);
sched_setaffinity(pid,sizeof(cpu_set_t),&mask); if((pid = fork()) == ){
do_something();
}
CPU_ZERO(&mask);
CPU_SET(,&mask);
sched_setaffinity(pid,sizeof(cpu_set_t),&mask); wait();
wait();
} void
do_something(void)
{
int i;
while(){
i = ;
}
}
sched_setaffinity
FreeBSD上可以通过调用cpuset_setaffinity设定
Nginx源码src/os/unix/nginx_setaffinity.c中实现对Linux和FreeBSD中设置亲和性的包装,根据配置文件设置Worker进程亲和性
当然,POSIX线程也可以设置亲和性:
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
const cpu_set_t *cpuset);
int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
cpu_set_t *cpuset);
已经运行的进程也可以通过外部命令设定:
taskset [options] mask command [arg]...
taskset [options] -p [mask] pid