Skip to content

Commit 776f379

Browse files
Gabriel Schulhofcodebytere
Gabriel Schulhof
authored andcommittedFeb 27, 2020
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 <info@bnoordhuis.nl> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
1 parent c27f0d1 commit 776f379

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
@@ -619,6 +619,8 @@
619619
'src/histogram.h',
620620
'src/histogram-inl.h',
621621
'src/js_stream.h',
622+
'src/large_pages/node_large_page.cc',
623+
'src/large_pages/node_large_page.h'
622624
'src/memory_tracker.h',
623625
'src/memory_tracker-inl.h',
624626
'src/module_wrap.h',
@@ -850,10 +852,6 @@
850852
'target_arch=="x64" and '
851853
'node_target_type=="executable"', {
852854
'defines': [ 'NODE_ENABLE_LARGE_CODE_PAGES=1' ],
853-
'sources': [
854-
'src/large_pages/node_large_page.cc',
855-
'src/large_pages/node_large_page.h'
856-
],
857855
}],
858856
[ 'use_openssl_def==1', {
859857
# 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"
@@ -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)
Please sign in to comment.