nginx/src
Andrew Clayton 1c59ff16d3 Better available cpu count determination.
With 'worker_processes auto;' the number of worker processes to create
is generally determined at runtime via sysconf(_SC_NPROCESSORS_ONLN);

However this value does not take into account any restrictions that have
been put in place via mechanisms like cpuset(7)'s and/or
sched_setaffinity(2).

So for example while a system may have 18 "cpus" available as returned
via sysconf(_SC_NPROCESSORS_ONLN) the actual number of allowed to run on
cpus may be much lower.

E.g.

On a 18 core system, with 'worker_processes auto;' nginx will create 18
worker processes (+ the master). E.g.

  $ pgrep -c nginx
  19

If however nginx has been restricted to run on just four. We will still
create 18 worker processes. E.g.

  $ taskset -a -c 0-3 ./sbin/nginx -c conf/minimal.conf
  $ pgrep -c nginx
  19

This can happen for example with 'docker --cpuset-cpus=', where you may
end up with many more worker processes than runnable cpus.

This commit uses sched_getaffinity() (where available, at least Linux &
FreeBSD) to determine the number of actual available runnable cpus.

So in the above example we now get

  $ taskset -a -c 0-3 ./sbin/nginx -c conf/minimal.conf
  $ pgrep -c nginx
  5

Four worker processes plus the master.

Closes: https://github.com/nginx/nginx/issues/855
2025-11-28 04:51:54 +00:00
..
core Version bump. 2025-11-06 15:34:58 +04:00
event SSL: fixed build with BoringSSL, broken by 38a701d88. 2025-11-10 23:27:53 +04:00
http Proxy: fixed segfault in URI change. 2025-11-26 22:46:22 +04:00
mail Mail: xtext encoding (RFC 3461) in XCLIENT LOGIN. 2025-09-26 17:04:20 +04:00
misc Cpp test: added stream. 2020-10-13 07:44:09 +03:00
os Better available cpu count determination. 2025-11-28 04:51:54 +00:00
stream Improved host header validation. 2025-11-26 19:51:40 +04:00