Skip to content

Commit 837e5af

Browse files
lundibundiTrott
authored andcommitted
src: clean up large_pages code
PR-URL: #31196 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent f5512ff commit 837e5af

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/large_pages/node_large_page.cc

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ struct text_region {
7878

7979
static const size_t hps = 2L * 1024 * 1024;
8080

81+
static void PrintWarning(const char* warn) {
82+
fprintf(stderr, "Hugepages WARNING: %s\n", warn);
83+
}
84+
8185
static void PrintSystemError(int error) {
82-
fprintf(stderr, "Hugepages WARNING: %s\n", strerror(error));
83-
return;
86+
PrintWarning(strerror(error));
8487
}
8588

8689
inline uintptr_t hugepage_align_up(uintptr_t addr) {
@@ -94,7 +97,7 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) {
9497
// The format of the maps file is the following
9598
// address perms offset dev inode pathname
9699
// 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
97-
// This is also handling the case where the first line is not the binary
100+
// This is also handling the case where the first line is not the binary.
98101

99102
static struct text_region FindNodeTextRegion() {
100103
#if defined(__linux__)
@@ -110,7 +113,7 @@ static struct text_region FindNodeTextRegion() {
110113

111114
ifs.open("/proc/self/maps");
112115
if (!ifs) {
113-
fprintf(stderr, "Could not open /proc/self/maps\n");
116+
PrintWarning("could not open /proc/self/maps");
114117
return nregion;
115118
}
116119

@@ -178,7 +181,7 @@ static struct text_region FindNodeTextRegion() {
178181
return nregion;
179182
}
180183

181-
// for struct kinfo_vmentry
184+
// Enough for struct kinfo_vmentry.
182185
numpg = numpg * 4 / 3;
183186
auto alg = std::vector<char>(numpg);
184187

@@ -262,39 +265,29 @@ static bool IsTransparentHugePagesEnabled() {
262265

263266
ifs.open("/sys/kernel/mm/transparent_hugepage/enabled");
264267
if (!ifs) {
265-
fprintf(stderr, "Could not open file: " \
266-
"/sys/kernel/mm/transparent_hugepage/enabled\n");
268+
PrintWarning("could not open /sys/kernel/mm/transparent_hugepage/enabled");
267269
return false;
268270
}
269271

270-
std::string always, madvise, never;
272+
std::string always, madvise;
271273
if (ifs.is_open()) {
272-
while (ifs >> always >> madvise >> never) {}
274+
while (ifs >> always >> madvise) {}
273275
}
274-
275-
int ret_status = false;
276-
277-
if (always.compare("[always]") == 0)
278-
ret_status = true;
279-
else if (madvise.compare("[madvise]") == 0)
280-
ret_status = true;
281-
else if (never.compare("[never]") == 0)
282-
ret_status = false;
283-
284276
ifs.close();
285-
return ret_status;
277+
278+
return always == "[always]" || madvise == "[madvise]";
286279
}
287280
#elif defined(__FreeBSD__)
288281
static bool IsSuperPagesEnabled() {
289-
// It is enabled by default on amd64
282+
// It is enabled by default on amd64.
290283
unsigned int super_pages = 0;
291284
size_t super_pages_length = sizeof(super_pages);
292-
if (sysctlbyname("vm.pmap.pg_ps_enabled", &super_pages,
293-
&super_pages_length, nullptr, 0) == -1 ||
294-
super_pages < 1) {
295-
return false;
296-
}
297-
return true;
285+
return sysctlbyname("vm.pmap.pg_ps_enabled",
286+
&super_pages,
287+
&super_pages_length,
288+
nullptr,
289+
0) != -1 &&
290+
super_pages >= 1;
298291
}
299292
#endif
300293

@@ -326,7 +319,7 @@ MoveTextRegionToLargePages(const text_region& r) {
326319
size_t size = r.to - r.from;
327320
void* start = r.from;
328321

329-
// Allocate temporary region preparing for copy
322+
// Allocate temporary region preparing for copy.
330323
nmem = mmap(nullptr, size,
331324
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
332325
if (nmem == MAP_FAILED) {
@@ -413,11 +406,11 @@ MoveTextRegionToLargePages(const text_region& r) {
413406
return ret;
414407
}
415408

416-
// This is the primary API called from main
409+
// This is the primary API called from main.
417410
int MapStaticCodeToLargePages() {
418411
struct text_region r = FindNodeTextRegion();
419412
if (r.found_text_region == false) {
420-
fprintf(stderr, "Hugepages WARNING: failed to find text region\n");
413+
PrintWarning("failed to find text region");
421414
return -1;
422415
}
423416

0 commit comments

Comments
 (0)