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" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 17 | #include <cassert> |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 21 | static ErrorOr<std::unique_ptr<MemoryBuffer>> |
| 22 | setupMemoryBuffer(std::string Path) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 23 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 24 | MemoryBuffer::getFileOrSTDIN(Path); |
| 25 | if (std::error_code EC = BufferOrErr.getError()) |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 26 | return EC; |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 27 | return std::move(BufferOrErr.get()); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 30 | static std::error_code initializeReader(InstrProfReader &Reader) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 31 | return Reader.readHeader(); |
| 32 | } |
| 33 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 34 | ErrorOr<std::unique_ptr<InstrProfReader>> |
| 35 | InstrProfReader::create(std::string Path) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 36 | // Set up the buffer to read. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 37 | auto BufferOrError = setupMemoryBuffer(Path); |
| 38 | if (std::error_code EC = BufferOrError.getError()) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 39 | return EC; |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 40 | return InstrProfReader::create(std::move(BufferOrError.get())); |
| 41 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 42 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 43 | ErrorOr<std::unique_ptr<InstrProfReader>> |
| 44 | InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { |
| 45 | // Sanity check the buffer. |
| 46 | if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) |
| 47 | return instrprof_error::too_large; |
| 48 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 49 | std::unique_ptr<InstrProfReader> Result; |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 50 | // Create the reader. |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 51 | if (IndexedInstrProfReader::hasFormat(*Buffer)) |
| 52 | Result.reset(new IndexedInstrProfReader(std::move(Buffer))); |
| 53 | else if (RawInstrProfReader64::hasFormat(*Buffer)) |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 54 | Result.reset(new RawInstrProfReader64(std::move(Buffer))); |
| 55 | else if (RawInstrProfReader32::hasFormat(*Buffer)) |
| 56 | Result.reset(new RawInstrProfReader32(std::move(Buffer))); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 57 | else |
Duncan P. N. Exon Smith | 4c5b7cb | 2014-03-21 20:42:34 +0000 | [diff] [blame] | 58 | Result.reset(new TextInstrProfReader(std::move(Buffer))); |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 59 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 60 | // Initialize the reader and return the result. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 61 | if (std::error_code EC = initializeReader(*Result)) |
| 62 | return EC; |
| 63 | |
| 64 | return std::move(Result); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 67 | ErrorOr<std::unique_ptr<IndexedInstrProfReader>> |
| 68 | IndexedInstrProfReader::create(std::string Path) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 69 | // Set up the buffer to read. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 70 | auto BufferOrError = setupMemoryBuffer(Path); |
| 71 | if (std::error_code EC = BufferOrError.getError()) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 72 | return EC; |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 73 | return IndexedInstrProfReader::create(std::move(BufferOrError.get())); |
| 74 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 75 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 76 | |
| 77 | ErrorOr<std::unique_ptr<IndexedInstrProfReader>> |
| 78 | IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { |
| 79 | // Sanity check the buffer. |
| 80 | if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max()) |
| 81 | return instrprof_error::too_large; |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 82 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 83 | // Create the reader. |
| 84 | if (!IndexedInstrProfReader::hasFormat(*Buffer)) |
| 85 | return instrprof_error::bad_magic; |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 86 | auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer)); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 87 | |
| 88 | // Initialize the reader and return the result. |
Justin Bogner | ab89ed7 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 89 | if (std::error_code EC = initializeReader(*Result)) |
| 90 | return EC; |
| 91 | |
| 92 | return std::move(Result); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void InstrProfIterator::Increment() { |
| 96 | if (Reader->readNextRecord(Record)) |
| 97 | *this = InstrProfIterator(); |
| 98 | } |
| 99 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 100 | std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { |
Justin Bogner | cf36a36 | 2014-07-29 22:29:23 +0000 | [diff] [blame] | 101 | // Skip empty lines and comments. |
| 102 | while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 103 | ++Line; |
| 104 | // If we hit EOF while looking for a name, we're done. |
| 105 | if (Line.is_at_end()) |
| 106 | return error(instrprof_error::eof); |
| 107 | |
| 108 | // Read the function name. |
| 109 | Record.Name = *Line++; |
| 110 | |
| 111 | // Read the function hash. |
| 112 | if (Line.is_at_end()) |
| 113 | return error(instrprof_error::truncated); |
Justin Bogner | f95ca07 | 2015-03-09 18:54:49 +0000 | [diff] [blame] | 114 | if ((Line++)->getAsInteger(0, Record.Hash)) |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 115 | return error(instrprof_error::malformed); |
| 116 | |
| 117 | // Read the number of counters. |
| 118 | uint64_t NumCounters; |
| 119 | if (Line.is_at_end()) |
| 120 | return error(instrprof_error::truncated); |
| 121 | if ((Line++)->getAsInteger(10, NumCounters)) |
| 122 | return error(instrprof_error::malformed); |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 123 | if (NumCounters == 0) |
| 124 | return error(instrprof_error::malformed); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 125 | |
| 126 | // Read each counter and fill our internal storage with the values. |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 127 | Record.Counts.clear(); |
| 128 | Record.Counts.reserve(NumCounters); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 129 | for (uint64_t I = 0; I < NumCounters; ++I) { |
| 130 | if (Line.is_at_end()) |
| 131 | return error(instrprof_error::truncated); |
| 132 | uint64_t Count; |
| 133 | if ((Line++)->getAsInteger(10, Count)) |
| 134 | return error(instrprof_error::malformed); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 135 | Record.Counts.push_back(Count); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 136 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 137 | |
| 138 | return success(); |
| 139 | } |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 140 | |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 141 | template <class IntPtrT> |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 142 | bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) { |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 143 | if (DataBuffer.getBufferSize() < sizeof(uint64_t)) |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 144 | return false; |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 145 | uint64_t Magic = |
| 146 | *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart()); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 147 | return RawInstrProf::getMagic<IntPtrT>() == Magic || |
| 148 | sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic; |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | template <class IntPtrT> |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 152 | std::error_code RawInstrProfReader<IntPtrT>::readHeader() { |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 153 | if (!hasFormat(*DataBuffer)) |
| 154 | return error(instrprof_error::bad_magic); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 155 | if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header)) |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 156 | return error(instrprof_error::bad_header); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 157 | auto *Header = reinterpret_cast<const RawInstrProf::Header *>( |
| 158 | DataBuffer->getBufferStart()); |
| 159 | ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>(); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 160 | return readHeader(*Header); |
| 161 | } |
| 162 | |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 163 | template <class IntPtrT> |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 164 | std::error_code |
| 165 | RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) { |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 166 | const char *End = DataBuffer->getBufferEnd(); |
| 167 | // Skip zero padding between profiles. |
| 168 | while (CurrentPos != End && *CurrentPos == 0) |
| 169 | ++CurrentPos; |
| 170 | // If there's nothing left, we're done. |
| 171 | if (CurrentPos == End) |
| 172 | return instrprof_error::eof; |
| 173 | // If there isn't enough space for another header, this is probably just |
| 174 | // garbage at the end of the file. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 175 | if (CurrentPos + sizeof(RawInstrProf::Header) > End) |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 176 | return instrprof_error::malformed; |
Justin Bogner | 54b1128 | 2014-09-12 21:22:55 +0000 | [diff] [blame] | 177 | // The writer ensures each profile is padded to start at an aligned address. |
| 178 | if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>()) |
| 179 | return instrprof_error::malformed; |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 180 | // The magic should have the same byte order as in the previous header. |
| 181 | uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 182 | if (Magic != swap(RawInstrProf::getMagic<IntPtrT>())) |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 183 | return instrprof_error::bad_magic; |
| 184 | |
| 185 | // There's another profile to read, so we need to process the header. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 186 | auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos); |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 187 | return readHeader(*Header); |
| 188 | } |
| 189 | |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 190 | template <class IntPtrT> |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 191 | std::error_code RawInstrProfReader<IntPtrT>::readHeader( |
| 192 | const RawInstrProf::Header &Header) { |
| 193 | if (swap(Header.Version) != RawInstrProf::Version) |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 194 | return error(instrprof_error::unsupported_version); |
| 195 | |
| 196 | CountersDelta = swap(Header.CountersDelta); |
| 197 | NamesDelta = swap(Header.NamesDelta); |
| 198 | auto DataSize = swap(Header.DataSize); |
| 199 | auto CountersSize = swap(Header.CountersSize); |
| 200 | auto NamesSize = swap(Header.NamesSize); |
| 201 | |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 202 | ptrdiff_t DataOffset = sizeof(RawInstrProf::Header); |
| 203 | ptrdiff_t CountersOffset = |
| 204 | DataOffset + sizeof(RawInstrProf::ProfileData<IntPtrT>) * DataSize; |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 205 | ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize; |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 206 | size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize; |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 207 | |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 208 | auto *Start = reinterpret_cast<const char *>(&Header); |
| 209 | if (Start + ProfileSize > DataBuffer->getBufferEnd()) |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 210 | return error(instrprof_error::bad_header); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 211 | |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 212 | Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>( |
| 213 | Start + DataOffset); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 214 | DataEnd = Data + DataSize; |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 215 | CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset); |
| 216 | NamesStart = Start + NamesOffset; |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 217 | ProfileEnd = Start + ProfileSize; |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 218 | |
| 219 | return success(); |
| 220 | } |
| 221 | |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 222 | template <class IntPtrT> |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 223 | std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) { |
| 224 | Record.Name = StringRef(getName(Data->NamePtr), swap(Data->NameSize)); |
| 225 | if (Record.Name.data() < NamesStart || |
| 226 | Record.Name.data() + Record.Name.size() > DataBuffer->getBufferEnd()) |
| 227 | return error(instrprof_error::malformed); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 228 | |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 229 | return success(); |
| 230 | } |
| 231 | |
| 232 | template <class IntPtrT> |
| 233 | std::error_code RawInstrProfReader<IntPtrT>::readFuncHash( |
| 234 | InstrProfRecord &Record) { |
| 235 | Record.Hash = swap(Data->FuncHash); |
| 236 | return success(); |
| 237 | } |
| 238 | |
| 239 | template <class IntPtrT> |
| 240 | std::error_code RawInstrProfReader<IntPtrT>::readRawCounts( |
| 241 | InstrProfRecord &Record) { |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 242 | uint32_t NumCounters = swap(Data->NumCounters); |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 243 | IntPtrT CounterPtr = Data->CounterPtr; |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 244 | if (NumCounters == 0) |
| 245 | return error(instrprof_error::malformed); |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 246 | |
| 247 | auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters); |
| 248 | auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 249 | |
| 250 | // Check bounds. |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 251 | if (RawCounts.data() < CountersStart || |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 252 | RawCounts.data() + RawCounts.size() > NamesStartAsCounter) |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 253 | return error(instrprof_error::malformed); |
| 254 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 255 | if (ShouldSwapBytes) { |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 256 | Record.Counts.clear(); |
| 257 | Record.Counts.reserve(RawCounts.size()); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 258 | for (uint64_t Count : RawCounts) |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 259 | Record.Counts.push_back(swap(Count)); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 260 | } else |
| 261 | Record.Counts = RawCounts; |
| 262 | |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 263 | return success(); |
| 264 | } |
| 265 | |
| 266 | template <class IntPtrT> |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 267 | std::error_code |
| 268 | RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) { |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 269 | if (atEnd()) |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 270 | if (std::error_code EC = readNextHeader(ProfileEnd)) |
| 271 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 272 | |
| 273 | // Read name ad set it in Record. |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 274 | if (std::error_code EC = readName(Record)) |
| 275 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 276 | |
| 277 | // Read FuncHash and set it in Record. |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 278 | if (std::error_code EC = readFuncHash(Record)) |
| 279 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 280 | |
| 281 | // Read raw counts and set Record. |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 282 | if (std::error_code EC = readRawCounts(Record)) |
| 283 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 284 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 285 | // Iterate. |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 286 | advanceData(); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 287 | return success(); |
| 288 | } |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 289 | |
| 290 | namespace llvm { |
| 291 | template class RawInstrProfReader<uint32_t>; |
| 292 | template class RawInstrProfReader<uint64_t>; |
| 293 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 294 | |
Justin Bogner | b5d368e | 2014-04-18 22:00:22 +0000 | [diff] [blame] | 295 | InstrProfLookupTrait::hash_value_type |
| 296 | InstrProfLookupTrait::ComputeHash(StringRef K) { |
| 297 | return IndexedInstrProf::ComputeHash(HashType, K); |
| 298 | } |
| 299 | |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 300 | typedef InstrProfLookupTrait::data_type data_type; |
| 301 | typedef InstrProfLookupTrait::offset_type offset_type; |
| 302 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 303 | bool InstrProfLookupTrait::ReadValueProfilingData( |
| 304 | const unsigned char *&D, const unsigned char *const End) { |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 305 | ErrorOr<std::unique_ptr<IndexedInstrProf::ValueProfData>> VDataPtrOrErr = |
| 306 | IndexedInstrProf::ValueProfData::getValueProfData( |
| 307 | D, End, ValueProfDataEndianness); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 308 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 309 | if (VDataPtrOrErr.getError()) |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 310 | return false; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 311 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 312 | VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), &HashKeys); |
| 313 | D += VDataPtrOrErr.get()->TotalSize; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 314 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 318 | data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, |
| 319 | offset_type N) { |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 320 | // Check if the data is corrupt. If so, don't try to read it. |
| 321 | if (N % sizeof(uint64_t)) |
| 322 | return data_type(); |
| 323 | |
| 324 | DataBuffer.clear(); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 325 | std::vector<uint64_t> CounterBuffer; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 326 | |
| 327 | using namespace support; |
| 328 | const unsigned char *End = D + N; |
| 329 | while (D < End) { |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 330 | // Read hash. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 331 | if (D + sizeof(uint64_t) >= End) |
| 332 | return data_type(); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 333 | uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D); |
| 334 | |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 335 | // Initialize number of counters for FormatVersion == 1. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 336 | uint64_t CountsSize = N / sizeof(uint64_t) - 1; |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 337 | // If format version is different then read the number of counters. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 338 | if (FormatVersion != 1) { |
| 339 | if (D + sizeof(uint64_t) > End) |
| 340 | return data_type(); |
| 341 | CountsSize = endian::readNext<uint64_t, little, unaligned>(D); |
| 342 | } |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 343 | // Read counter values. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 344 | if (D + CountsSize * sizeof(uint64_t) > End) |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 345 | return data_type(); |
| 346 | |
| 347 | CounterBuffer.clear(); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 348 | CounterBuffer.reserve(CountsSize); |
| 349 | for (uint64_t J = 0; J < CountsSize; ++J) |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 350 | CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D)); |
| 351 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 352 | DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer)); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 353 | |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 354 | // Read value profiling data. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 355 | if (FormatVersion > 2 && !ReadValueProfilingData(D, End)) { |
| 356 | DataBuffer.clear(); |
| 357 | return data_type(); |
| 358 | } |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 359 | } |
| 360 | return DataBuffer; |
| 361 | } |
| 362 | |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 363 | std::error_code |
| 364 | InstrProfReaderIndex::getRecords(StringRef FuncName, |
| 365 | ArrayRef<InstrProfRecord> &Data) { |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 366 | auto Iter = Index->find(FuncName); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 367 | if (Iter == Index->end()) |
| 368 | return instrprof_error::unknown_function; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 369 | |
| 370 | Data = (*Iter); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 371 | if (Data.empty()) |
| 372 | return instrprof_error::malformed; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 373 | |
| 374 | return instrprof_error::success; |
| 375 | } |
| 376 | |
| 377 | std::error_code InstrProfReaderIndex::getRecords( |
| 378 | ArrayRef<InstrProfRecord> &Data) { |
| 379 | if (atEnd()) return instrprof_error::eof; |
| 380 | |
| 381 | Data = *RecordIterator; |
| 382 | |
| 383 | if (Data.empty()) return instrprof_error::malformed; |
| 384 | |
| 385 | return instrprof_error::success; |
| 386 | } |
| 387 | |
| 388 | void InstrProfReaderIndex::Init(const unsigned char *Buckets, |
| 389 | const unsigned char *const Payload, |
| 390 | const unsigned char *const Base, |
| 391 | IndexedInstrProf::HashT HashType, |
| 392 | uint64_t Version) { |
| 393 | FormatVersion = Version; |
| 394 | Index.reset(IndexType::Create(Buckets, Payload, Base, |
| 395 | InstrProfLookupTrait(HashType, Version))); |
| 396 | // Form the map of hash values to const char* keys in profiling data. |
| 397 | std::vector<std::pair<uint64_t, const char *>> HashKeys; |
| 398 | for (auto Key : Index->keys()) { |
| 399 | const char *KeyTableRef = StringTable.insertString(Key); |
| 400 | HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef)); |
| 401 | } |
| 402 | std::sort(HashKeys.begin(), HashKeys.end(), less_first()); |
| 403 | HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end()); |
| 404 | // Set the hash key map for the InstrLookupTrait |
| 405 | Index->getInfoObj().setHashKeys(std::move(HashKeys)); |
| 406 | RecordIterator = Index->data_begin(); |
| 407 | } |
| 408 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 409 | bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 410 | if (DataBuffer.getBufferSize() < 8) |
| 411 | return false; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 412 | using namespace support; |
| 413 | uint64_t Magic = |
| 414 | endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart()); |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 415 | // Verify that it's magical. |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 416 | return Magic == IndexedInstrProf::Magic; |
| 417 | } |
| 418 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 419 | std::error_code IndexedInstrProfReader::readHeader() { |
Aaron Ballman | a7c9ed5 | 2014-05-01 17:16:24 +0000 | [diff] [blame] | 420 | const unsigned char *Start = |
| 421 | (const unsigned char *)DataBuffer->getBufferStart(); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 422 | const unsigned char *Cur = Start; |
Aaron Ballman | a7c9ed5 | 2014-05-01 17:16:24 +0000 | [diff] [blame] | 423 | if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 424 | return error(instrprof_error::truncated); |
| 425 | |
| 426 | using namespace support; |
| 427 | |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 428 | auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur); |
| 429 | Cur += sizeof(IndexedInstrProf::Header); |
| 430 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 431 | // Check the magic number. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 432 | uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 433 | if (Magic != IndexedInstrProf::Magic) |
| 434 | return error(instrprof_error::bad_magic); |
| 435 | |
| 436 | // Read the version. |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 437 | uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version); |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 438 | if (FormatVersion > IndexedInstrProf::Version) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 439 | return error(instrprof_error::unsupported_version); |
| 440 | |
| 441 | // Read the maximal function count. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 442 | MaxFunctionCount = |
| 443 | endian::byte_swap<uint64_t, little>(Header->MaxFunctionCount); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 444 | |
| 445 | // Read the hash type and start offset. |
| 446 | IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>( |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 447 | endian::byte_swap<uint64_t, little>(Header->HashType)); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 448 | if (HashType > IndexedInstrProf::HashT::Last) |
| 449 | return error(instrprof_error::unsupported_hash_type); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 450 | |
| 451 | uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 452 | |
| 453 | // The rest of the file is an on disk hash table. |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 454 | Index.Init(Start + HashOffset, Cur, Start, HashType, FormatVersion); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 455 | |
| 456 | return success(); |
| 457 | } |
| 458 | |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 459 | ErrorOr<InstrProfRecord> |
| 460 | IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, |
| 461 | uint64_t FuncHash) { |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 462 | ArrayRef<InstrProfRecord> Data; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 463 | std::error_code EC = Index.getRecords(FuncName, Data); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 464 | if (EC != instrprof_error::success) |
| 465 | return EC; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 466 | // Found it. Look for counters with the right hash. |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 467 | for (unsigned I = 0, E = Data.size(); I < E; ++I) { |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 468 | // Check for a match and fill the vector if there is one. |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 469 | if (Data[I].Hash == FuncHash) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 470 | return std::move(Data[I]); |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | return error(instrprof_error::hash_mismatch); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 476 | std::error_code |
| 477 | IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash, |
| 478 | std::vector<uint64_t> &Counts) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 479 | ErrorOr<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 480 | if (std::error_code EC = Record.getError()) |
| 481 | return EC; |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 482 | |
| 483 | Counts = Record.get().Counts; |
| 484 | return success(); |
| 485 | } |
| 486 | |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 487 | std::error_code IndexedInstrProfReader::readNextRecord( |
| 488 | InstrProfRecord &Record) { |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 489 | static unsigned RecordIndex = 0; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 490 | |
| 491 | ArrayRef<InstrProfRecord> Data; |
| 492 | |
| 493 | std::error_code EC = Index.getRecords(Data); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame^] | 494 | if (EC != instrprof_error::success) |
| 495 | return error(EC); |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 496 | |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 497 | Record = Data[RecordIndex++]; |
| 498 | if (RecordIndex >= Data.size()) { |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 499 | Index.advanceToNextKey(); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 500 | RecordIndex = 0; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 501 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 502 | return success(); |
| 503 | } |