InstrProf: Read raw binary profile in llvm-profdata
[oota-llvm.git] / lib / ProfileData / InstrProfReader.cpp
1 //=-- 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"
17
18 #include <cassert>
19
20 using namespace llvm;
21
22 static 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
34 error_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
44   if (Buffer->getBufferSize() < sizeof(uint64_t)) {
45     Result.reset(new TextInstrProfReader(Buffer));
46     Result->readHeader();
47     return instrprof_error::success;
48   }
49
50   uint64_t Magic = *(uint64_t *)Buffer->getBufferStart();
51   uint64_t SwappedMagic = sys::SwapByteOrder(Magic);
52   if (Magic == getRawMagic() || SwappedMagic == getRawMagic())
53     Result.reset(new RawInstrProfReader(Buffer));
54   else
55     Result.reset(new TextInstrProfReader(Buffer));
56   Result->readHeader();
57   return instrprof_error::success;
58 }
59
60 void InstrProfIterator::Increment() {
61   if (Reader->readNextRecord(Record))
62     *this = InstrProfIterator();
63 }
64
65 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
66   // Skip empty lines.
67   while (!Line.is_at_end() && Line->empty())
68     ++Line;
69   // If we hit EOF while looking for a name, we're done.
70   if (Line.is_at_end())
71     return error(instrprof_error::eof);
72
73   // Read the function name.
74   Record.Name = *Line++;
75
76   // Read the function hash.
77   if (Line.is_at_end())
78     return error(instrprof_error::truncated);
79   if ((Line++)->getAsInteger(10, Record.Hash))
80     return error(instrprof_error::malformed);
81
82   // Read the number of counters.
83   uint64_t NumCounters;
84   if (Line.is_at_end())
85     return error(instrprof_error::truncated);
86   if ((Line++)->getAsInteger(10, NumCounters))
87     return error(instrprof_error::malformed);
88
89   // Read each counter and fill our internal storage with the values.
90   Counts.clear();
91   Counts.reserve(NumCounters);
92   for (uint64_t I = 0; I < NumCounters; ++I) {
93     if (Line.is_at_end())
94       return error(instrprof_error::truncated);
95     uint64_t Count;
96     if ((Line++)->getAsInteger(10, Count))
97       return error(instrprof_error::malformed);
98     Counts.push_back(Count);
99   }
100   // Give the record a reference to our internal counter storage.
101   Record.Counts = Counts;
102
103   return success();
104 }
105
106 static uint64_t getRawVersion() {
107   return 1;
108 }
109 namespace {
110 }
111 RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
112     : DataBuffer(DataBuffer.release()) { }
113
114 error_code RawInstrProfReader::readHeader() {
115   if (DataBuffer->getBufferSize() < sizeof(RawHeader))
116     return error(instrprof_error::malformed);
117   const RawHeader *Header = (RawHeader *)DataBuffer->getBufferStart();
118   if (Header->Magic == getRawMagic())
119     ShouldSwapBytes = false;
120   else {
121     if (sys::SwapByteOrder(Header->Magic) != getRawMagic())
122       return error(instrprof_error::malformed);
123
124     ShouldSwapBytes = true;
125   }
126   return readHeader(*Header);
127 }
128
129 error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
130   if (swap(Header.Version) != getRawVersion())
131     return error(instrprof_error::unsupported_version);
132
133   CountersDelta = swap(Header.CountersDelta);
134   NamesDelta = swap(Header.NamesDelta);
135   auto DataSize = swap(Header.DataSize);
136   auto CountersSize = swap(Header.CountersSize);
137   auto NamesSize = swap(Header.NamesSize);
138
139   ptrdiff_t DataOffset = sizeof(RawHeader);
140   ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
141   ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
142   size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
143
144   if (FileSize != DataBuffer->getBufferSize())
145     return error(instrprof_error::malformed);
146
147   Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
148   DataEnd = Data + DataSize;
149   CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
150   NamesStart = DataBuffer->getBufferStart() + NamesOffset;
151
152   return success();
153 }
154
155 error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
156   if (Data == DataEnd)
157     return error(instrprof_error::eof);
158
159   // Get the raw data.
160   StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
161   auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
162                                 swap(Data->NumCounters));
163
164   // Check bounds.
165   if (RawName.data() < NamesStart ||
166       RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
167       RawCounts.data() < CountersStart ||
168       RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
169     return error(instrprof_error::malformed);
170
171   // Store the data in Record, byte-swapping as necessary.
172   Record.Hash = swap(Data->FuncHash);
173   Record.Name = RawName;
174   if (ShouldSwapBytes) {
175     Counts.clear();
176     Counts.reserve(RawCounts.size());
177     for (uint64_t Count : RawCounts)
178       Counts.push_back(swap(Count));
179     Record.Counts = Counts;
180   } else
181     Record.Counts = RawCounts;
182
183   // Iterate.
184   ++Data;
185   return success();
186 }