-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Large Page Support for Code Issue: 16198 #21064
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
Changes from 1 commit
0a76c7e
fa0e324
fdb45cd
b7791c2
6631eea
843089c
077bc01
7bdd9fc
b91de5a
7ef956a
b02e7a9
2af82b1
cae9285
30114b6
39e1f0d
49cd0de
2dd9e8c
51d0f02
2f672ee
31504cc
f998c58
9f15cfc
d6de361
29c7d13
9828036
600cf54
bf259e2
f0a6dcb
4610793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
i) CamelCase for methods, functions
ii) snake_case for variables/structs
iii) indentation (dont indent code inside namespace)- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,91 +62,91 @@ namespace node { | |
| #define PAGE_ALIGN_UP(x, a) ALIGN(x, a) | ||
| #define PAGE_ALIGN_DOWN(x, a) ((x) & ~((a) - 1)) | ||
|
|
||
| struct TextRegion { | ||
| struct text_region { | ||
| char* from; | ||
| char* to; | ||
| int total_hugepages; | ||
| bool found_text_region; | ||
| int total_hugepages; | ||
| bool found_text_region; | ||
| }; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how many of these fields are used?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, will look and make it consistent. |
||
| static void printSystemError(int error) { | ||
| fprintf(stderr, "Hugepages WARNING: %s\n", strerror(error)); | ||
| return; | ||
| } | ||
| static void PrintSystemError(int error) { | ||
| fprintf(stderr, "Hugepages WARNING: %s\n", strerror(error)); | ||
| return; | ||
| } | ||
|
|
||
| // 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 | ||
|
|
||
| static struct TextRegion find_node_text_region() { | ||
| std::ifstream ifs; | ||
| std::string map_line; | ||
| std::string permission; | ||
| char dash; | ||
| int64_t start, end; | ||
| const size_t hps = 2L * 1024 * 1024; | ||
| struct TextRegion nregion; | ||
|
|
||
| nregion.found_text_region = false; | ||
|
|
||
| ifs.open("/proc/self/maps"); | ||
| if (!ifs) { | ||
| fprintf(stderr, "Could not open /proc/self/maps\n"); | ||
| return nregion; | ||
| } | ||
| std::getline(ifs, map_line); | ||
| std::istringstream iss(map_line); | ||
| ifs.close(); | ||
|
|
||
| iss >> std::hex >> start; | ||
| iss >> dash; | ||
| iss >> std::hex >> end; | ||
| iss >> permission; | ||
|
|
||
| if (permission.compare("r-xp") == 0) { | ||
| start = reinterpret_cast<unsigned int64_t>(&__nodetext); | ||
| char* from = reinterpret_cast<char *>PAGE_ALIGN_UP(start, hps); | ||
| char* to = reinterpret_cast<char *>PAGE_ALIGN_DOWN(end, hps); | ||
|
|
||
| if (from < to) { | ||
| size_t size = to - from; | ||
| nregion.found_text_region = true; | ||
| nregion.from = from; | ||
| nregion.to = to; | ||
| nregion.total_hugepages = size / hps; | ||
| } | ||
| } | ||
|
|
||
| return nregion; | ||
| static struct text_region FindNodeTextRegion() { | ||
| std::ifstream ifs; | ||
| std::string map_line; | ||
| std::string permission; | ||
| char dash; | ||
| int64_t start, end; | ||
| const size_t hps = 2L * 1024 * 1024; | ||
| struct text_region nregion; | ||
|
|
||
| nregion.found_text_region = false; | ||
|
|
||
| ifs.open("/proc/self/maps"); | ||
| if (!ifs) { | ||
| fprintf(stderr, "Could not open /proc/self/maps\n"); | ||
| return nregion; | ||
| } | ||
| std::getline(ifs, map_line); | ||
| std::istringstream iss(map_line); | ||
| ifs.close(); | ||
|
|
||
| iss >> std::hex >> start; | ||
| iss >> dash; | ||
| iss >> std::hex >> end; | ||
| iss >> permission; | ||
|
|
||
| if (permission.compare("r-xp") == 0) { | ||
| start = reinterpret_cast<unsigned int64_t>(&__nodetext); | ||
| char* from = reinterpret_cast<char *>PAGE_ALIGN_UP(start, hps); | ||
| char* to = reinterpret_cast<char *>PAGE_ALIGN_DOWN(end, hps); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style nit: left-leaning pointers (e.g.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Fixed. |
||
|
|
||
| if (from < to) { | ||
| size_t size = to - from; | ||
| nregion.found_text_region = true; | ||
| nregion.from = from; | ||
| nregion.to = to; | ||
| nregion.total_hugepages = size / hps; | ||
| } | ||
| } | ||
|
|
||
| static bool IsTransparentHugePagesEnabled() { | ||
| std::ifstream ifs; | ||
| return nregion; | ||
| } | ||
|
|
||
| ifs.open("/sys/kernel/mm/transparent_hugepage/enabled"); | ||
| if (!ifs) { | ||
| fprintf(stderr, "Could not open file: " \ | ||
| "/sys/kernel/mm/transparent_hugepage/enabled\n"); | ||
| return false; | ||
| } | ||
| static bool IsTransparentHugePagesEnabled() { | ||
| std::ifstream ifs; | ||
|
|
||
| std::string always, madvise, never; | ||
| if (ifs.is_open()) { | ||
| while (ifs >> always >> madvise >> never) {} | ||
| } | ||
| ifs.open("/sys/kernel/mm/transparent_hugepage/enabled"); | ||
| if (!ifs) { | ||
| fprintf(stderr, "Could not open file: " \ | ||
| "/sys/kernel/mm/transparent_hugepage/enabled\n"); | ||
| return false; | ||
| } | ||
|
|
||
| int ret_status = false; | ||
| std::string always, madvise, never; | ||
| if (ifs.is_open()) { | ||
| while (ifs >> always >> madvise >> never) {} | ||
| } | ||
|
|
||
| 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; | ||
| int ret_status = false; | ||
|
|
||
| ifs.close(); | ||
| return ret_status; | ||
| } | ||
| 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; | ||
| } | ||
|
|
||
| // Moving the text region to large pages. We need to be very careful. | ||
| // a) This function itself should not be moved. | ||
|
|
@@ -157,97 +157,97 @@ struct TextRegion { | |
| // the same virtual address | ||
| // 3. madvise with MADV_HUGE_PAGE | ||
| // 3. If successful copy the code there and unmap the original region | ||
| int | ||
| __attribute__((__section__(".eh_frame"))) | ||
| __attribute__((__aligned__(2 * 1024 * 1024))) | ||
| __attribute__((__noinline__)) | ||
| __attribute__((__optimize__("2"))) | ||
| move_text_region_to_large_pages(struct TextRegion r) { | ||
| void* nmem = nullptr; | ||
| void* tmem = nullptr; | ||
| int ret = 0; | ||
|
|
||
| size_t size = r.to - r.from; | ||
| void* start = r.from; | ||
|
|
||
| // Allocate temporary region preparing for copy | ||
| nmem = mmap(nullptr, size, | ||
| PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| if (nmem == MAP_FAILED) { | ||
| printSystemError(errno); | ||
| return -1; | ||
| } | ||
|
|
||
| memcpy(nmem, r.from, size); | ||
| int | ||
| __attribute__((__section__(".eh_frame"))) | ||
| __attribute__((__aligned__(2 * 1024 * 1024))) | ||
| __attribute__((__noinline__)) | ||
| __attribute__((__optimize__("2"))) | ||
| MoveTextRegionToLargePages(struct text_region r) { | ||
| void* nmem = nullptr; | ||
| void* tmem = nullptr; | ||
| int ret = 0; | ||
|
|
||
| size_t size = r.to - r.from; | ||
| void* start = r.from; | ||
|
|
||
| // Allocate temporary region preparing for copy | ||
| nmem = mmap(nullptr, size, | ||
| PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| if (nmem == MAP_FAILED) { | ||
| PrintSystemError(errno); | ||
| return -1; | ||
| } | ||
|
|
||
| memcpy(nmem, r.from, size); | ||
|
|
||
| // We already know the original page is r-xp | ||
| // (PROT_READ, PROT_EXEC, MAP_PRIVATE) | ||
| // We want PROT_WRITE because we are writing into it. | ||
| // We want it at the fixed address and we use MAP_FIXED. | ||
| tmem = mmap(start, size, | ||
| PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1 , 0); | ||
| if (tmem == MAP_FAILED) { | ||
| printSystemError(errno); | ||
| munmap(nmem, size); | ||
| return -1; | ||
| } | ||
|
|
||
| ret = madvise(tmem, size, MADV_HUGEPAGE); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| ret = munmap(tmem, size); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| } | ||
| ret = munmap(nmem, size); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| memcpy(start, nmem, size); | ||
| ret = mprotect(start, size, PROT_READ | PROT_EXEC); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| ret = munmap(tmem, size); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| } | ||
| ret = munmap(nmem, size); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| // Release the old/temporary mapped region | ||
| ret = munmap(nmem, size); | ||
| if (ret == -1) { | ||
| printSystemError(errno); | ||
| } | ||
|
|
||
| return ret; | ||
| tmem = mmap(start, size, | ||
| PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1 , 0); | ||
| if (tmem == MAP_FAILED) { | ||
| PrintSystemError(errno); | ||
| munmap(nmem, size); | ||
| return -1; | ||
| } | ||
|
|
||
| ret = madvise(tmem, size, MADV_HUGEPAGE); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| ret = munmap(tmem, size); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| } | ||
| ret = munmap(nmem, size); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| } | ||
|
|
||
| // This is the primary API called from main | ||
| int map_static_code_to_large_pages() { | ||
| struct TextRegion r = find_node_text_region(); | ||
| if (r.found_text_region == false) { | ||
| fprintf(stderr, "Hugepages WARNING: failed to find text region \n"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (r.to <= reinterpret_cast<void *> (&move_text_region_to_large_pages)) | ||
| return move_text_region_to_large_pages(r); | ||
| return -1; | ||
| } | ||
|
|
||
| return -1; | ||
| memcpy(start, nmem, size); | ||
| ret = mprotect(start, size, PROT_READ | PROT_EXEC); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| ret = munmap(tmem, size); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| } | ||
|
|
||
| bool IsLargePagesEnabled() { | ||
| return IsTransparentHugePagesEnabled(); | ||
| ret = munmap(nmem, size); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| // Release the old/temporary mapped region | ||
| ret = munmap(nmem, size); | ||
| if (ret == -1) { | ||
| PrintSystemError(errno); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| // 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"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (r.to <= reinterpret_cast<void *> (&MoveTextRegionToLargePages)) | ||
| return MoveTextRegionToLargePages(r); | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| bool IsLargePagesEnabled() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style nit: there is an extra space after
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Fixed this too. |
||
| return IsTransparentHugePagesEnabled(); | ||
| } | ||
|
|
||
| } // namespace node | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I admit I kinda checked out after the first 100 lines. This file has so many style errors, can you fix those first, please?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you be specific? I ran the make -s lint and fixed all the ones pointed to by that.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linter doesn't catch everything but to name three things:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks I have updated the PR with 2) and 3). I am using standard indentation of 2 spaces.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @suresh-srinivas There’s https://github.com/nodejs/node/blob/master/CPP_STYLE_GUIDE.md that would help a lot with this. Also, comparing with other code in our codebase if you’re unsure. :) (In particular, we don’t indent code inside namespaces)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @addaleax thanks much. I will look at this and fix styles accordingly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @addaleax @bnoordhuis @gireeshpunathil fixed the style errors. Let me know if you see anything else. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ | |
|
|
||
| namespace node { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be enclosed in #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks I fixed it. |
||
| bool IsLargePagesEnabled(); | ||
| int map_static_code_to_large_pages(); | ||
| int MapStaticCodeToLargePages(); | ||
| } // namespace node | ||
|
|
||
| #endif // SRC_NODE_LARGE_PAGE_H_ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style nit: these could all be inline functions, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Yes fixed them to be inline functions.