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"
22 #include <sys/types.h>
23 #include <system_error>
24 #include <gflags/gflags.h>
26 DEFINE_int64(mlock_chunk_size, 1 << 20, // 1MB
27 "Maximum bytes to mlock/munlock/munmap at once "
28 "(will be rounded up to PAGESIZE)");
32 /* protected constructor */
33 MemoryMapping::MemoryMapping()
39 MemoryMapping::MemoryMapping(File file, off_t offset, off_t length)
44 init(std::move(file), offset, length, PROT_READ, false);
47 MemoryMapping::MemoryMapping(const char* name, off_t offset, off_t length)
48 : MemoryMapping(File(name), offset, length) { }
50 MemoryMapping::MemoryMapping(int fd, off_t offset, off_t length)
51 : MemoryMapping(File(fd), offset, length) { }
53 void MemoryMapping::init(File file,
54 off_t offset, off_t length,
57 off_t pageSize = sysconf(_SC_PAGESIZE);
60 // Round down the start of the mapped region
61 size_t skipStart = offset % pageSize;
64 file_ = std::move(file);
66 if (mapLength_ != -1) {
67 mapLength_ += skipStart;
69 // Round up the end of the mapped region
70 mapLength_ = (mapLength_ + pageSize - 1) / pageSize * pageSize;
75 CHECK_ERR(fstat(file_.fd(), &st));
76 off_t remaining = st.st_size - offset;
77 if (mapLength_ == -1) {
78 length = mapLength_ = remaining;
80 if (length > remaining) {
82 PCHECK(0 == ftruncate(file_.fd(), offset + length))
83 << "ftructate() failed, couldn't grow file";
89 if (mapLength_ > remaining) mapLength_ = remaining;
96 unsigned char* start = static_cast<unsigned char*>(
97 mmap(nullptr, mapLength_, prot, MAP_SHARED, file_.fd(), offset));
98 PCHECK(start != MAP_FAILED)
99 << " offset=" << offset
100 << " length=" << mapLength_;
102 data_.reset(start + skipStart, length);
108 off_t memOpChunkSize(off_t length) {
109 off_t chunkSize = length;
110 if (FLAGS_mlock_chunk_size <= 0) {
114 chunkSize = FLAGS_mlock_chunk_size;
115 off_t pageSize = sysconf(_SC_PAGESIZE);
116 off_t r = chunkSize % pageSize;
118 chunkSize += (pageSize - r);
124 * Run @op in chunks over the buffer @mem of @bufSize length.
127 * - success: true + amountSucceeded == bufSize (op success on whole buffer)
128 * - failure: false + amountSucceeded == nr bytes on which op succeeded.
130 bool memOpInChunks(std::function<int(void*, size_t)> op,
131 void* mem, size_t bufSize,
132 size_t& amountSucceeded) {
133 // unmap/mlock/munlock take a kernel semaphore and block other threads from
134 // doing other memory operations. If the size of the buffer is big the
135 // semaphore can be down for seconds (for benchmarks see
136 // http://kostja-osipov.livejournal.com/42963.html). Doing the operations in
137 // chunks breaks the locking into intervals and lets other threads do memory
138 // operations of their own.
140 size_t chunkSize = memOpChunkSize(bufSize);
142 char* addr = static_cast<char*>(mem);
145 while (amountSucceeded < bufSize) {
146 size_t size = std::min(chunkSize, bufSize - amountSucceeded);
147 if (op(addr + amountSucceeded, size) != 0) {
150 amountSucceeded += size;
156 } // anonymous namespace
158 bool MemoryMapping::mlock(LockMode lock) {
159 size_t amountSucceeded = 0;
160 locked_ = memOpInChunks(::mlock, mapStart_, mapLength_, amountSucceeded);
165 auto msg(folly::format(
166 "mlock({}) failed at {}",
167 mapLength_, amountSucceeded).str());
169 if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
170 PLOG(WARNING) << msg;
175 // only part of the buffer was mlocked, unlock it back
176 if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, amountSucceeded)) {
177 PLOG(WARNING) << "munlock()";
183 void MemoryMapping::munlock(bool dontneed) {
184 if (!locked_) return;
186 size_t amountSucceeded = 0;
187 if (!memOpInChunks(::munlock, mapStart_, mapLength_, amountSucceeded)) {
188 PLOG(WARNING) << "munlock()";
190 if (mapLength_ && dontneed &&
191 ::madvise(mapStart_, mapLength_, MADV_DONTNEED)) {
192 PLOG(WARNING) << "madvise()";
197 void MemoryMapping::hintLinearScan() {
198 advise(MADV_SEQUENTIAL);
201 MemoryMapping::~MemoryMapping() {
203 size_t amountSucceeded = 0;
204 if (!memOpInChunks(::munmap, mapStart_, mapLength_, amountSucceeded)) {
205 PLOG(FATAL) << folly::format(
206 "munmap({}) failed at {}",
207 mapLength_, amountSucceeded).str();
212 void MemoryMapping::advise(int advice) const {
213 if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) {
214 PLOG(WARNING) << "madvise()";
218 WritableMemoryMapping::WritableMemoryMapping(File file, off_t offset, off_t length) {
219 init(std::move(file), offset, length, PROT_READ | PROT_WRITE, true);