InstrProfReader.h 9.4 KB

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