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-02-23 05:02:05 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          45 : Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
      26             :                                             AddBufferFn AddBuffer) {
      27          45 :   if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
      28           0 :     return errorCodeToError(EC);
      29             : 
      30         699 :   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         128 :     sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
      35             :     // First, see if we have a cache hit.
      36             :     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
      37          64 :         MemoryBuffer::getFile(EntryPath);
      38          64 :     if (MBOrErr) {
      39          14 :       AddBuffer(Task, std::move(*MBOrErr));
      40             :       return AddStreamFn();
      41             :     }
      42             : 
      43             :     if (MBOrErr.getError() != errc::no_such_file_or_directory)
      44           0 :       report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
      45             :                          ": " + MBOrErr.getError().message() + "\n");
      46             : 
      47             :     // This native object stream is responsible for commiting the resulting
      48             :     // file to the cache and calling AddBuffer to add it to the link.
      49             :     struct CacheStream : NativeObjectStream {
      50             :       AddBufferFn AddBuffer;
      51             :       sys::fs::TempFile TempFile;
      52             :       std::string EntryPath;
      53             :       unsigned Task;
      54             : 
      55          50 :       CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
      56             :                   sys::fs::TempFile TempFile, std::string EntryPath,
      57             :                   unsigned Task)
      58          50 :           : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
      59             :             TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)),
      60         150 :             Task(Task) {}
      61             : 
      62         200 :       ~CacheStream() {
      63             :         // Make sure the stream is closed before committing it.
      64             :         OS.reset();
      65             : 
      66             :         // Open the file first to avoid racing with a cache pruner.
      67             :         ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
      68             :             MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName,
      69             :                                       /*FileSize*/ -1,
      70         100 :                                       /*RequiresNullTerminator*/ false);
      71          50 :         if (!MBOrErr)
      72           0 :           report_fatal_error(Twine("Failed to open new cache file ") +
      73             :                              TempFile.TmpName + ": " +
      74             :                              MBOrErr.getError().message() + "\n");
      75             : 
      76             :         // On POSIX systems, this will atomically replace the destination if
      77             :         // it already exists. We try to emulate this on Windows, but this may
      78             :         // fail with a permission denied error (for example, if the destination
      79             :         // is currently opened by another process that does not give us the
      80             :         // sharing permissions we need). Since the existing file should be
      81             :         // semantically equivalent to the one we are trying to write, we give
      82             :         // AddBuffer a copy of the bytes we wrote in that case. We do this
      83             :         // instead of just using the existing file, because the pruner might
      84             :         // delete the file before we get a chance to use it.
      85         100 :         Error E = TempFile.keep(EntryPath);
      86         200 :         E = handleErrors(std::move(E), [&](const ECError &E) -> Error {
      87           0 :           std::error_code EC = E.convertToErrorCode();
      88             :           if (EC != errc::permission_denied)
      89           0 :             return errorCodeToError(EC);
      90             : 
      91           0 :           auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(),
      92           0 :                                                        EntryPath);
      93           0 :           MBOrErr = std::move(MBCopy);
      94             : 
      95             :           // FIXME: should we consume the discard error?
      96           0 :           consumeError(TempFile.discard());
      97             : 
      98             :           return Error::success();
      99             :         });
     100             : 
     101          50 :         if (E)
     102           0 :           report_fatal_error(Twine("Failed to rename temporary file ") +
     103             :                              TempFile.TmpName + " to " + EntryPath + ": " +
     104           0 :                              toString(std::move(E)) + "\n");
     105             : 
     106         100 :         AddBuffer(Task, std::move(*MBOrErr));
     107         100 :       }
     108             :     };
     109             : 
     110         950 :     return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
     111             :       // Write to a temporary to avoid race condition
     112             :       SmallString<64> TempFilenameModel;
     113         100 :       sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
     114             :       Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create(
     115         100 :           TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write);
     116          50 :       if (!Temp) {
     117           0 :         errs() << "Error: " << toString(Temp.takeError()) << "\n";
     118           0 :         report_fatal_error("ThinLTO: Can't get a temporary file");
     119             :       }
     120             : 
     121             :       // This CacheStream will move the temporary file into the cache when done.
     122         150 :       return llvm::make_unique<CacheStream>(
     123         100 :           llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false),
     124         150 :           AddBuffer, std::move(*Temp), EntryPath.str(), Task);
     125         100 :     };
     126         135 :   };
     127             : }

Generated by: LCOV version 1.13