Revert "[lit] Use os.cpu_count() to cleanup TODO"

A bot owner contacted me.  I will re-land after confirming that this
doesn't break anyone (since it's low priority).

This reverts commit 9946b169c3.
This commit is contained in:
Julian Lettner 2021-01-25 13:31:27 -08:00
parent 12049d8885
commit db1a7089ea
3 changed files with 26 additions and 17 deletions

View File

@ -23,7 +23,7 @@ def parse_args():
metavar="N",
help="Number of workers used for testing",
type=_positive_int,
default=lit.util.usable_core_count())
default=lit.util.detectCPUs())
parser.add_argument("--config-prefix",
dest="configPrefix",
metavar="NAME",

View File

@ -110,7 +110,7 @@ class Run(object):
# threads counts toward the current process limit. Try to raise the (soft)
# process limit so that tests don't fail due to resource exhaustion.
def _increase_process_limit(self):
ncpus = lit.util.usable_core_count()
ncpus = lit.util.detectCPUs()
desired_limit = self.workers * ncpus * 2 # the 2 is a safety factor
# Importing the resource module will likely fail on Windows.

View File

@ -109,23 +109,32 @@ def to_unicode(s):
return s
def usable_core_count():
"""Return the number of cores the current process can use, if supported.
Otherwise, return the total number of cores (like `os.cpu_count()`).
Default to 1 if undetermined.
# TODO(yln): multiprocessing.cpu_count()
# TODO(python3): len(os.sched_getaffinity(0)) and os.cpu_count()
def detectCPUs():
"""Detects the number of CPUs on a system.
Cribbed from pp.
"""
try:
n = len(os.sched_getaffinity(0))
except AttributeError:
n = os.cpu_count() or 1
# On Windows, with more than 32 processes, process creation often fails with
# "Too many open files". FIXME: Check if there's a better fix.
if platform.system() == 'Windows':
return min(n, 32)
return n
# Linux, Unix and MacOS:
if hasattr(os, 'sysconf'):
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
# Linux & Unix:
ncpus = os.sysconf('SC_NPROCESSORS_ONLN')
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(subprocess.check_output(['sysctl', '-n', 'hw.ncpu'],
stderr=subprocess.STDOUT))
# Windows:
if 'NUMBER_OF_PROCESSORS' in os.environ:
ncpus = int(os.environ['NUMBER_OF_PROCESSORS'])
if ncpus > 0:
# With more than 32 processes, process creation often fails with
# "Too many open files". FIXME: Check if there's a better fix.
return min(ncpus, 32)
return 1 # Default
def mkdir(path):