Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: small cleanup in large_pages code #31196

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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