1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
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 clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProf.h"
18 #include "InstrProfIndexed.h"
24 static ErrorOr<std::unique_ptr<MemoryBuffer>>
25 setupMemoryBuffer(std::string Path) {
26 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
27 MemoryBuffer::getFileOrSTDIN(Path);
28 if (std::error_code EC = BufferOrErr.getError())
30 auto Buffer = std::move(BufferOrErr.get());
32 // Sanity check the file.
33 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
34 return instrprof_error::too_large;
35 return std::move(Buffer);
38 static std::error_code initializeReader(InstrProfReader &Reader) {
39 return Reader.readHeader();
42 ErrorOr<std::unique_ptr<InstrProfReader>>
43 InstrProfReader::create(std::string Path) {
44 // Set up the buffer to read.
45 auto BufferOrError = setupMemoryBuffer(Path);
46 if (std::error_code EC = BufferOrError.getError())
49 auto Buffer = std::move(BufferOrError.get());
50 std::unique_ptr<InstrProfReader> Result;
53 if (IndexedInstrProfReader::hasFormat(*Buffer))
54 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
55 else if (RawInstrProfReader64::hasFormat(*Buffer))
56 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
57 else if (RawInstrProfReader32::hasFormat(*Buffer))
58 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
60 Result.reset(new TextInstrProfReader(std::move(Buffer)));
62 // Initialize the reader and return the result.
63 if (std::error_code EC = initializeReader(*Result))
66 return std::move(Result);
69 std::error_code IndexedInstrProfReader::create(
70 std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result) {
71 // Set up the buffer to read.
72 auto BufferOrError = setupMemoryBuffer(Path);
73 if (std::error_code EC = BufferOrError.getError())
76 auto Buffer = std::move(BufferOrError.get());
78 if (!IndexedInstrProfReader::hasFormat(*Buffer))
79 return instrprof_error::bad_magic;
80 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
82 // Initialize the reader and return the result.
83 return initializeReader(*Result);
86 void InstrProfIterator::Increment() {
87 if (Reader->readNextRecord(Record))
88 *this = InstrProfIterator();
91 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
92 // Skip empty lines and comments.
93 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
95 // If we hit EOF while looking for a name, we're done.
97 return error(instrprof_error::eof);
99 // Read the function name.
100 Record.Name = *Line++;
102 // Read the function hash.
103 if (Line.is_at_end())
104 return error(instrprof_error::truncated);
105 if ((Line++)->getAsInteger(10, Record.Hash))
106 return error(instrprof_error::malformed);
108 // Read the number of counters.
109 uint64_t NumCounters;
110 if (Line.is_at_end())
111 return error(instrprof_error::truncated);
112 if ((Line++)->getAsInteger(10, NumCounters))
113 return error(instrprof_error::malformed);
114 if (NumCounters == 0)
115 return error(instrprof_error::malformed);
117 // Read each counter and fill our internal storage with the values.
119 Counts.reserve(NumCounters);
120 for (uint64_t I = 0; I < NumCounters; ++I) {
121 if (Line.is_at_end())
122 return error(instrprof_error::truncated);
124 if ((Line++)->getAsInteger(10, Count))
125 return error(instrprof_error::malformed);
126 Counts.push_back(Count);
128 // Give the record a reference to our internal counter storage.
129 Record.Counts = Counts;
134 template <class IntPtrT>
135 static uint64_t getRawMagic();
138 uint64_t getRawMagic<uint64_t>() {
140 uint64_t(255) << 56 |
141 uint64_t('l') << 48 |
142 uint64_t('p') << 40 |
143 uint64_t('r') << 32 |
144 uint64_t('o') << 24 |
145 uint64_t('f') << 16 |
151 uint64_t getRawMagic<uint32_t>() {
153 uint64_t(255) << 56 |
154 uint64_t('l') << 48 |
155 uint64_t('p') << 40 |
156 uint64_t('r') << 32 |
157 uint64_t('o') << 24 |
158 uint64_t('f') << 16 |
163 template <class IntPtrT>
164 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
165 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
168 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
169 return getRawMagic<IntPtrT>() == Magic ||
170 sys::getSwappedBytes(getRawMagic<IntPtrT>()) == Magic;
173 template <class IntPtrT>
174 std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
175 if (!hasFormat(*DataBuffer))
176 return error(instrprof_error::bad_magic);
177 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
178 return error(instrprof_error::bad_header);
180 reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
181 ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
182 return readHeader(*Header);
185 template <class IntPtrT>
187 RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
188 const char *End = DataBuffer->getBufferEnd();
189 // Skip zero padding between profiles.
190 while (CurrentPos != End && *CurrentPos == 0)
192 // If there's nothing left, we're done.
193 if (CurrentPos == End)
194 return instrprof_error::eof;
195 // If there isn't enough space for another header, this is probably just
196 // garbage at the end of the file.
197 if (CurrentPos + sizeof(RawHeader) > End)
198 return instrprof_error::malformed;
199 // The writer ensures each profile is padded to start at an aligned address.
200 if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
201 return instrprof_error::malformed;
202 // The magic should have the same byte order as in the previous header.
203 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
204 if (Magic != swap(getRawMagic<IntPtrT>()))
205 return instrprof_error::bad_magic;
207 // There's another profile to read, so we need to process the header.
208 auto *Header = reinterpret_cast<const RawHeader *>(CurrentPos);
209 return readHeader(*Header);
212 static uint64_t getRawVersion() {
216 template <class IntPtrT>
218 RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
219 if (swap(Header.Version) != getRawVersion())
220 return error(instrprof_error::unsupported_version);
222 CountersDelta = swap(Header.CountersDelta);
223 NamesDelta = swap(Header.NamesDelta);
224 auto DataSize = swap(Header.DataSize);
225 auto CountersSize = swap(Header.CountersSize);
226 auto NamesSize = swap(Header.NamesSize);
228 ptrdiff_t DataOffset = sizeof(RawHeader);
229 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
230 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
231 size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize;
233 auto *Start = reinterpret_cast<const char *>(&Header);
234 if (Start + ProfileSize > DataBuffer->getBufferEnd())
235 return error(instrprof_error::bad_header);
237 Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
238 DataEnd = Data + DataSize;
239 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
240 NamesStart = Start + NamesOffset;
241 ProfileEnd = Start + ProfileSize;
246 template <class IntPtrT>
248 RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
250 if (std::error_code EC = readNextHeader(ProfileEnd))
254 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
255 uint32_t NumCounters = swap(Data->NumCounters);
256 if (NumCounters == 0)
257 return error(instrprof_error::malformed);
258 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), NumCounters);
261 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
262 if (RawName.data() < NamesStart ||
263 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
264 RawCounts.data() < CountersStart ||
265 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
266 return error(instrprof_error::malformed);
268 // Store the data in Record, byte-swapping as necessary.
269 Record.Hash = swap(Data->FuncHash);
270 Record.Name = RawName;
271 if (ShouldSwapBytes) {
273 Counts.reserve(RawCounts.size());
274 for (uint64_t Count : RawCounts)
275 Counts.push_back(swap(Count));
276 Record.Counts = Counts;
278 Record.Counts = RawCounts;
286 template class RawInstrProfReader<uint32_t>;
287 template class RawInstrProfReader<uint64_t>;
290 InstrProfLookupTrait::hash_value_type
291 InstrProfLookupTrait::ComputeHash(StringRef K) {
292 return IndexedInstrProf::ComputeHash(HashType, K);
295 bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
296 if (DataBuffer.getBufferSize() < 8)
298 using namespace support;
300 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
301 return Magic == IndexedInstrProf::Magic;
304 std::error_code IndexedInstrProfReader::readHeader() {
305 const unsigned char *Start =
306 (const unsigned char *)DataBuffer->getBufferStart();
307 const unsigned char *Cur = Start;
308 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
309 return error(instrprof_error::truncated);
311 using namespace support;
313 // Check the magic number.
314 uint64_t Magic = endian::readNext<uint64_t, little, unaligned>(Cur);
315 if (Magic != IndexedInstrProf::Magic)
316 return error(instrprof_error::bad_magic);
319 FormatVersion = endian::readNext<uint64_t, little, unaligned>(Cur);
320 if (FormatVersion > IndexedInstrProf::Version)
321 return error(instrprof_error::unsupported_version);
323 // Read the maximal function count.
324 MaxFunctionCount = endian::readNext<uint64_t, little, unaligned>(Cur);
326 // Read the hash type and start offset.
327 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
328 endian::readNext<uint64_t, little, unaligned>(Cur));
329 if (HashType > IndexedInstrProf::HashT::Last)
330 return error(instrprof_error::unsupported_hash_type);
331 uint64_t HashOffset = endian::readNext<uint64_t, little, unaligned>(Cur);
333 // The rest of the file is an on disk hash table.
334 Index.reset(InstrProfReaderIndex::Create(Start + HashOffset, Cur, Start,
335 InstrProfLookupTrait(HashType)));
336 // Set up our iterator for readNextRecord.
337 RecordIterator = Index->data_begin();
342 std::error_code IndexedInstrProfReader::getFunctionCounts(
343 StringRef FuncName, uint64_t FuncHash, std::vector<uint64_t> &Counts) {
344 auto Iter = Index->find(FuncName);
345 if (Iter == Index->end())
346 return error(instrprof_error::unknown_function);
348 // Found it. Look for counters with the right hash.
349 ArrayRef<uint64_t> Data = (*Iter).Data;
351 for (uint64_t I = 0, E = Data.size(); I != E; I += NumCounts) {
352 // The function hash comes first.
353 uint64_t FoundHash = Data[I++];
354 // In v1, we have at least one count. Later, we have the number of counts.
356 return error(instrprof_error::malformed);
357 NumCounts = FormatVersion == 1 ? E - I : Data[I++];
358 // If we have more counts than data, this is bogus.
359 if (I + NumCounts > E)
360 return error(instrprof_error::malformed);
361 // Check for a match and fill the vector if there is one.
362 if (FoundHash == FuncHash) {
363 Counts = Data.slice(I, NumCounts);
367 return error(instrprof_error::hash_mismatch);
371 IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
372 // Are we out of records?
373 if (RecordIterator == Index->data_end())
374 return error(instrprof_error::eof);
376 // Record the current function name.
377 Record.Name = (*RecordIterator).Name;
379 ArrayRef<uint64_t> Data = (*RecordIterator).Data;
380 // Valid data starts with a hash and either a count or the number of counts.
381 if (CurrentOffset + 1 > Data.size())
382 return error(instrprof_error::malformed);
383 // First we have a function hash.
384 Record.Hash = Data[CurrentOffset++];
385 // In version 1 we knew the number of counters implicitly, but in newer
386 // versions we store the number of counters next.
388 FormatVersion == 1 ? Data.size() - CurrentOffset : Data[CurrentOffset++];
389 if (CurrentOffset + NumCounts > Data.size())
390 return error(instrprof_error::malformed);
391 // And finally the counts themselves.
392 Record.Counts = Data.slice(CurrentOffset, NumCounts);
394 // If we've exhausted this function's data, increment the record.
395 CurrentOffset += NumCounts;
396 if (CurrentOffset == Data.size()) {