2016-08-23 23:30:12 +02:00
|
|
|
//===-Caching.cpp - LLVM Link Time Optimizer Cache Handling ---------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-23 23:30:12 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Caching for ThinLTO.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/LTO/Caching.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-03-20 19:19:41 +01:00
|
|
|
#include "llvm/Support/Errc.h"
|
2016-09-23 23:33:43 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-08-23 23:30:12 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-11-10 18:08:21 +01:00
|
|
|
#include "llvm/Support/Process.h"
|
2016-08-23 23:30:12 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2018-07-04 16:17:10 +02:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-23 23:30:12 +02:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::lto;
|
|
|
|
|
2017-03-02 03:02:38 +01:00
|
|
|
Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
|
2017-03-17 01:34:07 +01:00
|
|
|
AddBufferFn AddBuffer) {
|
2017-03-02 03:02:38 +01:00
|
|
|
if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
return [=](unsigned Task, StringRef Key) -> AddStreamFn {
|
2017-03-20 17:41:57 +01:00
|
|
|
// This choice of file name allows the cache to be pruned (see pruneCache()
|
|
|
|
// in include/llvm/Support/CachePruning.h).
|
2016-09-23 23:33:43 +02:00
|
|
|
SmallString<64> EntryPath;
|
2017-03-20 17:41:57 +01:00
|
|
|
sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
|
|
|
|
// First, see if we have a cache hit.
|
2018-07-04 16:17:10 +02:00
|
|
|
int FD;
|
|
|
|
SmallString<64> ResultPath;
|
|
|
|
std::error_code EC = sys::fs::openFileForRead(
|
|
|
|
Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath);
|
|
|
|
if (!EC) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getOpenFile(FD, EntryPath,
|
|
|
|
/*FileSize*/ -1,
|
|
|
|
/*RequiresNullTerminator*/ false);
|
|
|
|
close(FD);
|
|
|
|
if (MBOrErr) {
|
|
|
|
AddBuffer(Task, std::move(*MBOrErr));
|
|
|
|
return AddStreamFn();
|
|
|
|
}
|
|
|
|
EC = MBOrErr.getError();
|
2016-09-23 23:33:43 +02:00
|
|
|
}
|
2016-08-23 23:30:12 +02:00
|
|
|
|
LTO: Keep file handles open for memory mapped files.
On Windows we've observed that if you open a file, write to it, map it into
memory and close the file handle, the contents of the memory mapping can
sometimes be incorrect. That was what we did when adding an entry to the
ThinLTO cache using the TempFile and MemoryBuffer classes, and it was causing
intermittent build failures on Chromium's ThinLTO bots on Windows. More
details are in the associated Chromium bug (crbug.com/786127).
We can prevent this from happening by keeping a handle to the file open while
the mapping is active. So this patch changes the mapped_file_region class to
duplicate the file handle when mapping the file and close it upon unmapping it.
One gotcha is that the file handle that we keep open must not have been
created with FILE_FLAG_DELETE_ON_CLOSE, as otherwise the operating system
will prevent other processes from opening the file. We can achieve this
by avoiding the use of FILE_FLAG_DELETE_ON_CLOSE altogether. Instead,
we use SetFileInformationByHandle with FileDispositionInfo to manage the
delete-on-close bit. This lets us remove the hack that we used to use to
clear the delete-on-close bit on a file opened with FILE_FLAG_DELETE_ON_CLOSE.
A downside of using SetFileInformationByHandle/FileDispositionInfo as
opposed to FILE_FLAG_DELETE_ON_CLOSE is that it prevents us from using
CreateFile to open the file while the flag is set, even within the same
process. This doesn't seem to matter for almost every client of TempFile,
except for LockFileManager, which calls sys::fs::create_link to create a
hard link from the lock file, and in the process of doing so tries to open
the file. To prevent this change from breaking LockFileManager I changed it
to stop using TempFile by effectively reverting r318550.
Differential Revision: https://reviews.llvm.org/D48051
llvm-svn: 334630
2018-06-13 20:03:14 +02:00
|
|
|
// On Windows we can fail to open a cache file with a permission denied
|
|
|
|
// error. This generally means that another process has requested to delete
|
|
|
|
// the file while it is still open, but it could also mean that another
|
|
|
|
// process has opened the file without the sharing permissions we need.
|
|
|
|
// Since the file is probably being deleted we handle it in the same way as
|
|
|
|
// if the file did not exist at all.
|
2018-07-04 16:17:10 +02:00
|
|
|
if (EC != errc::no_such_file_or_directory && EC != errc::permission_denied)
|
2017-03-17 01:34:07 +01:00
|
|
|
report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
|
2018-07-04 16:17:10 +02:00
|
|
|
": " + EC.message() + "\n");
|
2017-03-17 01:34:07 +01:00
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
// This native object stream is responsible for commiting the resulting
|
2017-03-17 01:34:07 +01:00
|
|
|
// file to the cache and calling AddBuffer to add it to the link.
|
2016-09-23 23:33:43 +02:00
|
|
|
struct CacheStream : NativeObjectStream {
|
2017-03-17 01:34:07 +01:00
|
|
|
AddBufferFn AddBuffer;
|
2017-11-15 20:09:22 +01:00
|
|
|
sys::fs::TempFile TempFile;
|
2016-09-23 23:33:43 +02:00
|
|
|
std::string EntryPath;
|
|
|
|
unsigned Task;
|
2016-08-23 23:30:12 +02:00
|
|
|
|
2017-03-17 01:34:07 +01:00
|
|
|
CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
|
2017-11-15 20:09:22 +01:00
|
|
|
sys::fs::TempFile TempFile, std::string EntryPath,
|
2016-09-23 23:33:43 +02:00
|
|
|
unsigned Task)
|
2017-03-17 01:34:07 +01:00
|
|
|
: NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
|
2017-11-15 20:09:22 +01:00
|
|
|
TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)),
|
|
|
|
Task(Task) {}
|
2016-09-23 23:33:43 +02:00
|
|
|
|
|
|
|
~CacheStream() {
|
2017-11-15 20:09:22 +01:00
|
|
|
// Make sure the stream is closed before committing it.
|
2016-09-23 23:33:43 +02:00
|
|
|
OS.reset();
|
2017-09-05 21:51:38 +02:00
|
|
|
|
|
|
|
// Open the file first to avoid racing with a cache pruner.
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
2017-11-15 20:09:22 +01:00
|
|
|
MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName,
|
|
|
|
/*FileSize*/ -1,
|
|
|
|
/*RequiresNullTerminator*/ false);
|
2017-10-03 02:44:21 +02:00
|
|
|
if (!MBOrErr)
|
|
|
|
report_fatal_error(Twine("Failed to open new cache file ") +
|
2017-11-15 20:09:22 +01:00
|
|
|
TempFile.TmpName + ": " +
|
2017-10-03 02:44:21 +02:00
|
|
|
MBOrErr.getError().message() + "\n");
|
2017-09-05 21:51:38 +02:00
|
|
|
|
2017-11-16 02:16:52 +01:00
|
|
|
// On POSIX systems, this will atomically replace the destination if
|
|
|
|
// it already exists. We try to emulate this on Windows, but this may
|
|
|
|
// fail with a permission denied error (for example, if the destination
|
|
|
|
// is currently opened by another process that does not give us the
|
|
|
|
// sharing permissions we need). Since the existing file should be
|
|
|
|
// semantically equivalent to the one we are trying to write, we give
|
|
|
|
// AddBuffer a copy of the bytes we wrote in that case. We do this
|
|
|
|
// instead of just using the existing file, because the pruner might
|
|
|
|
// delete the file before we get a chance to use it.
|
2017-11-15 20:09:22 +01:00
|
|
|
Error E = TempFile.keep(EntryPath);
|
|
|
|
E = handleErrors(std::move(E), [&](const ECError &E) -> Error {
|
|
|
|
std::error_code EC = E.convertToErrorCode();
|
|
|
|
if (EC != errc::permission_denied)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
|
|
|
auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(),
|
|
|
|
EntryPath);
|
2017-11-10 18:08:21 +01:00
|
|
|
MBOrErr = std::move(MBCopy);
|
2017-11-15 20:09:22 +01:00
|
|
|
|
|
|
|
// FIXME: should we consume the discard error?
|
|
|
|
consumeError(TempFile.discard());
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (E)
|
2017-09-05 21:51:38 +02:00
|
|
|
report_fatal_error(Twine("Failed to rename temporary file ") +
|
2017-11-15 20:09:22 +01:00
|
|
|
TempFile.TmpName + " to " + EntryPath + ": " +
|
|
|
|
toString(std::move(E)) + "\n");
|
2017-09-05 21:51:38 +02:00
|
|
|
|
2018-02-20 21:21:53 +01:00
|
|
|
AddBuffer(Task, std::move(*MBOrErr));
|
2016-09-23 23:33:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
|
|
|
|
// Write to a temporary to avoid race condition
|
2017-11-15 20:09:22 +01:00
|
|
|
SmallString<64> TempFilenameModel;
|
2017-03-16 19:20:06 +01:00
|
|
|
sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
|
2017-11-15 20:09:22 +01:00
|
|
|
Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create(
|
|
|
|
TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write);
|
|
|
|
if (!Temp) {
|
|
|
|
errs() << "Error: " << toString(Temp.takeError()) << "\n";
|
2016-09-23 23:33:43 +02:00
|
|
|
report_fatal_error("ThinLTO: Can't get a temporary file");
|
|
|
|
}
|
|
|
|
|
|
|
|
// This CacheStream will move the temporary file into the cache when done.
|
2016-09-24 01:23:23 +02:00
|
|
|
return llvm::make_unique<CacheStream>(
|
2017-11-15 20:09:22 +01:00
|
|
|
llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false),
|
|
|
|
AddBuffer, std::move(*Temp), EntryPath.str(), Task);
|
2016-09-23 23:33:43 +02:00
|
|
|
};
|
|
|
|
};
|
2016-08-23 23:30:12 +02:00
|
|
|
}
|