LLVM 23.0.0git
MappedFileRegionArena.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file Implements MappedFileRegionArena.
9///
10/// A bump pointer allocator, backed by a memory-mapped file.
11///
12/// The effect we want is:
13///
14/// Step 1. If it doesn't exist, create the file with an initial size.
15/// Step 2. Reserve virtual memory large enough for the max file size.
16/// Step 3. Map the file into memory in the reserved region.
17/// Step 4. Increase the file size and update the mapping when necessary.
18///
19/// However, updating the mapping is challenging when it needs to work portably,
20/// and across multiple processes without locking for every read. Our current
21/// implementation handles the steps above in following ways:
22///
23/// Step 1. Use \ref sys::fs::resize_file_sparse to grow the file to its max
24/// size (typically several GB). If the file system doesn't support
25/// sparse file, this may return a fully allocated file.
26/// Step 2. Call \ref sys::fs::mapped_file_region to map the entire file.
27/// Step 3. [Automatic as part of step 2.]
28/// Step 4. If supported, use \c fallocate or similiar APIs to ensure the file
29/// system storage for the sparse file so we won't end up with partial
30/// file if the disk is out of space.
31///
32/// Additionally, we attempt to resize the file to its actual data size when
33/// closing the mapping, if this is the only concurrent instance. This is done
34/// using file locks. Shrinking the file mitigates problems with having large
35/// files: on filesystems without sparse files it avoids unnecessary space use;
36/// it also avoids allocating the full size if another process copies the file,
37/// which typically loses sparseness. These mitigations only work while the file
38/// is not in use.
39///
40/// The capacity and the header offset is determined by the first user of the
41/// MappedFileRegionArena instance and any future mismatched value from the
42/// original will result in error on creation.
43///
44/// To support resizing, we use two separate file locks:
45/// 1. We use a shared reader lock on a ".shared" file until destruction.
46/// 2. We use a lock on the main file during initialization - shared to check
47/// the status, upgraded to exclusive to resize/initialize the file.
48///
49/// Then during destruction we attempt to get exclusive access on (1), which
50/// requires no concurrent readers. If so, we shrink the file. Using two
51/// separate locks simplifies the implementation and enables it to work on
52/// platforms (e.g. Windows) where a shared/reader lock prevents writing.
53//===----------------------------------------------------------------------===//
54
56#include "OnDiskCommon.h"
59
60#if LLVM_ON_UNIX
61#include <sys/stat.h>
62#if __has_include(<sys/param.h>)
63#include <sys/param.h>
64#endif
65#ifdef DEV_BSIZE
66#define MAPPED_FILE_BSIZE DEV_BSIZE
67#elif __linux__
68#define MAPPED_FILE_BSIZE 512
69#endif
70#endif
71
72using namespace llvm;
73using namespace llvm::cas;
74using namespace llvm::cas::ondisk;
75
76namespace {
77struct FileWithLock {
78 std::string Path;
79 int FD = -1;
80 std::optional<sys::fs::LockKind> Locked;
81
82private:
83 FileWithLock(std::string PathStr, Error &E) : Path(std::move(PathStr)) {
84 ErrorAsOutParameter EOP(&E);
85 if (std::error_code EC = sys::fs::openFileForReadWrite(
87 E = createFileError(Path, EC);
88 }
89
90public:
91 FileWithLock(FileWithLock &) = delete;
92 FileWithLock(FileWithLock &&Other) {
93 Path = std::move(Other.Path);
94 FD = Other.FD;
95 Other.FD = -1;
96 Locked = Other.Locked;
97 Other.Locked = std::nullopt;
98 }
99
100 ~FileWithLock() { consumeError(unlock()); }
101
102 static Expected<FileWithLock> open(StringRef Path) {
104 FileWithLock Result(Path.str(), E);
105 if (E)
106 return std::move(E);
107 return std::move(Result);
108 }
109
110 Error lock(sys::fs::LockKind LK) {
111 assert(!Locked && "already locked");
112 if (std::error_code EC = lockFileThreadSafe(FD, LK))
113 return createFileError(Path, EC);
114 Locked = LK;
115 return Error::success();
116 }
117
118 Error switchLock(sys::fs::LockKind LK) {
119 assert(Locked && "not locked");
120 if (auto E = unlock())
121 return E;
122
123 return lock(LK);
124 }
125
126 Error unlock() {
127 if (Locked) {
128 Locked = std::nullopt;
129 if (std::error_code EC = unlockFileThreadSafe(FD))
130 return createFileError(Path, EC);
131 }
132 return Error::success();
133 }
134
135 // Return true if succeed to lock the file exclusively.
136 bool tryLockExclusive() {
137 assert(!Locked && "can only try to lock if not locked");
138 if (tryLockFileThreadSafe(FD) == std::error_code()) {
139 Locked = sys::fs::LockKind::Exclusive;
140 return true;
141 }
142
143 return false;
144 }
145
146 // Release the lock so it will not be unlocked on destruction.
147 void release() {
148 Locked = std::nullopt;
149 FD = -1;
150 }
151};
152
153struct FileSizeInfo {
154 uint64_t Size;
155 uint64_t AllocatedSize;
156
157 static ErrorOr<FileSizeInfo> get(sys::fs::file_t File);
158};
159} // end anonymous namespace
160
162 const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset,
163 std::shared_ptr<ondisk::OnDiskCASLogger> Logger,
164 function_ref<Error(MappedFileRegionArena &)> NewFileConstructor) {
165 uint64_t MinCapacity = HeaderOffset + sizeof(Header);
166 if (Capacity < MinCapacity)
167 return createStringError(
168 std::make_error_code(std::errc::invalid_argument),
169 "capacity is too small to hold MappedFileRegionArena");
170
172 Result.Path = Path.str();
173 Result.Logger = std::move(Logger);
174
175 // Open the support file. See file comment for details of locking scheme.
176 SmallString<128> SharedFilePath(Result.Path);
177 SharedFilePath.append(".shared");
178
179 auto SharedFileLock = FileWithLock::open(SharedFilePath);
180 if (!SharedFileLock)
181 return SharedFileLock.takeError();
182 Result.SharedLockFD = SharedFileLock->FD;
183
184 // Take shared/reader lock that will be held until destroyImpl if construction
185 // is successful.
186 if (auto E = SharedFileLock->lock(sys::fs::LockKind::Shared))
187 return std::move(E);
188
189 // Take shared/reader lock for initialization.
190 auto MainFile = FileWithLock::open(Result.Path);
191 if (!MainFile)
192 return MainFile.takeError();
193 if (Error E = MainFile->lock(sys::fs::LockKind::Shared))
194 return std::move(E);
195 Result.FD = MainFile->FD;
196
198 auto FileSize = FileSizeInfo::get(File);
199 if (!FileSize)
200 return createFileError(Result.Path, FileSize.getError());
201
202 // If the size is smaller than the capacity, we need to initialize the file.
203 // It maybe empty, or may have been shrunk during a previous close.
204 if (FileSize->Size < Capacity) {
205 // Lock the file exclusively so only one process will do the initialization.
206 if (Error E = MainFile->switchLock(sys::fs::LockKind::Exclusive))
207 return std::move(E);
208 // Retrieve the current size now that we have exclusive access.
209 FileSize = FileSizeInfo::get(File);
210 if (!FileSize)
211 return createFileError(Result.Path, FileSize.getError());
212 }
213
214 if (FileSize->Size >= MinCapacity) {
215 // File is initialized. Read out the header to check for capacity and
216 // offset.
217 SmallVector<char, sizeof(Header)> HeaderContent(sizeof(Header));
218 auto Size = sys::fs::readNativeFileSlice(File, HeaderContent, HeaderOffset);
219 if (!Size)
220 return Size.takeError();
221
222 Header H;
223 memcpy(&H, HeaderContent.data(), sizeof(H));
224 if (H.HeaderOffset != HeaderOffset)
225 return createStringError(
226 std::make_error_code(std::errc::invalid_argument),
227 "specified header offset (" + utostr(HeaderOffset) +
228 ") does not match existing config (" + utostr(H.HeaderOffset) +
229 ")");
230
231 if (H.Capacity < MinCapacity)
232 return createStringError(
233 std::make_error_code(std::errc::bad_file_descriptor),
234 "capacity inside the MappedFileRegionArena is too small");
235
236 // If the capacity doesn't match, use the existing capacity instead.
237 if (H.Capacity != Capacity)
238 Capacity = H.Capacity;
239 }
240
241 // If the size is smaller than capacity, we need to resize the file.
242 if (FileSize->Size < Capacity) {
243 // Acquire the exclusive lock before resizing the file. In the rare case
244 // when opening a large CAS using a small requested size, a shared lock
245 // needs to switch to an exclusive lock here.
246 if (MainFile->Locked != sys::fs::LockKind::Exclusive) {
247 if (Error E = MainFile->switchLock(sys::fs::LockKind::Exclusive))
248 return std::move(E);
249 }
250 if (std::error_code EC =
251 sys::fs::resize_file_sparse(MainFile->FD, Capacity))
252 return createFileError(Result.Path, EC);
253 if (Result.Logger)
254 Result.Logger->logMappedFileRegionArenaResizeFile(
255 Result.Path, FileSize->Size, Capacity);
256 }
257
258 // Create the mapped region.
259 {
260 std::error_code EC;
261 const char *Name = nullptr;
262#ifdef _WIN32
263 // Give the file mapping a name to ensure the same mappings are
264 // shared across processes.
265 std::string MapName = Result.Path;
266 std::replace(MapName.begin(), MapName.end(), '\\', '/');
267 MapName = "Local\\" + MapName;
268 Name = MapName.c_str();
269#endif
271 File, sys::fs::mapped_file_region::readwrite, Capacity, 0, EC, Name);
272 if (EC)
273 return createFileError(Result.Path, EC);
274 Result.Region = std::move(Map);
275 }
276
277 // Initialize the header.
278 if (Error E = Result.initializeHeader(HeaderOffset))
279 return std::move(E);
280
281 if (FileSize->Size < MinCapacity) {
282 assert(MainFile->Locked == sys::fs::LockKind::Exclusive);
283 // If we need to fully initialize the file, call NewFileConstructor.
284 if (Error E = NewFileConstructor(Result))
285 return std::move(E);
286
287 Result.H->HeaderOffset.exchange(HeaderOffset);
288 Result.H->Capacity.exchange(Capacity);
289 }
290
291 if (MainFile->Locked == sys::fs::LockKind::Exclusive) {
292 // If holding an exclusive lock, we might have resized the file and
293 // performed some read/write to the file. Query the file size again to make
294 // sure everything is up-to-date. Otherwise, FileSize info is already
295 // up-to-date.
296 FileSize = FileSizeInfo::get(File);
297 if (!FileSize)
298 return createFileError(Result.Path, FileSize.getError());
299 Result.H->AllocatedSize.exchange(FileSize->AllocatedSize);
300 }
301
302 // Release the shared lock so it can be closed in destoryImpl().
303 SharedFileLock->release();
304 return std::move(Result);
305}
306
307void MappedFileRegionArena::destroyImpl() {
308 if (!FD)
309 return;
310
311 // Drop the shared lock indicating we are no longer accessing the file.
312 if (SharedLockFD)
313 (void)unlockFileThreadSafe(*SharedLockFD);
314
315 // Attempt to truncate the file if we can get exclusive access. Ignore any
316 // errors.
317 if (H) {
318 assert(SharedLockFD && "Must have shared lock file open");
319 if (tryLockFileThreadSafe(*SharedLockFD) == std::error_code()) {
320 size_t Size = size();
321 size_t Capacity = capacity();
322 // sync to file system to make sure all contents are up-to-date.
323 (void)Region.sync();
324 // unmap the file before resizing since that is the requirement for
325 // some platforms.
326 Region.unmap();
327 (void)sys::fs::resize_file(*FD, Size);
328 (void)unlockFileThreadSafe(*SharedLockFD);
329 if (Logger)
330 Logger->logMappedFileRegionArenaResizeFile(Path, Capacity, Size);
331 }
332 }
333
334 auto Close = [](std::optional<int> &FD) {
335 if (FD) {
337 sys::fs::closeFile(File);
338 FD = std::nullopt;
339 }
340 };
341
342 // Close the file and shared lock.
343 Close(FD);
344 Close(SharedLockFD);
345
346 if (Logger)
347 Logger->logMappedFileRegionArenaClose(Path);
348}
349
350Error MappedFileRegionArena::initializeHeader(uint64_t HeaderOffset) {
351 if (capacity() >= static_cast<uint64_t>(INT64_MAX))
352 return createStringError(make_error_code(std::errc::protocol_error),
353 "arena capacity does not fit in int64_t");
354 uint64_t HeaderEndOffset = HeaderOffset + sizeof(decltype(*H));
355 if (HeaderEndOffset > capacity())
356 return createStringError(make_error_code(std::errc::protocol_error),
357 "arena header extends past capacity");
358 if (!isAligned(Align::Of<decltype(*H)>(), HeaderOffset))
359 return createStringError(make_error_code(std::errc::protocol_error),
360 "arena header offset is not aligned");
361 H = reinterpret_cast<decltype(H)>(data() + HeaderOffset);
362
363 uint64_t ExistingValue = 0;
364 if (!H->BumpPtr.compare_exchange_strong(ExistingValue, HeaderEndOffset))
365 if (ExistingValue < HeaderEndOffset)
366 return createStringError(
367 make_error_code(std::errc::protocol_error),
368 "arena bump pointer is corrupt: 0x" +
369 utohexstr(ExistingValue, /*LowerCase=*/true));
370 if (Logger)
371 Logger->logMappedFileRegionArenaCreate(Path, *FD, data(), capacity(),
372 size());
373 return Error::success();
374}
375
377 return createStringError(std::make_error_code(std::errc::not_enough_memory),
378 "memory mapped file allocator is out of space");
379}
380
382 AllocSize = alignTo(AllocSize, getAlign());
383 uint64_t OldEnd = H->BumpPtr.fetch_add(AllocSize);
384 uint64_t NewEnd = OldEnd + AllocSize;
385 if (LLVM_UNLIKELY(NewEnd > capacity())) {
386 // Return the allocation. If the start already passed the end, that means
387 // some other concurrent allocations already consumed all the capacity.
388 // There is no need to return the original value. If the start was not
389 // passed the end, current allocation certainly bumped it passed the end.
390 // All other allocation afterwards must have failed and current allocation
391 // is in charge of return the allocation back to a valid value.
392 if (OldEnd <= capacity())
393 (void)H->BumpPtr.exchange(OldEnd);
394
395 if (Logger)
396 Logger->logMappedFileRegionArenaOom(Path, capacity(), OldEnd, AllocSize);
397
399 }
400
401 uint64_t DiskSize = H->AllocatedSize;
402 if (LLVM_UNLIKELY(NewEnd > DiskSize)) {
403 uint64_t NewSize;
404 // The minimum increment is a page, but allocate more to amortize the cost.
405 constexpr uint64_t Increment = 1 * 1024 * 1024; // 1 MB
406 if (Error E = preallocateFileTail(*FD, DiskSize, DiskSize + Increment)
407 .moveInto(NewSize))
408 return std::move(E);
409 assert(NewSize >= DiskSize + Increment);
410 // FIXME: on Darwin this can under-count the size if there is a race to
411 // preallocate disk, because the semantics of F_PREALLOCATE are to add bytes
412 // to the end of the file, not to allocate up to a fixed size.
413 // Any discrepancy will be resolved the next time the file is truncated and
414 // then reopend.
415 while (DiskSize < NewSize)
416 H->AllocatedSize.compare_exchange_strong(DiskSize, NewSize);
417 }
418
419 if (Logger)
420 Logger->logMappedFileRegionArenaAllocate(data(), OldEnd, AllocSize);
421
422 return OldEnd;
423}
424
425ErrorOr<FileSizeInfo> FileSizeInfo::get(sys::fs::file_t File) {
426#if LLVM_ON_UNIX && defined(MAPPED_FILE_BSIZE)
427 struct stat Status;
428 int StatRet = ::fstat(File, &Status);
429 if (StatRet)
430 return errnoAsErrorCode();
431 uint64_t AllocatedSize = uint64_t(Status.st_blksize) * MAPPED_FILE_BSIZE;
432 return FileSizeInfo{uint64_t(Status.st_size), AllocatedSize};
433#else
434 // Fallback: assume the file is fully allocated. Note: this may result in
435 // data loss on out-of-space.
437 if (std::error_code EC = sys::fs::status(File, Status))
438 return EC;
439 return FileSizeInfo{Status.getSize(), Status.getSize()};
440#endif
441}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define H(x, y, z)
Definition MD5.cpp:56
static Error createAllocatorOutOfSpaceError()
This file declares interface for MappedFileRegionArena, a bump pointer allocator, backed by a memory-...
This file declares interface for OnDiskCASLogger, an interface that can be used to log CAS events to ...
if(PassOpts->AAPipeline)
This file contains some functions that are useful when dealing with strings.
Represents either an error or a value T.
Definition ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI_FOR_TEST Expected< MappedFileRegionArena > create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset, std::shared_ptr< ondisk::OnDiskCASLogger > Logger, function_ref< Error(MappedFileRegionArena &)> NewFileConstructor)
Create a MappedFileRegionArena.
LLVM_ABI_FOR_TEST Expected< int64_t > allocateOffset(uint64_t AllocSize)
Allocate, returning the offset from data() instead of a pointer.
static constexpr Align getAlign()
Minimum alignment for allocations, currently hardcoded to 8B.
An efficient, type-erasing, non-owning reference to a callable.
Represents the result of a call to sys::fs::status().
Definition FileSystem.h:222
This class represents a memory mapped file.
@ readwrite
May access map via data and modify it. Written to path.
#define INT64_MAX
Definition DataTypes.h:71
std::error_code lockFileThreadSafe(int FD, llvm::sys::fs::LockKind Kind)
Thread-safe alternative to sys::fs::lockFile.
std::error_code unlockFileThreadSafe(int FD)
Thread-safe alternative to sys::fs::unlockFile.
std::error_code tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout=std::chrono::milliseconds(0), llvm::sys::fs::LockKind Kind=llvm::sys::fs::LockKind::Exclusive)
Thread-safe alternative to sys::fs::tryLockFile.
Expected< size_t > preallocateFileTail(int FD, size_t CurrentSize, size_t NewSize)
Allocate space for the file FD on disk, if the filesystem supports it.
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
@ CD_OpenAlways
CD_OpenAlways - When opening a file:
Definition FileSystem.h:764
LLVM_ABI Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
LLVM_ABI std::error_code resize_file_sparse(int FD, uint64_t Size)
Resize path to size with sparse files explicitly enabled.
LockKind
An enumeration for the lock kind.
LLVM_ABI std::error_code resize_file(int FD, uint64_t Size)
Resize path to size.
LLVM_ABI file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
std::error_code make_error_code(BitcodeError E)
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
std::string utostr(uint64_t X, bool isNeg=false)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
@ Increment
Incrementally increasing token ID.
Definition AllocToken.h:26
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition Error.h:1256
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
static constexpr Align Of()
Allow constructions of constexpr Align from types.
Definition Alignment.h:94
Header for MappedFileRegionArena.