safety catch for --cpus so it isn't higher than actual CPUs

This commit is contained in:
Hans-Christoph Steiner 2025-10-28 07:40:12 +01:00
parent 8421d61369
commit 950efbbb45
2 changed files with 19 additions and 6 deletions

View file

@ -148,13 +148,16 @@ def get_virt_cpus_opt(cpus):
If no CPU count is configured, calculate a reasonable default value.
"""
if cpus:
return cpus
cpu_cnt = os.cpu_count()
if cpu_cnt < 8:
return max(1, int(0.5 * cpu_cnt))
# use a quarter of available CPUs if there
return 2 + int(0.25 * cpu_cnt)
if not cpus:
if cpu_cnt < 8:
cpus = max(1, int(0.5 * cpu_cnt))
else:
# use a quarter of available CPUs if there
cpus = 2 + int(0.25 * cpu_cnt)
if min(cpus, cpu_cnt) != cpus:
logging.warning(f'Capping {cpus} CPUs to how many are available ({cpu_cnt}).')
return min(cpus, cpu_cnt)
def get_virt_memory_opt(memory):

View file

@ -145,6 +145,16 @@ class Up_run_vagrant(UpTest):
class Up_options(UpTest):
def test_get_virt_cpus_opt_default(self):
self.assertTrue(up.get_virt_cpus_opt(None) > 0)
def test_get_virt_cpus_opt_too_small(self):
self.assertTrue(up.get_virt_cpus_opt(0.1) > 0)
def test_get_virt_cpus_opt_too_big(self):
with self.assertLogs():
self.assertEqual(up.get_virt_cpus_opt(99999999), os.cpu_count())
def test_get_virt_memory_opt_default(self):
self.assertEqual(up.get_virt_memory_opt(None), 6 * 1024**3)