blob: d2e5fbd9b9a132c33d4e76f3b2a40965cefb608e [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
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000022static uint64_t getRawMagic() {
23 return
24 uint64_t('l') << 56 |
25 uint64_t('p') << 48 |
26 uint64_t('r') << 40 |
27 uint64_t('o') << 32 |
28 uint64_t('f') << 24 |
29 uint64_t('r') << 16 |
30 uint64_t('a') << 8 |
31 uint64_t('w');
32}
33
Justin Bognerf8d79192014-03-21 17:24:48 +000034error_code InstrProfReader::create(std::string Path,
35 std::unique_ptr<InstrProfReader> &Result) {
36 std::unique_ptr<MemoryBuffer> Buffer;
37 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
38 return EC;
39
40 // Sanity check the file.
41 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
42 return instrprof_error::too_large;
43
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000044 if (Buffer->getBufferSize() < sizeof(uint64_t)) {
45 Result.reset(new TextInstrProfReader(Buffer));
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000046 return Result->readHeader();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000047 }
Justin Bognerf8d79192014-03-21 17:24:48 +000048
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +000049 uint64_t Magic = *(uint64_t *)Buffer->getBufferStart();
50 uint64_t SwappedMagic = sys::SwapByteOrder(Magic);
51 if (Magic == getRawMagic() || SwappedMagic == getRawMagic())
52 Result.reset(new RawInstrProfReader(Buffer));
53 else
54 Result.reset(new TextInstrProfReader(Buffer));
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000055 return Result->readHeader();
Justin Bognerf8d79192014-03-21 17:24:48 +000056}
57
58void InstrProfIterator::Increment() {
59 if (Reader->readNextRecord(Record))
60 *this = InstrProfIterator();
61}
62
63error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
64 // Skip empty lines.
65 while (!Line.is_at_end() && Line->empty())
66 ++Line;
67 // If we hit EOF while looking for a name, we're done.
68 if (Line.is_at_end())
69 return error(instrprof_error::eof);
70
71 // Read the function name.
72 Record.Name = *Line++;
73
74 // Read the function hash.
75 if (Line.is_at_end())
76 return error(instrprof_error::truncated);
77 if ((Line++)->getAsInteger(10, Record.Hash))
78 return error(instrprof_error::malformed);
79
80 // Read the number of counters.
81 uint64_t NumCounters;
82 if (Line.is_at_end())
83 return error(instrprof_error::truncated);
84 if ((Line++)->getAsInteger(10, NumCounters))
85 return error(instrprof_error::malformed);
86
87 // Read each counter and fill our internal storage with the values.
88 Counts.clear();
89 Counts.reserve(NumCounters);
90 for (uint64_t I = 0; I < NumCounters; ++I) {
91 if (Line.is_at_end())
92 return error(instrprof_error::truncated);
93 uint64_t Count;
94 if ((Line++)->getAsInteger(10, Count))
95 return error(instrprof_error::malformed);
96 Counts.push_back(Count);
97 }
98 // Give the record a reference to our internal counter storage.
99 Record.Counts = Counts;
100
101 return success();
102}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000103
104static uint64_t getRawVersion() {
105 return 1;
106}
107namespace {
108}
109RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
110 : DataBuffer(DataBuffer.release()) { }
111
112error_code RawInstrProfReader::readHeader() {
113 if (DataBuffer->getBufferSize() < sizeof(RawHeader))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000114 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000115 const RawHeader *Header = (RawHeader *)DataBuffer->getBufferStart();
116 if (Header->Magic == getRawMagic())
117 ShouldSwapBytes = false;
118 else {
119 if (sys::SwapByteOrder(Header->Magic) != getRawMagic())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000120 return error(instrprof_error::bad_magic);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000121
122 ShouldSwapBytes = true;
123 }
124 return readHeader(*Header);
125}
126
127error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
128 if (swap(Header.Version) != getRawVersion())
129 return error(instrprof_error::unsupported_version);
130
131 CountersDelta = swap(Header.CountersDelta);
132 NamesDelta = swap(Header.NamesDelta);
133 auto DataSize = swap(Header.DataSize);
134 auto CountersSize = swap(Header.CountersSize);
135 auto NamesSize = swap(Header.NamesSize);
136
137 ptrdiff_t DataOffset = sizeof(RawHeader);
138 ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
139 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
140 size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
141
142 if (FileSize != DataBuffer->getBufferSize())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000143 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000144
145 Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
146 DataEnd = Data + DataSize;
147 CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
148 NamesStart = DataBuffer->getBufferStart() + NamesOffset;
149
150 return success();
151}
152
153error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
154 if (Data == DataEnd)
155 return error(instrprof_error::eof);
156
157 // Get the raw data.
158 StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
159 auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
160 swap(Data->NumCounters));
161
162 // Check bounds.
163 if (RawName.data() < NamesStart ||
164 RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
165 RawCounts.data() < CountersStart ||
166 RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
167 return error(instrprof_error::malformed);
168
169 // Store the data in Record, byte-swapping as necessary.
170 Record.Hash = swap(Data->FuncHash);
171 Record.Name = RawName;
172 if (ShouldSwapBytes) {
173 Counts.clear();
174 Counts.reserve(RawCounts.size());
175 for (uint64_t Count : RawCounts)
176 Counts.push_back(swap(Count));
177 Record.Counts = Counts;
178 } else
179 Record.Counts = RawCounts;
180
181 // Iterate.
182 ++Data;
183 return success();
184}