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