#368 Try to get number of CPUs for QEMU VM from STR_CPU_LIMIT environment variable
Merged 5 years ago by astepano. Opened 5 years ago by msrb.
msrb/standard-test-roles vm-cpus-from-env  into  master

@@ -185,17 +185,21 @@ 

  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)

  

  

Fallback to previous method if the variable is empty or doesn't exist.

Looks good, but, please add STR_ prefix. STR_CPU_LIMIT
Thank you.

rebased onto c51f5964be7b98c4c2f273b45f94e589003a2924

5 years ago

rebased onto d574e67

5 years ago

Commit bebca2c fixes this pull-request

Pull-Request has been merged by astepano

5 years ago

Pull-Request has been merged by astepano

5 years ago