The process affinity of nginx in affinity

Source: Internet
Author: User

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

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.