From 7adb0ba1f2405e393825107ebc5ddaa672cc6e94 Mon Sep 17 00:00:00 2001 From: Nick Lynch Date: Tue, 27 Apr 2021 09:46:51 +0100 Subject: [PATCH] chore: patch jsii-rosetta to limit the number of worker threads (#14389) jsii-rosetta uses multiple worker threads by default to speed up sample extraction. It defaults to spawning workers equal to half the number of cores. On extremely powerful build machines (e.g., CodeBuild X2_LARGE compute with 72 vCPUs), the high number of workers (36) results in thrash and high memory usage due to duplicate loads of source files. This was causing the v2 builds to fail with: "FATAL ERROR: NewSpace::Rebalance Allocation failed - JavaScript heap out of memory" The patch simply limits the top number of worker threads to an arbitrarily-chosen maximum limit of 16. We could simply disable the worker threads, but this takes much longer to process. With single-threading, rosetta takes ~35 minutes to extract samples from the CDK; with 16 workers, it takes ~3.5 minutes. I absolutely want to find the proper way to backfeed this into rosetta, but opting for the simple patch approach at the moment because our v2 builds have been blocked for about a week already. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* (cherry picked from commit 07fe771ded4d100fd98b7897aee141c53c1646de) --- patches/README.md | 14 ++++++++++++++ patches/jsii-rosetta+1.28.0.patch | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 patches/jsii-rosetta+1.28.0.patch diff --git a/patches/README.md b/patches/README.md index 46c0f0415dc2f..64be828bc94d0 100644 --- a/patches/README.md +++ b/patches/README.md @@ -27,3 +27,17 @@ $ time node_modules/.bin/lerna exec pwd lerna success exec Executed command in 219 packages: "pwd" 1.11 real 0.99 user 0.82 sys ``` + +## jsii-rosetta+1.28.0.patch + +jsii-rosetta uses multiple worker threads by default to speed up sample extraction. +It defaults to spawning workers equal to half the number of cores. On extremely +powerful build machines (e.g., CodeBuild X2_LARGE compute with 72 vCPUs), +the high number of workers (36) results in thrash and high memory usage due to +duplicate loads of source files. This was causing the v2 builds to fail with: +"FATAL ERROR: NewSpace::Rebalance Allocation failed - JavaScript heap out of memory" + +The patch simply limits the top number of worker threads to an arbitrarily-chosen +maximum limit of 16. We could simply disable the worker threads, but this takes much +longer to process. With single-threading, rosetta takes ~35 minutes to extract samples +from the CDK; with 16 workers, it takes ~3.5 minutes. diff --git a/patches/jsii-rosetta+1.28.0.patch b/patches/jsii-rosetta+1.28.0.patch new file mode 100644 index 0000000000000..89dc133e267ca --- /dev/null +++ b/patches/jsii-rosetta+1.28.0.patch @@ -0,0 +1,14 @@ +diff --git a/node_modules/jsii-rosetta/lib/commands/extract.js b/node_modules/jsii-rosetta/lib/commands/extract.js +index e695ea9..539038e 100644 +--- a/node_modules/jsii-rosetta/lib/commands/extract.js ++++ b/node_modules/jsii-rosetta/lib/commands/extract.js +@@ -104,7 +104,8 @@ exports.singleThreadedTranslateAll = singleThreadedTranslateAll; + async function workerBasedTranslateAll(worker, snippets, includeCompilerDiagnostics) { + // Use about half the advertised cores because hyperthreading doesn't seem to help that + // much (on my machine, using more than half the cores actually makes it slower). +- const N = Math.max(1, Math.ceil(os.cpus().length / 2)); ++ // Cap to a reasonable top-level limit to prevent thrash on machines with many, many cores. ++ const N = Math.min(16, Math.max(1, Math.ceil(os.cpus().length / 2))); + const snippetArr = Array.from(snippets); + const groups = util_1.divideEvenly(N, snippetArr); + logging.info(`Translating ${snippetArr.length} snippets using ${groups.length} workers`);