[lld] r298667 - Move a few functions to a new file Filesystem.{cpp, h}.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 23 17:15:17 PDT 2017


Author: ruiu
Date: Thu Mar 23 19:15:16 2017
New Revision: 298667

URL: http://llvm.org/viewvc/llvm-project?rev=298667&view=rev
Log:
Move a few functions to a new file Filesystem.{cpp,h}.

Added:
    lld/trunk/ELF/Filesystem.cpp
    lld/trunk/ELF/Filesystem.h
Modified:
    lld/trunk/ELF/CMakeLists.txt
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/CMakeLists.txt?rev=298667&r1=298666&r2=298667&view=diff
==============================================================================
--- lld/trunk/ELF/CMakeLists.txt (original)
+++ lld/trunk/ELF/CMakeLists.txt Thu Mar 23 19:15:16 2017
@@ -11,6 +11,7 @@ add_lld_library(lldELF
   DriverUtils.cpp
   EhFrame.cpp
   Error.cpp
+  Filesystem.cpp
   GdbIndex.cpp
   ICF.cpp
   InputFiles.cpp

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=298667&r1=298666&r2=298667&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Thu Mar 23 19:15:16 2017
@@ -26,6 +26,7 @@
 #include "Driver.h"
 #include "Config.h"
 #include "Error.h"
+#include "Filesystem.h"
 #include "ICF.h"
 #include "InputFiles.h"
 #include "InputSection.h"
@@ -43,7 +44,6 @@
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Object/Decompressor.h"
 #include "llvm/Support/CommandLine.h"
-#include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TarWriter.h"
 #include "llvm/Support/TargetSelect.h"
@@ -846,26 +846,6 @@ static uint64_t getImageBase(opt::InputA
   return V;
 }
 
-// Returns true if a given file seems to be writable.
-//
-// 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.
-// We also don't want to reimplement heuristics. So we'll let
-// FileOutputBuffer do the work.
-//
-// FileOutputBuffer doesn't touch a desitnation file until commit()
-// is called. We use that class without calling commit() to predict
-// if the given file is writable.
-static bool isWritable(StringRef Path) {
-  if (auto EC = FileOutputBuffer::create(Path, 1).getError()) {
-    error("cannot open output file " + Path + ": " + EC.message());
-    return false;
-  }
-  return true;
-}
-
 // Do actual linking. Note that when this function is called,
 // all linker scripts have already been parsed.
 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
@@ -883,7 +863,7 @@ template <class ELFT> void LinkerDriver:
   // Fail early if the output file is not writable. If a user has a long link,
   // e.g. due to a large LTO link, they do not wish to run it and find that it
   // failed because there was a mistake in their command-line.
-  if (!isWritable(Config->OutputFile))
+  if (!isFileWritable(Config->OutputFile))
     return;
 
   // Use default entry point name if no name was given via the command

Added: lld/trunk/ELF/Filesystem.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Filesystem.cpp?rev=298667&view=auto
==============================================================================
--- lld/trunk/ELF/Filesystem.cpp (added)
+++ lld/trunk/ELF/Filesystem.cpp Thu Mar 23 19:15:16 2017
@@ -0,0 +1,79 @@
+//===- Filesystem.cpp -----------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains a few utility functions to handle files.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Filesystem.h"
+#include "Config.h"
+#include "Error.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include <thread>
+
+using namespace llvm;
+
+using namespace lld;
+using namespace lld::elf;
+
+// 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.
+//
+// This function spawns a background thread to call unlink.
+// The calling thread returns almost immediately.
+void elf::unlinkAsync(StringRef Path) {
+  if (!Config->Threads || !sys::fs::exists(Config->OutputFile))
+    return;
+
+  // First, rename Path to avoid race condition. We cannot remove
+  // Path from a different thread because we are now going to create
+  // Path as a new file. If we do that in a different thread, the new
+  // thread can remove the new file.
+  SmallString<128> TempPath;
+  if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath))
+    return;
+  if (sys::fs::rename(Path, TempPath)) {
+    sys::fs::remove(TempPath);
+    return;
+  }
+
+  // Remove TempPath in background.
+  std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach();
+}
+
+// Returns true if a given file seems to be writable.
+//
+// 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.
+// We also don't want to reimplement heuristics. So we'll let
+// FileOutputBuffer do the work.
+//
+// FileOutputBuffer doesn't touch a desitnation file until commit()
+// is called. We use that class without calling commit() to predict
+// if the given file is writable.
+bool elf::isFileWritable(StringRef Path) {
+  if (auto EC = FileOutputBuffer::create(Path, 1).getError()) {
+    error("cannot open output file " + Path + ": " + EC.message());
+    return false;
+  }
+  return true;
+}

Added: lld/trunk/ELF/Filesystem.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Filesystem.h?rev=298667&view=auto
==============================================================================
--- lld/trunk/ELF/Filesystem.h (added)
+++ lld/trunk/ELF/Filesystem.h Thu Mar 23 19:15:16 2017
@@ -0,0 +1,22 @@
+//===- Filesystem.h ---------------------------------------------*- C++ -*-===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_ELF_FILESYSTEM_H
+#define LLD_ELF_FILESYSTEM_H
+
+#include "lld/Core/LLVM.h"
+
+namespace lld {
+namespace elf {
+void unlinkAsync(StringRef Path);
+bool isFileWritable(StringRef Path);
+}
+}
+
+#endif

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=298667&r1=298666&r2=298667&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Mar 23 19:15:16 2017
@@ -9,6 +9,7 @@
 
 #include "Writer.h"
 #include "Config.h"
+#include "Filesystem.h"
 #include "LinkerScript.h"
 #include "MapFile.h"
 #include "Memory.h"
@@ -21,10 +22,8 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/FileOutputBuffer.h"
-#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 #include <climits>
-#include <thread>
 
 using namespace llvm;
 using namespace llvm::ELF;
@@ -1747,41 +1746,6 @@ template <class ELFT> void Writer<ELFT>:
     Sec->writeHeaderTo<ELFT>(++SHdrs);
 }
 
-// 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.
-//
-// This function spawns a background thread to call unlink.
-// The calling thread returns almost immediately.
-static void unlinkAsync(StringRef Path) {
-  if (!Config->Threads || !sys::fs::exists(Config->OutputFile))
-    return;
-
-  // First, rename Path to avoid race condition. We cannot remove
-  // Path from a different thread because we are now going to create
-  // Path as a new file. If we do that in a different thread, the new
-  // thread can remove the new file.
-  SmallString<128> TempPath;
-  if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath))
-    return;
-  if (sys::fs::rename(Path, TempPath)) {
-    sys::fs::remove(TempPath);
-    return;
-  }
-
-  // Remove TempPath in background.
-  std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach();
-}
-
 // Open a result file.
 template <class ELFT> void Writer<ELFT>::openFile() {
   unlinkAsync(Config->OutputFile);




More information about the llvm-commits mailing list