Skip to content

Commit 809d8b5

Browse files
Gabriel Schulhoftargos
Gabriel Schulhof
authored andcommitted
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. Backport-PR-URL: #32092 PR-URL: #31904 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 5ea3d60 commit 809d8b5

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

node.gyp

+2-4
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@
621621
'src/histogram-inl.h',
622622
'src/http_parser_adaptor.h',
623623
'src/js_stream.h',
624+
'src/large_pages/node_large_page.cc',
625+
'src/large_pages/node_large_page.h'
624626
'src/memory_tracker.h',
625627
'src/memory_tracker-inl.h',
626628
'src/module_wrap.h',
@@ -857,10 +859,6 @@
857859
'target_arch=="x64" and '
858860
'node_target_type=="executable"', {
859861
'defines': [ 'NODE_ENABLE_LARGE_CODE_PAGES=1' ],
860-
'sources': [
861-
'src/large_pages/node_large_page.cc',
862-
'src/large_pages/node_large_page.h'
863-
],
864862
}],
865863
[ 'use_openssl_def==1', {
866864
# TODO(bnoordhuis) Make all platforms export the same list of symbols.

src/large_pages/node_large_page.cc

+53-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
// SPDX-License-Identifier: MIT
2222

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

@@ -35,7 +40,6 @@
3540
#endif
3641
#include <unistd.h> // readlink
3742

38-
#include <cerrno> // NOLINT(build/include)
3943
#include <climits> // PATH_MAX
4044
#include <clocale>
4145
#include <csignal>
@@ -71,7 +75,11 @@ extern char __executable_start;
7175
} // extern "C"
7276
#endif // defined(__linux__)
7377

78+
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
7479
namespace node {
80+
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
81+
82+
namespace {
7583

7684
struct text_region {
7785
char* from;
@@ -103,7 +111,7 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) {
103111
// 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
104112
// This is also handling the case where the first line is not the binary.
105113

106-
static struct text_region FindNodeTextRegion() {
114+
struct text_region FindNodeTextRegion() {
107115
struct text_region nregion;
108116
nregion.found_text_region = false;
109117
#if defined(__linux__)
@@ -263,7 +271,7 @@ static struct text_region FindNodeTextRegion() {
263271
}
264272

265273
#if defined(__linux__)
266-
static bool IsTransparentHugePagesEnabled() {
274+
bool IsTransparentHugePagesEnabled() {
267275
std::ifstream ifs;
268276

269277
ifs.open("/sys/kernel/mm/transparent_hugepage/enabled");
@@ -294,6 +302,8 @@ static bool IsSuperPagesEnabled() {
294302
}
295303
#endif
296304

305+
} // End of anonymous namespace
306+
297307
// Moving the text region to large pages. We need to be very careful.
298308
// 1: This function itself should not be moved.
299309
// We use a gcc attributes
@@ -408,32 +418,59 @@ MoveTextRegionToLargePages(const text_region& r) {
408418
if (-1 == munmap(nmem, size)) PrintSystemError(errno);
409419
return ret;
410420
}
421+
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
411422

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

420442
#if defined(__FreeBSD__)
421443
if (r.from < reinterpret_cast<void*>(&MoveTextRegionToLargePages))
422444
return -1;
423445
#endif
424446

425447
return MoveTextRegionToLargePages(r);
448+
#else
449+
return ENOTSUP;
450+
#endif
426451
}
427452

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
453+
const char* LargePagesError(int status) {
454+
switch (status) {
455+
case ENOTSUP:
456+
return "Mapping to large pages is not supported.";
457+
458+
case EACCES:
459+
return "Large pages are not enabled.";
460+
461+
case ENOENT:
462+
return "failed to find text region";
463+
464+
case -1:
465+
return "Mapping code to large pages failed. Reverting to default page "
466+
"size.";
467+
468+
case 0:
469+
return "OK";
470+
471+
default:
472+
return "Unknown error";
473+
}
437474
}
438475

439476
} // namespace node

src/large_pages/node_large_page.h

+1-2
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

+3-17
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"
@@ -891,25 +889,13 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
891889
}
892890
}
893891

894-
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
895892
if (per_process::cli_options->use_largepages == "on" ||
896893
per_process::cli_options->use_largepages == "silent") {
897-
if (node::IsLargePagesEnabled()) {
898-
if (node::MapStaticCodeToLargePages() != 0 &&
899-
per_process::cli_options->use_largepages != "silent") {
900-
fprintf(stderr,
901-
"Mapping code to large pages failed. Reverting to default page "
902-
"size.\n");
903-
}
904-
} else if (per_process::cli_options->use_largepages != "silent") {
905-
fprintf(stderr, "Large pages are not enabled.\n");
894+
int result = node::MapStaticCodeToLargePages();
895+
if (per_process::cli_options->use_largepages == "on" && result != 0) {
896+
fprintf(stderr, "%s\n", node::LargePagesError(result));
906897
}
907898
}
908-
#else
909-
if (per_process::cli_options->use_largepages == "on") {
910-
fprintf(stderr, "Mapping to large pages is not supported.\n");
911-
}
912-
#endif // NODE_ENABLE_LARGE_CODE_PAGES
913899

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

0 commit comments

Comments
 (0)