Skip to content

Commit 747fbb4

Browse files
kvakiltargos
authored andcommittedNov 26, 2023
deps: V8: cherry-pick 1a782f6543ae
Original commit message: [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 <mlippautz@chromium.org> Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Cr-Commit-Position: refs/heads/main@{#88447} Refs: v8/v8@1a782f6 PR-URL: #48523 Backport-PR-URL: #50098 Fixes: #25382 Fixes: #14917 Refs: nodejs/performance#93 Refs: nodejs/performance#89 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
1 parent a98addb commit 747fbb4

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
 

‎deps/v8/BUILD.gn

+14
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ declare_args() {
8282
# Sets -dENABLE_HUGEPAGE
8383
v8_enable_hugepage = false
8484

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

@@ -862,6 +873,9 @@ config("features") {
862873
if (v8_enable_hugepage) {
863874
defines += [ "ENABLE_HUGEPAGE" ]
864875
}
876+
if (v8_enable_private_mapping_fork_optimization) {
877+
defines += [ "V8_ENABLE_PRIVATE_MAPPING_FORK_OPTIMIZATION" ]
878+
}
865879
if (v8_enable_object_print) {
866880
defines += [ "OBJECT_PRINT" ]
867881
}

‎deps/v8/src/base/platform/platform-posix.cc

+6
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ void* Allocate(void* hint, size_t size, OS::MemoryPermission access,
174174
int flags = GetFlagsForMemoryPermission(access, page_type);
175175
void* result = mmap(hint, size, prot, flags, kMmapFd, kMmapFdOffset);
176176
if (result == MAP_FAILED) return nullptr;
177+
178+
#if V8_ENABLE_PRIVATE_MAPPING_FORK_OPTIMIZATION
179+
// This is advisory, so we ignore errors.
180+
madvise(result, size, MADV_DONTFORK);
181+
#endif
182+
177183
#if ENABLE_HUGEPAGE
178184
if (result != nullptr && size >= kHugePageSize) {
179185
const uintptr_t huge_start =

0 commit comments

Comments
 (0)
Please sign in to comment.