Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 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" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 17 | |
| 18 | #include <cassert> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 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)) |
| 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 Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame^] | 32 | // Create the reader. |
| 33 | if (RawInstrProfReader::hasFormat(*Buffer)) |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 34 | Result.reset(new RawInstrProfReader(Buffer)); |
| 35 | else |
| 36 | Result.reset(new TextInstrProfReader(Buffer)); |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame^] | 37 | |
| 38 | // Read the header and return the result. |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 39 | return Result->readHeader(); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void InstrProfIterator::Increment() { |
| 43 | if (Reader->readNextRecord(Record)) |
| 44 | *this = InstrProfIterator(); |
| 45 | } |
| 46 | |
| 47 | error_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 Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 87 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 88 | RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer) |
| 89 | : DataBuffer(DataBuffer.release()) { } |
| 90 | |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame^] | 91 | static uint64_t getRawMagic() { |
| 92 | return |
| 93 | uint64_t('l') << 56 | |
| 94 | uint64_t('p') << 48 | |
| 95 | uint64_t('r') << 40 | |
| 96 | uint64_t('o') << 32 | |
| 97 | uint64_t('f') << 24 | |
| 98 | uint64_t('r') << 16 | |
| 99 | uint64_t('a') << 8 | |
| 100 | uint64_t('w'); |
| 101 | } |
| 102 | |
| 103 | bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { |
| 104 | if (DataBuffer.getBufferSize() < sizeof(getRawMagic())) |
| 105 | return false; |
| 106 | const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart(); |
| 107 | return getRawMagic() == Header->Magic || |
| 108 | sys::SwapByteOrder(getRawMagic()) == Header->Magic; |
| 109 | } |
| 110 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 111 | error_code RawInstrProfReader::readHeader() { |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame^] | 112 | if (!hasFormat(*DataBuffer)) |
| 113 | return error(instrprof_error::bad_magic); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 114 | if (DataBuffer->getBufferSize() < sizeof(RawHeader)) |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 115 | return error(instrprof_error::bad_header); |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame^] | 116 | const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart(); |
| 117 | ShouldSwapBytes = Header->Magic != getRawMagic(); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 118 | return readHeader(*Header); |
| 119 | } |
| 120 | |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame^] | 121 | static uint64_t getRawVersion() { |
| 122 | return 1; |
| 123 | } |
| 124 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 125 | error_code RawInstrProfReader::readHeader(const RawHeader &Header) { |
| 126 | if (swap(Header.Version) != getRawVersion()) |
| 127 | return error(instrprof_error::unsupported_version); |
| 128 | |
| 129 | CountersDelta = swap(Header.CountersDelta); |
| 130 | NamesDelta = swap(Header.NamesDelta); |
| 131 | auto DataSize = swap(Header.DataSize); |
| 132 | auto CountersSize = swap(Header.CountersSize); |
| 133 | auto NamesSize = swap(Header.NamesSize); |
| 134 | |
| 135 | ptrdiff_t DataOffset = sizeof(RawHeader); |
| 136 | ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize; |
| 137 | ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize; |
| 138 | size_t FileSize = NamesOffset + sizeof(char) * NamesSize; |
| 139 | |
| 140 | if (FileSize != DataBuffer->getBufferSize()) |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 141 | return error(instrprof_error::bad_header); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 142 | |
| 143 | Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset); |
| 144 | DataEnd = Data + DataSize; |
| 145 | CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset); |
| 146 | NamesStart = DataBuffer->getBufferStart() + NamesOffset; |
| 147 | |
| 148 | return success(); |
| 149 | } |
| 150 | |
| 151 | error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { |
| 152 | if (Data == DataEnd) |
| 153 | return error(instrprof_error::eof); |
| 154 | |
| 155 | // Get the raw data. |
| 156 | StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize)); |
| 157 | auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), |
| 158 | swap(Data->NumCounters)); |
| 159 | |
| 160 | // Check bounds. |
| 161 | if (RawName.data() < NamesStart || |
| 162 | RawName.data() + RawName.size() > DataBuffer->getBufferEnd() || |
| 163 | RawCounts.data() < CountersStart || |
| 164 | RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart) |
| 165 | return error(instrprof_error::malformed); |
| 166 | |
| 167 | // Store the data in Record, byte-swapping as necessary. |
| 168 | Record.Hash = swap(Data->FuncHash); |
| 169 | Record.Name = RawName; |
| 170 | if (ShouldSwapBytes) { |
| 171 | Counts.clear(); |
| 172 | Counts.reserve(RawCounts.size()); |
| 173 | for (uint64_t Count : RawCounts) |
| 174 | Counts.push_back(swap(Count)); |
| 175 | Record.Counts = Counts; |
| 176 | } else |
| 177 | Record.Counts = RawCounts; |
| 178 | |
| 179 | // Iterate. |
| 180 | ++Data; |
| 181 | return success(); |
| 182 | } |