clang 23.0.0git
ModuleCache.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
10
13#include "llvm/ADT/ScopeExit.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/IOSandbox.h"
17#include "llvm/Support/LockFileManager.h"
18#include "llvm/Support/Path.h"
19
20using namespace clang;
21
23 auto [ByNameIt, ByNameInserted] = ByPath.insert({Path, nullptr});
24 if (!ByNameIt->second) {
25 // This is a compiler-internal input/output, let's bypass the sandbox.
26 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
27
28 // If we cannot get status of the module cache directory, try if trying to
29 // create it helps.
30 llvm::sys::fs::file_status Status;
31 if (std::error_code EC = llvm::sys::fs::status(Path, Status)) {
32 // Unless the status failed because the directory does not exist yet.
33 if (EC != std::errc::no_such_file_or_directory)
34 return nullptr;
35 // If we're unable to create the directory.
36 if (llvm::sys::fs::create_directories(Path))
37 return nullptr;
38 // If we're unable to stat the newly created directory.
39 if (llvm::sys::fs::status(Path, Status))
40 return nullptr;
41 }
42
43 llvm::sys::fs::UniqueID UID = Status.getUniqueID();
44 auto [ByUIDIt, ByUIDInserted] = ByUID.insert({UID, nullptr});
45 if (!ByUIDIt->second)
46 ByUIDIt->second = std::make_unique<ModuleCacheDirectory>();
47 ByNameIt->second = ByUIDIt->second.get();
48 }
49 return ByNameIt->second;
50}
51
52/// Write a new timestamp file with the given path.
53static void writeTimestampFile(StringRef TimestampFile) {
54 std::error_code EC;
55 llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::OF_None);
56}
57
58void clang::maybePruneImpl(StringRef Path, time_t PruneInterval,
59 time_t PruneAfter, bool PruneTopLevel) {
60 if (PruneInterval <= 0 || PruneAfter <= 0)
61 return;
62
63 // This is a compiler-internal input/output, let's bypass the sandbox.
64 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
65
66 llvm::SmallString<128> TimestampFile(Path);
67 llvm::sys::path::append(TimestampFile, "modules.timestamp");
68
69 // Try to stat() the timestamp file.
70 llvm::sys::fs::file_status StatBuf;
71 if (std::error_code EC = llvm::sys::fs::status(TimestampFile, StatBuf)) {
72 // If the timestamp file wasn't there, create one now.
73 if (EC == std::errc::no_such_file_or_directory)
74 writeTimestampFile(TimestampFile);
75 return;
76 }
77
78 // Check whether the time stamp is older than our pruning interval.
79 // If not, do nothing.
80 time_t TimestampModTime =
81 llvm::sys::toTimeT(StatBuf.getLastModificationTime());
82 time_t CurrentTime = time(nullptr);
83 if (CurrentTime - TimestampModTime <= PruneInterval)
84 return;
85
86 // Write a new timestamp file so that nobody else attempts to prune.
87 // There is a benign race condition here, if two Clang instances happen to
88 // notice at the same time that the timestamp is out-of-date.
89 writeTimestampFile(TimestampFile);
90
91 // Walk the entire module cache, looking for unused module files and module
92 // indices.
93 std::error_code EC;
94 auto TryPruneFile = [&](StringRef FilePath) {
95 // We only care about module and global module index files.
96 StringRef Filename = llvm::sys::path::filename(FilePath);
97 StringRef Extension = llvm::sys::path::extension(FilePath);
98 if (Extension != ".pcm" && Extension != ".timestamp" &&
99 Filename != "modules.idx")
100 return;
101
102 // Don't prune the pruning timestamp file.
103 if (Filename == "modules.timestamp")
104 return;
105
106 // Look at this file. If we can't stat it, there's nothing interesting
107 // there.
108 if (llvm::sys::fs::status(FilePath, StatBuf))
109 return;
110
111 // If the file has been used recently enough, leave it there.
112 time_t FileAccessTime = llvm::sys::toTimeT(StatBuf.getLastAccessedTime());
113 if (CurrentTime - FileAccessTime <= PruneAfter)
114 return;
115
116 // Remove the file.
117 llvm::sys::fs::remove(FilePath);
118
119 // Remove the timestamp file created by implicit module builds.
120 std::string TimestampFilename = FilePath.str() + ".timestamp";
121 llvm::sys::fs::remove(TimestampFilename);
122 };
123
124 for (llvm::sys::fs::directory_iterator Dir(Path, EC), DirEnd;
125 Dir != DirEnd && !EC; Dir.increment(EC)) {
126 // If we don't have a directory, try to prune it as a file in the root.
127 if (!llvm::sys::fs::is_directory(Dir->path())) {
128 if (PruneTopLevel)
129 TryPruneFile(Dir->path());
130 continue;
131 }
132
133 // Walk all the files within this directory.
134 for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
135 File != FileEnd && !EC; File.increment(EC))
136 TryPruneFile(File->path());
137
138 // If we removed all the files in the directory, remove the directory
139 // itself.
140 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
141 llvm::sys::fs::directory_iterator() &&
142 !EC)
143 llvm::sys::fs::remove(Dir->path());
144 }
145}
146
147std::error_code clang::writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer,
148 off_t &Size, time_t &ModTime) {
149 StringRef Extension = llvm::sys::path::extension(Path);
150 SmallString<128> ModelPath = StringRef(Path).drop_back(Extension.size());
151 ModelPath += "-%%%%%%%%";
152 ModelPath += Extension;
153 ModelPath += ".tmp";
154
155 std::error_code EC;
156 int FD;
157 SmallString<128> TmpPath;
158 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath))) {
159 if (EC != std::errc::no_such_file_or_directory)
160 return EC;
161
162 StringRef Dir = llvm::sys::path::parent_path(Path);
163 if (std::error_code InnerEC = llvm::sys::fs::create_directories(Dir))
164 return InnerEC;
165
166 if ((EC = llvm::sys::fs::createUniqueFile(ModelPath, FD, TmpPath)))
167 return EC;
168 }
169
170 llvm::sys::fs::file_status Status;
171 {
172 llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
173 OS << Buffer.getBuffer();
174 // Using the status from an open file descriptor ensures this is not racy.
175 if ((EC = llvm::sys::fs::status(FD, Status)))
176 return EC;
177 }
178
179 Size = Status.getSize();
180 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
181
182 // This preserves both size and modification time.
183 if ((EC = llvm::sys::fs::rename(TmpPath, Path)))
184 return EC;
185
186 return {};
187}
188
190clang::readImpl(StringRef FileName, off_t &Size, time_t &ModTime) {
192 llvm::sys::fs::openNativeFileForRead(FileName);
193 if (!FD)
194 return FD.takeError();
195 llvm::scope_exit CloseFD([&FD]() { llvm::sys::fs::closeFile(*FD); });
196 llvm::sys::fs::file_status Status;
197 if (std::error_code EC = llvm::sys::fs::status(*FD, Status))
198 return llvm::errorCodeToError(EC);
199 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
200 llvm::MemoryBuffer::getOpenFile(*FD, FileName, Status.getSize(),
201 /*RequiresNullTerminator=*/false);
202 if (!Buf)
203 return llvm::errorCodeToError(Buf.getError());
204 Size = Status.getSize();
205 ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
206 return std::move(*Buf);
207}
208
209namespace {
210class CrossProcessModuleCache : public ModuleCache {
211 InMemoryModuleCache InMemory;
212
213public:
214 std::unique_ptr<llvm::AdvisoryLock>
215 getLock(StringRef ModuleFilename) override {
216 return std::make_unique<llvm::LockFileManager>(ModuleFilename);
217 }
218
219 std::time_t getModuleTimestamp(StringRef ModuleFilename) override {
220 // This is a compiler-internal input/output, let's bypass the sandbox.
221 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
222
223 std::string TimestampFilename =
225 llvm::sys::fs::file_status Status;
226 if (llvm::sys::fs::status(TimestampFilename, Status) != std::error_code{})
227 return 0;
228 return llvm::sys::toTimeT(Status.getLastModificationTime());
229 }
230
231 void updateModuleTimestamp(StringRef ModuleFilename) override {
232 // This is a compiler-internal input/output, let's bypass the sandbox.
233 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
234
235 // Overwrite the timestamp file contents so that file's mtime changes.
236 std::error_code EC;
237 llvm::raw_fd_ostream OS(
239 llvm::sys::fs::OF_TextWithCRLF);
240 if (EC)
241 return;
242 OS << "Timestamp file\n";
243 OS.close();
244 OS.clear_error(); // Avoid triggering a fatal error.
245 }
246
247 void maybePrune(StringRef Path, time_t PruneInterval,
248 time_t PruneAfter) override {
249 // This is a compiler-internal input/output, let's bypass the sandbox.
250 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
251
252 maybePruneImpl(Path, PruneInterval, PruneAfter);
253 }
254
255 InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
256 const InMemoryModuleCache &getInMemoryModuleCache() const override {
257 return InMemory;
258 }
259
260 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
261 off_t &Size, time_t &ModTime) override {
262 // This is a compiler-internal input/output, let's bypass the sandbox.
263 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
264
265 return writeImpl(Path, Buffer, Size, ModTime);
266 }
267
268 Expected<std::unique_ptr<llvm::MemoryBuffer>>
269 read(StringRef FileName, off_t &Size, time_t &ModTime) override {
270 // This is a compiler-internal input/output, let's bypass the sandbox.
271 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
272
273 return readImpl(FileName, Size, ModTime);
274 }
275};
276} // namespace
277
278std::shared_ptr<ModuleCache> clang::createCrossProcessModuleCache() {
279 return std::make_shared<CrossProcessModuleCache>();
280}
static void writeTimestampFile(StringRef TimestampFile)
Write a new timestamp file with the given path.
In-memory cache for modules.
The address of an instance of this class represents the identity of a module cache directory.
Definition ModuleCache.h:33
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:37
virtual const ModuleCacheDirectory * getDirectoryPtr(StringRef Path)
Returns an opaque pointer representing the module cache directory.
static std::string getTimestampFilename(StringRef FileName)
Definition ModuleFile.h:188
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
The JSON file list parser is used to communicate input to InstallAPI.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter, bool PruneTopLevel=false)
Shared implementation of ModuleCache::maybePrune().
std::shared_ptr< ModuleCache > createCrossProcessModuleCache()
Creates new ModuleCache backed by a file system directory that may be operated on by multiple process...
std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::write().