Skip to content

Commit 9915aab

Browse files
Merge pull request #2804 from varungandhi-apple/vg-python3-is-lit
Cherry-pick Python3 changes to apple/stable/20210107
2 parents a0b6af1 + 8164616 commit 9915aab

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

llvm/utils/lit/lit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
from lit.main import main
44

llvm/utils/lit/lit/cl_arguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def parse_args():
3131
metavar="N",
3232
help="Number of workers used for testing",
3333
type=_positive_int,
34-
default=lit.util.detectCPUs())
34+
default=lit.util.usable_core_count())
3535
parser.add_argument("--config-prefix",
3636
dest="configPrefix",
3737
metavar="NAME",

llvm/utils/lit/lit/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _update_test(self, local_test, remote_test):
110110
# threads counts toward the current process limit. Try to raise the (soft)
111111
# process limit so that tests don't fail due to resource exhaustion.
112112
def _increase_process_limit(self):
113-
ncpus = lit.util.detectCPUs()
113+
ncpus = lit.util.usable_core_count()
114114
desired_limit = self.workers * ncpus * 2 # the 2 is a safety factor
115115

116116
# Importing the resource module will likely fail on Windows.

llvm/utils/lit/lit/util.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,23 @@ def to_unicode(s):
109109
return s
110110

111111

112-
# TODO(yln): multiprocessing.cpu_count()
113-
# TODO(python3): len(os.sched_getaffinity(0)) and os.cpu_count()
114-
def detectCPUs():
115-
"""Detects the number of CPUs on a system.
116-
117-
Cribbed from pp.
112+
def usable_core_count():
113+
"""Return the number of cores the current process can use, if supported.
114+
Otherwise, return the total number of cores (like `os.cpu_count()`).
115+
Default to 1 if undetermined.
118116
119117
"""
120-
# Linux, Unix and MacOS:
121-
if hasattr(os, 'sysconf'):
122-
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
123-
# Linux & Unix:
124-
ncpus = os.sysconf('SC_NPROCESSORS_ONLN')
125-
if isinstance(ncpus, int) and ncpus > 0:
126-
return ncpus
127-
else: # OSX:
128-
return int(subprocess.check_output(['sysctl', '-n', 'hw.ncpu'],
129-
stderr=subprocess.STDOUT))
130-
# Windows:
131-
if 'NUMBER_OF_PROCESSORS' in os.environ:
132-
ncpus = int(os.environ['NUMBER_OF_PROCESSORS'])
133-
if ncpus > 0:
134-
# With more than 32 processes, process creation often fails with
135-
# "Too many open files". FIXME: Check if there's a better fix.
136-
return min(ncpus, 32)
137-
return 1 # Default
118+
try:
119+
n = len(os.sched_getaffinity(0))
120+
except AttributeError:
121+
n = os.cpu_count() or 1
122+
123+
# On Windows, with more than 32 processes, process creation often fails with
124+
# "Too many open files". FIXME: Check if there's a better fix.
125+
if platform.system() == 'Windows':
126+
return min(n, 32)
127+
128+
return n
138129

139130

140131
def mkdir(path):

0 commit comments

Comments
 (0)