Skip to content

Commit 5756d1b

Browse files
committed
[ci] Use available CPUs in builds
This adds a script to determine the CPUs for each job based on the number of executors per node, assuming and equal allocation to each executor on a node.
1 parent 40fd36f commit 5756d1b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

tests/scripts/find_nproc.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
import argparse
20+
import multiprocessing
21+
22+
PREFIXES = {
23+
"i-": 1,
24+
"octo.aws.g4dn.52.42.180.210": 2,
25+
"octo.aws.g4dn.54.185.57.115": 2,
26+
"octo.aws.g4dn": 1,
27+
"octo.aws.c4": 4,
28+
"octo.aws.g4dn.cudabuild": 4,
29+
"Built-In Node": 2,
30+
"docs": 1,
31+
"octo.aws.arm": 4,
32+
}
33+
34+
35+
def guess_num_executors(node_name: str) -> int:
36+
# There is not a great way to get this from Jenkins directly apparently, so
37+
# guess it based on node nodes
38+
for prefix, count in PREFIXES.items():
39+
if node_name.startswith(prefix):
40+
return count
41+
42+
# Fallback to a safe option (4 is the wor
43+
return 4
44+
45+
46+
if __name__ == "__main__":
47+
help = "Determine max CPUs possible for this job"
48+
parser = argparse.ArgumentParser(description=help)
49+
parser.add_argument("--node-name", required=True, help="current node name")
50+
args = parser.parse_args()
51+
52+
executors = guess_num_executors(args.node_name)
53+
nproc = multiprocessing.cpu_count()
54+
55+
available_cpus = nproc // executors
56+
available_cpus = max(available_cpus, 1)
57+
58+
print(available_cpus)

tests/scripts/task_build.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ if [ "$HAS_SCCACHE" -eq "1" ]; then
4646
sccache --show-stats
4747
fi
4848

49+
50+
# Figure out the number of concurrent builds that can be running on this machine
51+
# TODO: Is there a way to get this from Jenkins directly? I don't think so
52+
# https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
53+
NUM_CPUS=$(python tests/scripts/find_nproc.py --node-name "$NODE_NAME")
54+
MAKE_PROCESSES_ARG="-j$NUM_CPUS"
55+
4956
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
50-
cmake --build . -- VERBOSE=1 "$2"
57+
cmake --build . -- VERBOSE=1 "$MAKE_PROCESSES_ARG"
5158

5259
if [ "$HAS_SCCACHE" -eq "1" ]; then
5360
sccache --show-stats

0 commit comments

Comments
 (0)