InstrProf: Read raw binary profile in llvm-profdata
[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(DataBuffer_.release()), 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 class RawInstrProfReader : public InstrProfReader {
130 private:
131   /// The profile data file contents.
132   std::unique_ptr<MemoryBuffer> DataBuffer;
133   /// The current set of counter values.
134   std::vector<uint64_t> Counts;
135   struct ProfileData {
136     const uint32_t NameSize;
137     const uint32_t NumCounters;
138     const uint64_t FuncHash;
139     const uint64_t NamePtr;
140     const uint64_t 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
160   RawInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
161   RawInstrProfReader &operator=(const TextInstrProfReader &)
162     LLVM_DELETED_FUNCTION;
163 public:
164   RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer);
165
166   error_code readHeader() override;
167   error_code readNextRecord(InstrProfRecord &Record) override;
168
169 private:
170   error_code readHeader(const RawHeader &Header);
171   template <class IntT>
172   IntT swap(IntT Int) const {
173     return ShouldSwapBytes ? sys::SwapByteOrder(Int) : Int;
174   }
175   const uint64_t *getCounter(uint64_t CounterPtr) const {
176     ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
177     return CountersStart + Offset;
178   }
179   const char *getName(uint64_t NamePtr) const {
180     ptrdiff_t Offset = (swap(NamePtr) - NamesDelta) / sizeof(char);
181     return NamesStart + Offset;
182   }
183 };
184
185 } // end namespace llvm
186
187 #endif // LLVM_PROFILEDATA_INSTRPROF_READER_H__