diff --git a/src/large_pages/node_large_page.cc b/src/large_pages/node_large_page.cc index 4e2f8fc4410316..5a4d302361ac19 100644 --- a/src/large_pages/node_large_page.cc +++ b/src/large_pages/node_large_page.cc @@ -78,9 +78,12 @@ struct text_region { static const size_t hps = 2L * 1024 * 1024; +static void PrintWarning(const char* warn) { + fprintf(stderr, "Hugepages WARNING: %s\n", warn); +} + static void PrintSystemError(int error) { - fprintf(stderr, "Hugepages WARNING: %s\n", strerror(error)); - return; + PrintWarning(strerror(error)); } inline uintptr_t hugepage_align_up(uintptr_t addr) { @@ -94,7 +97,7 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) { // The format of the maps file is the following // address perms offset dev inode pathname // 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 +// This is also handling the case where the first line is not the binary. static struct text_region FindNodeTextRegion() { #if defined(__linux__) @@ -110,7 +113,7 @@ static struct text_region FindNodeTextRegion() { ifs.open("/proc/self/maps"); if (!ifs) { - fprintf(stderr, "Could not open /proc/self/maps\n"); + PrintWarning("could not open /proc/self/maps"); return nregion; } @@ -178,7 +181,7 @@ static struct text_region FindNodeTextRegion() { return nregion; } - // for struct kinfo_vmentry + // Enough for struct kinfo_vmentry. numpg = numpg * 4 / 3; auto alg = std::vector(numpg); @@ -262,39 +265,29 @@ static bool IsTransparentHugePagesEnabled() { ifs.open("/sys/kernel/mm/transparent_hugepage/enabled"); if (!ifs) { - fprintf(stderr, "Could not open file: " \ - "/sys/kernel/mm/transparent_hugepage/enabled\n"); + PrintWarning("could not open /sys/kernel/mm/transparent_hugepage/enabled"); return false; } - std::string always, madvise, never; + std::string always, madvise; if (ifs.is_open()) { - while (ifs >> always >> madvise >> never) {} + while (ifs >> always >> madvise) {} } - - int ret_status = false; - - if (always.compare("[always]") == 0) - ret_status = true; - else if (madvise.compare("[madvise]") == 0) - ret_status = true; - else if (never.compare("[never]") == 0) - ret_status = false; - ifs.close(); - return ret_status; + + return always == "[always]" || madvise == "[madvise]"; } #elif defined(__FreeBSD__) static bool IsSuperPagesEnabled() { - // It is enabled by default on amd64 + // It is enabled by default on amd64. unsigned int super_pages = 0; size_t super_pages_length = sizeof(super_pages); - if (sysctlbyname("vm.pmap.pg_ps_enabled", &super_pages, - &super_pages_length, nullptr, 0) == -1 || - super_pages < 1) { - return false; - } - return true; + return sysctlbyname("vm.pmap.pg_ps_enabled", + &super_pages, + &super_pages_length, + nullptr, + 0) != -1 && + super_pages >= 1; } #endif @@ -326,7 +319,7 @@ MoveTextRegionToLargePages(const text_region& r) { size_t size = r.to - r.from; void* start = r.from; - // Allocate temporary region preparing for copy + // Allocate temporary region preparing for copy. nmem = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (nmem == MAP_FAILED) { @@ -410,11 +403,11 @@ MoveTextRegionToLargePages(const text_region& r) { return ret; } -// This is the primary API called from main +// This is the primary API called from main. int MapStaticCodeToLargePages() { struct text_region r = FindNodeTextRegion(); if (r.found_text_region == false) { - fprintf(stderr, "Hugepages WARNING: failed to find text region\n"); + PrintWarning("failed to find text region"); return -1; }