Skip to content

Commit a8b142f

Browse files
committed
Limit cores on specific machines
1 parent 27dc96f commit a8b142f

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ If you don't want a machine to be included when the user selects "machine == 'al
9393
include_in_all = false
9494
```
9595

96+
You may limit the number of cores used to build Python with the `use_cores` option. This may be necessary, for example, on cloud VMs.
97+
98+
```
99+
use_cores = 2
100+
```
101+
96102
### Try a benchmarking run
97103

98104
There are instructions for running a benchmarking action already in the `README.md` of your repo. Look there and give it a try!

bench_runner/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44

55
import functools
66
from pathlib import Path
7+
from typing import Any
78

89
try:
910
import tomllib
1011
except ImportError:
1112
import tomli as tomllib # type: ignore
1213

1314

15+
from . import runners
16+
17+
1418
@functools.cache
1519
def get_bench_runner_config(
1620
filepath: Path | str = Path("bench_runner.toml"),
1721
):
1822
with Path(filepath).open("rb") as fd:
1923
return tomllib.load(fd)
24+
25+
26+
def get_config_for_current_runner() -> dict[str, Any]:
27+
config = get_bench_runner_config()
28+
runner = runners.get_runner_for_hostname()
29+
return config.get("runners", {}).get(runner.nickname, {})

bench_runner/result.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from operator import itemgetter
1010
from pathlib import Path
1111
import re
12-
import socket
1312
import subprocess
1413
import sys
1514
from typing import Any, Callable, Iterable, Sequence
@@ -524,7 +523,7 @@ def from_scratch(
524523
flags: Iterable[str] | None = None,
525524
) -> "Result":
526525
result = cls(
527-
_clean(runners.get_nickname_for_hostname(socket.gethostname())),
526+
_clean(runners.get_nickname_for_hostname()),
528527
_clean(_get_architecture(python)),
529528
_clean_for_url(fork),
530529
_clean(ref[:20]),

bench_runner/runners.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import functools
55
import os
6+
import socket
67

78

89
from . import config
@@ -80,13 +81,19 @@ def get_runners_by_nickname() -> dict[str, Runner]:
8081
return {x.nickname: x for x in get_runners()}
8182

8283

83-
def get_nickname_for_hostname(hostname: str) -> str:
84+
def get_nickname_for_hostname(hostname: str | None = None) -> str:
8485
# The envvar BENCHMARK_MACHINE_NICKNAME is used to override the machine that
8586
# results are reported for.
8687
if "BENCHMARK_MACHINE_NICKNAME" in os.environ:
8788
return os.environ["BENCHMARK_MACHINE_NICKNAME"]
88-
return get_runners_by_hostname().get(hostname, unknown_runner).nickname
89+
return get_runner_for_hostname(hostname).nickname
8990

9091

9192
def get_runner_by_nickname(nickname: str) -> Runner:
9293
return get_runners_by_nickname().get(nickname, unknown_runner)
94+
95+
96+
def get_runner_for_hostname(hostname: str | None = None) -> Runner:
97+
if hostname is None:
98+
hostname = socket.gethostname()
99+
return get_runners_by_hostname().get(hostname, unknown_runner)

bench_runner/scripts/workflow.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
from bench_runner import benchmark_definitions
18+
from bench_runner import config
1819
from bench_runner import flags as mflags
1920
from bench_runner import git
2021
from bench_runner.result import has_result
@@ -132,6 +133,7 @@ def checkout_benchmarks():
132133

133134
def compile_unix(cpython: PathLike, flags: list[str], pgo: bool, pystats: bool) -> None:
134135
cpython = Path(cpython)
136+
cfg = config.get_config_for_current_runner()
135137

136138
env = os.environ.copy()
137139
if "CLANG" in flags:
@@ -169,9 +171,15 @@ def compile_unix(cpython: PathLike, flags: list[str], pgo: bool, pystats: bool)
169171
if configure_flags := os.environ.get("PYTHON_CONFIGURE_FLAGS"):
170172
args.extend(shlex.split(configure_flags))
171173

174+
make_args = []
175+
if cores := cfg.get("use_cores", None):
176+
make_args.extend(["-j", str(cores)])
177+
else:
178+
make_args.extend(["-j"])
179+
172180
with contextlib.chdir(cpython):
173181
subprocess.check_call(["./configure", *args], env=env)
174-
subprocess.check_call(["make", "-j"], env=env)
182+
subprocess.check_call(["make", *make_args], env=env)
175183

176184

177185
def compile_windows(

0 commit comments

Comments
 (0)