Skip to content

Commit 8a7f607

Browse files
author
Gabriel Schulhof
committed
src: include large pages source unconditionally
Restrict the usage of the C preprocessor directive enabling large pages support to the large pages implementation. This cleans up the code in src/node.cc. PR-URL: #31904 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 56305ee commit 8a7f607

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

node.gyp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@
406406
'src/env-inl.h',
407407
'src/handle_wrap.h',
408408
'src/js_stream.h',
409+
'src/large_pages/node_large_page.cc',
410+
'src/large_pages/node_large_page.h'
409411
'src/module_wrap.h',
410412
'src/node.h',
411413
'src/node_buffer.h',
@@ -610,10 +612,6 @@
610612
'target_arch=="x64" and '
611613
'node_target_type=="executable"', {
612614
'defines': [ 'NODE_ENABLE_LARGE_CODE_PAGES=1' ],
613-
'sources': [
614-
'src/large_pages/node_large_page.cc',
615-
'src/large_pages/node_large_page.h'
616-
],
617615
}],
618616
[ 'use_openssl_def==1', {
619617
# TODO(bnoordhuis) Make all platforms export the same list of symbols.

src/large_pages/node_large_page.cc

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
#include "node_internals.h"
2424
#include "node_large_page.h"
25+
26+
#include <cerrno> // NOLINT(build/include)
27+
28+
// Besides returning ENOTSUP at runtime we do nothing if this define is missing.
29+
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
2530
#include "util.h"
2631
#include "uv.h"
2732

@@ -36,7 +41,6 @@
3641
#endif
3742
#include <unistd.h> // readlink
3843

39-
#include <cerrno> // NOLINT(build/include)
4044
#include <climits> // PATH_MAX
4145
#include <clocale>
4246
#include <csignal>
@@ -79,7 +83,11 @@ extern char __executable_start;
7983
} // extern "C"
8084
#endif // defined(__linux__)
8185

86+
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
8287
namespace node {
88+
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
89+
90+
namespace {
8391

8492
struct text_region {
8593
char* from;
@@ -111,7 +119,7 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) {
111119
// 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
112120
// This is also handling the case where the first line is not the binary.
113121

114-
static struct text_region FindNodeTextRegion() {
122+
struct text_region FindNodeTextRegion() {
115123
struct text_region nregion;
116124
nregion.found_text_region = false;
117125
#if defined(__linux__)
@@ -271,7 +279,7 @@ static struct text_region FindNodeTextRegion() {
271279
}
272280

273281
#if defined(__linux__)
274-
static bool IsTransparentHugePagesEnabled() {
282+
bool IsTransparentHugePagesEnabled() {
275283
std::ifstream ifs;
276284

277285
ifs.open("/sys/kernel/mm/transparent_hugepage/enabled");
@@ -302,6 +310,8 @@ static bool IsSuperPagesEnabled() {
302310
}
303311
#endif
304312

313+
} // End of anonymous namespace
314+
305315
// Moving the text region to large pages. We need to be very careful.
306316
// 1: This function itself should not be moved.
307317
// We use a gcc attributes
@@ -416,32 +426,59 @@ MoveTextRegionToLargePages(const text_region& r) {
416426
if (-1 == munmap(nmem, size)) PrintSystemError(errno);
417427
return ret;
418428
}
429+
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
419430

420431
// This is the primary API called from main.
421432
int MapStaticCodeToLargePages() {
433+
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
434+
bool have_thp = false;
435+
#if defined(__linux__)
436+
have_thp = IsTransparentHugePagesEnabled();
437+
#elif defined(__FreeBSD__)
438+
have_thp = IsSuperPagesEnabled();
439+
#elif defined(__APPLE__)
440+
// pse-36 flag is present in recent mac x64 products.
441+
have_thp = true;
442+
#endif
443+
if (!have_thp)
444+
return EACCES;
445+
422446
struct text_region r = FindNodeTextRegion();
423-
if (r.found_text_region == false) {
424-
PrintWarning("failed to find text region");
425-
return -1;
426-
}
447+
if (r.found_text_region == false)
448+
return ENOENT;
427449

428450
#if defined(__FreeBSD__)
429451
if (r.from < reinterpret_cast<void*>(&MoveTextRegionToLargePages))
430452
return -1;
431453
#endif
432454

433455
return MoveTextRegionToLargePages(r);
456+
#else
457+
return ENOTSUP;
458+
#endif
434459
}
435460

436-
bool IsLargePagesEnabled() {
437-
#if defined(__linux__)
438-
return IsTransparentHugePagesEnabled();
439-
#elif defined(__FreeBSD__)
440-
return IsSuperPagesEnabled();
441-
#elif defined(__APPLE__)
442-
// pse-36 flag is present in recent mac x64 products.
443-
return true;
444-
#endif
461+
const char* LargePagesError(int status) {
462+
switch (status) {
463+
case ENOTSUP:
464+
return "Mapping to large pages is not supported.";
465+
466+
case EACCES:
467+
return "Large pages are not enabled.";
468+
469+
case ENOENT:
470+
return "failed to find text region";
471+
472+
case -1:
473+
return "Mapping code to large pages failed. Reverting to default page "
474+
"size.";
475+
476+
case 0:
477+
return "OK";
478+
479+
default:
480+
return "Unknown error";
481+
}
445482
}
446483

447484
} // namespace node

src/large_pages/node_large_page.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525

2626
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2727

28-
2928
namespace node {
30-
bool IsLargePagesEnabled();
3129
int MapStaticCodeToLargePages();
30+
const char* LargePagesError(int status);
3231
} // namespace node
3332

3433
#endif // NODE_WANT_INTERNALS

src/node.cc

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@
7474
#include "../deps/v8/src/third_party/vtune/v8-vtune.h"
7575
#endif
7676

77-
#ifdef NODE_ENABLE_LARGE_CODE_PAGES
7877
#include "large_pages/node_large_page.h"
79-
#endif
8078

8179
#include <errno.h>
8280
#include <fcntl.h> // _O_RDWR
@@ -3029,25 +3027,13 @@ int Start(int argc, char** argv) {
30293027
// This needs to run *before* V8::Initialize().
30303028
Init(&args, &exec_args);
30313029

3032-
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
30333030
if (per_process_opts->use_largepages == "on" ||
30343031
per_process_opts->use_largepages == "silent") {
3035-
if (node::IsLargePagesEnabled()) {
3036-
if (node::MapStaticCodeToLargePages() != 0 &&
3037-
per_process_opts->use_largepages != "silent") {
3038-
fprintf(stderr,
3039-
"Mapping code to large pages failed. Reverting to default page "
3040-
"size.\n");
3041-
}
3042-
} else if (per_process_opts->use_largepages != "silent") {
3043-
fprintf(stderr, "Large pages are not enabled.\n");
3032+
int result = node::MapStaticCodeToLargePages();
3033+
if (per_process_opts->use_largepages == "on" && result != 0) {
3034+
fprintf(stderr, "%s\n", node::LargePagesError(result));
30443035
}
30453036
}
3046-
#else
3047-
if (per_process_opts->use_largepages == "on") {
3048-
fprintf(stderr, "Mapping to large pages is not supported.\n");
3049-
}
3050-
#endif // NODE_ENABLE_LARGE_CODE_PAGES
30513037

30523038
#if HAVE_OPENSSL
30533039
{

0 commit comments

Comments
 (0)