rocksdb/port/mmap.cc
Peter Dillinger 8367f0d2d7 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: ca83fcc47e50fabf7595069380edd2954f4f879c
2022-10-17 17:10:16 -07:00

99 lines
2.4 KiB
C++

// 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