move assignment operators for folly::Synchronized
[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_chunk_size, 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_chunk_size <= 0) {
104     return chunkSize;
105   }
106
107   chunkSize = FLAGS_mlock_chunk_size;
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
135   char* addr = static_cast<char*>(mem);
136   amountSucceeded = 0;
137
138   while (amountSucceeded < bufSize) {
139     size_t size = std::min(chunkSize, bufSize - amountSucceeded);
140     if (op(addr + amountSucceeded, size) != 0) {
141       return false;
142     }
143     amountSucceeded += size;
144   }
145
146   return true;
147 }
148
149 }  // anonymous namespace
150
151 bool MemoryMapping::mlock(LockMode lock) {
152   size_t amountSucceeded = 0;
153   locked_ = memOpInChunks(::mlock, mapStart_, mapLength_, amountSucceeded);
154   if (locked_) {
155     return true;
156   }
157
158   auto msg(folly::format(
159     "mlock({}) failed at {}",
160     mapLength_, amountSucceeded).str());
161
162   if (lock == LockMode::TRY_LOCK && (errno == EPERM || errno == ENOMEM)) {
163     PLOG(WARNING) << msg;
164   } else {
165     PLOG(FATAL) << msg;
166   }
167
168   // only part of the buffer was mlocked, unlock it back
169   if (!memOpInChunks(::munlock, mapStart_, amountSucceeded, amountSucceeded)) {
170     PLOG(WARNING) << "munlock()";
171   }
172
173   return false;
174 }
175
176 void MemoryMapping::munlock(bool dontneed) {
177   if (!locked_) return;
178
179   size_t amountSucceeded = 0;
180   if (!memOpInChunks(::munlock, mapStart_, mapLength_, amountSucceeded)) {
181     PLOG(WARNING) << "munlock()";
182   }
183   if (mapLength_ && dontneed &&
184       ::madvise(mapStart_, mapLength_, MADV_DONTNEED)) {
185     PLOG(WARNING) << "madvise()";
186   }
187   locked_ = false;
188 }
189
190 void MemoryMapping::hintLinearScan() {
191   advise(MADV_SEQUENTIAL);
192 }
193
194 MemoryMapping::~MemoryMapping() {
195   if (mapLength_) {
196     size_t amountSucceeded = 0;
197     if (!memOpInChunks(::munmap, mapStart_, mapLength_, amountSucceeded)) {
198       PLOG(FATAL) << folly::format(
199         "munmap({}) failed at {}",
200         mapLength_, amountSucceeded).str();
201     }
202   }
203 }
204
205 void MemoryMapping::advise(int advice) const {
206   if (mapLength_ && ::madvise(mapStart_, mapLength_, advice)) {
207     PLOG(WARNING) << "madvise()";
208   }
209 }
210
211 WritableMemoryMapping::WritableMemoryMapping(File file, off_t offset, off_t length) {
212   init(std::move(file), offset, length, PROT_READ | PROT_WRITE, true);
213 }
214
215 }  // namespace folly