From d574e67233f18e78193b6e1e5bf94bc299b076ee Mon Sep 17 00:00:00 2001 From: Michal Srb Date: Jul 16 2019 10:13:05 +0000 Subject: Try to get number of CPUs for QEMU VM from STR_CPU_LIMIT environment variable Fallback to previous method if the variable is empty or doesn't exist. --- diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2 index af0f19d..a22ecc8 100755 --- a/inventory/standard-inventory-qcow2 +++ b/inventory/standard-inventory-qcow2 @@ -185,17 +185,21 @@ def inv_list(subjects): def get_qemu_smp_arg(): """Determine the number of CPUs that should be visible in the guest. See e.g. https://www.redhat.com/archives/libvirt-users/2017-June/msg00025.html - We want to match the number of host physical cores. + We want to match the number of host physical cores, or to go with the value + provided explicitly via the STR_CPU_LIMIT environment variable. """ - # We may be run in a cgroup with fewer cores available than physical. - available_cpu = int(subprocess.check_output(['nproc']).strip()) - # https://stackoverflow.com/questions/6481005/how-to-obtain-the-number-of-cpus-cores-in-linux-from-the-command-line - core_sockets = set() - for line in subprocess.check_output(['lscpu', '-b', '-p=Core,Socket'], universal_newlines=True).split("\n"): - if line.startswith('#'): - continue - core_sockets.add(line.strip()) - sockets = min(available_cpu, len(core_sockets)) + if os.environ.get('STR_CPU_LIMIT'): + sockets = os.environ.get('STR_CPU_LIMIT') + else: + # We may be run in a cgroup with fewer cores available than physical. + available_cpu = int(subprocess.check_output(['nproc']).strip()) + # https://stackoverflow.com/questions/6481005/how-to-obtain-the-number-of-cpus-cores-in-linux-from-the-command-line + core_sockets = set() + for line in subprocess.check_output(['lscpu', '-b', '-p=Core,Socket'], universal_newlines=True).split("\n"): + if line.startswith('#'): + continue + core_sockets.add(line.strip()) + sockets = min(available_cpu, len(core_sockets)) return '{},sockets={},cores=1,threads=1'.format(sockets, sockets)