-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-14548: [C++] Add madvise random support for memory mapped file #11588
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1047,21 +1047,45 @@ Status MemoryMapRemap(void* addr, size_t old_size, size_t new_size, int fildes, | |
| #endif | ||
| } | ||
|
|
||
| Status MemoryAdviseWillNeed(const std::vector<MemoryRegion>& regions) { | ||
| MemoryRegion align_region(const MemoryRegion& region, size_t page_mask, | ||
| size_t page_size) { | ||
| const auto addr = reinterpret_cast<uintptr_t>(region.addr); | ||
| const auto aligned_addr = addr & page_mask; | ||
| DCHECK_LT(addr - aligned_addr, page_size); | ||
| return {reinterpret_cast<void*>(aligned_addr), | ||
| region.size + static_cast<size_t>(addr - aligned_addr)}; | ||
| } | ||
|
|
||
| #ifdef POSIX_MADV_WILLNEED | ||
| Status MemoryAdvise(const std::vector<MemoryRegion>& regions, int advice) { | ||
| const auto page_size = static_cast<size_t>(GetPageSize()); | ||
| DCHECK_GT(page_size, 0); | ||
| const size_t page_mask = ~(page_size - 1); | ||
| DCHECK_EQ(page_mask & page_size, page_size); | ||
|
|
||
| auto align_region = [=](const MemoryRegion& region) -> MemoryRegion { | ||
| const auto addr = reinterpret_cast<uintptr_t>(region.addr); | ||
| const auto aligned_addr = addr & page_mask; | ||
| DCHECK_LT(addr - aligned_addr, page_size); | ||
| return {reinterpret_cast<void*>(aligned_addr), | ||
| region.size + static_cast<size_t>(addr - aligned_addr)}; | ||
| }; | ||
| for (const auto& region : regions) { | ||
| if (region.size != 0) { | ||
| const auto aligned = align_region(region, page_mask, page_size); | ||
| int err = posix_madvise(aligned.addr, aligned.size, advice); | ||
| // EBADF can be returned on Linux in the following cases: | ||
| // - the kernel version is older than 3.9 | ||
| // - the kernel was compiled with CONFIG_SWAP disabled (ARROW-9577) | ||
| if (err != 0 && err != EBADF) { | ||
| return IOErrorFromErrno(err, "posix_madvise failed"); | ||
| } | ||
| } | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| #endif | ||
|
|
||
| Status MemoryAdviseWillNeed(const std::vector<MemoryRegion>& regions) { | ||
| #ifdef _WIN32 | ||
| const auto page_size = static_cast<size_t>(GetPageSize()); | ||
| DCHECK_GT(page_size, 0); | ||
| const size_t page_mask = ~(page_size - 1); | ||
| DCHECK_EQ(page_mask & page_size, page_size); | ||
|
|
||
| // PrefetchVirtualMemory() is available on Windows 8 or later | ||
| struct PrefetchEntry { // Like WIN32_MEMORY_RANGE_ENTRY | ||
| void* VirtualAddress; | ||
|
|
@@ -1078,7 +1102,7 @@ Status MemoryAdviseWillNeed(const std::vector<MemoryRegion>& regions) { | |
| entries.reserve(regions.size()); | ||
| for (const auto& region : regions) { | ||
| if (region.size != 0) { | ||
| entries.emplace_back(align_region(region)); | ||
| entries.emplace_back(align_region(region, page_mask, page_size)); | ||
| } | ||
| } | ||
| if (!entries.empty() && | ||
|
|
@@ -1090,19 +1114,15 @@ Status MemoryAdviseWillNeed(const std::vector<MemoryRegion>& regions) { | |
| } | ||
| return Status::OK(); | ||
| #elif defined(POSIX_MADV_WILLNEED) | ||
| for (const auto& region : regions) { | ||
| if (region.size != 0) { | ||
| const auto aligned = align_region(region); | ||
| int err = posix_madvise(aligned.addr, aligned.size, POSIX_MADV_WILLNEED); | ||
| // EBADF can be returned on Linux in the following cases: | ||
| // - the kernel version is older than 3.9 | ||
| // - the kernel was compiled with CONFIG_SWAP disabled (ARROW-9577) | ||
| if (err != 0 && err != EBADF) { | ||
| return IOErrorFromErrno(err, "posix_madvise failed"); | ||
| } | ||
| } | ||
| } | ||
|
||
| return MemoryAdvise(regions, POSIX_MADV_WILLNEED); | ||
| #else | ||
| return Status::OK(); | ||
| #endif | ||
| } | ||
|
|
||
| Status MemoryAdviseRandom(const std::vector<MemoryRegion>& regions) { | ||
| #ifdef POSIX_MADV_RANDOM | ||
| return MemoryAdvise(regions, POSIX_MADV_RANDOM); | ||
| #else | ||
| return Status::OK(); | ||
| #endif | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Previously we already have
WillNeedAPI inMemoryMappedFileto advise OS about the needed ranges, I add anAdviseRandomAPI similarly to indicate the random access pattern. Initially, I would like to make this API consistent withWillNeedand simply call itRandombut I think this may be slightly confusing as well, so I name itAdviseRandomcurrently. Let me know if you have other naming suggestion for this API.