1//===-Caching.cpp - LLVM Local File Cache ---------------------------------===//
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
7//===----------------------------------------------------------------------===//
9// This file implements the localCache function, which simplifies creating,
10// adding to, and querying a local file system cache. localCache takes care of
11// periodically pruning older files from the cache using a CachePruningPolicy.
13//===----------------------------------------------------------------------===//
21#if !defined(_MSC_VER) && !defined(__MINGW32__)
30 const Twine &TempFilePrefixRef,
31 const Twine &CacheDirectoryPathRef,
34 // Create local copies which are safely captured-by-copy in lambdas
37 TempFilePrefixRef.
toVector(TempFilePrefix);
38 CacheDirectoryPathRef.
toVector(CacheDirectoryPath);
42 // This choice of file name allows the cache to be pruned (see pruneCache()
43 // in include/llvm/Support/CachePruning.h).
46 // First, see if we have a cache hit.
55 /*RequiresNullTerminator=*/false);
58 AddBuffer(Task,
ModuleName, std::move(*MBOrErr));
66 // On Windows we can fail to open a cache file with a permission denied
67 // error. This generally means that another process has requested to delete
68 // the file while it is still open, but it could also mean that another
69 // process has opened the file without the sharing permissions we need.
70 // Since the file is probably being deleted we handle it in the same way as
71 // if the file did not exist at all.
74 EntryPath +
": " + EC.message() +
"\n");
76 // This file stream is responsible for commiting the resulting file to the
77 // cache and calling AddBuffer to add it to the link.
84 CacheStream(std::unique_ptr<raw_pwrite_stream> OS,
AddBufferFn AddBuffer,
88 AddBuffer(std::move(AddBuffer)), TempFile(std::move(TempFile)),
96 // Make sure the stream is closed before committing it.
99 // Open the file first to avoid racing with a cache pruner.
103 /*FileSize=*/-1,
/*RequiresNullTerminator=*/false);
105 std::error_code EC = MBOrErr.
getError();
107 TempFile.TmpName +
": " +
108 EC.message() +
"\n");
111 // On POSIX systems, this will atomically replace the destination if
112 // it already exists. We try to emulate this on Windows, but this may
113 // fail with a permission denied error (for example, if the destination
114 // is currently opened by another process that does not give us the
115 // sharing permissions we need). Since the existing file should be
116 // semantically equivalent to the one we are trying to write, we give
117 // AddBuffer a copy of the bytes we wrote in that case. We do this
118 // instead of just using the existing file, because the pruner might
119 // delete the file before we get a chance to use it.
120 E = TempFile.keep(ObjectPathName);
122 std::error_code EC = E.convertToErrorCode();
125 EC,
Twine(
"Failed to rename temporary file ") +
126 TempFile.TmpName +
" to " + ObjectPathName +
": " +
127 EC.message() +
"\n");
131 MBOrErr = std::move(MBCopy);
133 // FIXME: should we consume the discard error?
142 AddBuffer(Task,
ModuleName, std::move(*MBOrErr));
148 ->
Expected<std::unique_ptr<CachedFileStream>> {
149 // Create the cache directory if not already done. Doing this lazily
150 // ensures the filesystem isn't mutated until the cache is.
152 CacheDirectoryPath,
/*IgnoreExisting=*/true))
154 CacheDirectoryPath +
": " +
157 // Write to a temporary to avoid race condition
160 TempFilePrefix +
"-%%%%%%.tmp.o");
166 ": Can't get a temporary file");
168 // This CacheStream will move the temporary file into the cache when done.
169 return std::make_unique<CacheStream>(
170 std::make_unique<raw_fd_ostream>(Temp->FD,
/* ShouldClose */ false),
171 AddBuffer, std::move(*Temp), std::string(EntryPath),
ModuleName.str(),
This class wraps an output stream for a file.
CachedFileStream(std::unique_ptr< raw_pwrite_stream > OS, std::string OSPath="")
virtual Error commit()
Must be called exactly once after the writes to OS have been completed but before the CachedFileStrea...
This class wraps a std::error_code in a Error.
Represents either an error or a value T.
std::error_code getError() const
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.
Error takeError()
Take ownership of the stored error.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
LLVM_ABI void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Represents a temporary file.
static LLVM_ABI Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write, OpenFlags ExtraFlags=OF_None)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
@ OF_UpdateAtime
Force files Atime to be updated on access.
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
LLVM_ABI std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
LLVM_ABI file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
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 handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
@ no_such_file_or_directory
std::function< void(unsigned Task, const Twine &ModuleName, std::unique_ptr< MemoryBuffer > MB)> AddBufferFn
This type defines the callback to add a pre-existing file (e.g.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
std::function< Expected< std::unique_ptr< CachedFileStream > >( unsigned Task, const Twine &ModuleName)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
void consumeError(Error Err)
Consume a Error without doing anything.
LLVM_ABI Expected< FileCache > localCache(const Twine &CacheNameRef, const Twine &TempFilePrefixRef, const Twine &CacheDirectoryPathRef, AddBufferFn AddBuffer=[](size_t Task, const Twine &ModuleName, std::unique_ptr< MemoryBuffer > MB) {})
Create a local file system cache which uses the given cache name, temporary file prefix,...
This type represents a file cache system that manages caching of files.