-
-
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
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,65 +35,81 @@ | |
| #include <string> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <sstream> | ||
|
|
||
| // The functions in this file map the text segment of node into 2M pages. | ||
| // The algorithm is quite simple | ||
| // The algorithm is simple | ||
| // 1. Find the text region of node binary in memory | ||
| // Examine the /proc/self/maps to determine the currently mapped text | ||
| // region and obtain the start and end | ||
| // Modify the start to point to the very beginning of node text segment | ||
| // (from variable nodetext setup in ld.script) | ||
| // Align the address of start and end to Large Page Boundaries | ||
| // | ||
| // 2. Move the text region to large pages | ||
| // Map a new area and copy the original code there | ||
| // Use mmap using the start address with MAP_FIXED so we get exactly the | ||
| // same virtual address | ||
| // Use madvise with MADV_HUGE_PAGE to use Anonymous 2M Pages | ||
| // If successful copy the code there and unmap the original region. | ||
|
|
||
| extern char __executable_start; | ||
| extern char __etext; | ||
| extern char __nodetext; | ||
|
|
||
| namespace node { | ||
| namespace largepages { | ||
| #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) | ||
| #define PAGE_ALIGN_UP(x, a) ALIGN(x, a) | ||
| #define PAGE_ALIGN_DOWN(x, a) ((x) & ~((a) - 1)) | ||
|
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: these could all be inline functions, right?
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. Yes fixed them to be inline functions. |
||
|
|
||
| struct TextRegion { | ||
| void * from; | ||
| void * to; | ||
| int totalHugePages; | ||
| int64_t offset; | ||
| bool found_text_region; | ||
| void * from; | ||
| void * to; | ||
| 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; | ||
| } | ||
|
|
||
|
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 am against printing anything - if this whole exercise works or fails, let it be transparent to outside.
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. It is easy to trip up on some OS configuration/settings. At the beginning as this is getting tested on more systems, we wanted to be able to provide some messages for users so we can also fix those problems. |
||
| // 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() { | ||
| FILE *f; | ||
| int64_t start, end, offset, inode; | ||
| char perm[5], dev[6], name[256]; | ||
| int ret; | ||
| const size_t hugePageSize = 2L * 1024 * 1024; | ||
| 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; | ||
|
|
||
| f = fopen("/proc/self/maps", "r"); | ||
| ret = fscanf(f, "%lx-%lx %4s %lx %5s %ld %s\n", | ||
| &start, &end, perm, &offset, dev, &inode, name); | ||
| fclose(f); | ||
| ifs.open("/proc/self/maps"); | ||
|
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. Assumes
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. ok, added checks. |
||
| 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 (ret == 7 && | ||
| perm[0] == 'r' && perm[1] == '-' && perm[2] == 'x') { | ||
| // Checking if the region is from node binary and executable | ||
| if (permission.compare("r-xp") == 0) { | ||
| start = (unsigned int64_t) &__nodetext; | ||
|
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. Don't use C style casts, use proper C++ casts (in this case
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. ok fixed that. in fact took out that line and use nodetext directly. |
||
| char *from = reinterpret_cast<char *>PAGE_ALIGN_UP(start, hugePageSize); | ||
| char *to = reinterpret_cast<char *>PAGE_ALIGN_DOWN(end, hugePageSize); | ||
| 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 = (intptr_t)to - (intptr_t)from; | ||
| size_t size = to - from; | ||
| nregion.found_text_region = true; | ||
| nregion.from = from; | ||
| nregion.to = to; | ||
| nregion.offset = offset; | ||
| nregion.totalHugePages = size/hugePageSize; | ||
| return nregion; | ||
| nregion.total_hugepages = size / hps; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -105,7 +121,8 @@ struct TextRegion { | |
|
|
||
| ifs.open("/sys/kernel/mm/transparent_hugepage/enabled"); | ||
| if (!ifs) { | ||
| fprintf(stderr, "WARNING: Couldn't check hugepages support\n"); | ||
| fprintf(stderr, "Could not open file: " \ | ||
| "/sys/kernel/mm/transparent_hugepage/enabled\n"); | ||
| return false; | ||
|
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. can we follow a consistent return value convention? (returning null vs. -1 vs errno) I suggest look arounf in other APIs for the prevailing convention.
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 |
||
| } | ||
|
|
||
|
|
@@ -127,13 +144,15 @@ struct TextRegion { | |
| return ret_status; | ||
| } | ||
|
|
||
| // Moving the text region to large pages. We need to be very careful. | ||
| // a) This function itself should not be moved. | ||
| // We use a gcc option to put it outside the ".text" section | ||
| // b) This function should not call any function(s) that might be moved. | ||
| // 1. We map a new area and copy the original code there | ||
| // 2. We mmap using HUGE_TLB | ||
| // 3. If successful we copy the code there and unmap the original region. | ||
| // Moving the text region to large pages. We need to be very careful. | ||
| // a) This function itself should not be moved. | ||
| // We use a gcc option to put it outside the ".text" section | ||
| // b) This function should not call any function(s) that might be moved. | ||
| // 1. map a new area and copy the original code there | ||
| // 2. mmap using the start address with MAP_FIXED so we get exactly | ||
| // 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))) | ||
|
|
@@ -156,6 +175,10 @@ struct TextRegion { | |
|
|
||
| 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, | ||
|
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. what are the chances you get what you want at the same location?
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. If addr is not NULL, then the kernel takes it as a hint about where to place the mapping. Since we are using MAP_FIXED it is not a hint but a requirement. We are relying on the new mapping being at the same virtual address so we dont have to do any fix up of the code. Otherwise we will have to fixup the branches, offsets etc.
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. ok - I was thinking on the lines on |
||
| PROT_READ | PROT_WRITE | PROT_EXEC, | ||
| MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1 , 0); | ||
|
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. what is the rationale behind predefining the memory attributes? ideally we want to inherit the attribute from the old small pages?
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. We do want to be able to write to this so PROT_WRITE is needed. We are using MAP_FIXED to place the mapping at exactly that address.
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. Updated the code comments |
||
|
|
@@ -166,7 +189,7 @@ struct TextRegion { | |
| } | ||
|
|
||
| if (tmem != start) { | ||
| fprintf(stderr, "Unable to allocate hugepages.n"); | ||
| fprintf(stderr, "Unable to allocate hugepages\n"); | ||
| munmap(nmem, size); | ||
| munmap(tmem, size); | ||
| return -1; | ||
|
|
@@ -213,14 +236,14 @@ struct TextRegion { | |
|
|
||
| // This is the primary API called from main | ||
| int map_static_code_to_large_pages() { | ||
| struct TextRegion n = find_node_text_region(); | ||
| if (n.found_text_region == false) { | ||
| fprintf(stderr, "Hugepages WARNING: failed to map static code\n"); | ||
| 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 (n.to <= reinterpret_cast<void *> (&move_text_region_to_large_pages)) | ||
| return move_text_region_to_large_pages(n); | ||
| if (r.to <= reinterpret_cast<void *> (&move_text_region_to_large_pages)) | ||
| return move_text_region_to_large_pages(r); | ||
|
|
||
| return -1; | ||
| } | ||
|
|
@@ -229,5 +252,4 @@ struct TextRegion { | |
| return isTransparentHugePagesEnabled(); | ||
| } | ||
|
|
||
| } // namespace largepages | ||
| } // 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 |
|---|---|---|
|
|
@@ -24,10 +24,8 @@ | |
| #define SRC_NODE_LARGE_PAGE_H_ | ||
|
|
||
| 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. |
||
| namespace largepages { | ||
| bool isLargePagesEnabled(); | ||
| int map_static_code_to_large_pages(); | ||
| } // namespace largepages | ||
| } // 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.
worthwhile to expand the algorithm a bit more - helps later maintainance.
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.
Sure that sounds good. I will add more details.