src/os/unix/ngx_setaffinity.c - nginx-1.7.10

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. #include <sys/cpuset.h>

  8. void
  9. ngx_setaffinity(uint64_t cpu_affinity, ngx_log_t *log)
  10. {
  11.     cpuset_t    mask;
  12.     ngx_uint_t  i;

  13.     ngx_log_error(NGX_LOG_NOTICE, log, 0,
  14.                   "cpuset_setaffinity(0x%08Xl)", cpu_affinity);

  15.     CPU_ZERO(&mask);
  16.     i = 0;
  17.     do {
  18.         if (cpu_affinity & 1) {
  19.             CPU_SET(i, &mask);
  20.         }
  21.         i++;
  22.         cpu_affinity >>= 1;
  23.     } while (cpu_affinity);

  24.     if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
  25.                            sizeof(cpuset_t), &mask) == -1)
  26.     {
  27.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  28.                       "cpuset_setaffinity() failed");
  29.     }
  30. }

  31. #elif (NGX_HAVE_SCHED_SETAFFINITY)

  32. void
  33. ngx_setaffinity(uint64_t cpu_affinity, ngx_log_t *log)
  34. {
  35.     cpu_set_t   mask;
  36.     ngx_uint_t  i;

  37.     ngx_log_error(NGX_LOG_NOTICE, log, 0,
  38.                   "sched_setaffinity(0x%08Xl)", cpu_affinity);

  39.     CPU_ZERO(&mask);
  40.     i = 0;
  41.     do {
  42.         if (cpu_affinity & 1) {
  43.             CPU_SET(i, &mask);
  44.         }
  45.         i++;
  46.         cpu_affinity >>= 1;
  47.     } while (cpu_affinity);

  48.     if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
  49.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  50.                       "sched_setaffinity() failed");
  51.     }
  52. }

  53. #endif