2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/MemoryMapping.h"
18 #include "folly/Format.h"
21 #include "folly/experimental/io/HugePages.h"
26 #include <sys/types.h>
27 #include <system_error>
28 #include <gflags/gflags.h>
30 DEFINE_int64(mlock_chunk_size, 1 << 20, // 1MB
31 "Maximum bytes to mlock/munlock/munmap at once "
32 "(will be rounded up to PAGESIZE)");
35 #define MAP_POPULATE 0
40 MemoryMapping::MemoryMapping(MemoryMapping&& other) {
44 MemoryMapping::MemoryMapping(File file, off_t offset, off_t length,
46 : file_(std::move(file)),
47 options_(std::move(options)) {
52 MemoryMapping::MemoryMapping(const char* name, off_t offset, off_t length,
54 : MemoryMapping(File(name), offset, length, options) { }
56 MemoryMapping::MemoryMapping(int fd, off_t offset, off_t length,
58 : MemoryMapping(File(fd), offset, length, options) { }
60 MemoryMapping::MemoryMapping(AnonymousType, off_t length, Options options)
61 : options_(std::move(options)) {
68 void getDeviceOptions(dev_t device, off_t& pageSize, bool& autoExtend) {
69 auto ps = getHugePageSizeForDevice(device);
76 inline void getDeviceOptions(dev_t device, off_t& pageSize,
82 void MemoryMapping::init(off_t offset, off_t length) {
83 const bool grow = options_.grow;
84 const bool anon = !file_;
85 CHECK(!(grow && anon));
87 off_t& pageSize = options_.pageSize;
91 // On Linux, hugetlbfs file systems don't require ftruncate() to grow the
92 // file, and (on kernels before 2.6.24) don't even allow it. Also, the file
93 // size is always a multiple of the page size.
94 bool autoExtend = false;
98 CHECK_ERR(fstat(file_.fd(), &st));
101 getDeviceOptions(st.st_dev, pageSize, autoExtend);
105 DCHECK_EQ(offset, 0);
106 CHECK_EQ(pageSize, 0);
111 pageSize = sysconf(_SC_PAGESIZE);
114 CHECK_GT(pageSize, 0);
115 CHECK_EQ(pageSize & (pageSize - 1), 0); // power of two
118 // Round down the start of the mapped region
119 size_t skipStart = offset % pageSize;
123 if (mapLength_ != -1) {
124 mapLength_ += skipStart;
126 // Round up the end of the mapped region
127 mapLength_ = (mapLength_ + pageSize - 1) / pageSize * pageSize;
130 off_t remaining = anon ? length : st.st_size - offset;
132 if (mapLength_ == -1) {
133 length = mapLength_ = remaining;
135 if (length > remaining) {
138 PCHECK(0 == ftruncate(file_.fd(), offset + length))
139 << "ftruncate() failed, couldn't grow file to "
143 // Extend mapping to multiple of page size, don't use ftruncate
144 remaining = mapLength_;
150 if (mapLength_ > remaining) {
151 mapLength_ = remaining;
159 int flags = options_.shared ? MAP_SHARED : MAP_PRIVATE;
160 if (anon) flags |= MAP_ANONYMOUS;
161 if (options_.prefault) flags |= MAP_POPULATE;
163 // The standard doesn't actually require PROT_NONE to be zero...
164 int prot = PROT_NONE;
165 if (options_.readable || options_.writable) {
166 prot = ((options_.readable ? PROT_READ : 0) |
167 (options_.writable ? PROT_WRITE : 0));
170 unsigned char* start = static_cast<unsigned char*>(
171 mmap(options_.address, mapLength_, prot, flags, file_.fd(), offset));
172 PCHECK(start != MAP_FAILED)
173 << " offset=" << offset
174 << " length=" << mapLength_;
176 data_.reset(start + skipStart, length);
182 off_t memOpChunkSize(off_t length, off_t pageSize) {
183 off_t chunkSize = length;
184 if (FLAGS_mlock_chunk_size <= 0) {
188 chunkSize = FLAGS_mlock_chunk_size;
189 off_t r = chunkSize % pageSize;
191 chunkSize += (pageSize - r);
197 * Run @op in chunks over the buffer @mem of @bufSize length.
200 * - success: true + amountSucceeded == bufSize (op success on whole buffer)
201 * - failure: false + amountSucceeded == nr bytes on which op succeeded.
203 bool memOpInChunks(std::function<int(void*, size_t)> op,
204 void* mem, size_t bufSize, off_t pageSize,
205 size_t& amountSucceeded) {
206 // unmap/mlock/munlock take a kernel semaphore and block other threads from
207 // doing other memory operations. If the size of the buffer is big the
208 // semaphore can be down for seconds (for benchmarks see
209 // http://kostja-osipov.livejournal.com/42963.html). Doing the operations in
210 // chunks breaks the locking into intervals and lets other threads do memory
211 // operations of their own.
213 size_t chunkSize = memOpChunkSize(bufSize, pageSize);
215 char* addr = static_cast<char*>(mem);
218 while (amountSucceeded < bufSize) {
219 size_t size = std::min(chunkSize, bufSize - amountSucceeded);
220 if (op(addr + amountSucceeded, size) != 0) {
223 amountSucceeded += size;
229 } // anonymous namespace
231 bool MemoryMapping::mlock(LockMode lock) {
232 size_t amountSucceeded = 0;
233 locked_ = memOpInChunks(::mlock, mapStart_, mapLength_, options_.pageSize,
239 auto msg(folly::format(
240 "mlock({}) failed at {}",
241 mapLength_, amountSucceeded).str());
243 if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
244 PLOG(WARNING) << msg;
249 // only part of the buffer was mlocked, unlock it back
250 if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, options_.pageSize,
252 PLOG(WARNING) << "munlock()";
258 void MemoryMapping::munlock(bool dontneed) {
259 if (!locked_) return;
261 size_t amountSucceeded = 0;
262 if (!memOpInChunks(::munlock, mapStart_, mapLength_, options_.pageSize,
264 PLOG(WARNING) << "munlock()";
266 if (mapLength_ && dontneed &&
267 ::madvise(mapStart_, mapLength_, MADV_DONTNEED)) {
268 PLOG(WARNING) << "madvise()";
273 void MemoryMapping::hintLinearScan() {
274 advise(MADV_SEQUENTIAL);
277 MemoryMapping::~MemoryMapping() {
279 size_t amountSucceeded = 0;
280 if (!memOpInChunks(::munmap, mapStart_, mapLength_, options_.pageSize,
282 PLOG(FATAL) << folly::format(
283 "munmap({}) failed at {}",
284 mapLength_, amountSucceeded).str();
289 void MemoryMapping::advise(int advice) const {
290 if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) {
291 PLOG(WARNING) << "madvise()";
295 MemoryMapping& MemoryMapping::operator=(MemoryMapping other) {
300 void MemoryMapping::swap(MemoryMapping& other) {
302 swap(this->file_, other.file_);
303 swap(this->mapStart_, other.mapStart_);
304 swap(this->mapLength_, other.mapLength_);
305 swap(this->options_, other.options_);
306 swap(this->locked_, other.locked_);
307 swap(this->data_, other.data_);
310 void swap(MemoryMapping& a, MemoryMapping& b) { a.swap(b); }
312 void alignedForwardMemcpy(void* dst, const void* src, size_t size) {
313 assert(reinterpret_cast<uintptr_t>(src) % alignof(unsigned long) == 0);
314 assert(reinterpret_cast<uintptr_t>(dst) % alignof(unsigned long) == 0);
316 auto srcl = static_cast<const unsigned long*>(src);
317 auto dstl = static_cast<unsigned long*>(dst);
319 while (size >= sizeof(unsigned long)) {
321 size -= sizeof(unsigned long);
324 auto srcc = reinterpret_cast<const unsigned char*>(srcl);
325 auto dstc = reinterpret_cast<unsigned char*>(dstl);
333 void mmapFileCopy(const char* src, const char* dest, mode_t mode) {
334 MemoryMapping srcMap(src);
335 srcMap.hintLinearScan();
337 MemoryMapping destMap(
338 File(dest, O_RDWR | O_CREAT | O_TRUNC, mode),
340 srcMap.range().size(),
341 MemoryMapping::writable());
343 alignedForwardMemcpy(destMap.writableRange().data(),
344 srcMap.range().data(),
345 srcMap.range().size());