17#if __has_include(<sys/file.h>)
26#if __has_include(<fcntl.h>)
30#if __has_include(<sys/mount.h>)
35#if __has_include(<sys/sysctl.h>)
36#include <sys/sysctl.h>
45 static std::once_flag Flag;
47 std::call_once(Flag, [&Err] {
49 constexpr const char *EnvVar =
"LLVM_CAS_MAX_MAPPING_SIZE";
57 "invalid value for %s: expected integer", EnvVar);
62 return std::move(Err);
78 return std::error_code();
79 return std::error_code(errno, std::generic_category());
90 if (flock(FD, LOCK_UN) == 0)
91 return std::error_code();
92 return std::error_code(errno, std::generic_category());
105 auto Start = std::chrono::steady_clock::now();
110 return std::error_code();
112 if (
Error == EWOULDBLOCK) {
116 std::this_thread::sleep_for(std::chrono::milliseconds(1));
119 return std::error_code(
Error, std::generic_category());
120 }
while (std::chrono::steady_clock::now() < End);
133 if (EC == std::errc::not_supported)
136#if defined(HAVE_POSIX_FALLOCATE)
137 if (EC == std::errc::invalid_argument && CurrentSize < NewSize && // len > 0
138 NewSize < std::numeric_limits<off_t>::max())
145 "failed to allocate to CAS file: " + EC.message());
147#if defined(HAVE_POSIX_FALLOCATE)
149 if (
int Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize))
150 return CreateError(std::error_code(Err, std::generic_category()));
152#elif defined(__APPLE__)
154 FAlloc.fst_flags = F_ALLOCATEALL;
155#if defined(F_ALLOCATEPERSIST) && \
156 defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
157 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000
159 FAlloc.fst_flags |= F_ALLOCATEPERSIST;
161 FAlloc.fst_posmode = F_PEOFPOSMODE;
162 FAlloc.fst_offset = 0;
163 FAlloc.fst_length = NewSize - CurrentSize;
164 FAlloc.fst_bytesalloc = 0;
165 if (fcntl(FD, F_PREALLOCATE, &FAlloc))
167 assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize);
168 return CurrentSize + FAlloc.fst_bytesalloc;
177#if defined(__APPLE__) && __has_include(<sys/mount.h>)
180 StringRef Path =
P.toNullTerminatedStringRef(PathStorage);
181 struct statfs StatFS;
182 if (statfs(Path.data(), &StatFS) != 0)
185 if (strcmp(StatFS.f_fstypename,
"tmpfs") == 0)
194#if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME)
196 size_t TVLen =
sizeof(TV);
197 int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME};
198 if (sysctl(KernBoot, 2, &TV, &TVLen,
nullptr, 0) < 0)
200 "failed to get boottime");
201 if (TVLen !=
sizeof(TV))
207#elif defined(__linux__)
213 return Status.getLastModificationTime().time_since_epoch().count();
233 TmpPath = UniqueTmpPath;
250 if (!TmpPath.empty())
252 if (!UniqueTmpPath.empty())
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint64_t OnDiskCASMaxMappingSize
Provides a library for accessing information about this process and other processes on the operating ...
Helper for Errors used as out-parameters.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef - Represent a constant reference to a string, i.e.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
Error renameTo(StringRef RenameToPath)
Rename the new unique file to RenameToPath.
Expected< StringRef > createAndCopyFrom(StringRef ParentPath, StringRef CopyFromPath)
Create a new unique file path under ParentPath and copy the contents of CopyFromPath into it.
static LLVM_ABI std::optional< std::string > GetEnv(StringRef name)
Represents the result of a call to sys::fs::status().
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 rename(const Twine &from, const Twine &to)
Rename from to to.
LLVM_ABI std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None, unsigned Mode=all_read|all_write)
Create a uniquely named file.
LLVM_ABI std::error_code lockFile(int FD, LockKind Kind=LockKind::Exclusive)
Lock the file.
LLVM_ABI std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
LLVM_ABI std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
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.
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
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.
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...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
@ 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.