mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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: https://github.com/nodejs/node/pull/31904 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
This commit is contained in:
6
node.gyp
6
node.gyp
@@ -620,6 +620,8 @@
|
||||
'src/histogram.h',
|
||||
'src/histogram-inl.h',
|
||||
'src/js_stream.h',
|
||||
'src/large_pages/node_large_page.cc',
|
||||
'src/large_pages/node_large_page.h'
|
||||
'src/memory_tracker.h',
|
||||
'src/memory_tracker-inl.h',
|
||||
'src/module_wrap.h',
|
||||
@@ -851,10 +853,6 @@
|
||||
'target_arch=="x64" and '
|
||||
'node_target_type=="executable"', {
|
||||
'defines': [ 'NODE_ENABLE_LARGE_CODE_PAGES=1' ],
|
||||
'sources': [
|
||||
'src/large_pages/node_large_page.cc',
|
||||
'src/large_pages/node_large_page.h'
|
||||
],
|
||||
}],
|
||||
[ 'use_openssl_def==1', {
|
||||
# TODO(bnoordhuis) Make all platforms export the same list of symbols.
|
||||
|
||||
@@ -21,6 +21,11 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "node_large_page.h"
|
||||
|
||||
#include <cerrno> // NOLINT(build/include)
|
||||
|
||||
// Besides returning ENOTSUP at runtime we do nothing if this define is missing.
|
||||
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
|
||||
#include "util.h"
|
||||
#include "uv.h"
|
||||
|
||||
@@ -35,7 +40,6 @@
|
||||
#endif
|
||||
#include <unistd.h> // readlink
|
||||
|
||||
#include <cerrno> // NOLINT(build/include)
|
||||
#include <climits> // PATH_MAX
|
||||
#include <clocale>
|
||||
#include <csignal>
|
||||
@@ -71,7 +75,11 @@ extern char __executable_start;
|
||||
} // extern "C"
|
||||
#endif // defined(__linux__)
|
||||
|
||||
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
|
||||
namespace node {
|
||||
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
|
||||
|
||||
namespace {
|
||||
|
||||
struct text_region {
|
||||
char* from;
|
||||
@@ -103,7 +111,7 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) {
|
||||
// 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
|
||||
// This is also handling the case where the first line is not the binary.
|
||||
|
||||
static struct text_region FindNodeTextRegion() {
|
||||
struct text_region FindNodeTextRegion() {
|
||||
struct text_region nregion;
|
||||
nregion.found_text_region = false;
|
||||
#if defined(__linux__)
|
||||
@@ -263,7 +271,7 @@ static struct text_region FindNodeTextRegion() {
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
static bool IsTransparentHugePagesEnabled() {
|
||||
bool IsTransparentHugePagesEnabled() {
|
||||
std::ifstream ifs;
|
||||
|
||||
ifs.open("/sys/kernel/mm/transparent_hugepage/enabled");
|
||||
@@ -294,6 +302,8 @@ static bool IsSuperPagesEnabled() {
|
||||
}
|
||||
#endif
|
||||
|
||||
} // End of anonymous namespace
|
||||
|
||||
// Moving the text region to large pages. We need to be very careful.
|
||||
// 1: This function itself should not be moved.
|
||||
// We use a gcc attributes
|
||||
@@ -408,14 +418,26 @@ MoveTextRegionToLargePages(const text_region& r) {
|
||||
if (-1 == munmap(nmem, size)) PrintSystemError(errno);
|
||||
return ret;
|
||||
}
|
||||
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
|
||||
|
||||
// This is the primary API called from main.
|
||||
int MapStaticCodeToLargePages() {
|
||||
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
|
||||
bool have_thp = false;
|
||||
#if defined(__linux__)
|
||||
have_thp = IsTransparentHugePagesEnabled();
|
||||
#elif defined(__FreeBSD__)
|
||||
have_thp = IsSuperPagesEnabled();
|
||||
#elif defined(__APPLE__)
|
||||
// pse-36 flag is present in recent mac x64 products.
|
||||
have_thp = true;
|
||||
#endif
|
||||
if (!have_thp)
|
||||
return EACCES;
|
||||
|
||||
struct text_region r = FindNodeTextRegion();
|
||||
if (r.found_text_region == false) {
|
||||
PrintWarning("failed to find text region");
|
||||
return -1;
|
||||
}
|
||||
if (r.found_text_region == false)
|
||||
return ENOENT;
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
if (r.from < reinterpret_cast<void*>(&MoveTextRegionToLargePages))
|
||||
@@ -423,17 +445,32 @@ int MapStaticCodeToLargePages() {
|
||||
#endif
|
||||
|
||||
return MoveTextRegionToLargePages(r);
|
||||
}
|
||||
|
||||
bool IsLargePagesEnabled() {
|
||||
#if defined(__linux__)
|
||||
return IsTransparentHugePagesEnabled();
|
||||
#elif defined(__FreeBSD__)
|
||||
return IsSuperPagesEnabled();
|
||||
#elif defined(__APPLE__)
|
||||
// pse-36 flag is present in recent mac x64 products.
|
||||
return true;
|
||||
#else
|
||||
return ENOTSUP;
|
||||
#endif
|
||||
}
|
||||
|
||||
const char* LargePagesError(int status) {
|
||||
switch (status) {
|
||||
case ENOTSUP:
|
||||
return "Mapping to large pages is not supported.";
|
||||
|
||||
case EACCES:
|
||||
return "Large pages are not enabled.";
|
||||
|
||||
case ENOENT:
|
||||
return "failed to find text region";
|
||||
|
||||
case -1:
|
||||
return "Mapping code to large pages failed. Reverting to default page "
|
||||
"size.";
|
||||
|
||||
case 0:
|
||||
return "OK";
|
||||
|
||||
default:
|
||||
return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
|
||||
@@ -25,10 +25,9 @@
|
||||
|
||||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
|
||||
|
||||
namespace node {
|
||||
bool IsLargePagesEnabled();
|
||||
int MapStaticCodeToLargePages();
|
||||
const char* LargePagesError(int status);
|
||||
} // namespace node
|
||||
|
||||
#endif // NODE_WANT_INTERNALS
|
||||
|
||||
20
src/node.cc
20
src/node.cc
@@ -65,9 +65,7 @@
|
||||
#include "inspector/worker_inspector.h" // ParentInspectorHandle
|
||||
#endif
|
||||
|
||||
#ifdef NODE_ENABLE_LARGE_CODE_PAGES
|
||||
#include "large_pages/node_large_page.h"
|
||||
#endif
|
||||
|
||||
#ifdef NODE_REPORT
|
||||
#include "node_report.h"
|
||||
@@ -936,25 +934,13 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
|
||||
if (per_process::cli_options->use_largepages == "on" ||
|
||||
per_process::cli_options->use_largepages == "silent") {
|
||||
if (node::IsLargePagesEnabled()) {
|
||||
if (node::MapStaticCodeToLargePages() != 0 &&
|
||||
per_process::cli_options->use_largepages != "silent") {
|
||||
fprintf(stderr,
|
||||
"Mapping code to large pages failed. Reverting to default page "
|
||||
"size.\n");
|
||||
}
|
||||
} else if (per_process::cli_options->use_largepages != "silent") {
|
||||
fprintf(stderr, "Large pages are not enabled.\n");
|
||||
int result = node::MapStaticCodeToLargePages();
|
||||
if (per_process::cli_options->use_largepages == "on" && result != 0) {
|
||||
fprintf(stderr, "%s\n", node::LargePagesError(result));
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (per_process::cli_options->use_largepages == "on") {
|
||||
fprintf(stderr, "Mapping to large pages is not supported.\n");
|
||||
}
|
||||
#endif // NODE_ENABLE_LARGE_CODE_PAGES
|
||||
|
||||
if (per_process::cli_options->print_version) {
|
||||
printf("%s\n", NODE_VERSION);
|
||||
|
||||
Reference in New Issue
Block a user