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_INSTRPROFREADER_H
16 #define LLVM_PROFILEDATA_INSTRPROFREADER_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ProfileData/InstrProf.h"
21 #include "llvm/Support/EndianStream.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/LineIterator.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/OnDiskHashTable.h"
26 #include <iterator>
27 
28 namespace llvm {
29 
30 class InstrProfReader;
31 
32 /// Profiling information for a single function.
33 struct InstrProfRecord {
InstrProfRecordInstrProfRecord34   InstrProfRecord() {}
InstrProfRecordInstrProfRecord35   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:
InstrProfIterator()50   InstrProfIterator() : Reader(nullptr) {}
InstrProfIterator(InstrProfReader * Reader)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:
InstrProfReader()66   InstrProfReader() : LastError(instrprof_error::success) {}
~InstrProfReader()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.
begin()74   InstrProfIterator begin() { return InstrProfIterator(this); }
end()75   InstrProfIterator end() { return InstrProfIterator(); }
76 
77 protected:
78   /// Set the current std::error_code and return same.
error(std::error_code EC)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.
success()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.
isEOF()89   bool isEOF() { return LastError == instrprof_error::eof; }
90   /// Return true if the reader encountered an error reading profiling data.
hasError()91   bool hasError() { return LastError && !isEOF(); }
92   /// Get the current error code.
getError()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 ErrorOr<std::unique_ptr<InstrProfReader>> create(std::string Path);
98 
99   static ErrorOr<std::unique_ptr<InstrProfReader>>
100   create(std::unique_ptr<MemoryBuffer> Buffer);
101 };
102 
103 /// Reader for the simple text based instrprof format.
104 ///
105 /// This format is a simple text format that's suitable for test data. Records
106 /// are separated by one or more blank lines, and record fields are separated by
107 /// new lines.
108 ///
109 /// Each record consists of a function name, a function hash, a number of
110 /// counters, and then each counter value, in that order.
111 class TextInstrProfReader : public InstrProfReader {
112 private:
113   /// The profile data file contents.
114   std::unique_ptr<MemoryBuffer> DataBuffer;
115   /// Iterator over the profile data.
116   line_iterator Line;
117   /// The current set of counter values.
118   std::vector<uint64_t> Counts;
119 
120   TextInstrProfReader(const TextInstrProfReader &) = delete;
121   TextInstrProfReader &operator=(const TextInstrProfReader &) = delete;
122 public:
TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)123   TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
124       : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
125 
126   /// Read the header.
readHeader()127   std::error_code readHeader() override { return success(); }
128   /// Read a single record.
129   std::error_code readNextRecord(InstrProfRecord &Record) override;
130 };
131 
132 /// Reader for the raw instrprof binary format from runtime.
133 ///
134 /// This format is a raw memory dump of the instrumentation-baed profiling data
135 /// from the runtime.  It has no index.
136 ///
137 /// Templated on the unsigned type whose size matches pointers on the platform
138 /// that wrote the profile.
139 template <class IntPtrT>
140 class RawInstrProfReader : public InstrProfReader {
141 private:
142   /// The profile data file contents.
143   std::unique_ptr<MemoryBuffer> DataBuffer;
144   /// The current set of counter values.
145   std::vector<uint64_t> Counts;
146   struct ProfileData {
147     const uint32_t NameSize;
148     const uint32_t NumCounters;
149     const uint64_t FuncHash;
150     const IntPtrT NamePtr;
151     const IntPtrT CounterPtr;
152   };
153   struct RawHeader {
154     const uint64_t Magic;
155     const uint64_t Version;
156     const uint64_t DataSize;
157     const uint64_t CountersSize;
158     const uint64_t NamesSize;
159     const uint64_t CountersDelta;
160     const uint64_t NamesDelta;
161   };
162 
163   bool ShouldSwapBytes;
164   uint64_t CountersDelta;
165   uint64_t NamesDelta;
166   const ProfileData *Data;
167   const ProfileData *DataEnd;
168   const uint64_t *CountersStart;
169   const char *NamesStart;
170   const char *ProfileEnd;
171 
172   RawInstrProfReader(const RawInstrProfReader &) = delete;
173   RawInstrProfReader &operator=(const RawInstrProfReader &) = delete;
174 public:
RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)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>
swap(IntT Int)186   IntT swap(IntT Int) const {
187     return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
188   }
getCounter(IntPtrT CounterPtr)189   const uint64_t *getCounter(IntPtrT CounterPtr) const {
190     ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
191     return CountersStart + Offset;
192   }
getName(IntPtrT NamePtr)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> DataBuffer;
210   IndexedInstrProf::HashT HashType;
211 public:
InstrProfLookupTrait(IndexedInstrProf::HashT HashType)212   InstrProfLookupTrait(IndexedInstrProf::HashT HashType) : HashType(HashType) {}
213 
214   struct data_type {
data_typedata_type215     data_type(StringRef Name, ArrayRef<uint64_t> Data)
216         : Name(Name), Data(Data) {}
217     StringRef Name;
218     ArrayRef<uint64_t> Data;
219   };
220   typedef StringRef internal_key_type;
221   typedef StringRef external_key_type;
222   typedef uint64_t hash_value_type;
223   typedef uint64_t offset_type;
224 
EqualKey(StringRef A,StringRef B)225   static bool EqualKey(StringRef A, StringRef B) { return A == B; }
GetInternalKey(StringRef K)226   static StringRef GetInternalKey(StringRef K) { return K; }
227 
228   hash_value_type ComputeHash(StringRef K);
229 
230   static std::pair<offset_type, offset_type>
ReadKeyDataLength(const unsigned char * & D)231   ReadKeyDataLength(const unsigned char *&D) {
232     using namespace support;
233     offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D);
234     offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D);
235     return std::make_pair(KeyLen, DataLen);
236   }
237 
ReadKey(const unsigned char * D,offset_type N)238   StringRef ReadKey(const unsigned char *D, offset_type N) {
239     return StringRef((const char *)D, N);
240   }
241 
ReadData(StringRef K,const unsigned char * D,offset_type N)242   data_type ReadData(StringRef K, const unsigned char *D, offset_type N) {
243     DataBuffer.clear();
244     if (N % sizeof(uint64_t))
245       // The data is corrupt, don't try to read it.
246       return data_type("", DataBuffer);
247 
248     using namespace support;
249     // We just treat the data as opaque here. It's simpler to handle in
250     // IndexedInstrProfReader.
251     unsigned NumEntries = N / sizeof(uint64_t);
252     DataBuffer.reserve(NumEntries);
253     for (unsigned I = 0; I < NumEntries; ++I)
254       DataBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
255     return data_type(K, DataBuffer);
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   /// Offset into our current data set.
271   size_t CurrentOffset;
272   /// The file format version of the profile data.
273   uint64_t FormatVersion;
274   /// The maximal execution count among all functions.
275   uint64_t MaxFunctionCount;
276 
277   IndexedInstrProfReader(const IndexedInstrProfReader &) = delete;
278   IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete;
279 public:
IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)280   IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
281       : DataBuffer(std::move(DataBuffer)), Index(nullptr), CurrentOffset(0) {}
282 
283   /// Return true if the given buffer is in an indexed instrprof format.
284   static bool hasFormat(const MemoryBuffer &DataBuffer);
285 
286   /// Read the file header.
287   std::error_code readHeader() override;
288   /// Read a single record.
289   std::error_code readNextRecord(InstrProfRecord &Record) override;
290 
291   /// Fill Counts with the profile data for the given function name.
292   std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
293                                     std::vector<uint64_t> &Counts);
294   /// Return the maximum of all known function counts.
getMaximumFunctionCount()295   uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
296 
297   /// Factory method to create an indexed reader.
298   static ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
299   create(std::string Path);
300 
301   static ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
302   create(std::unique_ptr<MemoryBuffer> Buffer);
303 };
304 
305 } // end namespace llvm
306 
307 #endif
308