Fix worker count to use CPU affinity correctly and be properly bounded
We were using the `cpu_count`, which doesn't respect container affinity. Now, we use `cpu_affinity` and also bound to make sure we don't start a million workers Fixes https://jira.coreos.com/browse/QUAY-928
This commit is contained in:
parent
e80c56e441
commit
b26a131085
7 changed files with 115 additions and 10 deletions
31
util/workers.py
Normal file
31
util/workers.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import os
|
||||
import psutil
|
||||
|
||||
def get_worker_count(worker_kind_name, multiplier, minimum=None, maximum=None):
|
||||
""" Returns the number of gunicorn workers to run for the given worker kind,
|
||||
based on a combination of environment variable, multiplier, minimum (if any),
|
||||
and number of accessible CPU cores.
|
||||
"""
|
||||
minimum = minimum or multiplier
|
||||
maximum = maximum or (multiplier * multiplier)
|
||||
|
||||
# Check for an override via an environment variable.
|
||||
override_value = os.environ.get('WORKER_COUNT_' + worker_kind_name.upper())
|
||||
if override_value is not None:
|
||||
return max(override_value, minimum)
|
||||
|
||||
override_value = os.environ.get('WORKER_COUNT')
|
||||
if override_value is not None:
|
||||
return max(override_value, minimum)
|
||||
|
||||
# Load the number of CPU cores via affinity, and use that to calculate the
|
||||
# number of workers to run.
|
||||
p = psutil.Process(os.getpid())
|
||||
|
||||
try:
|
||||
cpu_count = len(p.cpu_affinity())
|
||||
except AttributeError:
|
||||
# cpu_affinity isn't supported on this platform. Assume 2.
|
||||
cpu_count = 2
|
||||
|
||||
return min(max(cpu_count * multiplier, minimum), maximum)
|
Reference in a new issue