blob: d0493d34c203ae3b5d67838eff4ec65044f5ef3a [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
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000024static std::error_code
25setupMemoryBuffer(std::string Path, std::unique_ptr<MemoryBuffer> &Buffer) {
26 if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
Justin Bognerf8d79192014-03-21 17:24:48 +000027 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
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000035static std::error_code initializeReader(InstrProfReader &Reader) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000036 return Reader.readHeader();
37}
38
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000039std::error_code
40InstrProfReader::create(std::string Path,
41 std::unique_ptr<InstrProfReader> &Result) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000042 // Set up the buffer to read.
43 std::unique_ptr<MemoryBuffer> Buffer;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000044 if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
Justin Bognerb7aa2632014-04-18 21:48:40 +000045 return EC;
Justin Bognerf8d79192014-03-21 17:24:48 +000046
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000047 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000048 if (IndexedInstrProfReader::hasFormat(*Buffer))
49 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
50 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000051 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
52 else if (RawInstrProfReader32::hasFormat(*Buffer))
53 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000054 else
Duncan P. N. Exon Smith4c5b7cb2014-03-21 20:42:34 +000055 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000056
Justin Bognerb7aa2632014-04-18 21:48:40 +000057 // Initialize the reader and return the result.
58 return initializeReader(*Result);
59}
60
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000061std::error_code IndexedInstrProfReader::create(
Justin Bognerb7aa2632014-04-18 21:48:40 +000062 std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result) {
63 // Set up the buffer to read.
64 std::unique_ptr<MemoryBuffer> Buffer;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000065 if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
Justin Bognerb7aa2632014-04-18 21:48:40 +000066 return EC;
67
68 // Create the reader.
69 if (!IndexedInstrProfReader::hasFormat(*Buffer))
70 return instrprof_error::bad_magic;
71 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
72
73 // Initialize the reader and return the result.
74 return initializeReader(*Result);
Justin Bognerf8d79192014-03-21 17:24:48 +000075}
76
77void InstrProfIterator::Increment() {
78 if (Reader->readNextRecord(Record))
79 *this = InstrProfIterator();
80}
81
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000082std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognerf8d79192014-03-21 17:24:48 +000083 // Skip empty lines.
84 while (!Line.is_at_end() && Line->empty())
85 ++Line;
86 // If we hit EOF while looking for a name, we're done.
87 if (Line.is_at_end())
88 return error(instrprof_error::eof);
89
90 // Read the function name.
91 Record.Name = *Line++;
92
93 // Read the function hash.
94 if (Line.is_at_end())
95 return error(instrprof_error::truncated);
96 if ((Line++)->getAsInteger(10, Record.Hash))
97 return error(instrprof_error::malformed);
98
99 // Read the number of counters.
100 uint64_t NumCounters;
101 if (Line.is_at_end())
102 return error(instrprof_error::truncated);
103 if ((Line++)->getAsInteger(10, NumCounters))
104 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000105 if (NumCounters == 0)
106 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000107
108 // Read each counter and fill our internal storage with the values.
109 Counts.clear();
110 Counts.reserve(NumCounters);
111 for (uint64_t I = 0; I < NumCounters; ++I) {
112 if (Line.is_at_end())
113 return error(instrprof_error::truncated);
114 uint64_t Count;
115 if ((Line++)->getAsInteger(10, Count))
116 return error(instrprof_error::malformed);
117 Counts.push_back(Count);
118 }
119 // Give the record a reference to our internal counter storage.
120 Record.Counts = Counts;
121
122 return success();
123}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000124
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000125template <class IntPtrT>
126static uint64_t getRawMagic();
127
128template <>
129uint64_t getRawMagic<uint64_t>() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000130 return
Duncan P. N. Exon Smith745a2bf2014-03-21 20:42:37 +0000131 uint64_t(255) << 56 |
132 uint64_t('l') << 48 |
133 uint64_t('p') << 40 |
134 uint64_t('r') << 32 |
135 uint64_t('o') << 24 |
136 uint64_t('f') << 16 |
137 uint64_t('r') << 8 |
138 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000139}
140
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000141template <>
142uint64_t getRawMagic<uint32_t>() {
143 return
144 uint64_t(255) << 56 |
145 uint64_t('l') << 48 |
146 uint64_t('p') << 40 |
147 uint64_t('r') << 32 |
148 uint64_t('o') << 24 |
149 uint64_t('f') << 16 |
150 uint64_t('R') << 8 |
151 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000152}
153
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000154template <class IntPtrT>
155bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000156 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000157 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000158 uint64_t Magic =
159 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
160 return getRawMagic<IntPtrT>() == Magic ||
Artyom Skrobovef5e8672014-06-14 11:36:01 +0000161 sys::getSwappedBytes(getRawMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000162}
163
164template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000165std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000166 if (!hasFormat(*DataBuffer))
167 return error(instrprof_error::bad_magic);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000168 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000169 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000170 auto *Header =
171 reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000172 ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000173 return readHeader(*Header);
174}
175
Justin Bognera119f322014-05-16 00:38:00 +0000176template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000177std::error_code
178RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000179 const char *End = DataBuffer->getBufferEnd();
180 // Skip zero padding between profiles.
181 while (CurrentPos != End && *CurrentPos == 0)
182 ++CurrentPos;
183 // If there's nothing left, we're done.
184 if (CurrentPos == End)
185 return instrprof_error::eof;
186 // If there isn't enough space for another header, this is probably just
187 // garbage at the end of the file.
188 if (CurrentPos + sizeof(RawHeader) > End)
189 return instrprof_error::malformed;
190 // The magic should have the same byte order as in the previous header.
191 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
192 if (Magic != swap(getRawMagic<IntPtrT>()))
193 return instrprof_error::bad_magic;
194
195 // There's another profile to read, so we need to process the header.
196 auto *Header = reinterpret_cast<const RawHeader *>(CurrentPos);
197 return readHeader(*Header);
198}
199
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000200static uint64_t getRawVersion() {
201 return 1;
202}
203
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000204template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000205std::error_code
206RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000207 if (swap(Header.Version) != getRawVersion())
208 return error(instrprof_error::unsupported_version);
209
210 CountersDelta = swap(Header.CountersDelta);
211 NamesDelta = swap(Header.NamesDelta);
212 auto DataSize = swap(Header.DataSize);
213 auto CountersSize = swap(Header.CountersSize);
214 auto NamesSize = swap(Header.NamesSize);
215
216 ptrdiff_t DataOffset = sizeof(RawHeader);
217 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
218 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Justin Bognera119f322014-05-16 00:38:00 +0000219 size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000220
Justin Bognera119f322014-05-16 00:38:00 +0000221 auto *Start = reinterpret_cast<const char *>(&Header);
222 if (Start + ProfileSize > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000223 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000224
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000225 Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000226 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000227 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
228 NamesStart = Start + NamesOffset;
Justin Bognera119f322014-05-16 00:38:00 +0000229 ProfileEnd = Start + ProfileSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000230
231 return success();
232}
233
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000234template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000235std::error_code
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000236RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000237 if (Data == DataEnd)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000238 if (std::error_code EC = readNextHeader(ProfileEnd))
Justin Bognera119f322014-05-16 00:38:00 +0000239 return EC;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000240
241 // Get the raw data.
242 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
Justin Bognerb59d7c72014-04-25 02:45:33 +0000243 uint32_t NumCounters = swap(Data->NumCounters);
244 if (NumCounters == 0)
245 return error(instrprof_error::malformed);
246 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), NumCounters);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000247
248 // Check bounds.
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000249 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000250 if (RawName.data() < NamesStart ||
251 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
252 RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000253 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000254 return error(instrprof_error::malformed);
255
256 // Store the data in Record, byte-swapping as necessary.
257 Record.Hash = swap(Data->FuncHash);
258 Record.Name = RawName;
259 if (ShouldSwapBytes) {
260 Counts.clear();
261 Counts.reserve(RawCounts.size());
262 for (uint64_t Count : RawCounts)
263 Counts.push_back(swap(Count));
264 Record.Counts = Counts;
265 } else
266 Record.Counts = RawCounts;
267
268 // Iterate.
269 ++Data;
270 return success();
271}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000272
273namespace llvm {
274template class RawInstrProfReader<uint32_t>;
275template class RawInstrProfReader<uint64_t>;
276}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000277
Justin Bognerb5d368e2014-04-18 22:00:22 +0000278InstrProfLookupTrait::hash_value_type
279InstrProfLookupTrait::ComputeHash(StringRef K) {
280 return IndexedInstrProf::ComputeHash(HashType, K);
281}
282
Justin Bognerb7aa2632014-04-18 21:48:40 +0000283bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
284 if (DataBuffer.getBufferSize() < 8)
285 return false;
286 using namespace support;
287 uint64_t Magic =
288 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
289 return Magic == IndexedInstrProf::Magic;
290}
291
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000292std::error_code IndexedInstrProfReader::readHeader() {
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000293 const unsigned char *Start =
294 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000295 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000296 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000297 return error(instrprof_error::truncated);
298
299 using namespace support;
300
301 // Check the magic number.
302 uint64_t Magic = endian::readNext<uint64_t, little, unaligned>(Cur);
303 if (Magic != IndexedInstrProf::Magic)
304 return error(instrprof_error::bad_magic);
305
306 // Read the version.
307 uint64_t Version = endian::readNext<uint64_t, little, unaligned>(Cur);
308 if (Version != IndexedInstrProf::Version)
309 return error(instrprof_error::unsupported_version);
310
311 // Read the maximal function count.
312 MaxFunctionCount = endian::readNext<uint64_t, little, unaligned>(Cur);
313
314 // Read the hash type and start offset.
315 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
316 endian::readNext<uint64_t, little, unaligned>(Cur));
317 if (HashType > IndexedInstrProf::HashT::Last)
318 return error(instrprof_error::unsupported_hash_type);
319 uint64_t HashOffset = endian::readNext<uint64_t, little, unaligned>(Cur);
320
321 // The rest of the file is an on disk hash table.
322 Index.reset(InstrProfReaderIndex::Create(Start + HashOffset, Cur, Start,
323 InstrProfLookupTrait(HashType)));
324 // Set up our iterator for readNextRecord.
325 RecordIterator = Index->data_begin();
326
327 return success();
328}
329
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000330std::error_code IndexedInstrProfReader::getFunctionCounts(
Justin Bognerb7aa2632014-04-18 21:48:40 +0000331 StringRef FuncName, uint64_t &FuncHash, std::vector<uint64_t> &Counts) {
332 const auto &Iter = Index->find(FuncName);
333 if (Iter == Index->end())
334 return error(instrprof_error::unknown_function);
335
336 // Found it. Make sure it's valid before giving back a result.
337 const InstrProfRecord &Record = *Iter;
338 if (Record.Name.empty())
339 return error(instrprof_error::malformed);
340 FuncHash = Record.Hash;
341 Counts = Record.Counts;
342 return success();
343}
344
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000345std::error_code
346IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000347 // Are we out of records?
348 if (RecordIterator == Index->data_end())
349 return error(instrprof_error::eof);
350
351 // Read the next one.
352 Record = *RecordIterator;
353 ++RecordIterator;
354 if (Record.Name.empty())
355 return error(instrprof_error::malformed);
356 return success();
357}