Improve / refactor anonymous mmap capabilities (#10810)
Summary: The motivation for this change is a planned feature (related to HyperClockCache) that will depend on a large array that can essentially grow automatically, up to some bound, without the pointer address changing and with guaranteed zero-initialization of the data. Anonymous mmaps provide such functionality, and this change provides an internal API for that. The other existing use of anonymous mmap in RocksDB is for allocating in huge pages. That code and other related Arena code used some awkward non-RAII and pre-C++11 idioms, so I cleaned up much of that as well, with RAII, move semantics, constexpr, etc. More specifcs: * Minimize conditional compilation * Add Windows support for anonymous mmaps * Use std::deque instead of std::vector for more efficient bag Pull Request resolved: https://github.com/facebook/rocksdb/pull/10810 Test Plan: unit test added for new functionality Reviewed By: riversand963 Differential Revision: D40347204 Pulled By: pdillinger fbshipit-source-id: ca83fcc47e50fabf7595069380edd2954f4f879cmain
parent
11c0d1310e
commit
8367f0d2d7
@ -0,0 +1,98 @@ |
|||||||
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
// This source code is licensed under both the GPLv2 (found in the
|
||||||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||||||
|
// (found in the LICENSE.Apache file in the root directory).
|
||||||
|
|
||||||
|
#include "port/mmap.h" |
||||||
|
|
||||||
|
#include <cassert> |
||||||
|
#include <cstdio> |
||||||
|
#include <cstring> |
||||||
|
#include <new> |
||||||
|
#include <utility> |
||||||
|
|
||||||
|
#include "util/hash.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
MemMapping::~MemMapping() { |
||||||
|
#ifdef OS_WIN |
||||||
|
if (addr_ != nullptr) { |
||||||
|
(void)::UnmapViewOfFile(addr_); |
||||||
|
} |
||||||
|
if (page_file_handle_ != NULL) { |
||||||
|
(void)::CloseHandle(page_file_handle_); |
||||||
|
} |
||||||
|
#else // OS_WIN -> !OS_WIN
|
||||||
|
if (addr_ != nullptr) { |
||||||
|
auto status = munmap(addr_, length_); |
||||||
|
assert(status == 0); |
||||||
|
if (status != 0) { |
||||||
|
// TODO: handle error?
|
||||||
|
} |
||||||
|
} |
||||||
|
#endif // OS_WIN
|
||||||
|
} |
||||||
|
|
||||||
|
MemMapping::MemMapping(MemMapping&& other) noexcept { |
||||||
|
*this = std::move(other); |
||||||
|
} |
||||||
|
|
||||||
|
MemMapping& MemMapping::operator=(MemMapping&& other) noexcept { |
||||||
|
if (&other == this) { |
||||||
|
return *this; |
||||||
|
} |
||||||
|
this->~MemMapping(); |
||||||
|
std::memcpy(this, &other, sizeof(*this)); |
||||||
|
new (&other) MemMapping(); |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
MemMapping MemMapping::AllocateAnonymous(size_t length, bool huge) { |
||||||
|
MemMapping mm; |
||||||
|
mm.length_ = length; |
||||||
|
assert(mm.addr_ == nullptr); |
||||||
|
if (length == 0) { |
||||||
|
// OK to leave addr as nullptr
|
||||||
|
return mm; |
||||||
|
} |
||||||
|
int huge_flag = 0; |
||||||
|
#ifdef OS_WIN |
||||||
|
if (huge) { |
||||||
|
#ifdef FILE_MAP_LARGE_PAGES |
||||||
|
huge_flag = FILE_MAP_LARGE_PAGES; |
||||||
|
#endif // FILE_MAP_LARGE_PAGES
|
||||||
|
} |
||||||
|
mm.page_file_handle_ = ::CreateFileMapping( |
||||||
|
INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | SEC_COMMIT, |
||||||
|
Upper32of64(length), Lower32of64(length), nullptr); |
||||||
|
if (mm.page_file_handle_ == NULL) { |
||||||
|
// Failure
|
||||||
|
return mm; |
||||||
|
} |
||||||
|
mm.addr_ = ::MapViewOfFile(mm.page_file_handle_, FILE_MAP_WRITE | huge_flag, |
||||||
|
0, 0, length); |
||||||
|
#else // OS_WIN -> !OS_WIN
|
||||||
|
if (huge) { |
||||||
|
#ifdef MAP_HUGETLB |
||||||
|
huge_flag = MAP_HUGETLB; |
||||||
|
#endif // MAP_HUGE_TLB
|
||||||
|
} |
||||||
|
mm.addr_ = mmap(nullptr, length, PROT_READ | PROT_WRITE, |
||||||
|
MAP_PRIVATE | MAP_ANONYMOUS | huge_flag, -1, 0); |
||||||
|
if (mm.addr_ == MAP_FAILED) { |
||||||
|
mm.addr_ = nullptr; |
||||||
|
} |
||||||
|
#endif // OS_WIN
|
||||||
|
return mm; |
||||||
|
} |
||||||
|
|
||||||
|
MemMapping MemMapping::AllocateHuge(size_t length) { |
||||||
|
return AllocateAnonymous(length, /*huge*/ true); |
||||||
|
} |
||||||
|
|
||||||
|
MemMapping MemMapping::AllocateLazyZeroed(size_t length) { |
||||||
|
return AllocateAnonymous(length, /*huge*/ false); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
@ -0,0 +1,70 @@ |
|||||||
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
// This source code is licensed under both the GPLv2 (found in the
|
||||||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||||||
|
// (found in the LICENSE.Apache file in the root directory).
|
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#ifdef OS_WIN |
||||||
|
#include <windows.h> |
||||||
|
// ^^^ Must come first
|
||||||
|
#include <memoryapi.h> |
||||||
|
#else |
||||||
|
#include <sys/mman.h> |
||||||
|
#endif // OS_WIN
|
||||||
|
|
||||||
|
#include <cstdint> |
||||||
|
|
||||||
|
#include "rocksdb/rocksdb_namespace.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
// An RAII wrapper for mmaped memory
|
||||||
|
class MemMapping { |
||||||
|
public: |
||||||
|
static constexpr bool kHugePageSupported = |
||||||
|
#if defined(MAP_HUGETLB) || defined(FILE_MAP_LARGE_PAGES) |
||||||
|
true; |
||||||
|
#else |
||||||
|
false; |
||||||
|
#endif |
||||||
|
|
||||||
|
// Allocate memory requesting to be backed by huge pages
|
||||||
|
static MemMapping AllocateHuge(size_t length); |
||||||
|
|
||||||
|
// Allocate memory that is only lazily mapped to resident memory and
|
||||||
|
// guaranteed to be zero-initialized. Note that some platforms like
|
||||||
|
// Linux allow memory over-commit, where only the used portion of memory
|
||||||
|
// matters, while other platforms require enough swap space (page file) to
|
||||||
|
// back the full mapping.
|
||||||
|
static MemMapping AllocateLazyZeroed(size_t length); |
||||||
|
|
||||||
|
// No copies
|
||||||
|
MemMapping(const MemMapping&) = delete; |
||||||
|
MemMapping& operator=(const MemMapping&) = delete; |
||||||
|
// Move
|
||||||
|
MemMapping(MemMapping&&) noexcept; |
||||||
|
MemMapping& operator=(MemMapping&&) noexcept; |
||||||
|
|
||||||
|
// Releases the mapping
|
||||||
|
~MemMapping(); |
||||||
|
|
||||||
|
inline void* Get() const { return addr_; } |
||||||
|
inline size_t Length() const { return length_; } |
||||||
|
|
||||||
|
private: |
||||||
|
MemMapping() {} |
||||||
|
|
||||||
|
// The mapped memory, or nullptr on failure / not supported
|
||||||
|
void* addr_ = nullptr; |
||||||
|
// The known usable number of bytes starting at that address
|
||||||
|
size_t length_ = 0; |
||||||
|
|
||||||
|
#ifdef OS_WIN |
||||||
|
HANDLE page_file_handle_ = NULL; |
||||||
|
#endif // OS_WIN
|
||||||
|
|
||||||
|
static MemMapping AllocateAnonymous(size_t length, bool huge); |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
Loading…
Reference in new issue