InstrProf: Check pointer size in raw profile
[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/ProfileData/InstrProf.h"
20 #include "llvm/Support/LineIterator.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/Endian.h"
23
24 #include <iterator>
25
26 namespace llvm {
27
28 class InstrProfReader;
29
30 /// Profiling information for a single function.
31 struct InstrProfRecord {
32   StringRef Name;
33   uint64_t Hash;
34   ArrayRef<uint64_t> Counts;
35 };
36
37 /// A file format agnostic iterator over profiling data.
38 class InstrProfIterator : public std::iterator<std::input_iterator_tag,
39                                                InstrProfRecord> {
40   InstrProfReader *Reader;
41   InstrProfRecord Record;
42
43   void Increment();
44 public:
45   InstrProfIterator() : Reader(nullptr) {}
46   InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); }
47
48   InstrProfIterator &operator++() { Increment(); return *this; }
49   bool operator==(const InstrProfIterator &RHS) { return Reader == RHS.Reader; }
50   bool operator!=(const InstrProfIterator &RHS) { return Reader != RHS.Reader; }
51   InstrProfRecord &operator*() { return Record; }
52   InstrProfRecord *operator->() { return &Record; }
53 };
54
55 /// Base class and interface for reading profiling data of any known instrprof
56 /// format. Provides an iterator over InstrProfRecords.
57 class InstrProfReader {
58   error_code LastError;
59 public:
60   InstrProfReader() : LastError(instrprof_error::success) {}
61   virtual ~InstrProfReader() {}
62
63   /// Read the header.  Required before reading first record.
64   virtual error_code readHeader() = 0;
65   /// Read a single record.
66   virtual error_code readNextRecord(InstrProfRecord &Record) = 0;
67   /// Iterator over profile data.
68   InstrProfIterator begin() { return InstrProfIterator(this); }
69   InstrProfIterator end() { return InstrProfIterator(); }
70
71 protected:
72   /// Set the current error_code and return same.
73   error_code error(error_code EC) {
74     LastError = EC;
75     return EC;
76   }
77
78   /// Clear the current error code and return a successful one.
79   error_code success() { return error(instrprof_error::success); }
80
81 public:
82   /// Return true if the reader has finished reading the profile data.
83   bool isEOF() { return LastError == instrprof_error::eof; }
84   /// Return true if the reader encountered an error reading profiling data.
85   bool hasError() { return LastError && !isEOF(); }
86   /// Get the current error code.
87   error_code getError() { return LastError; }
88
89   /// Factory method to create an appropriately typed reader for the given
90   /// instrprof file.
91   static error_code create(std::string Path,
92                            std::unique_ptr<InstrProfReader> &Result);
93 };
94
95 /// Reader for the simple text based instrprof format.
96 ///
97 /// This format is a simple text format that's suitable for test data. Records
98 /// are separated by one or more blank lines, and record fields are separated by
99 /// new lines.
100 ///
101 /// Each record consists of a function name, a function hash, a number of
102 /// counters, and then each counter value, in that order.
103 class TextInstrProfReader : public InstrProfReader {
104 private:
105   /// The profile data file contents.
106   std::unique_ptr<MemoryBuffer> DataBuffer;
107   /// Iterator over the profile data.
108   line_iterator Line;
109   /// The current set of counter values.
110   std::vector<uint64_t> Counts;
111
112   TextInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
113   TextInstrProfReader &operator=(const TextInstrProfReader &)
114     LLVM_DELETED_FUNCTION;
115 public:
116   TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
117       : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, '#') {}
118
119   /// Read the header.
120   error_code readHeader() override { return success(); }
121   /// Read a single record.
122   error_code readNextRecord(InstrProfRecord &Record) override;
123 };
124
125 /// Reader for the raw instrprof binary format from runtime.
126 ///
127 /// This format is a raw memory dump of the instrumentation-baed profiling data
128 /// from the runtime.  It has no index.
129 ///
130 /// Templated on the unsigned type whose size matches pointers on the platform
131 /// that wrote the profile.
132 template <class IntPtrT>
133 class RawInstrProfReader : public InstrProfReader {
134 private:
135   /// The profile data file contents.
136   std::unique_ptr<MemoryBuffer> DataBuffer;
137   /// The current set of counter values.
138   std::vector<uint64_t> Counts;
139   struct ProfileData {
140     const uint32_t NameSize;
141     const uint32_t NumCounters;
142     const uint64_t FuncHash;
143     const IntPtrT NamePtr;
144     const IntPtrT CounterPtr;
145   };
146   struct RawHeader {
147     const uint64_t Magic;
148     const uint64_t Version;
149     const uint64_t DataSize;
150     const uint64_t CountersSize;
151     const uint64_t NamesSize;
152     const uint64_t CountersDelta;
153     const uint64_t NamesDelta;
154   };
155
156   bool ShouldSwapBytes;
157   uint64_t CountersDelta;
158   uint64_t NamesDelta;
159   const ProfileData *Data;
160   const ProfileData *DataEnd;
161   const uint64_t *CountersStart;
162   const char *NamesStart;
163
164   RawInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
165   RawInstrProfReader &operator=(const TextInstrProfReader &)
166     LLVM_DELETED_FUNCTION;
167 public:
168   RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
169       : DataBuffer(std::move(DataBuffer)) { }
170
171   static bool hasFormat(const MemoryBuffer &DataBuffer);
172   error_code readHeader() override;
173   error_code readNextRecord(InstrProfRecord &Record) override;
174
175 private:
176   error_code readHeader(const RawHeader &Header);
177   template <class IntT>
178   IntT swap(IntT Int) const {
179     return ShouldSwapBytes ? sys::SwapByteOrder(Int) : Int;
180   }
181   const uint64_t *getCounter(IntPtrT CounterPtr) const {
182     ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
183     return CountersStart + Offset;
184   }
185   const char *getName(IntPtrT NamePtr) const {
186     ptrdiff_t Offset = (swap(NamePtr) - NamesDelta) / sizeof(char);
187     return NamesStart + Offset;
188   }
189 };
190
191 typedef RawInstrProfReader<uint32_t> RawInstrProfReader32;
192 typedef RawInstrProfReader<uint64_t> RawInstrProfReader64;
193
194 } // end namespace llvm
195
196 #endif // LLVM_PROFILEDATA_INSTRPROF_READER_H_