MemoryMapping
[folly.git] / folly / MemoryMapping.cpp
1 /*
2  * Copyright 2013 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 #include <fcntl.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <system_error>
24
25 DEFINE_int64(mlock_max_size_at_once, 1 << 20,  // 1MB
26              "Maximum bytes to mlock/munlock/munmap at once "
27              "(will be rounded up to PAGESIZE)");
28
29 namespace folly {
30
31 /* protected constructor */
32 MemoryMapping::MemoryMapping()
33   : mapStart_(nullptr)
34   , mapLength_(0)
35   , locked_(false) {
36 }
37
38 MemoryMapping::MemoryMapping(File file, off_t offset, off_t length)
39   : mapStart_(nullptr)
40   , mapLength_(0)
41   , locked_(false) {
42
43   init(std::move(file), offset, length, PROT_READ, false);
44 }
45
46 void MemoryMapping::init(File file,
47                          off_t offset, off_t length,
48                          int prot,
49                          bool grow) {
50   off_t pageSize = sysconf(_SC_PAGESIZE);
51   CHECK_GE(offset, 0);
52
53   // Round down the start of the mapped region
54   size_t skipStart = offset % pageSize;
55   offset -= skipStart;
56
57   file_ = std::move(file);
58   mapLength_ = length;
59   if (mapLength_ != -1) {
60     mapLength_ += skipStart;
61
62     // Round up the end of the mapped region
63     mapLength_ = (mapLength_ + pageSize - 1) / pageSize * pageSize;
64   }
65
66   // stat the file
67   struct stat st;
68   CHECK_ERR(fstat(file_.fd(), &st));
69   off_t remaining = st.st_size - offset;
70   if (mapLength_ == -1) {
71     length = mapLength_ = remaining;
72   } else {
73     if (length > remaining) {
74       if (grow) {
75         PCHECK(0 == ftruncate(file_.fd(), offset + length))
76           << "ftructate() failed, couldn't grow file";
77         remaining = length;
78       } else {
79         length = remaining;
80       }
81     }
82     if (mapLength_ > remaining) mapLength_ = remaining;
83   }
84
85   if (length == 0) {
86     mapLength_ = 0;
87     mapStart_ = nullptr;
88   } else {
89     unsigned char* start = static_cast<unsigned char*>(
90       mmap(nullptr, mapLength_, prot, MAP_SHARED, file_.fd(), offset));
91     PCHECK(start != MAP_FAILED)
92       << " offset=" << offset
93       << " length=" << mapLength_;
94     mapStart_ = start;
95     data_.reset(start + skipStart, length);
96   }
97 }
98
99 namespace {
100
101 off_t memOpChunkSize(off_t length) {
102   off_t chunkSize = length;
103   if (FLAGS_mlock_max_size_at_once <= 0) {
104     return chunkSize;
105   }
106
107   chunkSize = FLAGS_mlock_max_size_at_once;
108   off_t pageSize = sysconf(_SC_PAGESIZE);
109   off_t r = chunkSize % pageSize;
110   if (r) {
111     chunkSize += (pageSize - r);
112   }
113   return chunkSize;
114 }
115
116 /**
117  * Run @op in chunks over the buffer @mem of @bufSize length.
118  *
119  * Return:
120  * - success: true + amountSucceeded == bufSize (op success on whole buffer)
121  * - failure: false + amountSucceeded == nr bytes on which op succeeded.
122  */
123 bool memOpInChunks(std::function<int(void*, size_t)> op,
124                    void* mem, size_t bufSize,
125                    size_t& amountSucceeded) {
126   // unmap/mlock/munlock take a kernel semaphore and block other threads from
127   // doing other memory operations. If the size of the buffer is big the
128   // semaphore can be down for seconds (for benchmarks see
129   // http://kostja-osipov.livejournal.com/42963.html).  Doing the operations in
130   // chunks breaks the locking into intervals and lets other threads do memory
131   // operations of their own.
132
133   size_t chunkSize = memOpChunkSize(bufSize);
134   size_t chunkCount = bufSize / chunkSize;
135
136   char* addr = static_cast<char*>(mem);
137   amountSucceeded = 0;
138
139   while (amountSucceeded < bufSize) {
140     size_t size = std::min(chunkSize, bufSize - amountSucceeded);
141     if (op(addr + amountSucceeded, size) != 0) {
142       return false;
143     }
144     amountSucceeded += size;
145   }
146
147   return true;
148 }
149
150 }  // anonymous namespace
151
152 bool MemoryMapping::mlock(LockMode lock) {
153   size_t amountSucceeded = 0;
154   locked_ = memOpInChunks(::mlock, mapStart_, mapLength_, amountSucceeded);
155   if (locked_) {
156     return true;
157   }
158
159   auto msg(folly::format(
160     "mlock({}) failed at {}",
161     mapLength_, amountSucceeded).str());
162
163   if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
164     PLOG(WARNING) << msg;
165   } else {
166     PLOG(FATAL) << msg;
167   }
168
169   // only part of the buffer was mlocked, unlock it back
170   if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, amountSucceeded)) {
171     PLOG(WARNING) << "munlock()";
172   }
173
174   return false;
175 }
176
177 void MemoryMapping::munlock(bool dontneed) {
178   if (!locked_) return;
179
180   size_t amountSucceeded = 0;
181   if (!memOpInChunks(::munlock, mapStart_, mapLength_, amountSucceeded)) {
182     PLOG(WARNING) << "munlock()";
183   }
184   if (mapLength_ && dontneed &&
185       ::madvise(mapStart_, mapLength_, MADV_DONTNEED)) {
186     PLOG(WARNING) << "madvise()";
187   }
188   locked_ = false;
189 }
190
191 void MemoryMapping::hintLinearScan() {
192   advise(MADV_SEQUENTIAL);
193 }
194
195 MemoryMapping::~MemoryMapping() {
196   if (mapLength_) {
197     size_t amountSucceeded = 0;
198     if (!memOpInChunks(::munmap, mapStart_, mapLength_, amountSucceeded)) {
199       PLOG(FATAL) << folly::format(
200         "munmap({}) failed at {}",
201         mapLength_, amountSucceeded).str();
202     }
203   }
204 }
205
206 void MemoryMapping::advise(int advice) const {
207   if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) {
208     PLOG(WARNING) << "madvise()";
209   }
210 }
211
212 WritableMemoryMapping::WritableMemoryMapping(File file, off_t offset, off_t length) {
213   init(std::move(file), offset, length, PROT_READ | PROT_WRITE, true);
214 }
215
216 }  // namespace folly