Skip to content

Commit 72a4e62

Browse files
joker-ephaugusto2112
authored andcommitted
[LIT] Workaround the 60 processed limit on Windows (llvm#157759)
Python multiprocessing is limited to 60 workers at most: https://github.com/python/cpython/blob/6bc65c30ff1fd0b581a2c93416496fc720bc442c/Lib/concurrent/futures/process.py#L669-L672 The limit being per thread pool, we can work around it by using multiple pools on windows when we want to actually use more workers.
1 parent 024d738 commit 72a4e62

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

llvm/utils/lit/lit/run.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
import lit.util
88
import lit.worker
99

10+
# Windows has a limit of 60 workers per pool.
11+
# This is defined in the multiprocessing module implementation.
12+
# See: https://github.com/python/cpython/blob/6bc65c30ff1fd0b581a2c93416496fc720bc442c/Lib/concurrent/futures/process.py#L669-L672
13+
WINDOWS_MAX_WORKERS_PER_POOL = 60
14+
15+
16+
def _ceilDiv(a, b):
17+
return (a + b - 1) // b
1018

1119
class MaxFailuresError(Exception):
1220
pass
@@ -72,25 +80,65 @@ def _execute(self, deadline):
7280
if v is not None
7381
}
7482

75-
pool = multiprocessing.Pool(
76-
self.workers, lit.worker.initialize, (self.lit_config, semaphores)
83+
# Windows has a limit of 60 workers per pool, so we need to use multiple pools
84+
# if we have more workers requested than the limit.
85+
# Also, allow to override the limit with the LIT_WINDOWS_MAX_WORKERS_PER_POOL environment variable.
86+
max_workers_per_pool = (
87+
WINDOWS_MAX_WORKERS_PER_POOL if os.name == "nt" else self.workers
88+
)
89+
max_workers_per_pool = int(
90+
os.getenv("LIT_WINDOWS_MAX_WORKERS_PER_POOL", max_workers_per_pool)
7791
)
7892

79-
async_results = [
80-
pool.apply_async(
81-
lit.worker.execute, args=[test], callback=self.progress_callback
93+
num_pools = max(1, _ceilDiv(self.workers, max_workers_per_pool))
94+
95+
# Distribute self.workers across num_pools as evenly as possible
96+
workers_per_pool_list = [self.workers // num_pools] * num_pools
97+
for pool_idx in range(self.workers % num_pools):
98+
workers_per_pool_list[pool_idx] += 1
99+
100+
if num_pools > 1:
101+
self.lit_config.note(
102+
"Using %d pools balancing %d workers total distributed as %s (Windows worker limit workaround)"
103+
% (num_pools, self.workers, workers_per_pool_list)
82104
)
83-
for test in self.tests
84-
]
85-
pool.close()
105+
106+
# Create multiple pools
107+
pools = []
108+
for pool_size in workers_per_pool_list:
109+
pool = multiprocessing.Pool(
110+
pool_size, lit.worker.initialize, (self.lit_config, semaphores)
111+
)
112+
pools.append(pool)
113+
114+
# Distribute tests across pools
115+
tests_per_pool = _ceilDiv(len(self.tests), num_pools)
116+
async_results = []
117+
118+
for pool_idx, pool in enumerate(pools):
119+
start_idx = pool_idx * tests_per_pool
120+
end_idx = min(start_idx + tests_per_pool, len(self.tests))
121+
for test in self.tests[start_idx:end_idx]:
122+
ar = pool.apply_async(
123+
lit.worker.execute, args=[test], callback=self.progress_callback
124+
)
125+
async_results.append(ar)
126+
127+
# Close all pools
128+
for pool in pools:
129+
pool.close()
86130

87131
try:
88132
self._wait_for(async_results, deadline)
89133
except:
90-
pool.terminate()
134+
# Terminate all pools on exception
135+
for pool in pools:
136+
pool.terminate()
91137
raise
92138
finally:
93-
pool.join()
139+
# Join all pools
140+
for pool in pools:
141+
pool.join()
94142

95143
def _wait_for(self, async_results, deadline):
96144
timeout = deadline - time.time()

llvm/utils/lit/lit/util.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ def usable_core_count():
114114
except AttributeError:
115115
n = os.cpu_count() or 1
116116

117-
# On Windows with more than 60 processes, multiprocessing's call to
118-
# _winapi.WaitForMultipleObjects() prints an error and lit hangs.
119-
if platform.system() == "Windows":
120-
return min(n, 60)
121-
122117
return n
123118

124119
def abs_path_preserve_drive(path):
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Create a directory with 20 files and check the number of pools and workers per pool that lit will use.
2+
3+
# RUN: rm -Rf %t.dir && mkdir -p %t.dir
4+
# RUN: python -c "for i in range(20): open(rf'%t.dir/file{i}.txt', 'w').write('RUN:')"
5+
6+
# RUN: echo "import lit.formats" > %t.dir/lit.cfg
7+
# RUN: echo "config.name = \"top-level-suite\"" >> %t.dir/lit.cfg
8+
# RUN: echo "config.suffixes = [\".txt\"]" >> %t.dir/lit.cfg
9+
# RUN: echo "config.test_format = lit.formats.ShTest()" >> %t.dir/lit.cfg
10+
11+
12+
# 15 workers per pool max, 100 workers total max: we expect lit to cap the workers to the number of files
13+
# RUN: env "LIT_WINDOWS_MAX_WORKERS_PER_POOL=15" %{lit} -s %t.dir/ -j100 > %t.out 2>&1
14+
# CHECK: Using 2 pools balancing 20 workers total distributed as [10, 10]
15+
# CHECK: Passed: 20
16+
17+
# 5 workers per pool max, 17 workers total max
18+
# RUN: env "LIT_WINDOWS_MAX_WORKERS_PER_POOL=5" %{lit} -s %t.dir/ -j17 >> %t.out 2>&1
19+
# CHECK: Using 4 pools balancing 17 workers total distributed as [5, 4, 4, 4]
20+
# CHECK: Passed: 20
21+
22+
# 19 workers per pool max, 19 workers total max
23+
# RUN: env "LIT_WINDOWS_MAX_WORKERS_PER_POOL=19" %{lit} -s %t.dir/ -j19 >> %t.out 2>&1
24+
# CHECK-NOT: workers total distributed as
25+
# CHECK: Passed: 20
26+
27+
# RUN: cat %t.out | FileCheck %s

0 commit comments

Comments
 (0)