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