2017-03-24 08:15:16 +08:00
|
|
|
//===- Filesystem.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2017-03-24 08:15:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a few utility functions to handle files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-12 00:30:55 +08:00
|
|
|
#include "lld/Common/Filesystem.h"
|
2017-11-18 05:40:38 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-03-24 08:15:16 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2017-10-06 07:01:11 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
[Support] Move LLD's parallel algorithm wrappers to support
Essentially takes the lld/Common/Threads.h wrappers and moves them to
the llvm/Support/Paralle.h algorithm header.
The changes are:
- Remove policy parameter, since all clients use `par`.
- Rename the methods to `parallelSort` etc to match LLVM style, since
they are no longer C++17 pstl compatible.
- Move algorithms from llvm::parallel:: to llvm::, since they have
"parallel" in the name and are no longer overloads of the regular
algorithms.
- Add range overloads
- Use the sequential algorithm directly when 1 thread is requested
(skips task grouping)
- Fix the index type of parallelForEachN to size_t. Nobody in LLVM was
using any other parameter, and it made overload resolution hard for
for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t.
Remove Threads.h and update LLD for that.
This is a prerequisite for parallel public symbol processing in the PDB
library, which is in LLVM.
Reviewed By: MaskRay, aganea
Differential Revision: https://reviews.llvm.org/D79390
2020-05-05 11:03:19 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2020-07-03 19:54:24 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-11-18 05:40:38 +08:00
|
|
|
#if LLVM_ON_UNIX
|
2017-11-08 07:01:37 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2017-11-08 07:09:54 +08:00
|
|
|
#include <thread>
|
2017-03-24 08:15:16 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
|
|
|
|
// Removes a given file asynchronously. This is a performance hack,
|
|
|
|
// so remove this when operating systems are improved.
|
|
|
|
//
|
|
|
|
// On Linux (and probably on other Unix-like systems), unlink(2) is a
|
|
|
|
// noticeably slow system call. As of 2016, unlink takes 250
|
|
|
|
// milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
|
|
|
|
//
|
|
|
|
// To create a new result file, we first remove existing file. So, if
|
|
|
|
// you repeatedly link a 1 GB program in a regular compile-link-debug
|
|
|
|
// cycle, every cycle wastes 250 milliseconds only to remove a file.
|
|
|
|
// Since LLD can link a 1 GB binary in about 5 seconds, that waste
|
|
|
|
// actually counts.
|
|
|
|
//
|
2017-11-08 07:01:37 +08:00
|
|
|
// This function spawns a background thread to remove the file.
|
2017-03-24 08:15:16 +08:00
|
|
|
// The calling thread returns almost immediately.
|
2019-03-12 00:30:55 +08:00
|
|
|
void lld::unlinkAsync(StringRef path) {
|
2020-07-13 18:58:30 +08:00
|
|
|
if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
|
|
|
|
return;
|
|
|
|
|
2017-11-08 07:01:37 +08:00
|
|
|
// Removing a file is async on windows.
|
2018-04-10 21:15:21 +08:00
|
|
|
#if defined(_WIN32)
|
2020-07-03 19:54:24 +08:00
|
|
|
// On Windows co-operative programs can be expected to open LLD's
|
|
|
|
// output in FILE_SHARE_DELETE mode. This allows us to delete the
|
|
|
|
// file (by moving it to a temporary filename and then deleting
|
|
|
|
// it) so that we can link another output file that overwrites
|
|
|
|
// the existing file, even if the current file is in use.
|
|
|
|
//
|
|
|
|
// This is done on a best effort basis - we do not error if the
|
|
|
|
// operation fails. The consequence is merely that the user
|
|
|
|
// experiences an inconvenient work-flow.
|
|
|
|
//
|
|
|
|
// The code here allows LLD to work on all versions of Windows.
|
|
|
|
// However, at Windows 10 1903 it seems that the behavior of
|
|
|
|
// Windows has changed, so that we could simply delete the output
|
|
|
|
// file. This code should be simplified once support for older
|
|
|
|
// versions of Windows is dropped.
|
|
|
|
//
|
|
|
|
// Warning: It seems that the WINVER and _WIN32_WINNT preprocessor
|
|
|
|
// defines affect the behavior of the Windows versions of the calls
|
|
|
|
// we are using here. If this code stops working this is worth
|
|
|
|
// bearing in mind.
|
|
|
|
SmallString<128> tmpName;
|
|
|
|
if (!sys::fs::createUniqueFile(path + "%%%%%%%%.tmp", tmpName)) {
|
|
|
|
if (!sys::fs::rename(path, tmpName))
|
|
|
|
path = tmpName;
|
|
|
|
else
|
|
|
|
sys::fs::remove(tmpName);
|
|
|
|
}
|
2019-07-11 14:00:40 +08:00
|
|
|
sys::fs::remove(path);
|
2017-11-08 07:01:37 +08:00
|
|
|
#else
|
2020-07-13 18:58:30 +08:00
|
|
|
if (parallel::strategy.ThreadsRequested == 1)
|
2017-03-24 08:15:16 +08:00
|
|
|
return;
|
|
|
|
|
2017-11-08 07:01:37 +08:00
|
|
|
// We cannot just remove path from a different thread because we are now going
|
|
|
|
// to create path as a new file.
|
|
|
|
// Instead we open the file and unlink it on this thread. The unlink is fast
|
|
|
|
// since the open fd guarantees that it is not removing the last reference.
|
|
|
|
int fd;
|
2017-11-08 12:22:40 +08:00
|
|
|
std::error_code ec = sys::fs::openFileForRead(path, fd);
|
2017-11-08 07:01:37 +08:00
|
|
|
sys::fs::remove(path);
|
|
|
|
|
2019-02-15 07:41:23 +08:00
|
|
|
if (ec)
|
|
|
|
return;
|
|
|
|
|
2017-11-08 07:01:37 +08:00
|
|
|
// close and therefore remove TempPath in background.
|
2019-02-15 07:41:23 +08:00
|
|
|
std::mutex m;
|
|
|
|
std::condition_variable cv;
|
|
|
|
bool started = false;
|
|
|
|
std::thread([&, fd] {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m);
|
|
|
|
started = true;
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
|
|
|
::close(fd);
|
|
|
|
}).detach();
|
|
|
|
|
|
|
|
// GLIBC 2.26 and earlier have race condition that crashes an entire process
|
|
|
|
// if the main thread calls exit(2) while other thread is starting up.
|
|
|
|
std::unique_lock<std::mutex> l(m);
|
|
|
|
cv.wait(l, [&] { return started; });
|
2017-11-08 07:01:37 +08:00
|
|
|
#endif
|
2017-03-24 08:15:16 +08:00
|
|
|
}
|
|
|
|
|
2017-04-27 00:14:46 +08:00
|
|
|
// Simulate file creation to see if Path is writable.
|
2017-03-24 08:15:16 +08:00
|
|
|
//
|
|
|
|
// Determining whether a file is writable or not is amazingly hard,
|
|
|
|
// and after all the only reliable way of doing that is to actually
|
|
|
|
// create a file. But we don't want to do that in this function
|
|
|
|
// because LLD shouldn't update any file if it will end in a failure.
|
2017-04-27 00:14:46 +08:00
|
|
|
// We also don't want to reimplement heuristics to determine if a
|
|
|
|
// file is writable. So we'll let FileOutputBuffer do the work.
|
2017-03-24 08:15:16 +08:00
|
|
|
//
|
2020-01-07 02:21:05 +08:00
|
|
|
// FileOutputBuffer doesn't touch a destination file until commit()
|
2017-03-24 08:15:16 +08:00
|
|
|
// is called. We use that class without calling commit() to predict
|
|
|
|
// if the given file is writable.
|
2019-03-12 00:30:55 +08:00
|
|
|
std::error_code lld::tryCreateFile(StringRef path) {
|
2017-04-27 00:15:07 +08:00
|
|
|
if (path.empty())
|
|
|
|
return std::error_code();
|
2017-11-09 07:07:32 +08:00
|
|
|
if (path == "-")
|
|
|
|
return std::error_code();
|
2017-11-08 09:05:52 +08:00
|
|
|
return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
|
2017-03-24 08:15:16 +08:00
|
|
|
}
|