1 //=-- InstrProfReader.h - Instrumented profiling readers ----------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading profiling data for instrumentation
11 // based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_INSTRPROF_READER_H_
16 #define LLVM_PROFILEDATA_INSTRPROF_READER_H_
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"
30 class InstrProfReader;
32 /// Profiling information for a single function.
33 struct InstrProfRecord {
35 InstrProfRecord(StringRef Name, uint64_t Hash, ArrayRef<uint64_t> Counts)
36 : Name(Name), Hash(Hash), Counts(Counts) {}
39 ArrayRef<uint64_t> Counts;
42 /// A file format agnostic iterator over profiling data.
43 class InstrProfIterator : public std::iterator<std::input_iterator_tag,
45 InstrProfReader *Reader;
46 InstrProfRecord Record;
50 InstrProfIterator() : Reader(nullptr) {}
51 InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); }
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; }
60 /// Base class and interface for reading profiling data of any known instrprof
61 /// format. Provides an iterator over InstrProfRecords.
62 class InstrProfReader {
65 InstrProfReader() : LastError(instrprof_error::success) {}
66 virtual ~InstrProfReader() {}
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(); }
77 /// Set the current error_code and return same.
78 error_code error(error_code EC) {
83 /// Clear the current error code and return a successful one.
84 error_code success() { return error(instrprof_error::success); }
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; }
94 /// Factory method to create an appropriately typed reader for the given
96 static error_code create(std::string Path,
97 std::unique_ptr<InstrProfReader> &Result);
100 /// Reader for the simple text based instrprof format.
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
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 {
110 /// The profile data file contents.
111 std::unique_ptr<MemoryBuffer> DataBuffer;
112 /// Iterator over the profile data.
114 /// The current set of counter values.
115 std::vector<uint64_t> Counts;
117 TextInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
118 TextInstrProfReader &operator=(const TextInstrProfReader &)
119 LLVM_DELETED_FUNCTION;
121 TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
122 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, '#') {}
125 error_code readHeader() override { return success(); }
126 /// Read a single record.
127 error_code readNextRecord(InstrProfRecord &Record) override;
130 /// Reader for the raw instrprof binary format from runtime.
132 /// This format is a raw memory dump of the instrumentation-baed profiling data
133 /// from the runtime. It has no index.
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 {
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;
145 const uint32_t NameSize;
146 const uint32_t NumCounters;
147 const uint64_t FuncHash;
148 const IntPtrT NamePtr;
149 const IntPtrT CounterPtr;
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;
161 bool ShouldSwapBytes;
162 uint64_t CountersDelta;
164 const ProfileData *Data;
165 const ProfileData *DataEnd;
166 const uint64_t *CountersStart;
167 const char *NamesStart;
168 const char *ProfileEnd;
170 RawInstrProfReader(const TextInstrProfReader &) LLVM_DELETED_FUNCTION;
171 RawInstrProfReader &operator=(const TextInstrProfReader &)
172 LLVM_DELETED_FUNCTION;
174 RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
175 : DataBuffer(std::move(DataBuffer)) { }
177 static bool hasFormat(const MemoryBuffer &DataBuffer);
178 error_code readHeader() override;
179 error_code readNextRecord(InstrProfRecord &Record) override;
182 error_code readNextHeader(const char *CurrentPos);
183 error_code readHeader(const RawHeader &Header);
184 template <class IntT>
185 IntT swap(IntT Int) const {
186 return ShouldSwapBytes ? sys::SwapByteOrder(Int) : Int;
188 const uint64_t *getCounter(IntPtrT CounterPtr) const {
189 ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
190 return CountersStart + Offset;
192 const char *getName(IntPtrT NamePtr) const {
193 ptrdiff_t Offset = (swap(NamePtr) - NamesDelta) / sizeof(char);
194 return NamesStart + Offset;
198 typedef RawInstrProfReader<uint32_t> RawInstrProfReader32;
199 typedef RawInstrProfReader<uint64_t> RawInstrProfReader64;
201 namespace IndexedInstrProf {
202 enum class HashT : uint32_t;
205 /// Trait for lookups into the on-disk hash table for the binary instrprof
207 class InstrProfLookupTrait {
208 std::vector<uint64_t> CountBuffer;
209 IndexedInstrProf::HashT HashType;
211 InstrProfLookupTrait(IndexedInstrProf::HashT HashType) : HashType(HashType) {}
213 typedef InstrProfRecord data_type;
214 typedef StringRef internal_key_type;
215 typedef StringRef external_key_type;
216 typedef uint64_t hash_value_type;
217 typedef uint64_t offset_type;
219 static bool EqualKey(StringRef A, StringRef B) { return A == B; }
220 static StringRef GetInternalKey(StringRef K) { return K; }
222 hash_value_type ComputeHash(StringRef K);
224 static std::pair<offset_type, offset_type>
225 ReadKeyDataLength(const unsigned char *&D) {
226 using namespace support;
227 offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D);
228 offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D);
229 return std::make_pair(KeyLen, DataLen);
232 StringRef ReadKey(const unsigned char *D, offset_type N) {
233 return StringRef((const char *)D, N);
236 InstrProfRecord ReadData(StringRef K, const unsigned char *D, offset_type N) {
237 if (N < 2 * sizeof(uint64_t) || N % sizeof(uint64_t)) {
238 // The data is corrupt, don't try to read it.
240 return InstrProfRecord("", 0, CountBuffer);
243 using namespace support;
245 // The first stored value is the hash.
246 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
247 // Each counter follows.
248 unsigned NumCounters = N / sizeof(uint64_t) - 1;
250 CountBuffer.reserve(NumCounters - 1);
251 for (unsigned I = 0; I < NumCounters; ++I)
252 CountBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
254 return InstrProfRecord(K, Hash, CountBuffer);
257 typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
258 InstrProfReaderIndex;
260 /// Reader for the indexed binary instrprof format.
261 class IndexedInstrProfReader : public InstrProfReader {
263 /// The profile data file contents.
264 std::unique_ptr<MemoryBuffer> DataBuffer;
265 /// The index into the profile data.
266 std::unique_ptr<InstrProfReaderIndex> Index;
267 /// Iterator over the profile data.
268 InstrProfReaderIndex::data_iterator RecordIterator;
269 /// The maximal execution count among all fucntions.
270 uint64_t MaxFunctionCount;
272 IndexedInstrProfReader(const IndexedInstrProfReader &) LLVM_DELETED_FUNCTION;
273 IndexedInstrProfReader &operator=(const IndexedInstrProfReader &)
274 LLVM_DELETED_FUNCTION;
276 IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
277 : DataBuffer(std::move(DataBuffer)), Index(nullptr),
278 RecordIterator(InstrProfReaderIndex::data_iterator()) {}
280 /// Return true if the given buffer is in an indexed instrprof format.
281 static bool hasFormat(const MemoryBuffer &DataBuffer);
283 /// Read the file header.
284 error_code readHeader() override;
285 /// Read a single record.
286 error_code readNextRecord(InstrProfRecord &Record) override;
288 /// Fill Counts with the profile data for the given function name.
289 error_code getFunctionCounts(StringRef FuncName, uint64_t &FuncHash,
290 std::vector<uint64_t> &Counts);
291 /// Return the maximum of all known function counts.
292 uint64_t getMaximumFunctionCount() { return MaxFunctionCount; }
294 /// Factory method to create an indexed reader.
295 static error_code create(std::string Path,
296 std::unique_ptr<IndexedInstrProfReader> &Result);
299 } // end namespace llvm
301 #endif // LLVM_PROFILEDATA_INSTRPROF_READER_H_