LLVM 22.0.0git
OnDiskCommon.cpp
Go to the documentation of this file.
1//===- OnDiskCommon.cpp ---------------------------------------------------===//
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
9#include "OnDiskCommon.h"
10#include "llvm/Support/Error.h"
13#include <mutex>
14#include <thread>
15
16#if __has_include(<sys/file.h>)
17#include <sys/file.h>
18#ifdef LOCK_SH
19#define HAVE_FLOCK 1
20#else
21#define HAVE_FLOCK 0
22#endif
23#endif
24
25#if __has_include(<fcntl.h>)
26#include <fcntl.h>
27#endif
28
29#if __has_include(<sys/mount.h>)
30#include <sys/mount.h> // statfs
31#endif
32
33#ifdef __APPLE__
34#if __has_include(<sys/sysctl.h>)
35#include <sys/sysctl.h>
36#endif
37#endif
38
39using namespace llvm;
40
42
44 static std::once_flag Flag;
45 Error Err = Error::success();
46 std::call_once(Flag, [&Err] {
47 ErrorAsOutParameter EAO(&Err);
48 constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE";
49 auto Value = sys::Process::GetEnv(EnvVar);
50 if (!Value)
51 return;
52
54 if (StringRef(*Value).getAsInteger(/*auto*/ 0, Size))
56 "invalid value for %s: expected integer", EnvVar);
58 });
59
60 if (Err)
61 return std::move(Err);
62
64 return std::nullopt;
65
67}
68
72
73std::error_code cas::ondisk::lockFileThreadSafe(int FD,
74 sys::fs::LockKind Kind) {
75#if HAVE_FLOCK
76 if (flock(FD, Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) == 0)
77 return std::error_code();
78 return std::error_code(errno, std::generic_category());
79#elif defined(_WIN32)
80 // On Windows this implementation is thread-safe.
81 return sys::fs::lockFile(FD, Kind);
82#else
83 return make_error_code(std::errc::no_lock_available);
84#endif
85}
86
87std::error_code cas::ondisk::unlockFileThreadSafe(int FD) {
88#if HAVE_FLOCK
89 if (flock(FD, LOCK_UN) == 0)
90 return std::error_code();
91 return std::error_code(errno, std::generic_category());
92#elif defined(_WIN32)
93 // On Windows this implementation is thread-safe.
94 return sys::fs::unlockFile(FD);
95#else
96 return make_error_code(std::errc::no_lock_available);
97#endif
98}
99
100std::error_code
101cas::ondisk::tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout,
102 sys::fs::LockKind Kind) {
103#if HAVE_FLOCK
104 auto Start = std::chrono::steady_clock::now();
105 auto End = Start + Timeout;
106 do {
107 if (flock(FD, (Kind == sys::fs::LockKind::Exclusive ? LOCK_EX : LOCK_SH) |
108 LOCK_NB) == 0)
109 return std::error_code();
110 int Error = errno;
111 if (Error == EWOULDBLOCK) {
112 if (Timeout.count() == 0)
113 break;
114 // Match sys::fs::tryLockFile, which sleeps for 1 ms per attempt.
115 std::this_thread::sleep_for(std::chrono::milliseconds(1));
116 continue;
117 }
118 return std::error_code(Error, std::generic_category());
119 } while (std::chrono::steady_clock::now() < End);
120 return make_error_code(std::errc::no_lock_available);
121#elif defined(_WIN32)
122 // On Windows this implementation is thread-safe.
123 return sys::fs::tryLockFile(FD, Timeout, Kind);
124#else
125 return make_error_code(std::errc::no_lock_available);
126#endif
127}
128
130 size_t NewSize) {
131 auto CreateError = [&](std::error_code EC) -> Expected<size_t> {
132 if (EC == std::errc::not_supported)
133 // Ignore ENOTSUP in case the filesystem cannot preallocate.
134 return NewSize;
135#if defined(HAVE_POSIX_FALLOCATE)
136 if (EC == std::errc::invalid_argument && CurrentSize < NewSize && // len > 0
137 NewSize < std::numeric_limits<off_t>::max()) // 0 <= offset, len < max
138 // Prior to 2024, POSIX required EINVAL for cases that should be ENOTSUP,
139 // so handle it the same as above if it is not one of the other ways to
140 // get EINVAL.
141 return NewSize;
142#endif
143 return createStringError(EC,
144 "failed to allocate to CAS file: " + EC.message());
145 };
146#if defined(HAVE_POSIX_FALLOCATE)
147 // Note: posix_fallocate returns its error directly, not via errno.
148 if (int Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize))
149 return CreateError(std::error_code(Err, std::generic_category()));
150 return NewSize;
151#elif defined(__APPLE__)
152 fstore_t FAlloc;
153 FAlloc.fst_flags = F_ALLOCATEALL;
154#if defined(F_ALLOCATEPERSIST) && \
155 defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
156 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000
157 // F_ALLOCATEPERSIST is introduced in macOS 13.
158 FAlloc.fst_flags |= F_ALLOCATEPERSIST;
159#endif
160 FAlloc.fst_posmode = F_PEOFPOSMODE;
161 FAlloc.fst_offset = 0;
162 FAlloc.fst_length = NewSize - CurrentSize;
163 FAlloc.fst_bytesalloc = 0;
164 if (fcntl(FD, F_PREALLOCATE, &FAlloc))
165 return CreateError(errnoAsErrorCode());
166 assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize);
167 return CurrentSize + FAlloc.fst_bytesalloc;
168#else
169 (void)CreateError; // Silence unused variable.
170 return NewSize; // Pretend it worked.
171#endif
172}
173
175 // Add exceptions to use small database file here.
176#if defined(__APPLE__) && __has_include(<sys/mount.h>)
177 // macOS tmpfs does not support sparse tails.
178 SmallString<128> PathStorage;
179 StringRef Path = P.toNullTerminatedStringRef(PathStorage);
180 struct statfs StatFS;
181 if (statfs(Path.data(), &StatFS) != 0)
182 return false;
183
184 if (strcmp(StatFS.f_fstypename, "tmpfs") == 0)
185 return true;
186#endif
187 // Default to use regular database file.
188 return false;
189}
190
192#ifdef __APPLE__
193#if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME)
194 struct timeval TV;
195 size_t TVLen = sizeof(TV);
196 int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME};
197 if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0)
199 "failed to get boottime");
200 if (TVLen != sizeof(TV))
201 return createStringError("sysctl kern.boottime unexpected format");
202 return TV.tv_sec;
203#else
204 return 0;
205#endif
206#elif defined(__linux__)
207 // Use the mtime for /proc, which is recreated during system boot.
208 // We could also read /proc/stat and search for 'btime'.
210 if (std::error_code EC = sys::fs::status("/proc", Status))
211 return createFileError("/proc", EC);
212 return Status.getLastModificationTime().time_since_epoch().count();
213#else
214 return 0;
215#endif
216}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint64_t OnDiskCASMaxMappingSize
#define P(N)
Provides a library for accessing information about this process and other processes on the operating ...
Helper for Errors used as out-parameters.
Definition Error.h:1144
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
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:472
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
static LLVM_ABI std::optional< std::string > GetEnv(StringRef name)
Represents the result of a call to sys::fs::status().
Definition FileSystem.h:222
LLVM_ABI_FOR_TEST void setMaxMappingSize(uint64_t Size)
Set MaxMappingSize for ondisk CAS.
Expected< std::optional< uint64_t > > getOverriddenMaxMappingSize()
Retrieves an overridden maximum mapping size for CAS files, if any, speicified by LLVM_CAS_MAX_MAPPIN...
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< uint64_t > getBootTime()
Get boot time for the OS.
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.
bool useSmallMappingSize(const Twine &Path)
Whether to use a small file mapping for ondisk databases created in Path.
LLVM_ABI std::error_code lockFile(int FD, LockKind Kind=LockKind::Exclusive)
Lock the file.
LLVM_ABI std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout=std::chrono::milliseconds(0), LockKind Kind=LockKind::Exclusive)
Try to locks the file during the specified time.
LockKind
An enumeration for the lock kind.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI std::error_code unlockFile(int FD)
Unlock the file.
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:1399
std::error_code make_error_code(BitcodeError E)
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
@ Timeout
Reached timeout while waiting for the owner to release the lock.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition Error.h:1240