Skip to content

Commit 1a782f6

Browse files
Keyhan VakilV8 LUCI CQ
authored andcommitted
[base] add build flag to use MADV_DONTFORK
Embedders like Node.js and Electron expose fork(2)/execve(2) to their users. Unfortunately when the V8 heap is very large, these APIs become rather slow on Linux, due to the kernel needing to do all the bookkeeping for the forked process (in clone's dup_mmap and execve's exec_mmap). Of course, this is useless because the forked child thread will never actually need to access the V8 heap. Add a new build flag v8_enable_private_mapping_fork_optimization which marks all pages allocated by OS::Allocate as MADV_DONTFORK. This improves the performance of Node.js's fork/execve combination by 10x on a 600 MB heap. Fixed: v8:7381 Change-Id: Ib649f774d4a932b41886313ce89acc369923699d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4602858 Commit-Queue: Michael Lippautz <[email protected]> Reviewed-by: Michael Lippautz <[email protected]> Cr-Commit-Position: refs/heads/main@{#88447}
1 parent 99ceb96 commit 1a782f6

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

BUILD.gn

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ declare_args() {
8383
# Sets -dENABLE_HUGEPAGE
8484
v8_enable_hugepage = false
8585

86+
# Sets -dV8_ENABLE_PRIVATE_MAPPING_FORK_OPTIMIZATION.
87+
#
88+
# This flag speeds up the performance of fork/execve on Linux systems for
89+
# embedders which use it (like Node.js). It works by marking the pages that
90+
# V8 allocates as MADV_DONTFORK. Without MADV_DONTFORK, the Linux kernel
91+
# spends a long time manipulating page mappings on fork and exec which the
92+
# child process doesn't generally need to access.
93+
#
94+
# See v8:7381 for more details.
95+
v8_enable_private_mapping_fork_optimization = false
96+
8697
# Sets -dENABLE_HANDLE_ZAPPING.
8798
v8_enable_handle_zapping = is_asan || is_debug
8899

@@ -1005,6 +1016,9 @@ config("features") {
10051016
if (v8_enable_hugepage) {
10061017
defines += [ "ENABLE_HUGEPAGE" ]
10071018
}
1019+
if (v8_enable_private_mapping_fork_optimization) {
1020+
defines += [ "V8_ENABLE_PRIVATE_MAPPING_FORK_OPTIMIZATION" ]
1021+
}
10081022
if (v8_enable_object_print) {
10091023
defines += [ "OBJECT_PRINT" ]
10101024
}

src/base/platform/platform-posix.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ void* Allocate(void* hint, size_t size, OS::MemoryPermission access,
160160
int flags = GetFlagsForMemoryPermission(access, page_type);
161161
void* result = mmap(hint, size, prot, flags, kMmapFd, kMmapFdOffset);
162162
if (result == MAP_FAILED) return nullptr;
163+
164+
#if V8_ENABLE_PRIVATE_MAPPING_FORK_OPTIMIZATION
165+
// This is advisory, so we ignore errors.
166+
madvise(result, size, MADV_DONTFORK);
167+
#endif
168+
163169
#if ENABLE_HUGEPAGE
164170
if (result != nullptr && size >= kHugePageSize) {
165171
const uintptr_t huge_start =

0 commit comments

Comments
 (0)