Removed old FBVector compatibility functions
[folly.git] / folly / MemoryMapping.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "folly/MemoryMapping.h"
18 #include "folly/Format.h"
19
20 #ifdef __linux__
21 #include "folly/experimental/io/HugePages.h"
22 #endif
23
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <sys/types.h>
27 #include <system_error>
28 #include <gflags/gflags.h>
29
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)");
33
34 #ifndef MAP_POPULATE
35 #define MAP_POPULATE 0
36 #endif
37
38 namespace folly {
39
40 MemoryMapping::MemoryMapping(MemoryMapping&& other) {
41   swap(other);
42 }
43
44 MemoryMapping::MemoryMapping(File file, off_t offset, off_t length,
45                              Options options)
46   : file_(std::move(file)),
47     options_(std::move(options)) {
48   CHECK(file_);
49   init(offset, length);
50 }
51
52 MemoryMapping::MemoryMapping(const char* name, off_t offset, off_t length,
53                              Options options)
54   : MemoryMapping(File(name), offset, length, options) { }
55
56 MemoryMapping::MemoryMapping(int fd, off_t offset, off_t length,
57                              Options options)
58   : MemoryMapping(File(fd), offset, length, options) { }
59
60 MemoryMapping::MemoryMapping(AnonymousType, off_t length, Options options)
61   : options_(std::move(options)) {
62   init(0, length);
63 }
64
65 namespace {
66
67 #ifdef __linux__
68 void getDeviceOptions(dev_t device, off_t& pageSize, bool& autoExtend) {
69   auto ps = getHugePageSizeForDevice(device);
70   if (ps) {
71     pageSize = ps->size;
72     autoExtend = true;
73   }
74 }
75 #else
76 inline void getDeviceOptions(dev_t device, off_t& pageSize,
77                              bool& autoExtend) { }
78 #endif
79
80 }  // namespace
81
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));
86
87   off_t& pageSize = options_.pageSize;
88
89   struct stat st;
90
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;
95
96   if (!anon) {
97     // Stat the file
98     CHECK_ERR(fstat(file_.fd(), &st));
99
100     if (pageSize == 0) {
101       getDeviceOptions(st.st_dev, pageSize, autoExtend);
102     }
103   } else {
104     DCHECK(!file_);
105     DCHECK_EQ(offset, 0);
106     CHECK_EQ(pageSize, 0);
107     CHECK_GE(length, 0);
108   }
109
110   if (pageSize == 0) {
111     pageSize = sysconf(_SC_PAGESIZE);
112   }
113
114   CHECK_GT(pageSize, 0);
115   CHECK_EQ(pageSize & (pageSize - 1), 0);  // power of two
116   CHECK_GE(offset, 0);
117
118   // Round down the start of the mapped region
119   size_t skipStart = offset % pageSize;
120   offset -= skipStart;
121
122   mapLength_ = length;
123   if (mapLength_ != -1) {
124     mapLength_ += skipStart;
125
126     // Round up the end of the mapped region
127     mapLength_ = (mapLength_ + pageSize - 1) / pageSize * pageSize;
128   }
129
130   off_t remaining = anon ? length : st.st_size - offset;
131
132   if (mapLength_ == -1) {
133     length = mapLength_ = remaining;
134   } else {
135     if (length > remaining) {
136       if (grow) {
137         if (!autoExtend) {
138           PCHECK(0 == ftruncate(file_.fd(), offset + length))
139             << "ftruncate() failed, couldn't grow file to "
140             << offset + length;
141           remaining = length;
142         } else {
143           // Extend mapping to multiple of page size, don't use ftruncate
144           remaining = mapLength_;
145         }
146       } else {
147         length = remaining;
148       }
149     }
150     if (mapLength_ > remaining) {
151       mapLength_ = remaining;
152     }
153   }
154
155   if (length == 0) {
156     mapLength_ = 0;
157     mapStart_ = nullptr;
158   } else {
159     int flags = options_.shared ? MAP_SHARED : MAP_PRIVATE;
160     if (anon) flags |= MAP_ANONYMOUS;
161     if (options_.prefault) flags |= MAP_POPULATE;
162
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));
168     }
169
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_;
175     mapStart_ = start;
176     data_.reset(start + skipStart, length);
177   }
178 }
179
180 namespace {
181
182 off_t memOpChunkSize(off_t length, off_t pageSize) {
183   off_t chunkSize = length;
184   if (FLAGS_mlock_chunk_size <= 0) {
185     return chunkSize;
186   }
187
188   chunkSize = FLAGS_mlock_chunk_size;
189   off_t r = chunkSize % pageSize;
190   if (r) {
191     chunkSize += (pageSize - r);
192   }
193   return chunkSize;
194 }
195
196 /**
197  * Run @op in chunks over the buffer @mem of @bufSize length.
198  *
199  * Return:
200  * - success: true + amountSucceeded == bufSize (op success on whole buffer)
201  * - failure: false + amountSucceeded == nr bytes on which op succeeded.
202  */
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.
212
213   size_t chunkSize = memOpChunkSize(bufSize, pageSize);
214
215   char* addr = static_cast<char*>(mem);
216   amountSucceeded = 0;
217
218   while (amountSucceeded < bufSize) {
219     size_t size = std::min(chunkSize, bufSize - amountSucceeded);
220     if (op(addr + amountSucceeded, size) != 0) {
221       return false;
222     }
223     amountSucceeded += size;
224   }
225
226   return true;
227 }
228
229 }  // anonymous namespace
230
231 bool MemoryMapping::mlock(LockMode lock) {
232   size_t amountSucceeded = 0;
233   locked_ = memOpInChunks(::mlock, mapStart_, mapLength_, options_.pageSize,
234                           amountSucceeded);
235   if (locked_) {
236     return true;
237   }
238
239   auto msg(folly::format(
240     "mlock({}) failed at {}",
241     mapLength_, amountSucceeded).str());
242
243   if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
244     PLOG(WARNING) << msg;
245   } else {
246     PLOG(FATAL) << msg;
247   }
248
249   // only part of the buffer was mlocked, unlock it back
250   if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, options_.pageSize,
251                      amountSucceeded)) {
252     PLOG(WARNING) << "munlock()";
253   }
254
255   return false;
256 }
257
258 void MemoryMapping::munlock(bool dontneed) {
259   if (!locked_) return;
260
261   size_t amountSucceeded = 0;
262   if (!memOpInChunks(::munlock, mapStart_, mapLength_, options_.pageSize,
263                      amountSucceeded)) {
264     PLOG(WARNING) << "munlock()";
265   }
266   if (mapLength_ && dontneed &&
267       ::madvise(mapStart_, mapLength_, MADV_DONTNEED)) {
268     PLOG(WARNING) << "madvise()";
269   }
270   locked_ = false;
271 }
272
273 void MemoryMapping::hintLinearScan() {
274   advise(MADV_SEQUENTIAL);
275 }
276
277 MemoryMapping::~MemoryMapping() {
278   if (mapLength_) {
279     size_t amountSucceeded = 0;
280     if (!memOpInChunks(::munmap, mapStart_, mapLength_, options_.pageSize,
281                        amountSucceeded)) {
282       PLOG(FATAL) << folly::format(
283         "munmap({}) failed at {}",
284         mapLength_, amountSucceeded).str();
285     }
286   }
287 }
288
289 void MemoryMapping::advise(int advice) const {
290   if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) {
291     PLOG(WARNING) << "madvise()";
292   }
293 }
294
295 MemoryMapping& MemoryMapping::operator=(MemoryMapping other) {
296   swap(other);
297   return *this;
298 }
299
300 void MemoryMapping::swap(MemoryMapping& other) {
301   using std::swap;
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_);
308 }
309
310 void swap(MemoryMapping& a, MemoryMapping& b) { a.swap(b); }
311
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);
315
316   auto srcl = static_cast<const unsigned long*>(src);
317   auto dstl = static_cast<unsigned long*>(dst);
318
319   while (size >= sizeof(unsigned long)) {
320     *dstl++ = *srcl++;
321     size -= sizeof(unsigned long);
322   }
323
324   auto srcc = reinterpret_cast<const unsigned char*>(srcl);
325   auto dstc = reinterpret_cast<unsigned char*>(dstl);
326
327   while (size != 0) {
328     *dstc++ = *srcc++;
329     --size;
330   }
331 }
332
333 void mmapFileCopy(const char* src, const char* dest, mode_t mode) {
334   MemoryMapping srcMap(src);
335   srcMap.hintLinearScan();
336
337   MemoryMapping destMap(
338       File(dest, O_RDWR | O_CREAT | O_TRUNC, mode),
339       0,
340       srcMap.range().size(),
341       MemoryMapping::writable());
342
343   alignedForwardMemcpy(destMap.writableRange().data(),
344                        srcMap.range().data(),
345                        srcMap.range().size());
346 }
347
348 }  // namespace folly