src/os/unix/ngx_setaffinity.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. */


  4. #include <ngx_config.h>
  5. #include <ngx_core.h>


  6. #if (NGX_HAVE_CPUSET_SETAFFINITY)

  7. void
  8. ngx_setaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log)
  9. {
  10.     ngx_uint_t  i;

  11.     for (i = 0; i < CPU_SETSIZE; i++) {
  12.         if (CPU_ISSET(i, cpu_affinity)) {
  13.             ngx_log_error(NGX_LOG_NOTICE, log, 0,
  14.                           "cpuset_setaffinity(): using cpu #%ui", i);
  15.         }
  16.     }

  17.     if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
  18.                            sizeof(cpuset_t), cpu_affinity) == -1)
  19.     {
  20.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  21.                       "cpuset_setaffinity() failed");
  22.     }
  23. }

  24. #elif (NGX_HAVE_SCHED_SETAFFINITY)

  25. void
  26. ngx_setaffinity(ngx_cpuset_t *cpu_affinity, ngx_log_t *log)
  27. {
  28.     ngx_uint_t  i;

  29.     for (i = 0; i < CPU_SETSIZE; i++) {
  30.         if (CPU_ISSET(i, cpu_affinity)) {
  31.             ngx_log_error(NGX_LOG_NOTICE, log, 0,
  32.                           "sched_setaffinity(): using cpu #%ui", i);
  33.         }
  34.     }

  35.     if (sched_setaffinity(0, sizeof(cpu_set_t), cpu_affinity) == -1) {
  36.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  37.                       "sched_setaffinity() failed");
  38.     }
  39. }

  40. #endif