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"
22 error_code InstrProfReader::create(std::string Path,
23 std::unique_ptr<InstrProfReader> &Result) {
24 std::unique_ptr<MemoryBuffer> Buffer;
25 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
28 // Sanity check the file.
29 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
30 return instrprof_error::too_large;
33 if (RawInstrProfReader64::hasFormat(*Buffer))
34 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
35 else if (RawInstrProfReader32::hasFormat(*Buffer))
36 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
38 Result.reset(new TextInstrProfReader(std::move(Buffer)));
40 // Read the header and return the result.
41 return Result->readHeader();
44 void InstrProfIterator::Increment() {
45 if (Reader->readNextRecord(Record))
46 *this = InstrProfIterator();
49 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
51 while (!Line.is_at_end() && Line->empty())
53 // If we hit EOF while looking for a name, we're done.
55 return error(instrprof_error::eof);
57 // Read the function name.
58 Record.Name = *Line++;
60 // Read the function hash.
62 return error(instrprof_error::truncated);
63 if ((Line++)->getAsInteger(10, Record.Hash))
64 return error(instrprof_error::malformed);
66 // Read the number of counters.
69 return error(instrprof_error::truncated);
70 if ((Line++)->getAsInteger(10, NumCounters))
71 return error(instrprof_error::malformed);
73 // Read each counter and fill our internal storage with the values.
75 Counts.reserve(NumCounters);
76 for (uint64_t I = 0; I < NumCounters; ++I) {
78 return error(instrprof_error::truncated);
80 if ((Line++)->getAsInteger(10, Count))
81 return error(instrprof_error::malformed);
82 Counts.push_back(Count);
84 // Give the record a reference to our internal counter storage.
85 Record.Counts = Counts;
90 template <class IntPtrT>
91 static uint64_t getRawMagic();
94 uint64_t getRawMagic<uint64_t>() {
100 uint64_t('o') << 24 |
101 uint64_t('f') << 16 |
107 uint64_t getRawMagic<uint32_t>() {
109 uint64_t(255) << 56 |
110 uint64_t('l') << 48 |
111 uint64_t('p') << 40 |
112 uint64_t('r') << 32 |
113 uint64_t('o') << 24 |
114 uint64_t('f') << 16 |
119 template <class IntPtrT>
120 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
121 if (DataBuffer.getBufferSize() < sizeof(getRawMagic<IntPtrT>()))
123 const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
124 return getRawMagic<IntPtrT>() == Header->Magic ||
125 sys::SwapByteOrder(getRawMagic<IntPtrT>()) == Header->Magic;
128 template <class IntPtrT>
129 error_code RawInstrProfReader<IntPtrT>::readHeader() {
130 if (!hasFormat(*DataBuffer))
131 return error(instrprof_error::bad_magic);
132 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
133 return error(instrprof_error::bad_header);
134 const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart();
135 ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
136 return readHeader(*Header);
139 static uint64_t getRawVersion() {
143 template <class IntPtrT>
144 error_code RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
145 if (swap(Header.Version) != getRawVersion())
146 return error(instrprof_error::unsupported_version);
148 CountersDelta = swap(Header.CountersDelta);
149 NamesDelta = swap(Header.NamesDelta);
150 auto DataSize = swap(Header.DataSize);
151 auto CountersSize = swap(Header.CountersSize);
152 auto NamesSize = swap(Header.NamesSize);
154 ptrdiff_t DataOffset = sizeof(RawHeader);
155 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
156 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
157 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
159 if (FileSize != DataBuffer->getBufferSize())
160 return error(instrprof_error::bad_header);
162 Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
163 DataEnd = Data + DataSize;
164 CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
165 NamesStart = DataBuffer->getBufferStart() + NamesOffset;
170 template <class IntPtrT>
172 RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
174 return error(instrprof_error::eof);
177 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
178 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
179 swap(Data->NumCounters));
182 if (RawName.data() < NamesStart ||
183 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
184 RawCounts.data() < CountersStart ||
185 RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
186 return error(instrprof_error::malformed);
188 // Store the data in Record, byte-swapping as necessary.
189 Record.Hash = swap(Data->FuncHash);
190 Record.Name = RawName;
191 if (ShouldSwapBytes) {
193 Counts.reserve(RawCounts.size());
194 for (uint64_t Count : RawCounts)
195 Counts.push_back(swap(Count));
196 Record.Counts = Counts;
198 Record.Counts = RawCounts;
206 template class RawInstrProfReader<uint32_t>;
207 template class RawInstrProfReader<uint64_t>;