blob: 12c3c8256be8ee14cf007a62913c2a1d3421e007 [file] [log] [blame]
Justin Bognerf8d79192014-03-21 17:24:48 +00001//=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
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 clang's
11// instrumentation based PGO and coverage.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ProfileData/InstrProfReader.h"
16#include "llvm/ProfileData/InstrProf.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000017
Justin Bognerb7aa2632014-04-18 21:48:40 +000018#include "InstrProfIndexed.h"
19
Justin Bognerf8d79192014-03-21 17:24:48 +000020#include <cassert>
21
22using namespace llvm;
23
Justin Bognerb7aa2632014-04-18 21:48:40 +000024static error_code setupMemoryBuffer(std::string Path,
25 std::unique_ptr<MemoryBuffer> &Buffer) {
Justin Bognerf8d79192014-03-21 17:24:48 +000026 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
27 return EC;
28
29 // Sanity check the file.
30 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
31 return instrprof_error::too_large;
Justin Bognerb7aa2632014-04-18 21:48:40 +000032 return instrprof_error::success;
33}
34
35static error_code initializeReader(InstrProfReader &Reader) {
36 return Reader.readHeader();
37}
38
39error_code InstrProfReader::create(std::string Path,
40 std::unique_ptr<InstrProfReader> &Result) {
41 // Set up the buffer to read.
42 std::unique_ptr<MemoryBuffer> Buffer;
43 if (error_code EC = setupMemoryBuffer(Path, Buffer))
44 return EC;
Justin Bognerf8d79192014-03-21 17:24:48 +000045
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000046 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000047 if (IndexedInstrProfReader::hasFormat(*Buffer))
48 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
49 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000050 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
51 else if (RawInstrProfReader32::hasFormat(*Buffer))
52 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000053 else
Duncan P. N. Exon Smith4c5b7cb2014-03-21 20:42:34 +000054 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000055
Justin Bognerb7aa2632014-04-18 21:48:40 +000056 // Initialize the reader and return the result.
57 return initializeReader(*Result);
58}
59
60error_code IndexedInstrProfReader::create(
61 std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result) {
62 // Set up the buffer to read.
63 std::unique_ptr<MemoryBuffer> Buffer;
64 if (error_code EC = setupMemoryBuffer(Path, Buffer))
65 return EC;
66
67 // Create the reader.
68 if (!IndexedInstrProfReader::hasFormat(*Buffer))
69 return instrprof_error::bad_magic;
70 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
71
72 // Initialize the reader and return the result.
73 return initializeReader(*Result);
Justin Bognerf8d79192014-03-21 17:24:48 +000074}
75
76void InstrProfIterator::Increment() {
77 if (Reader->readNextRecord(Record))
78 *this = InstrProfIterator();
79}
80
81error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
82 // Skip empty lines.
83 while (!Line.is_at_end() && Line->empty())
84 ++Line;
85 // If we hit EOF while looking for a name, we're done.
86 if (Line.is_at_end())
87 return error(instrprof_error::eof);
88
89 // Read the function name.
90 Record.Name = *Line++;
91
92 // Read the function hash.
93 if (Line.is_at_end())
94 return error(instrprof_error::truncated);
95 if ((Line++)->getAsInteger(10, Record.Hash))
96 return error(instrprof_error::malformed);
97
98 // Read the number of counters.
99 uint64_t NumCounters;
100 if (Line.is_at_end())
101 return error(instrprof_error::truncated);
102 if ((Line++)->getAsInteger(10, NumCounters))
103 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000104 if (NumCounters == 0)
105 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000106
107 // Read each counter and fill our internal storage with the values.
108 Counts.clear();
109 Counts.reserve(NumCounters);
110 for (uint64_t I = 0; I < NumCounters; ++I) {
111 if (Line.is_at_end())
112 return error(instrprof_error::truncated);
113 uint64_t Count;
114 if ((Line++)->getAsInteger(10, Count))
115 return error(instrprof_error::malformed);
116 Counts.push_back(Count);
117 }
118 // Give the record a reference to our internal counter storage.
119 Record.Counts = Counts;
120
121 return success();
122}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000123
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000124template <class IntPtrT>
125static uint64_t getRawMagic();
126
127template <>
128uint64_t getRawMagic<uint64_t>() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000129 return
Duncan P. N. Exon Smith745a2bf2014-03-21 20:42:37 +0000130 uint64_t(255) << 56 |
131 uint64_t('l') << 48 |
132 uint64_t('p') << 40 |
133 uint64_t('r') << 32 |
134 uint64_t('o') << 24 |
135 uint64_t('f') << 16 |
136 uint64_t('r') << 8 |
137 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000138}
139
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000140template <>
141uint64_t getRawMagic<uint32_t>() {
142 return
143 uint64_t(255) << 56 |
144 uint64_t('l') << 48 |
145 uint64_t('p') << 40 |
146 uint64_t('r') << 32 |
147 uint64_t('o') << 24 |
148 uint64_t('f') << 16 |
149 uint64_t('R') << 8 |
150 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000151}
152
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000153template <class IntPtrT>
154bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000155 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000156 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000157 uint64_t Magic =
158 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
159 return getRawMagic<IntPtrT>() == Magic ||
160 sys::SwapByteOrder(getRawMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000161}
162
163template <class IntPtrT>
164error_code RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000165 if (!hasFormat(*DataBuffer))
166 return error(instrprof_error::bad_magic);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000167 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000168 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000169 auto *Header =
170 reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000171 ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000172 return readHeader(*Header);
173}
174
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000175static uint64_t getRawVersion() {
176 return 1;
177}
178
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000179template <class IntPtrT>
180error_code RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000181 if (swap(Header.Version) != getRawVersion())
182 return error(instrprof_error::unsupported_version);
183
184 CountersDelta = swap(Header.CountersDelta);
185 NamesDelta = swap(Header.NamesDelta);
186 auto DataSize = swap(Header.DataSize);
187 auto CountersSize = swap(Header.CountersSize);
188 auto NamesSize = swap(Header.NamesSize);
189
190 ptrdiff_t DataOffset = sizeof(RawHeader);
191 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
192 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
193 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
194
195 if (FileSize != DataBuffer->getBufferSize())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000196 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000197
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000198 const char *Start = DataBuffer->getBufferStart();
199 Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000200 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000201 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
202 NamesStart = Start + NamesOffset;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000203
204 return success();
205}
206
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000207template <class IntPtrT>
208error_code
209RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000210 if (Data == DataEnd)
211 return error(instrprof_error::eof);
212
213 // Get the raw data.
214 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
Justin Bognerb59d7c72014-04-25 02:45:33 +0000215 uint32_t NumCounters = swap(Data->NumCounters);
216 if (NumCounters == 0)
217 return error(instrprof_error::malformed);
218 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), NumCounters);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000219
220 // Check bounds.
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000221 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000222 if (RawName.data() < NamesStart ||
223 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
224 RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000225 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000226 return error(instrprof_error::malformed);
227
228 // Store the data in Record, byte-swapping as necessary.
229 Record.Hash = swap(Data->FuncHash);
230 Record.Name = RawName;
231 if (ShouldSwapBytes) {
232 Counts.clear();
233 Counts.reserve(RawCounts.size());
234 for (uint64_t Count : RawCounts)
235 Counts.push_back(swap(Count));
236 Record.Counts = Counts;
237 } else
238 Record.Counts = RawCounts;
239
240 // Iterate.
241 ++Data;
242 return success();
243}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000244
245namespace llvm {
246template class RawInstrProfReader<uint32_t>;
247template class RawInstrProfReader<uint64_t>;
248}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000249
Justin Bognerb5d368e2014-04-18 22:00:22 +0000250InstrProfLookupTrait::hash_value_type
251InstrProfLookupTrait::ComputeHash(StringRef K) {
252 return IndexedInstrProf::ComputeHash(HashType, K);
253}
254
Justin Bognerb7aa2632014-04-18 21:48:40 +0000255bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
256 if (DataBuffer.getBufferSize() < 8)
257 return false;
258 using namespace support;
259 uint64_t Magic =
260 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
261 return Magic == IndexedInstrProf::Magic;
262}
263
264error_code IndexedInstrProfReader::readHeader() {
265 const unsigned char *Start = (unsigned char *)DataBuffer->getBufferStart();
266 const unsigned char *Cur = Start;
267 if ((unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
268 return error(instrprof_error::truncated);
269
270 using namespace support;
271
272 // Check the magic number.
273 uint64_t Magic = endian::readNext<uint64_t, little, unaligned>(Cur);
274 if (Magic != IndexedInstrProf::Magic)
275 return error(instrprof_error::bad_magic);
276
277 // Read the version.
278 uint64_t Version = endian::readNext<uint64_t, little, unaligned>(Cur);
279 if (Version != IndexedInstrProf::Version)
280 return error(instrprof_error::unsupported_version);
281
282 // Read the maximal function count.
283 MaxFunctionCount = endian::readNext<uint64_t, little, unaligned>(Cur);
284
285 // Read the hash type and start offset.
286 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
287 endian::readNext<uint64_t, little, unaligned>(Cur));
288 if (HashType > IndexedInstrProf::HashT::Last)
289 return error(instrprof_error::unsupported_hash_type);
290 uint64_t HashOffset = endian::readNext<uint64_t, little, unaligned>(Cur);
291
292 // The rest of the file is an on disk hash table.
293 Index.reset(InstrProfReaderIndex::Create(Start + HashOffset, Cur, Start,
294 InstrProfLookupTrait(HashType)));
295 // Set up our iterator for readNextRecord.
296 RecordIterator = Index->data_begin();
297
298 return success();
299}
300
301error_code IndexedInstrProfReader::getFunctionCounts(
302 StringRef FuncName, uint64_t &FuncHash, std::vector<uint64_t> &Counts) {
303 const auto &Iter = Index->find(FuncName);
304 if (Iter == Index->end())
305 return error(instrprof_error::unknown_function);
306
307 // Found it. Make sure it's valid before giving back a result.
308 const InstrProfRecord &Record = *Iter;
309 if (Record.Name.empty())
310 return error(instrprof_error::malformed);
311 FuncHash = Record.Hash;
312 Counts = Record.Counts;
313 return success();
314}
315
316error_code IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
317 // Are we out of records?
318 if (RecordIterator == Index->data_end())
319 return error(instrprof_error::eof);
320
321 // Read the next one.
322 Record = *RecordIterator;
323 ++RecordIterator;
324 if (Record.Name.empty())
325 return error(instrprof_error::malformed);
326 return success();
327}