LCOV - code coverage report
Current view: top level - lib/LTO - Caching.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 27 40 67.5 %
Date: 2018-06-17 00:07:59 Functions: 9 10 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-Caching.cpp - LLVM Link Time Optimizer Cache Handling ---------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the Caching for ThinLTO.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/LTO/Caching.h"
      15             : #include "llvm/ADT/StringExtras.h"
      16             : #include "llvm/Support/Errc.h"
      17             : #include "llvm/Support/MemoryBuffer.h"
      18             : #include "llvm/Support/Path.h"
      19             : #include "llvm/Support/Process.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : 
      22             : using namespace llvm;
      23             : using namespace llvm::lto;
      24             : 
      25          51 : Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
      26             :                                             AddBufferFn AddBuffer) {
      27          51 :   if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
      28           0 :     return errorCodeToError(EC);
      29             : 
      30         797 :   return [=](unsigned Task, StringRef Key) -> AddStreamFn {
      31             :     // This choice of file name allows the cache to be pruned (see pruneCache()
      32             :     // in include/llvm/Support/CachePruning.h).
      33             :     SmallString<64> EntryPath;
      34         148 :     sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
      35             :     // First, see if we have a cache hit.
      36             :     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
      37          74 :         MemoryBuffer::getFile(EntryPath);
      38          74 :     if (MBOrErr) {
      39          22 :       AddBuffer(Task, std::move(*MBOrErr));
      40             :       return AddStreamFn();
      41             :     }
      42             : 
      43             :     // On Windows we can fail to open a cache file with a permission denied
      44             :     // error. This generally means that another process has requested to delete
      45             :     // the file while it is still open, but it could also mean that another
      46             :     // process has opened the file without the sharing permissions we need.
      47             :     // Since the file is probably being deleted we handle it in the same way as
      48             :     // if the file did not exist at all.
      49             :     if (MBOrErr.getError() != errc::no_such_file_or_directory &&
      50             :         MBOrErr.getError() != errc::permission_denied)
      51           0 :       report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
      52             :                          ": " + MBOrErr.getError().message() + "\n");
      53             : 
      54             :     // This native object stream is responsible for commiting the resulting
      55             :     // file to the cache and calling AddBuffer to add it to the link.
      56             :     struct CacheStream : NativeObjectStream {
      57             :       AddBufferFn AddBuffer;
      58             :       sys::fs::TempFile TempFile;
      59             :       std::string EntryPath;
      60             :       unsigned Task;
      61             : 
      62          52 :       CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
      63             :                   sys::fs::TempFile TempFile, std::string EntryPath,
      64             :                   unsigned Task)
      65          52 :           : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
      66             :             TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)),
      67         156 :             Task(Task) {}
      68             : 
      69         208 :       ~CacheStream() {
      70             :         // Make sure the stream is closed before committing it.
      71             :         OS.reset();
      72             : 
      73             :         // Open the file first to avoid racing with a cache pruner.
      74             :         ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
      75             :             MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName,
      76             :                                       /*FileSize*/ -1,
      77         104 :                                       /*RequiresNullTerminator*/ false);
      78          52 :         if (!MBOrErr)
      79           0 :           report_fatal_error(Twine("Failed to open new cache file ") +
      80             :                              TempFile.TmpName + ": " +
      81             :                              MBOrErr.getError().message() + "\n");
      82             : 
      83             :         // On POSIX systems, this will atomically replace the destination if
      84             :         // it already exists. We try to emulate this on Windows, but this may
      85             :         // fail with a permission denied error (for example, if the destination
      86             :         // is currently opened by another process that does not give us the
      87             :         // sharing permissions we need). Since the existing file should be
      88             :         // semantically equivalent to the one we are trying to write, we give
      89             :         // AddBuffer a copy of the bytes we wrote in that case. We do this
      90             :         // instead of just using the existing file, because the pruner might
      91             :         // delete the file before we get a chance to use it.
      92         104 :         Error E = TempFile.keep(EntryPath);
      93         208 :         E = handleErrors(std::move(E), [&](const ECError &E) -> Error {
      94           0 :           std::error_code EC = E.convertToErrorCode();
      95             :           if (EC != errc::permission_denied)
      96           0 :             return errorCodeToError(EC);
      97             : 
      98           0 :           auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(),
      99           0 :                                                        EntryPath);
     100           0 :           MBOrErr = std::move(MBCopy);
     101             : 
     102             :           // FIXME: should we consume the discard error?
     103           0 :           consumeError(TempFile.discard());
     104             : 
     105             :           return Error::success();
     106             :         });
     107             : 
     108          52 :         if (E)
     109           0 :           report_fatal_error(Twine("Failed to rename temporary file ") +
     110             :                              TempFile.TmpName + " to " + EntryPath + ": " +
     111           0 :                              toString(std::move(E)) + "\n");
     112             : 
     113         104 :         AddBuffer(Task, std::move(*MBOrErr));
     114         104 :       }
     115             :     };
     116             : 
     117         988 :     return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
     118             :       // Write to a temporary to avoid race condition
     119             :       SmallString<64> TempFilenameModel;
     120         104 :       sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
     121             :       Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create(
     122         104 :           TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write);
     123          52 :       if (!Temp) {
     124           0 :         errs() << "Error: " << toString(Temp.takeError()) << "\n";
     125           0 :         report_fatal_error("ThinLTO: Can't get a temporary file");
     126             :       }
     127             : 
     128             :       // This CacheStream will move the temporary file into the cache when done.
     129         156 :       return llvm::make_unique<CacheStream>(
     130         104 :           llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false),
     131         156 :           AddBuffer, std::move(*Temp), EntryPath.str(), Task);
     132         104 :     };
     133         153 :   };
     134             : }

Generated by: LCOV version 1.13