From 1fb20cf9e1e02552bf094d2ca8211135d5096888 Mon Sep 17 00:00:00 2001 From: Maria Mozgunova Date: Mon, 18 Jul 2022 16:08:16 +0300 Subject: [PATCH 1/2] [bug] get_cpus is not supporting cgroup2 #11635 --- conan/tools/build/cpu.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/conan/tools/build/cpu.py b/conan/tools/build/cpu.py index d9a6ecefab0..cb464074dea 100644 --- a/conan/tools/build/cpu.py +++ b/conan/tools/build/cpu.py @@ -1,5 +1,6 @@ import math import multiprocessing +import os from conans.util.files import load @@ -15,8 +16,18 @@ def _cpu_count(): try: try: # This is necessary to deduce docker cpu_count - cfs_quota_us = int(load("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")) - cfs_period_us = int(load("/sys/fs/cgroup/cpu/cpu.cfs_period_us")) + # cgroup2 + if os.path.exists("/sys/fs/cgroup/cgroup.controllers"): + cpu_max = load("/sys/fs/cgroup/cpu.max").split() + if cpu_max[0] == "max": + return 1 + elif len(cpu_max) == 1: + cfs_quota_us, cfs_period_us = int(cpu_max[0]), 1 + else: + cfs_quota_us, cfs_period_us = map(int, cpu_max) + else: # cgroup1 + cfs_quota_us = int(load("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")) + cfs_period_us = int(load("/sys/fs/cgroup/cpu/cpu.cfs_period_us")) if cfs_quota_us > 0 and cfs_period_us > 0: return int(math.ceil(cfs_quota_us / cfs_period_us)) except (EnvironmentError, TypeError): From 9e4332820075acba79ee8aa4efc895101500a87d Mon Sep 17 00:00:00 2001 From: Maria Mozgunova Date: Thu, 21 Jul 2022 13:10:06 +0300 Subject: [PATCH 2/2] Fix cgroup2 "max" case #11635 When $MAX == "max", there is no limit on the cpu usage set Use multiprocessing.cpu_count() function in that case --- conan/tools/build/cpu.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/conan/tools/build/cpu.py b/conan/tools/build/cpu.py index cb464074dea..2de22771b73 100644 --- a/conan/tools/build/cpu.py +++ b/conan/tools/build/cpu.py @@ -16,15 +16,15 @@ def _cpu_count(): try: try: # This is necessary to deduce docker cpu_count + cfs_quota_us = cfs_period_us = 0 # cgroup2 if os.path.exists("/sys/fs/cgroup/cgroup.controllers"): cpu_max = load("/sys/fs/cgroup/cpu.max").split() - if cpu_max[0] == "max": - return 1 - elif len(cpu_max) == 1: - cfs_quota_us, cfs_period_us = int(cpu_max[0]), 1 - else: - cfs_quota_us, cfs_period_us = map(int, cpu_max) + if cpu_max[0] != "max": + if len(cpu_max) == 1: + cfs_quota_us, cfs_period_us = int(cpu_max[0]), 100_000 + else: + cfs_quota_us, cfs_period_us = map(int, cpu_max) else: # cgroup1 cfs_quota_us = int(load("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")) cfs_period_us = int(load("/sys/fs/cgroup/cpu/cpu.cfs_period_us"))