Skip to content

Commit

Permalink
src: clean up large_pages code
Browse files Browse the repository at this point in the history
PR-URL: #31196
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
lundibundi authored and MylesBorins committed Jan 16, 2020
1 parent 256289f commit 1ad9070
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/large_pages/node_large_page.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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__)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<char>(numpg);

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -413,11 +406,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;
}

Expand Down

0 comments on commit 1ad9070

Please sign in to comment.