Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
| } | ||
| } | ||
| } | ||
ContributorAuthor 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 extract this piece of code from | ||
| 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 | ||
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.