blob: 48e740eaf42223a6b51b7ecba6c595138c0d5f6a [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
18#include <cassert>
19
20using namespace llvm;
21
22error_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))
26 return EC;
27
28 // Sanity check the file.
29 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
30 return instrprof_error::too_large;
31
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000032 // Create the reader.
33 if (RawInstrProfReader::hasFormat(*Buffer))
Duncan P. N. Exon Smith4c5b7cb2014-03-21 20:42:34 +000034 Result.reset(new RawInstrProfReader(std::move(Buffer)));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000035 else
Duncan P. N. Exon Smith4c5b7cb2014-03-21 20:42:34 +000036 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000037
38 // Read the header and return the result.
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000039 return Result->readHeader();
Justin Bognerf8d79192014-03-21 17:24:48 +000040}
41
42void InstrProfIterator::Increment() {
43 if (Reader->readNextRecord(Record))
44 *this = InstrProfIterator();
45}
46
47error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
48 // Skip empty lines.
49 while (!Line.is_at_end() && Line->empty())
50 ++Line;
51 // If we hit EOF while looking for a name, we're done.
52 if (Line.is_at_end())
53 return error(instrprof_error::eof);
54
55 // Read the function name.
56 Record.Name = *Line++;
57
58 // Read the function hash.
59 if (Line.is_at_end())
60 return error(instrprof_error::truncated);
61 if ((Line++)->getAsInteger(10, Record.Hash))
62 return error(instrprof_error::malformed);
63
64 // Read the number of counters.
65 uint64_t NumCounters;
66 if (Line.is_at_end())
67 return error(instrprof_error::truncated);
68 if ((Line++)->getAsInteger(10, NumCounters))
69 return error(instrprof_error::malformed);
70
71 // Read each counter and fill our internal storage with the values.
72 Counts.clear();
73 Counts.reserve(NumCounters);
74 for (uint64_t I = 0; I < NumCounters; ++I) {
75 if (Line.is_at_end())
76 return error(instrprof_error::truncated);
77 uint64_t Count;
78 if ((Line++)->getAsInteger(10, Count))
79 return error(instrprof_error::malformed);
80 Counts.push_back(Count);
81 }
82 // Give the record a reference to our internal counter storage.
83 Record.Counts = Counts;
84
85 return success();
86}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000087
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000088static uint64_t getRawMagic() {
89 return
Duncan P. N. Exon Smith745a2bf2014-03-21 20:42:37 +000090 uint64_t(255) << 56 |
91 uint64_t('l') << 48 |
92 uint64_t('p') << 40 |
93 uint64_t('r') << 32 |
94 uint64_t('o') << 24 |
95 uint64_t('f') << 16 |
96 uint64_t('r') << 8 |
97 uint64_t(129);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000098}
99
100bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
101 if (DataBuffer.getBufferSize() < sizeof(getRawMagic()))
102 return false;
103 const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
104 return getRawMagic() == Header->Magic ||
105 sys::SwapByteOrder(getRawMagic()) == Header->Magic;
106}
107
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000108error_code RawInstrProfReader::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000109 if (!hasFormat(*DataBuffer))
110 return error(instrprof_error::bad_magic);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000111 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000112 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000113 const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart();
114 ShouldSwapBytes = Header->Magic != getRawMagic();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000115 return readHeader(*Header);
116}
117
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000118static uint64_t getRawVersion() {
119 return 1;
120}
121
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000122error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
123 if (swap(Header.Version) != getRawVersion())
124 return error(instrprof_error::unsupported_version);
125
126 CountersDelta = swap(Header.CountersDelta);
127 NamesDelta = swap(Header.NamesDelta);
128 auto DataSize = swap(Header.DataSize);
129 auto CountersSize = swap(Header.CountersSize);
130 auto NamesSize = swap(Header.NamesSize);
131
132 ptrdiff_t DataOffset = sizeof(RawHeader);
133 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
134 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
135 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
136
137 if (FileSize != DataBuffer->getBufferSize())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000138 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000139
140 Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
141 DataEnd = Data + DataSize;
142 CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
143 NamesStart = DataBuffer->getBufferStart() + NamesOffset;
144
145 return success();
146}
147
148error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
149 if (Data == DataEnd)
150 return error(instrprof_error::eof);
151
152 // Get the raw data.
153 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
154 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
155 swap(Data->NumCounters));
156
157 // Check bounds.
158 if (RawName.data() < NamesStart ||
159 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
160 RawCounts.data() < CountersStart ||
161 RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
162 return error(instrprof_error::malformed);
163
164 // Store the data in Record, byte-swapping as necessary.
165 Record.Hash = swap(Data->FuncHash);
166 Record.Name = RawName;
167 if (ShouldSwapBytes) {
168 Counts.clear();
169 Counts.reserve(RawCounts.size());
170 for (uint64_t Count : RawCounts)
171 Counts.push_back(swap(Count));
172 Record.Counts = Counts;
173 } else
174 Record.Counts = RawCounts;
175
176 // Iterate.
177 ++Data;
178 return success();
179}