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