Skip to content

Commit 286251f

Browse files
committed
Improve CPU counting
Use sched_getaffinity() if available. Fall back to 1 if the number of CPUs cannot be determined.
1 parent b91d7ee commit 286251f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

check-all-the-things

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import collections
2727
import configparser
2828
import fnmatch
2929
import glob
30-
import multiprocessing
3130
import os
3231
import re
3332
import pty
@@ -1180,6 +1179,15 @@ def terminal_working():
11801179
return False
11811180

11821181

1182+
def get_cpu_count():
1183+
try:
1184+
sched_getaffinity = os.sched_getaffinity
1185+
except AttributeError:
1186+
return os.cpu_count() or 1
1187+
else:
1188+
return len(sched_getaffinity(0))
1189+
1190+
11831191
def main():
11841192
(checks, flags) = parse_conf()
11851193
all_checks = set(checks.keys())
@@ -1214,7 +1222,7 @@ def main():
12141222
)
12151223
ap.add_argument('--jobs', '-j', metavar='N', type=int, nargs='?',
12161224
help="passed to tools that can parallelize their checks",
1217-
default=1).completer = RangeCompleter(1, multiprocessing.cpu_count())
1225+
default=1).completer = RangeCompleter(1, get_cpu_count())
12181226
ap.add_argument('--checks', '-c', metavar='selectors', nargs=1,
12191227
help="alter the set of checks to be run based on check names"
12201228
" (example: = cppcheck + lintian duck - duck)",
@@ -1299,7 +1307,7 @@ def main():
12991307
set_debian_substvars(checks)
13001308
sys.exit()
13011309
if options.jobs is None:
1302-
options.jobs = multiprocessing.cpu_count()
1310+
options.jobs = get_cpu_count()
13031311
jobs = options.jobs
13041312
run = options.commands == 'run'
13051313
verbosity = options.command_verbosity

0 commit comments

Comments
 (0)