-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e430bda
commit a42fddd
Showing
3 changed files
with
53 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 32 additions & 12 deletions
44
packages/cli/src/cmds/validator/keymanager/decryptKeystoreDefinitions/poolSize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
let defaultPoolSize: number; | ||
import os from "node:os"; | ||
|
||
try { | ||
if (typeof navigator !== "undefined") { | ||
defaultPoolSize = navigator.hardwareConcurrency ?? 4; | ||
} else { | ||
defaultPoolSize = (await import("node:os")).cpus().length; | ||
} | ||
} catch (e) { | ||
defaultPoolSize = 8; | ||
} | ||
/** | ||
* Amount of memory used to decrypt a single keystore | ||
* calculated from https://github.com/ethereum/staking-deposit-cli/blob/d7b530442d6e0921c9db84b3a1cf6b3ecd6b9d35/staking_deposit/key_handling/keystore.py#L190-L195 | ||
*/ | ||
export const KEYSTORE_MEMORY_USAGE = 268435456; | ||
/** | ||
* Maximum amount of memory to use per thread | ||
* conservatively, 2GB | ||
*/ | ||
export const MAX_MEMORY_USAGE_PER_THREAD = 2147483648; | ||
|
||
export const MAX_CONCURRENCY_PER_THREAD = Math.floor(MAX_MEMORY_USAGE_PER_THREAD / KEYSTORE_MEMORY_USAGE); | ||
|
||
/** | ||
* Cross-platform aprox number of logical cores | ||
* Figure out what the best combination of workers and job concurrency is to best utilize available memory | ||
*/ | ||
export {defaultPoolSize}; | ||
export function calculateThreadpoolConcurrency(): {numWorkers: number; jobConcurrency: number} { | ||
const defaultPoolSize = os.cpus().length; | ||
// Don't eat all available memory | ||
const freeMem = os.freemem() * 0.8; | ||
let numWorkers = defaultPoolSize; | ||
let jobConcurrency = 1; | ||
for (let i = defaultPoolSize; i > 0; i--) { | ||
const iConcurrency = Math.floor(freeMem / i / KEYSTORE_MEMORY_USAGE); | ||
if (iConcurrency <= MAX_CONCURRENCY_PER_THREAD && iConcurrency > jobConcurrency) { | ||
numWorkers = i; | ||
jobConcurrency = iConcurrency; | ||
} | ||
} | ||
return { | ||
numWorkers, | ||
jobConcurrency, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters