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