Home | History | Annotate | Download | only in ProfileData
      1 //=-- InstrProfReader.h - Instrumented profiling readers ----------*- C++ -*-=//
      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 contains support for reading profiling data for instrumentation
     11 // based PGO and coverage.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_PROFILEDATA_INSTRPROF_READER_H_
     16 #define LLVM_PROFILEDATA_INSTRPROF_READER_H_
     17 
     18 #include "llvm/ADT/ArrayRef.h"
     19 #include "llvm/ADT/StringExtras.h"
     20 #include "llvm/ProfileData/InstrProf.h"
     21 #include "llvm/Support/LineIterator.h"
     22 #include "llvm/Support/MemoryBuffer.h"
     23 #include "llvm/Support/EndianStream.h"
     24 #include "llvm/Support/OnDiskHashTable.h"
     25 
     26 #include <iterator>
     27 
     28 namespace llvm {
     29 
     30 class InstrProfReader;
     31 
     32 /// Profiling information for a single function.
     33 struct InstrProfRecord {
     34   InstrProfRecord() {}
     35   InstrProfRecord(StringRef Name, uint64_t Hash, ArrayRef<uint64_t> Counts)
     36       : Name(Name), Hash(Hash), Counts(Counts) {}
     37   StringRef Name;
     38   uint64_t Hash;
     39   ArrayRef<uint64_t> Counts;
     40 };
     41 
     42 /// A file format agnostic iterator over profiling data.
     43 class InstrProfIterator : public std::iterator<std::input_iterator_tag,
     44                                                InstrProfRecord> {
     45   InstrProfReader *Reader;
     46   InstrProfRecord Record;
     47 
     48   void Increment();
     49 public:
     50   InstrProfIterator() : Reader(nullptr) {}
     51   InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); }
     52 
     53   InstrProfIterator &operator++() { Increment(); return *this; }
     54   bool operator==(const InstrProfIterator &RHS) { return Reader == RHS.Reader; }
     55   bool operator!=(const InstrProfIterator &RHS) { return Reader != RHS.Reader; }
     56   InstrProfRecord &operator*() { return Record; }
     57   InstrProfRecord *operator->() { return &Record; }
     58 };
     59 
     60 /// Base class and interface for reading profiling data of any known instrprof
     61 /// format. Provides an iterator over InstrProfRecords.
     62 class InstrProfReader {
     63   std::error_code LastError;
     64 
     65 public:
     66   InstrProfReader() : LastError(instrprof_error::success) {}
     67   virtual ~InstrProfReader() {}
     68 
     69   /// Read the header.  Required before reading first record.
     70   virtual std::error_code readHeader() = 0;
     71   /// Read a single record.
     72   virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0;
     73   /// Iterator over profile data.
     74   InstrProfIterator begin() { return InstrProfIterator(this); }
     75   InstrProfIterator end() { return InstrProfIterator(); }
     76 
     77 protected:
     78   /// Set the current std::error_code and return same.
     79   std::error_code error(std::error_code EC) {
     80     LastError = EC;
     81     return EC;
     82   }
     83 
     84   /// Clear the current error code and return a successful one.
     85   std::error_code success() { return error(instrprof_error::success); }
     86 
     87 public:
     88   /// Return true if the reader has finished reading the profile data.
     89   bool isEOF() { return LastError == instrprof_error::eof; }
     90   /// Return true if the reader encountered an error reading profiling data.
     91   bool hasError() { return LastError && !isEOF(); }
     92   /// Get the current error code.
     93   std::error_code getError() { return LastError; }
     94 
     95   /// Factory method to create an appropriately typed reader for the given
     96   /// instrprof file.
     97   static std::error_code create(std::string Path,
     98                                 std::unique_ptr<InstrProfReader> &Result);
     99 };
    100 
    101 /// Reader for the simple text based instrprof format.
    102 ///
    103 /// This format is a simple text format that's suitable for test data. Records
    104 /// are separated by one or more blank lines, and record fields are separated by
    105 /// new lines.
    106 ///
    107 /// Each record consists of a function name, a function hash, a number of
    108 /// counters, and then each counter value, in that order.
    109 class TextInstrProfReader : public InstrProfReader {
    110 private:
    111   /// The profile data file contents.
    112   std::unique_ptr<MemoryBuffer> DataBuffer;
    113   /// Iterator over the profile data.
    114   line_iterator Line;
    115   /// The current set of counter values.
    116   std::vector<uint64_t> Counts;
    117 
    118   TextInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
    119   TextInstrProfReader &operator=(const TextInstrProfReader &)
    120     LLVM_DELETED_FUNCTION;
    121 public:
    122   TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
    123       : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, '#') {}
    124 
    125   /// Read the header.
    126   std::error_code readHeader() override { return success(); }
    127   /// Read a single record.
    128   std::error_code readNextRecord(InstrProfRecord &Record) override;
    129 };
    130 
    131 /// Reader for the raw instrprof binary format from runtime.
    132 ///
    133 /// This format is a raw memory dump of the instrumentation-baed profiling data
    134 /// from the runtime.  It has no index.
    135 ///
    136 /// Templated on the unsigned type whose size matches pointers on the platform
    137 /// that wrote the profile.
    138 template <class IntPtrT>
    139 class RawInstrProfReader : public InstrProfReader {
    140 private:
    141   /// The profile data file contents.
    142   std::unique_ptr<MemoryBuffer> DataBuffer;
    143   /// The current set of counter values.
    144   std::vector<uint64_t> Counts;
    145   struct ProfileData {
    146     const uint32_t NameSize;
    147     const uint32_t NumCounters;
    148     const uint64_t FuncHash;
    149     const IntPtrT NamePtr;
    150     const IntPtrT CounterPtr;
    151   };
    152   struct RawHeader {
    153     const uint64_t Magic;
    154     const uint64_t Version;
    155     const uint64_t DataSize;
    156     const uint64_t CountersSize;
    157     const uint64_t NamesSize;
    158     const uint64_t CountersDelta;
    159     const uint64_t NamesDelta;
    160   };
    161 
    162   bool ShouldSwapBytes;
    163   uint64_t CountersDelta;
    164   uint64_t NamesDelta;
    165   const ProfileData *Data;
    166   const ProfileData *DataEnd;
    167   const uint64_t *CountersStart;
    168   const char *NamesStart;
    169   const char *ProfileEnd;
    170 
    171   RawInstrProfReader(const RawInstrProfReader &) LLVM_DELETED_FUNCTION;
    172   RawInstrProfReader &operator=(const RawInstrProfReader &)
    173     LLVM_DELETED_FUNCTION;
    174 public:
    175   RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
    176       : DataBuffer(std::move(DataBuffer)) { }
    177 
    178   static bool hasFormat(const MemoryBuffer &DataBuffer);
    179   std::error_code readHeader() override;
    180   std::error_code readNextRecord(InstrProfRecord &Record) override;
    181 
    182 private:
    183   std::error_code readNextHeader(const char *CurrentPos);
    184   std::error_code readHeader(const RawHeader &Header);
    185   template <class IntT>
    186   IntT swap(IntT Int) const {
    187     return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
    188   }
    189   const uint64_t *getCounter(IntPtrT CounterPtr) const {
    190     ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
    191     return CountersStart + Offset;
    192   }
    193   const char *getName(IntPtrT NamePtr) const {
    194     ptrdiff_t Offset = (swap(NamePtr) - NamesDelta) / sizeof(char);
    195     return NamesStart + Offset;
    196   }
    197 };
    198 
    199 typedef RawInstrProfReader<uint32_t> RawInstrProfReader32;
    200 typedef RawInstrProfReader<uint64_t> RawInstrProfReader64;
    201 
    202 namespace IndexedInstrProf {
    203 enum class HashT : uint32_t;
    204 }
    205 
    206 /// Trait for lookups into the on-disk hash table for the binary instrprof
    207 /// format.
    208 class InstrProfLookupTrait {
    209   std::vector<uint64_t> CountBuffer;
    210   IndexedInstrProf::HashT HashType;
    211 public:
    212   InstrProfLookupTrait(IndexedInstrProf::HashT HashType) : HashType(HashType) {}
    213 
    214   typedef InstrProfRecord data_type;
    215   typedef StringRef internal_key_type;
    216   typedef StringRef external_key_type;
    217   typedef uint64_t hash_value_type;
    218   typedef uint64_t offset_type;
    219 
    220   static bool EqualKey(StringRef A, StringRef B) { return A == B; }
    221   static StringRef GetInternalKey(StringRef K) { return K; }
    222 
    223   hash_value_type ComputeHash(StringRef K);
    224 
    225   static std::pair<offset_type, offset_type>
    226   ReadKeyDataLength(const unsigned char *&D) {
    227     using namespace support;
    228     offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D);
    229     offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D);
    230     return std::make_pair(KeyLen, DataLen);
    231   }
    232 
    233   StringRef ReadKey(const unsigned char *D, offset_type N) {
    234     return StringRef((const char *)D, N);
    235   }
    236 
    237   InstrProfRecord ReadData(StringRef K, const unsigned char *D, offset_type N) {
    238     if (N < 2 * sizeof(uint64_t) || N % sizeof(uint64_t)) {
    239       // The data is corrupt, don't try to read it.
    240       CountBuffer.clear();
    241       return InstrProfRecord("", 0, CountBuffer);
    242     }
    243 
    244     using namespace support;
    245 
    246     // The first stored value is the hash.
    247     uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
    248     // Each counter follows.
    249     unsigned NumCounters = N / sizeof(uint64_t) - 1;
    250     CountBuffer.clear();
    251     CountBuffer.reserve(NumCounters - 1);
    252     for (unsigned I = 0; I < NumCounters; ++I)
    253       CountBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
    254 
    255     return InstrProfRecord(K, Hash, CountBuffer);
    256   }
    257 };
    258 typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
    259     InstrProfReaderIndex;
    260 
    261 /// Reader for the indexed binary instrprof format.
    262 class IndexedInstrProfReader : public InstrProfReader {
    263 private:
    264   /// The profile data file contents.
    265   std::unique_ptr<MemoryBuffer> DataBuffer;
    266   /// The index into the profile data.
    267   std::unique_ptr<InstrProfReaderIndex> Index;
    268   /// Iterator over the profile data.
    269   InstrProfReaderIndex::data_iterator RecordIterator;
    270   /// The maximal execution count among all fucntions.
    271   uint64_t MaxFunctionCount;
    272 
    273   IndexedInstrProfReader(const IndexedInstrProfReader &) LLVM_DELETED_FUNCTION;
    274   IndexedInstrProfReader &operator=(const IndexedInstrProfReader &)
    275     LLVM_DELETED_FUNCTION;
    276 public:
    277   IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
    278       : DataBuffer(std::move(DataBuffer)), Index(nullptr),
    279         RecordIterator(InstrProfReaderIndex::data_iterator()) {}
    280 
    281   /// Return true if the given buffer is in an indexed instrprof format.
    282   static bool hasFormat(const MemoryBuffer &DataBuffer);
    283 
    284   /// Read the file header.
    285   std::error_code readHeader() override;
    286   /// Read a single record.
    287   std::error_code readNextRecord(InstrProfRecord &Record) override;
    288 
    289   /// Fill Counts with the profile data for the given function name.
    290   std::error_code getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
    291                                     std::vector<uint64_t> &Counts);
    292   /// Return the maximum of all known function counts.
    293   uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
    294 
    295   /// Factory method to create an indexed reader.
    296   static std::error_code
    297   create(std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result);
    298 };
    299 
    300 } // end namespace llvm
    301 
    302 #endif // LLVM_PROFILEDATA_INSTRPROF_READER_H_
    303