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"
|
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) {
|
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)
|
2019-07-11 14:00:40 +08:00
|
|
|
sys::fs::remove(path);
|
2017-11-08 07:01:37 +08:00
|
|
|
#else
|
[lld][COFF][ELF][WebAssembly] Replace --[no-]threads /threads[:no] with --threads={1,2,...} /threads:{1,2,...}
--no-threads is a name copied from gold.
gold has --no-thread, --thread-count and several other --thread-count-*.
There are needs to customize the number of threads (running several lld
processes concurrently or customizing the number of LTO threads).
Having a single --threads=N is a straightforward replacement of gold's
--no-threads + --thread-count.
--no-threads is used rarely. So just delete --no-threads instead of
keeping it for compatibility for a while.
If --threads= is specified (ELF,wasm; COFF /threads: is similar),
--thinlto-jobs= defaults to --threads=,
otherwise all available hardware threads are used.
There is currently no way to override a --threads={1,2,...}. It is still
a debate whether we should use --threads=all.
Reviewed By: rnk, aganea
Differential Revision: https://reviews.llvm.org/D76885
2020-03-18 03:40:19 +08:00
|
|
|
if (parallel::strategy.ThreadsRequested == 1 || !sys::fs::exists(path) ||
|
2017-11-07 10:00:51 +08:00
|
|
|
!sys::fs::is_regular_file(path))
|
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
|
|
|
}
|