Skip to content

Commit 1179844

Browse files
andy31415pull[bot]
authored andcommitted
Run zap in parallel by default for zap_regen_all.py (#23766)
* Run zap in parallel on regen all. This makes my local zap run in 88 seconds instead of about 222 * Restyle * Fix arguments for generate.py
1 parent 5297e33 commit 1179844

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

scripts/tools/zap/generate.py

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class CmdLineArgs:
3232
templateFile: str
3333
outputDir: str
3434
runBootstrap: bool
35+
parallel: bool = True
3536

3637

3738
CHIP_ROOT_DIR = os.path.realpath(
@@ -103,6 +104,9 @@ def runArgumentsParser() -> CmdLineArgs:
103104
help='Output directory for the generated files (default: automatically selected)')
104105
parser.add_argument('--run-bootstrap', default=None, action='store_true',
105106
help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
107+
parser.add_argument('--parallel', action='store_true')
108+
parser.add_argument('--no-parallel', action='store_false', dest='parallel')
109+
parser.set_defaults(parallel=True)
106110
args = parser.parse_args()
107111

108112
# By default, this script assumes that the global CHIP template is used with
@@ -259,6 +263,11 @@ def main():
259263

260264
# The maximum memory usage is over 4GB (#15620)
261265
os.environ["NODE_OPTIONS"] = "--max-old-space-size=8192"
266+
267+
if cmdLineArgs.parallel:
268+
# Parallel-compatible runs will need separate state
269+
os.environ["ZAP_TEMPSTATE"] = "1"
270+
262271
runGeneration(cmdLineArgs.zapFile, cmdLineArgs.zclFile, cmdLineArgs.templateFile, cmdLineArgs.outputDir)
263272

264273
prettifiers = [

scripts/tools/zap_regen_all.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import sys
2222
import subprocess
2323
import logging
24+
import multiprocessing
25+
2426
from dataclasses import dataclass
2527

2628
CHIP_ROOT_DIR = os.path.realpath(
@@ -113,6 +115,11 @@ def setupArgumentsParser():
113115
help="Don't do any generation, just log what targets would be generated (default: False)")
114116
parser.add_argument('--run-bootstrap', default=None, action='store_true',
115117
help='Automatically run ZAP bootstrap. By default the bootstrap is not triggered')
118+
119+
parser.add_argument('--parallel', action='store_true')
120+
parser.add_argument('--no-parallel', action='store_false', dest='parallel')
121+
parser.set_defaults(parallel=True)
122+
116123
return parser.parse_args()
117124

118125

@@ -253,6 +260,14 @@ def getTargets(type, test_target):
253260
return targets
254261

255262

263+
def _ParallelGenerateOne(target):
264+
"""
265+
Helper method to be passed to multiprocessing parallel generation of
266+
items.
267+
"""
268+
target.generate()
269+
270+
256271
def main():
257272
logging.basicConfig(
258273
level=logging.INFO,
@@ -269,8 +284,15 @@ def main():
269284
if args.run_bootstrap:
270285
subprocess.check_call(os.path.join(CHIP_ROOT_DIR, "scripts/tools/zap/zap_bootstrap.sh"), shell=True)
271286

272-
for target in targets:
273-
target.generate()
287+
if args.parallel:
288+
# Ensure each zap run is independent
289+
os.environ['ZAP_TEMPSTATE'] = '1'
290+
with multiprocessing.Pool() as pool:
291+
for _ in pool.imap_unordered(_ParallelGenerateOne, targets):
292+
pass
293+
else:
294+
for target in targets:
295+
target.generate()
274296

275297

276298
if __name__ == '__main__':

0 commit comments

Comments
 (0)