blob: 4f110a5712243d736c2ebd660bf1b90e7a9998ef [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);
104
105 // Read each counter and fill our internal storage with the values.
106 Counts.clear();
107 Counts.reserve(NumCounters);
108 for (uint64_t I = 0; I < NumCounters; ++I) {
109 if (Line.is_at_end())
110 return error(instrprof_error::truncated);
111 uint64_t Count;
112 if ((Line++)->getAsInteger(10, Count))
113 return error(instrprof_error::malformed);
114 Counts.push_back(Count);
115 }
116 // Give the record a reference to our internal counter storage.
117 Record.Counts = Counts;
118
119 return success();
120}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000121
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000122template <class IntPtrT>
123static uint64_t getRawMagic();
124
125template <>
126uint64_t getRawMagic<uint64_t>() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000127 return
Duncan P. N. Exon Smith745a2bf2014-03-21 20:42:37 +0000128 uint64_t(255) << 56 |
129 uint64_t('l') << 48 |
130 uint64_t('p') << 40 |
131 uint64_t('r') << 32 |
132 uint64_t('o') << 24 |
133 uint64_t('f') << 16 |
134 uint64_t('r') << 8 |
135 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000136}
137
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000138template <>
139uint64_t getRawMagic<uint32_t>() {
140 return
141 uint64_t(255) << 56 |
142 uint64_t('l') << 48 |
143 uint64_t('p') << 40 |
144 uint64_t('r') << 32 |
145 uint64_t('o') << 24 |
146 uint64_t('f') << 16 |
147 uint64_t('R') << 8 |
148 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000149}
150
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000151template <class IntPtrT>
152bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000153 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000154 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000155 uint64_t Magic =
156 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
157 return getRawMagic<IntPtrT>() == Magic ||
158 sys::SwapByteOrder(getRawMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000159}
160
161template <class IntPtrT>
162error_code RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000163 if (!hasFormat(*DataBuffer))
164 return error(instrprof_error::bad_magic);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000165 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000166 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000167 auto *Header =
168 reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000169 ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000170 return readHeader(*Header);
171}
172
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000173static uint64_t getRawVersion() {
174 return 1;
175}
176
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000177template <class IntPtrT>
178error_code RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000179 if (swap(Header.Version) != getRawVersion())
180 return error(instrprof_error::unsupported_version);
181
182 CountersDelta = swap(Header.CountersDelta);
183 NamesDelta = swap(Header.NamesDelta);
184 auto DataSize = swap(Header.DataSize);
185 auto CountersSize = swap(Header.CountersSize);
186 auto NamesSize = swap(Header.NamesSize);
187
188 ptrdiff_t DataOffset = sizeof(RawHeader);
189 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
190 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
191 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
192
193 if (FileSize != DataBuffer->getBufferSize())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000194 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000195
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000196 const char *Start = DataBuffer->getBufferStart();
197 Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000198 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000199 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
200 NamesStart = Start + NamesOffset;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000201
202 return success();
203}
204
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000205template <class IntPtrT>
206error_code
207RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000208 if (Data == DataEnd)
209 return error(instrprof_error::eof);
210
211 // Get the raw data.
212 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
213 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
214 swap(Data->NumCounters));
215
216 // Check bounds.
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000217 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000218 if (RawName.data() < NamesStart ||
219 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
220 RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000221 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000222 return error(instrprof_error::malformed);
223
224 // Store the data in Record, byte-swapping as necessary.
225 Record.Hash = swap(Data->FuncHash);
226 Record.Name = RawName;
227 if (ShouldSwapBytes) {
228 Counts.clear();
229 Counts.reserve(RawCounts.size());
230 for (uint64_t Count : RawCounts)
231 Counts.push_back(swap(Count));
232 Record.Counts = Counts;
233 } else
234 Record.Counts = RawCounts;
235
236 // Iterate.
237 ++Data;
238 return success();
239}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000240
241namespace llvm {
242template class RawInstrProfReader<uint32_t>;
243template class RawInstrProfReader<uint64_t>;
244}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000245
Justin Bognerb5d368e2014-04-18 22:00:22 +0000246InstrProfLookupTrait::hash_value_type
247InstrProfLookupTrait::ComputeHash(StringRef K) {
248 return IndexedInstrProf::ComputeHash(HashType, K);
249}
250
Justin Bognerb7aa2632014-04-18 21:48:40 +0000251bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
252 if (DataBuffer.getBufferSize() < 8)
253 return false;
254 using namespace support;
255 uint64_t Magic =
256 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
257 return Magic == IndexedInstrProf::Magic;
258}
259
260error_code IndexedInstrProfReader::readHeader() {
261 const unsigned char *Start = (unsigned char *)DataBuffer->getBufferStart();
262 const unsigned char *Cur = Start;
263 if ((unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
264 return error(instrprof_error::truncated);
265
266 using namespace support;
267
268 // Check the magic number.
269 uint64_t Magic = endian::readNext<uint64_t, little, unaligned>(Cur);
270 if (Magic != IndexedInstrProf::Magic)
271 return error(instrprof_error::bad_magic);
272
273 // Read the version.
274 uint64_t Version = endian::readNext<uint64_t, little, unaligned>(Cur);
275 if (Version != IndexedInstrProf::Version)
276 return error(instrprof_error::unsupported_version);
277
278 // Read the maximal function count.
279 MaxFunctionCount = endian::readNext<uint64_t, little, unaligned>(Cur);
280
281 // Read the hash type and start offset.
282 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
283 endian::readNext<uint64_t, little, unaligned>(Cur));
284 if (HashType > IndexedInstrProf::HashT::Last)
285 return error(instrprof_error::unsupported_hash_type);
286 uint64_t HashOffset = endian::readNext<uint64_t, little, unaligned>(Cur);
287
288 // The rest of the file is an on disk hash table.
289 Index.reset(InstrProfReaderIndex::Create(Start + HashOffset, Cur, Start,
290 InstrProfLookupTrait(HashType)));
291 // Set up our iterator for readNextRecord.
292 RecordIterator = Index->data_begin();
293
294 return success();
295}
296
297error_code IndexedInstrProfReader::getFunctionCounts(
298 StringRef FuncName, uint64_t &FuncHash, std::vector<uint64_t> &Counts) {
299 const auto &Iter = Index->find(FuncName);
300 if (Iter == Index->end())
301 return error(instrprof_error::unknown_function);
302
303 // Found it. Make sure it's valid before giving back a result.
304 const InstrProfRecord &Record = *Iter;
305 if (Record.Name.empty())
306 return error(instrprof_error::malformed);
307 FuncHash = Record.Hash;
308 Counts = Record.Counts;
309 return success();
310}
311
312error_code IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
313 // Are we out of records?
314 if (RecordIterator == Index->data_end())
315 return error(instrprof_error::eof);
316
317 // Read the next one.
318 Record = *RecordIterator;
319 ++RecordIterator;
320 if (Record.Name.empty())
321 return error(instrprof_error::malformed);
322 return success();
323}