-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use os.sched_getaffinity to get info about available CPUs
when running inside containers with CPU limits python's multiprocessing.cpu_count() will return total number of CPU's available on the machine. However in reality the process and its children will not be able to be scheduled on all CPUs. This issue is most pronounced with taskset/cpuset is set for the process. Causing scheduling contention over limited number of CPU's. In case of CFS cpu limits, its similar, but due to bursty nature, at least in a short timeframe the processes will be able to be scheduled on all CPUs. There is no straightforward way to read the CPU shares assigned via CFS. So just handling simple case when affinity is explicitly set should be a good start
- Loading branch information
1 parent
34caa75
commit 4cc644a
Showing
3 changed files
with
23 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
import multiprocessing | ||
|
||
|
||
def get_available_cpus() -> int: | ||
try: | ||
cpu_count = os.sched_getaffinity(0) | ||
except AttributeError: | ||
cpu_count = multiprocessing.cpu_count() | ||
|
||
# Note: Windows Server 2016 has an issue https://bugs.python.org/issue26903 | ||
if os.name == "nt": | ||
MAX = 61 | ||
cpu_count = min(cpu_count, MAX) | ||
|
||
return cpu_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters