blob: 58dbeef6531398d004709191f1d52f6e61ca6c04 [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"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000017#include <cassert>
18
19using namespace llvm;
20
Diego Novillofcd55602014-11-03 00:51:45 +000021static ErrorOr<std::unique_ptr<MemoryBuffer>>
22setupMemoryBuffer(std::string Path) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000023 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
24 MemoryBuffer::getFileOrSTDIN(Path);
25 if (std::error_code EC = BufferOrErr.getError())
Justin Bognerf8d79192014-03-21 17:24:48 +000026 return EC;
Justin Bogner2b6c5372015-02-18 01:58:17 +000027 return std::move(BufferOrErr.get());
Justin Bognerb7aa2632014-04-18 21:48:40 +000028}
29
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000030static std::error_code initializeReader(InstrProfReader &Reader) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000031 return Reader.readHeader();
32}
33
Diego Novillofcd55602014-11-03 00:51:45 +000034ErrorOr<std::unique_ptr<InstrProfReader>>
35InstrProfReader::create(std::string Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000036 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000037 auto BufferOrError = setupMemoryBuffer(Path);
38 if (std::error_code EC = BufferOrError.getError())
Justin Bognerb7aa2632014-04-18 21:48:40 +000039 return EC;
Justin Bogner2b6c5372015-02-18 01:58:17 +000040 return InstrProfReader::create(std::move(BufferOrError.get()));
41}
Justin Bognerf8d79192014-03-21 17:24:48 +000042
Justin Bogner2b6c5372015-02-18 01:58:17 +000043ErrorOr<std::unique_ptr<InstrProfReader>>
44InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
45 // Sanity check the buffer.
46 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
47 return instrprof_error::too_large;
48
Diego Novillofcd55602014-11-03 00:51:45 +000049 std::unique_ptr<InstrProfReader> Result;
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000050 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000051 if (IndexedInstrProfReader::hasFormat(*Buffer))
52 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
53 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000054 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
55 else if (RawInstrProfReader32::hasFormat(*Buffer))
56 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000057 else
Duncan P. N. Exon Smith4c5b7cb2014-03-21 20:42:34 +000058 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000059
Justin Bognerb7aa2632014-04-18 21:48:40 +000060 // Initialize the reader and return the result.
Diego Novillofcd55602014-11-03 00:51:45 +000061 if (std::error_code EC = initializeReader(*Result))
62 return EC;
63
64 return std::move(Result);
Justin Bognerb7aa2632014-04-18 21:48:40 +000065}
66
Justin Bognerab89ed72015-02-16 21:28:58 +000067ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
68IndexedInstrProfReader::create(std::string Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000069 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000070 auto BufferOrError = setupMemoryBuffer(Path);
71 if (std::error_code EC = BufferOrError.getError())
Justin Bognerb7aa2632014-04-18 21:48:40 +000072 return EC;
Justin Bogner2b6c5372015-02-18 01:58:17 +000073 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
74}
Justin Bognerb7aa2632014-04-18 21:48:40 +000075
Justin Bogner2b6c5372015-02-18 01:58:17 +000076
77ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
78IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
79 // Sanity check the buffer.
80 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
81 return instrprof_error::too_large;
Justin Bognerab89ed72015-02-16 21:28:58 +000082
Justin Bognerb7aa2632014-04-18 21:48:40 +000083 // Create the reader.
84 if (!IndexedInstrProfReader::hasFormat(*Buffer))
85 return instrprof_error::bad_magic;
Justin Bogner2b6c5372015-02-18 01:58:17 +000086 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
Justin Bognerb7aa2632014-04-18 21:48:40 +000087
88 // Initialize the reader and return the result.
Justin Bognerab89ed72015-02-16 21:28:58 +000089 if (std::error_code EC = initializeReader(*Result))
90 return EC;
91
92 return std::move(Result);
Justin Bognerf8d79192014-03-21 17:24:48 +000093}
94
95void InstrProfIterator::Increment() {
96 if (Reader->readNextRecord(Record))
97 *this = InstrProfIterator();
98}
99
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000100std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognercf36a362014-07-29 22:29:23 +0000101 // Skip empty lines and comments.
102 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
Justin Bognerf8d79192014-03-21 17:24:48 +0000103 ++Line;
104 // If we hit EOF while looking for a name, we're done.
105 if (Line.is_at_end())
106 return error(instrprof_error::eof);
107
108 // Read the function name.
109 Record.Name = *Line++;
110
111 // Read the function hash.
112 if (Line.is_at_end())
113 return error(instrprof_error::truncated);
Justin Bognerf95ca072015-03-09 18:54:49 +0000114 if ((Line++)->getAsInteger(0, Record.Hash))
Justin Bognerf8d79192014-03-21 17:24:48 +0000115 return error(instrprof_error::malformed);
116
117 // Read the number of counters.
118 uint64_t NumCounters;
119 if (Line.is_at_end())
120 return error(instrprof_error::truncated);
121 if ((Line++)->getAsInteger(10, NumCounters))
122 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000123 if (NumCounters == 0)
124 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000125
126 // Read each counter and fill our internal storage with the values.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000127 Record.Counts.clear();
128 Record.Counts.reserve(NumCounters);
Justin Bognerf8d79192014-03-21 17:24:48 +0000129 for (uint64_t I = 0; I < NumCounters; ++I) {
130 if (Line.is_at_end())
131 return error(instrprof_error::truncated);
132 uint64_t Count;
133 if ((Line++)->getAsInteger(10, Count))
134 return error(instrprof_error::malformed);
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000135 Record.Counts.push_back(Count);
Justin Bognerf8d79192014-03-21 17:24:48 +0000136 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000137
138 return success();
139}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000140
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000141template <class IntPtrT>
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000142bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000143 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000144 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000145 uint64_t Magic =
146 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000147 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
148 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000149}
150
151template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000152std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000153 if (!hasFormat(*DataBuffer))
154 return error(instrprof_error::bad_magic);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000155 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000156 return error(instrprof_error::bad_header);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000157 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
158 DataBuffer->getBufferStart());
159 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000160 return readHeader(*Header);
161}
162
Justin Bognera119f322014-05-16 00:38:00 +0000163template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000164std::error_code
165RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000166 const char *End = DataBuffer->getBufferEnd();
167 // Skip zero padding between profiles.
168 while (CurrentPos != End && *CurrentPos == 0)
169 ++CurrentPos;
170 // If there's nothing left, we're done.
171 if (CurrentPos == End)
172 return instrprof_error::eof;
173 // If there isn't enough space for another header, this is probably just
174 // garbage at the end of the file.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000175 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
Justin Bognera119f322014-05-16 00:38:00 +0000176 return instrprof_error::malformed;
Justin Bogner54b11282014-09-12 21:22:55 +0000177 // The writer ensures each profile is padded to start at an aligned address.
178 if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
179 return instrprof_error::malformed;
Justin Bognera119f322014-05-16 00:38:00 +0000180 // The magic should have the same byte order as in the previous header.
181 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000182 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
Justin Bognera119f322014-05-16 00:38:00 +0000183 return instrprof_error::bad_magic;
184
185 // There's another profile to read, so we need to process the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000186 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
Justin Bognera119f322014-05-16 00:38:00 +0000187 return readHeader(*Header);
188}
189
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000190template <class IntPtrT>
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000191std::error_code RawInstrProfReader<IntPtrT>::readHeader(
192 const RawInstrProf::Header &Header) {
193 if (swap(Header.Version) != RawInstrProf::Version)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000194 return error(instrprof_error::unsupported_version);
195
196 CountersDelta = swap(Header.CountersDelta);
197 NamesDelta = swap(Header.NamesDelta);
198 auto DataSize = swap(Header.DataSize);
199 auto CountersSize = swap(Header.CountersSize);
200 auto NamesSize = swap(Header.NamesSize);
201
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000202 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
203 ptrdiff_t CountersOffset =
204 DataOffset + sizeof(RawInstrProf::ProfileData<IntPtrT>) * DataSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000205 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Justin Bognera119f322014-05-16 00:38:00 +0000206 size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000207
Justin Bognera119f322014-05-16 00:38:00 +0000208 auto *Start = reinterpret_cast<const char *>(&Header);
209 if (Start + ProfileSize > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000210 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000211
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000212 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
213 Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000214 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000215 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
216 NamesStart = Start + NamesOffset;
Justin Bognera119f322014-05-16 00:38:00 +0000217 ProfileEnd = Start + ProfileSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000218
219 return success();
220}
221
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000222template <class IntPtrT>
Xinliang David Licf4a1282015-10-28 19:34:04 +0000223std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
224 Record.Name = StringRef(getName(Data->NamePtr), swap(Data->NameSize));
225 if (Record.Name.data() < NamesStart ||
226 Record.Name.data() + Record.Name.size() > DataBuffer->getBufferEnd())
227 return error(instrprof_error::malformed);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000228
Xinliang David Licf4a1282015-10-28 19:34:04 +0000229 return success();
230}
231
232template <class IntPtrT>
233std::error_code RawInstrProfReader<IntPtrT>::readFuncHash(
234 InstrProfRecord &Record) {
235 Record.Hash = swap(Data->FuncHash);
236 return success();
237}
238
239template <class IntPtrT>
240std::error_code RawInstrProfReader<IntPtrT>::readRawCounts(
241 InstrProfRecord &Record) {
Justin Bognerb59d7c72014-04-25 02:45:33 +0000242 uint32_t NumCounters = swap(Data->NumCounters);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000243 IntPtrT CounterPtr = Data->CounterPtr;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000244 if (NumCounters == 0)
245 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000246
247 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
248 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000249
250 // Check bounds.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000251 if (RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000252 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000253 return error(instrprof_error::malformed);
254
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000255 if (ShouldSwapBytes) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000256 Record.Counts.clear();
257 Record.Counts.reserve(RawCounts.size());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000258 for (uint64_t Count : RawCounts)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000259 Record.Counts.push_back(swap(Count));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000260 } else
261 Record.Counts = RawCounts;
262
Xinliang David Licf4a1282015-10-28 19:34:04 +0000263 return success();
264}
265
266template <class IntPtrT>
267std::error_code RawInstrProfReader<IntPtrT>::readNextRecord(
268 InstrProfRecord &Record) {
269 if (atEnd())
270 if (std::error_code EC = readNextHeader(ProfileEnd)) return EC;
271
272 // Read name ad set it in Record.
273 if (std::error_code EC = readName(Record)) return EC;
274
275 // Read FuncHash and set it in Record.
276 if (std::error_code EC = readFuncHash(Record)) return EC;
277
278 // Read raw counts and set Record.
279 if (std::error_code EC = readRawCounts(Record)) return EC;
280
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000281 // Iterate.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000282 advanceData();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000283 return success();
284}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000285
286namespace llvm {
287template class RawInstrProfReader<uint32_t>;
288template class RawInstrProfReader<uint64_t>;
289}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000290
Justin Bognerb5d368e2014-04-18 22:00:22 +0000291InstrProfLookupTrait::hash_value_type
292InstrProfLookupTrait::ComputeHash(StringRef K) {
293 return IndexedInstrProf::ComputeHash(HashType, K);
294}
295
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000296typedef InstrProfLookupTrait::data_type data_type;
297typedef InstrProfLookupTrait::offset_type offset_type;
298
Justin Bogner9e9a0572015-09-29 22:13:58 +0000299bool InstrProfLookupTrait::ReadValueProfilingData(
300 const unsigned char *&D, const unsigned char *const End) {
301
302 using namespace support;
303 // Read number of value kinds with value sites.
304 if (D + sizeof(uint64_t) > End)
305 return false;
306 uint64_t ValueKindCount = endian::readNext<uint64_t, little, unaligned>(D);
307
Xinliang David Li2004f002015-11-02 05:08:23 +0000308 InstrProfRecord &ProfRecord = DataBuffer.back();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000309 for (uint32_t Kind = 0; Kind < ValueKindCount; ++Kind) {
310
311 // Read value kind and number of value sites for kind.
312 if (D + 2 * sizeof(uint64_t) > End)
313 return false;
Xinliang David Li2004f002015-11-02 05:08:23 +0000314
Justin Bogner9e9a0572015-09-29 22:13:58 +0000315 uint64_t ValueKind = endian::readNext<uint64_t, little, unaligned>(D);
316 uint64_t ValueSiteCount = endian::readNext<uint64_t, little, unaligned>(D);
317
Xinliang David Li2004f002015-11-02 05:08:23 +0000318 ProfRecord.reserveSites(ValueKind, ValueSiteCount);
319
Justin Bogner9e9a0572015-09-29 22:13:58 +0000320 for (uint64_t VSite = 0; VSite < ValueSiteCount; ++VSite) {
321 // Read number of value data pairs at value site.
322 if (D + sizeof(uint64_t) > End)
323 return false;
Xinliang David Li2004f002015-11-02 05:08:23 +0000324
Justin Bogner9e9a0572015-09-29 22:13:58 +0000325 uint64_t ValueDataCount =
326 endian::readNext<uint64_t, little, unaligned>(D);
327
328 // Check if there are as many ValueDataPairs as ValueDataCount in memory.
329 if (D + (ValueDataCount << 1) * sizeof(uint64_t) > End)
330 return false;
331
Xinliang David Li2004f002015-11-02 05:08:23 +0000332 std::unique_ptr<InstrProfValueData[]> VDataPtr(
333 ValueDataCount == 0 ? nullptr
334 : new InstrProfValueData[ValueDataCount]);
335
Justin Bogner9e9a0572015-09-29 22:13:58 +0000336 for (uint64_t VCount = 0; VCount < ValueDataCount; ++VCount) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000337 VDataPtr[VCount].Value =
338 endian::readNext<uint64_t, little, unaligned>(D);
339 VDataPtr[VCount].Count =
340 endian::readNext<uint64_t, little, unaligned>(D);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000341 }
Xinliang David Li2004f002015-11-02 05:08:23 +0000342 ProfRecord.addValueData(ValueKind, VSite, VDataPtr.get(), ValueDataCount,
343 &HashKeys);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000344 }
345 }
346 return true;
347}
348
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000349data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
350 offset_type N) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000351 // Check if the data is corrupt. If so, don't try to read it.
352 if (N % sizeof(uint64_t))
353 return data_type();
354
355 DataBuffer.clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000356 std::vector<uint64_t> CounterBuffer;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000357
358 using namespace support;
359 const unsigned char *End = D + N;
360 while (D < End) {
Xinliang David Lic7583872015-10-13 16:35:59 +0000361 // Read hash.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000362 if (D + sizeof(uint64_t) >= End)
363 return data_type();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000364 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
365
Xinliang David Lic7583872015-10-13 16:35:59 +0000366 // Initialize number of counters for FormatVersion == 1.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000367 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
Xinliang David Lic7583872015-10-13 16:35:59 +0000368 // If format version is different then read the number of counters.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000369 if (FormatVersion != 1) {
370 if (D + sizeof(uint64_t) > End)
371 return data_type();
372 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
373 }
Xinliang David Lic7583872015-10-13 16:35:59 +0000374 // Read counter values.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000375 if (D + CountsSize * sizeof(uint64_t) > End)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000376 return data_type();
377
378 CounterBuffer.clear();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000379 CounterBuffer.reserve(CountsSize);
380 for (uint64_t J = 0; J < CountsSize; ++J)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000381 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
382
Xinliang David Li2004f002015-11-02 05:08:23 +0000383 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000384
Xinliang David Lic7583872015-10-13 16:35:59 +0000385 // Read value profiling data.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000386 if (FormatVersion > 2 && !ReadValueProfilingData(D, End)) {
387 DataBuffer.clear();
388 return data_type();
389 }
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000390 }
391 return DataBuffer;
392}
393
Xinliang David Li140f4c42015-10-28 04:20:31 +0000394std::error_code InstrProfReaderIndex::getRecords(
395 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
396 auto Iter = Index->find(FuncName);
397 if (Iter == Index->end()) return instrprof_error::unknown_function;
398
399 Data = (*Iter);
400 if (Data.empty()) return instrprof_error::malformed;
401
402 return instrprof_error::success;
403}
404
405std::error_code InstrProfReaderIndex::getRecords(
406 ArrayRef<InstrProfRecord> &Data) {
407 if (atEnd()) return instrprof_error::eof;
408
409 Data = *RecordIterator;
410
411 if (Data.empty()) return instrprof_error::malformed;
412
413 return instrprof_error::success;
414}
415
416void InstrProfReaderIndex::Init(const unsigned char *Buckets,
417 const unsigned char *const Payload,
418 const unsigned char *const Base,
419 IndexedInstrProf::HashT HashType,
420 uint64_t Version) {
421 FormatVersion = Version;
422 Index.reset(IndexType::Create(Buckets, Payload, Base,
423 InstrProfLookupTrait(HashType, Version)));
424 // Form the map of hash values to const char* keys in profiling data.
425 std::vector<std::pair<uint64_t, const char *>> HashKeys;
426 for (auto Key : Index->keys()) {
427 const char *KeyTableRef = StringTable.insertString(Key);
428 HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef));
429 }
430 std::sort(HashKeys.begin(), HashKeys.end(), less_first());
431 HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end());
432 // Set the hash key map for the InstrLookupTrait
433 Index->getInfoObj().setHashKeys(std::move(HashKeys));
434 RecordIterator = Index->data_begin();
435}
436
Justin Bognerb7aa2632014-04-18 21:48:40 +0000437bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000438 if (DataBuffer.getBufferSize() < 8) return false;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000439 using namespace support;
440 uint64_t Magic =
441 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
Xinliang David Lic7583872015-10-13 16:35:59 +0000442 // Verify that it's magical.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000443 return Magic == IndexedInstrProf::Magic;
444}
445
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000446std::error_code IndexedInstrProfReader::readHeader() {
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000447 const unsigned char *Start =
448 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000449 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000450 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000451 return error(instrprof_error::truncated);
452
453 using namespace support;
454
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000455 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
456 Cur += sizeof(IndexedInstrProf::Header);
457
Justin Bognerb7aa2632014-04-18 21:48:40 +0000458 // Check the magic number.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000459 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000460 if (Magic != IndexedInstrProf::Magic)
461 return error(instrprof_error::bad_magic);
462
463 // Read the version.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000464 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
Justin Bogner821d7472014-08-01 22:50:07 +0000465 if (FormatVersion > IndexedInstrProf::Version)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000466 return error(instrprof_error::unsupported_version);
467
468 // Read the maximal function count.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000469 MaxFunctionCount =
470 endian::byte_swap<uint64_t, little>(Header->MaxFunctionCount);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000471
472 // Read the hash type and start offset.
473 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000474 endian::byte_swap<uint64_t, little>(Header->HashType));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000475 if (HashType > IndexedInstrProf::HashT::Last)
476 return error(instrprof_error::unsupported_hash_type);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000477
478 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000479
480 // The rest of the file is an on disk hash table.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000481 Index.Init(Start + HashOffset, Cur, Start, HashType, FormatVersion);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000482
483 return success();
484}
485
Xinliang David Li2004f002015-11-02 05:08:23 +0000486ErrorOr<InstrProfRecord> IndexedInstrProfReader::getInstrProfRecord(
487 StringRef FuncName, uint64_t FuncHash) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000488 ArrayRef<InstrProfRecord> Data;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000489 std::error_code EC = Index.getRecords(FuncName, Data);
490 if (EC != instrprof_error::success) return EC;
Justin Bogner821d7472014-08-01 22:50:07 +0000491 // Found it. Look for counters with the right hash.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000492 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +0000493 // Check for a match and fill the vector if there is one.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000494 if (Data[I].Hash == FuncHash) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000495 return std::move(Data[I]);
Justin Bogner821d7472014-08-01 22:50:07 +0000496 }
497 }
498 return error(instrprof_error::hash_mismatch);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000499}
500
Xinliang David Li2004f002015-11-02 05:08:23 +0000501std::error_code IndexedInstrProfReader::getFunctionCounts(
502 StringRef FuncName, uint64_t FuncHash, std::vector<uint64_t> &Counts) {
503 ErrorOr<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
504 if (std::error_code EC = Record.getError()) return EC;
505
506 Counts = Record.get().Counts;
507 return success();
508}
509
Xinliang David Li140f4c42015-10-28 04:20:31 +0000510std::error_code IndexedInstrProfReader::readNextRecord(
511 InstrProfRecord &Record) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000512 static unsigned RecordIndex = 0;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000513
514 ArrayRef<InstrProfRecord> Data;
515
516 std::error_code EC = Index.getRecords(Data);
517 if (EC != instrprof_error::success) return error(EC);
518
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000519 Record = Data[RecordIndex++];
520 if (RecordIndex >= Data.size()) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000521 Index.advanceToNextKey();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000522 RecordIndex = 0;
Justin Bogner821d7472014-08-01 22:50:07 +0000523 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000524 return success();
525}