1 //===- Filesystem.cpp -----------------------------------------------------===// 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 // 9 // This file contains a few utility functions to handle files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "lld/Common/Filesystem.h" 14 #include "lld/Common/Threads.h" 15 #include "llvm/Config/llvm-config.h" 16 #include "llvm/Support/FileOutputBuffer.h" 17 #include "llvm/Support/FileSystem.h" 18 #if LLVM_ON_UNIX 19 #include <unistd.h> 20 #endif 21 #include <thread> 22 23 using namespace llvm; 24 using namespace lld; 25 26 // Removes a given file asynchronously. This is a performance hack, 27 // so remove this when operating systems are improved. 28 // 29 // On Linux (and probably on other Unix-like systems), unlink(2) is a 30 // noticeably slow system call. As of 2016, unlink takes 250 31 // milliseconds to remove a 1 GB file on ext4 filesystem on my machine. 32 // 33 // To create a new result file, we first remove existing file. So, if 34 // you repeatedly link a 1 GB program in a regular compile-link-debug 35 // cycle, every cycle wastes 250 milliseconds only to remove a file. 36 // Since LLD can link a 1 GB binary in about 5 seconds, that waste 37 // actually counts. 38 // 39 // This function spawns a background thread to remove the file. 40 // The calling thread returns almost immediately. 41 void lld::unlinkAsync(StringRef path) { 42 // Removing a file is async on windows. 43 #if defined(_WIN32) 44 sys::fs::remove(path); 45 #else 46 if (!threadsEnabled || !sys::fs::exists(path) || 47 !sys::fs::is_regular_file(path)) 48 return; 49 50 // We cannot just remove path from a different thread because we are now going 51 // to create path as a new file. 52 // Instead we open the file and unlink it on this thread. The unlink is fast 53 // since the open fd guarantees that it is not removing the last reference. 54 int fd; 55 std::error_code ec = sys::fs::openFileForRead(path, fd); 56 sys::fs::remove(path); 57 58 if (ec) 59 return; 60 61 // close and therefore remove TempPath in background. 62 std::mutex m; 63 std::condition_variable cv; 64 bool started = false; 65 std::thread([&, fd] { 66 { 67 std::lock_guard<std::mutex> l(m); 68 started = true; 69 cv.notify_all(); 70 } 71 ::close(fd); 72 }).detach(); 73 74 // GLIBC 2.26 and earlier have race condition that crashes an entire process 75 // if the main thread calls exit(2) while other thread is starting up. 76 std::unique_lock<std::mutex> l(m); 77 cv.wait(l, [&] { return started; }); 78 #endif 79 } 80 81 // Simulate file creation to see if Path is writable. 82 // 83 // Determining whether a file is writable or not is amazingly hard, 84 // and after all the only reliable way of doing that is to actually 85 // create a file. But we don't want to do that in this function 86 // because LLD shouldn't update any file if it will end in a failure. 87 // We also don't want to reimplement heuristics to determine if a 88 // file is writable. So we'll let FileOutputBuffer do the work. 89 // 90 // FileOutputBuffer doesn't touch a desitnation file until commit() 91 // is called. We use that class without calling commit() to predict 92 // if the given file is writable. 93 std::error_code lld::tryCreateFile(StringRef path) { 94 if (path.empty()) 95 return std::error_code(); 96 if (path == "-") 97 return std::error_code(); 98 return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError()); 99 } 100