Nginx uses the multi-process master/worker structure, the worker process number is the CPU number is the highest efficiency, nginx through the affinity for each worker process binds a CPU, avoids the process switching to bring the consumption, simultaneously can guarantee the cache hit ratio.
Nginx configuration file conf/nginx.conf for the number of worker processes, and affinity configuration commands:
Worker_processes 4010000100001;
The application can invoke the interface settings proactively:
Linux process:
int sched_setaffinity (pid_t pid, size_t cpusetsize, *mask); int sched_getaffinity (pid_t pid, size_t cpusetsize, *mask);
Example:
1 #define_gnu_source2 3#include <sched.h>4#include <unistd.h>5 6 voidDo_something (void);7 8 int 9Mainvoid)Ten { One pid_t pid; A cpu_set_t Mask; - - if(PID = fork ()) = =0){ the do_something (); - } -Cpu_zero (&mask); -Cpu_set (0,&mask); +Sched_setaffinity (PID,sizeof(cpu_set_t),&mask); - + if(PID = fork ()) = =0){ A do_something (); at } -Cpu_zero (&mask); -Cpu_set (1,&mask); -Sched_setaffinity (PID,sizeof(cpu_set_t),&mask); - - wait (); in wait (); - } to + void -Do_something (void) the { * inti; $ while(1){Panax Notoginsengi =1; - } the}
sched_setaffinity
FreeBSD can be set by calling Cpuset_setaffinity.
Nginx source SRC/OS/UNIX/NGINX_SETAFFINITY.C to achieve the compatibility of Linux and FreeBSD in the packaging, according to the configuration file set worker process affinity
Of course, POSIX threads can also set affinity:
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, *cpuset);
Processes that are already running can also be set by external commands:
taskset [options] Mask command [ARG] ... -p [Mask] pid
The process affinity of nginx in affinity