LCOV - code coverage report
Current view: top level - lib/LTO - Caching.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 30 39 76.9 %
Date: 2017-09-14 15:23:50 Functions: 9 9 100.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/FileSystem.h"
      18             : #include "llvm/Support/MemoryBuffer.h"
      19             : #include "llvm/Support/Path.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : 
      22             : using namespace llvm;
      23             : using namespace llvm::lto;
      24             : 
      25          34 : Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
      26             :                                             AddBufferFn AddBuffer) {
      27          68 :   if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
      28           0 :     return errorCodeToError(EC);
      29             : 
      30        1148 :   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          96 :     SmallString<64> EntryPath;
      34         192 :     sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
      35             :     // First, see if we have a cache hit.
      36             :     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
      37          96 :         MemoryBuffer::getFile(EntryPath);
      38          48 :     if (MBOrErr) {
      39          24 :       AddBuffer(Task, std::move(*MBOrErr), EntryPath);
      40             :       return AddStreamFn();
      41             :     }
      42             : 
      43         132 :     if (MBOrErr.getError() != errc::no_such_file_or_directory)
      44           0 :       report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
      45           0 :                          ": " + 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             :       std::string TempFilename;
      52             :       std::string EntryPath;
      53             :       unsigned Task;
      54             : 
      55          44 :       CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
      56             :                   std::string TempFilename, std::string EntryPath,
      57             :                   unsigned Task)
      58         132 :           : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
      59          44 :             TempFilename(std::move(TempFilename)),
      60         308 :             EntryPath(std::move(EntryPath)), Task(Task) {}
      61             : 
      62         308 :       ~CacheStream() {
      63             :         // Make sure the file is closed before committing it.
      64          88 :         OS.reset();
      65             : 
      66             :         // Open the file first to avoid racing with a cache pruner.
      67             :         ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
      68         132 :             MemoryBuffer::getFile(TempFilename);
      69             : 
      70             :         // This is atomic on POSIX systems.
      71         132 :         if (auto EC = sys::fs::rename(TempFilename, EntryPath))
      72           0 :           report_fatal_error(Twine("Failed to rename temporary file ") +
      73           0 :                              TempFilename + ": " + EC.message() + "\n");
      74             : 
      75          44 :         if (!MBOrErr)
      76           0 :           report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
      77           0 :                              ": " + MBOrErr.getError().message() + "\n");
      78         264 :         AddBuffer(Task, std::move(*MBOrErr), EntryPath);
      79          88 :       }
      80             :     };
      81             : 
      82        1232 :     return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
      83             :       // Write to a temporary to avoid race condition
      84             :       int TempFD;
      85         176 :       SmallString<64> TempFilenameModel, TempFilename;
      86         176 :       sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
      87             :       std::error_code EC =
      88             :           sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename,
      89          88 :                                     sys::fs::owner_read | sys::fs::owner_write);
      90          44 :       if (EC) {
      91           0 :         errs() << "Error: " << EC.message() << "\n";
      92           0 :         report_fatal_error("ThinLTO: Can't get a temporary file");
      93             :       }
      94             : 
      95             :       // This CacheStream will move the temporary file into the cache when done.
      96         132 :       return llvm::make_unique<CacheStream>(
      97          88 :           llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true),
      98         264 :           AddBuffer, TempFilename.str(), EntryPath.str(), Task);
      99         132 :     };
     100         102 :   };
     101             : }

Generated by: LCOV version 1.13