Skip to content

Commit 2d72f17

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.
1 parent cdac185 commit 2d72f17

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

node.gyp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@
620620
'src/histogram.h',
621621
'src/histogram-inl.h',
622622
'src/js_stream.h',
623+
'src/large_pages/node_large_page.cc',
624+
'src/large_pages/node_large_page.h'
623625
'src/memory_tracker.h',
624626
'src/memory_tracker-inl.h',
625627
'src/module_wrap.h',
@@ -851,10 +853,6 @@
851853
'target_arch=="x64" and '
852854
'node_target_type=="executable"', {
853855
'defines': [ 'NODE_ENABLE_LARGE_CODE_PAGES=1' ],
854-
'sources': [
855-
'src/large_pages/node_large_page.cc',
856-
'src/large_pages/node_large_page.h'
857-
],
858856
}],
859857
[ 'use_openssl_def==1', {
860858
# TODO(bnoordhuis) Make all platforms export the same list of symbols.

src/large_pages/node_large_page.cc

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// SPDX-License-Identifier: MIT
2222

2323
#include "node_large_page.h"
24+
25+
// Besides returning ENOTSUP at runtime we do nothing if this define is missing.
26+
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
2427
#include "util.h"
2528
#include "uv.h"
2629

@@ -73,6 +76,8 @@ extern char __executable_start;
7376

7477
namespace node {
7578

79+
namespace {
80+
7681
struct text_region {
7782
char* from;
7883
char* to;
@@ -103,7 +108,7 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) {
103108
// 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
104109
// This is also handling the case where the first line is not the binary.
105110

106-
static struct text_region FindNodeTextRegion() {
111+
struct text_region FindNodeTextRegion() {
107112
struct text_region nregion;
108113
nregion.found_text_region = false;
109114
#if defined(__linux__)
@@ -263,7 +268,7 @@ static struct text_region FindNodeTextRegion() {
263268
}
264269

265270
#if defined(__linux__)
266-
static bool IsTransparentHugePagesEnabled() {
271+
bool IsTransparentHugePagesEnabled() {
267272
std::ifstream ifs;
268273

269274
ifs.open("/sys/kernel/mm/transparent_hugepage/enabled");
@@ -294,6 +299,8 @@ static bool IsSuperPagesEnabled() {
294299
}
295300
#endif
296301

302+
} // End of anonymous namespace
303+
297304
// Moving the text region to large pages. We need to be very careful.
298305
// 1: This function itself should not be moved.
299306
// We use a gcc attributes
@@ -408,32 +415,59 @@ MoveTextRegionToLargePages(const text_region& r) {
408415
if (-1 == munmap(nmem, size)) PrintSystemError(errno);
409416
return ret;
410417
}
418+
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
411419

412420
// This is the primary API called from main.
413421
int MapStaticCodeToLargePages() {
422+
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
423+
bool have_thp = false;
424+
#if defined(__linux__)
425+
have_thp = IsTransparentHugePagesEnabled();
426+
#elif defined(__FreeBSD__)
427+
have_thp = IsSuperPagesEnabled();
428+
#elif defined(__APPLE__)
429+
// pse-36 flag is present in recent mac x64 products.
430+
have_thp = true;
431+
#endif
432+
if (!have_thp)
433+
return EACCES;
434+
414435
struct text_region r = FindNodeTextRegion();
415-
if (r.found_text_region == false) {
416-
PrintWarning("failed to find text region");
417-
return -1;
418-
}
436+
if (r.found_text_region == false)
437+
return ENOENT;
419438

420439
#if defined(__FreeBSD__)
421440
if (r.from < reinterpret_cast<void*>(&MoveTextRegionToLargePages))
422441
return -1;
423442
#endif
424443

425444
return MoveTextRegionToLargePages(r);
445+
#else
446+
return ENOTSUP;
447+
#endif
426448
}
427449

428-
bool IsLargePagesEnabled() {
429-
#if defined(__linux__)
430-
return IsTransparentHugePagesEnabled();
431-
#elif defined(__FreeBSD__)
432-
return IsSuperPagesEnabled();
433-
#elif defined(__APPLE__)
434-
// pse-36 flag is present in recent mac x64 products.
435-
return true;
436-
#endif
450+
const char* LargePagesError(int status) {
451+
switch (status) {
452+
case ENOTSUP:
453+
return "Mapping to large pages is not supported.";
454+
455+
case EACCES:
456+
return "Large pages are not enabled.";
457+
458+
case ENOENT:
459+
return "failed to find text region";
460+
461+
case -1:
462+
return "Mapping code to large pages failed. Reverting to default page "
463+
"size.";
464+
465+
case 0:
466+
return "OK";
467+
468+
default:
469+
return "Unknown error";
470+
}
437471
}
438472

439473
} // 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
@@ -65,9 +65,7 @@
6565
#include "inspector/worker_inspector.h" // ParentInspectorHandle
6666
#endif
6767

68-
#ifdef NODE_ENABLE_LARGE_CODE_PAGES
6968
#include "large_pages/node_large_page.h"
70-
#endif
7169

7270
#ifdef NODE_REPORT
7371
#include "node_report.h"
@@ -936,25 +934,13 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
936934
}
937935
}
938936

939-
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
940937
if (per_process::cli_options->use_largepages == "on" ||
941938
per_process::cli_options->use_largepages == "silent") {
942-
if (node::IsLargePagesEnabled()) {
943-
if (node::MapStaticCodeToLargePages() != 0 &&
944-
per_process::cli_options->use_largepages != "silent") {
945-
fprintf(stderr,
946-
"Mapping code to large pages failed. Reverting to default page "
947-
"size.\n");
948-
}
949-
} else if (per_process::cli_options->use_largepages != "silent") {
950-
fprintf(stderr, "Large pages are not enabled.\n");
939+
int result = node::MapStaticCodeToLargePages();
940+
if (per_process::cli_options->use_largepages == "on" && result != 0) {
941+
fprintf(stderr, "%s\n", node::LargePagesError(result));
951942
}
952943
}
953-
#else
954-
if (per_process::cli_options->use_largepages == "on") {
955-
fprintf(stderr, "Mapping to large pages is not supported.\n");
956-
}
957-
#endif // NODE_ENABLE_LARGE_CODE_PAGES
958944

959945
if (per_process::cli_options->print_version) {
960946
printf("%s\n", NODE_VERSION);

0 commit comments

Comments
 (0)