Skip to content

Commit 182aa77

Browse files
Totktonadaylobankov
authored andcommitted
Calculate parallel jobs based on available CPUs
We're going to enable new ARM64 runners in CI, where test-run is invoked in a Docker container. At the same time, the runner is an LXD container created by the service provider. In this circumstances, the Docker container sees all the 128 online CPUs, but the runner may have only some of them available (depending on the pricing plan). Python 3.3+ has a function to determine available CPUs, so we can reduce the parallelism to this value. We fall back to the online CPUs count on Python < 3.3. After this change, test-run follows a CPU affinity mask set by `taskset` (and, I guess, by `numactl`). The change is similar to replacing `nproc --all` to `nproc`. The change only affects the default behavior, which can be overwritten by passing the `--jobs` (or `-j`) CLI option or using the `TEST_RUN_JOBS` environment variable. Reported in tarantool/tarantool#10102
1 parent 84ebae5 commit 182aa77

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
import json
1010
import subprocess
11+
import multiprocessing
1112
from lib.colorer import color_stdout
1213

1314
try:
@@ -31,6 +32,12 @@
3132
# Python 2.7.
3233
get_terminal_size = None
3334

35+
try:
36+
# Python 3.3+
37+
from os import sched_getaffinity
38+
except ImportError:
39+
sched_getaffinity = None
40+
3441
UNIX_SOCKET_LEN_LIMIT = 107
3542

3643
# Useful for very coarse version differentiation.
@@ -384,3 +391,22 @@ def terminal_columns():
384391
if get_terminal_size:
385392
return get_terminal_size().columns
386393
return 80
394+
395+
396+
def cpu_count():
397+
"""
398+
Return available CPU count available for the current process.
399+
400+
The result is the same as one from the `nproc` command.
401+
402+
It may be smaller than all the online CPUs count. For example,
403+
an LXD container may have limited available CPUs or it may be
404+
reduced by `taskset` or `numactl` commands.
405+
406+
If it is impossible to determine the available CPUs count (for
407+
example on Python < 3.3), fallback to the all online CPUs
408+
count.
409+
"""
410+
if sched_getaffinity:
411+
return len(sched_getaffinity(0))
412+
return multiprocessing.cpu_count()

test-run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from lib.colorer import color_stdout
5959
from lib.colorer import separator
6060
from lib.colorer import test_line
61+
from lib.utils import cpu_count
6162
from lib.utils import find_tags
6263
from lib.utils import shlex_quote
6364
from lib.error import TestRunInitError
@@ -86,7 +87,7 @@ def main_loop_parallel():
8687
jobs = args.jobs
8788
if jobs < 1:
8889
# faster result I got was with 2 * cpu_count
89-
jobs = 2 * multiprocessing.cpu_count()
90+
jobs = 2 * cpu_count()
9091

9192
if jobs > 0:
9293
color_stdout("Running in parallel with %d workers\n\n" % jobs,

0 commit comments

Comments
 (0)