Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 1 | //===- Trace.cpp - XRay Trace Loading implementation. ---------------------===// |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 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 | // XRay log reader implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 13 | #include "llvm/XRay/Trace.h" |
| 14 | #include "llvm/ADT/STLExtras.h" |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/DataExtractor.h" |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Error.h" |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 18 | #include "llvm/XRay/YAMLXRayRecord.h" |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::xray; |
| 22 | using llvm::yaml::Input; |
| 23 | |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 24 | namespace { |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 25 | using XRayRecordStorage = |
| 26 | std::aligned_storage<sizeof(XRayRecord), alignof(XRayRecord)>::type; |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 27 | |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 28 | // Populates the FileHeader reference by reading the first 32 bytes of the file. |
| 29 | Error readBinaryFormatHeader(StringRef Data, XRayFileHeader &FileHeader) { |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 30 | // FIXME: Maybe deduce whether the data is little or big-endian using some |
| 31 | // magic bytes in the beginning of the file? |
| 32 | |
| 33 | // First 32 bytes of the file will always be the header. We assume a certain |
| 34 | // format here: |
| 35 | // |
| 36 | // (2) uint16 : version |
| 37 | // (2) uint16 : type |
| 38 | // (4) uint32 : bitfield |
| 39 | // (8) uint64 : cycle frequency |
| 40 | // (16) - : padding |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 41 | |
| 42 | DataExtractor HeaderExtractor(Data, true, 8); |
| 43 | uint32_t OffsetPtr = 0; |
| 44 | FileHeader.Version = HeaderExtractor.getU16(&OffsetPtr); |
| 45 | FileHeader.Type = HeaderExtractor.getU16(&OffsetPtr); |
| 46 | uint32_t Bitfield = HeaderExtractor.getU32(&OffsetPtr); |
| 47 | FileHeader.ConstantTSC = Bitfield & 1uL; |
| 48 | FileHeader.NonstopTSC = Bitfield & 1uL << 1; |
| 49 | FileHeader.CycleFrequency = HeaderExtractor.getU64(&OffsetPtr); |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 50 | std::memcpy(&FileHeader.FreeFormData, Data.bytes_begin() + OffsetPtr, 16); |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 51 | if (FileHeader.Version != 1) |
| 52 | return make_error<StringError>( |
| 53 | Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version), |
| 54 | std::make_error_code(std::errc::invalid_argument)); |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 55 | return Error::success(); |
| 56 | } |
| 57 | |
| 58 | Error loadNaiveFormatLog(StringRef Data, XRayFileHeader &FileHeader, |
| 59 | std::vector<XRayRecord> &Records) { |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 60 | if (Data.size() < 32) |
| 61 | return make_error<StringError>( |
| 62 | "Not enough bytes for an XRay log.", |
| 63 | std::make_error_code(std::errc::invalid_argument)); |
| 64 | |
| 65 | if (Data.size() - 32 == 0 || Data.size() % 32 != 0) |
| 66 | return make_error<StringError>( |
| 67 | "Invalid-sized XRay data.", |
| 68 | std::make_error_code(std::errc::invalid_argument)); |
| 69 | |
| 70 | if (auto E = readBinaryFormatHeader(Data, FileHeader)) |
| 71 | return E; |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 72 | |
| 73 | // Each record after the header will be 32 bytes, in the following format: |
| 74 | // |
| 75 | // (2) uint16 : record type |
| 76 | // (1) uint8 : cpu id |
| 77 | // (1) uint8 : type |
| 78 | // (4) sint32 : function id |
| 79 | // (8) uint64 : tsc |
| 80 | // (4) uint32 : thread id |
| 81 | // (12) - : padding |
| 82 | for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(32)) { |
| 83 | DataExtractor RecordExtractor(S, true, 8); |
| 84 | uint32_t OffsetPtr = 0; |
| 85 | Records.emplace_back(); |
| 86 | auto &Record = Records.back(); |
| 87 | Record.RecordType = RecordExtractor.getU16(&OffsetPtr); |
| 88 | Record.CPU = RecordExtractor.getU8(&OffsetPtr); |
| 89 | auto Type = RecordExtractor.getU8(&OffsetPtr); |
| 90 | switch (Type) { |
| 91 | case 0: |
| 92 | Record.Type = RecordTypes::ENTER; |
| 93 | break; |
| 94 | case 1: |
| 95 | Record.Type = RecordTypes::EXIT; |
| 96 | break; |
| 97 | default: |
| 98 | return make_error<StringError>( |
| 99 | Twine("Unknown record type '") + Twine(int{Type}) + "'", |
NAKAMURA Takumi | b09bec2 | 2017-01-11 01:06:57 +0000 | [diff] [blame] | 100 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 101 | } |
| 102 | Record.FuncId = RecordExtractor.getSigned(&OffsetPtr, sizeof(int32_t)); |
| 103 | Record.TSC = RecordExtractor.getU64(&OffsetPtr); |
| 104 | Record.TId = RecordExtractor.getU32(&OffsetPtr); |
| 105 | } |
| 106 | return Error::success(); |
| 107 | } |
| 108 | |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 109 | /// When reading from a Flight Data Recorder mode log, metadata records are |
| 110 | /// sparse compared to packed function records, so we must maintain state as we |
| 111 | /// read through the sequence of entries. This allows the reader to denormalize |
| 112 | /// the CPUId and Thread Id onto each Function Record and transform delta |
| 113 | /// encoded TSC values into absolute encodings on each record. |
| 114 | struct FDRState { |
| 115 | uint16_t CPUId; |
| 116 | uint16_t ThreadId; |
| 117 | uint64_t BaseTSC; |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 118 | |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 119 | /// Encode some of the state transitions for the FDR log reader as explicit |
| 120 | /// checks. These are expectations for the next Record in the stream. |
| 121 | enum class Token { |
| 122 | NEW_BUFFER_RECORD_OR_EOF, |
| 123 | WALLCLOCK_RECORD, |
| 124 | NEW_CPU_ID_RECORD, |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 125 | FUNCTION_SEQUENCE, |
| 126 | SCAN_TO_END_OF_THREAD_BUF, |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 127 | CUSTOM_EVENT_DATA, |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 128 | }; |
| 129 | Token Expects; |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 130 | |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 131 | // Each threads buffer may have trailing garbage to scan over, so we track our |
| 132 | // progress. |
| 133 | uint64_t CurrentBufferSize; |
| 134 | uint64_t CurrentBufferConsumed; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 135 | }; |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 136 | |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 137 | const char *fdrStateToTwine(const FDRState::Token &state) { |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 138 | switch (state) { |
| 139 | case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF: |
| 140 | return "NEW_BUFFER_RECORD_OR_EOF"; |
| 141 | case FDRState::Token::WALLCLOCK_RECORD: |
| 142 | return "WALLCLOCK_RECORD"; |
| 143 | case FDRState::Token::NEW_CPU_ID_RECORD: |
| 144 | return "NEW_CPU_ID_RECORD"; |
| 145 | case FDRState::Token::FUNCTION_SEQUENCE: |
| 146 | return "FUNCTION_SEQUENCE"; |
| 147 | case FDRState::Token::SCAN_TO_END_OF_THREAD_BUF: |
| 148 | return "SCAN_TO_END_OF_THREAD_BUF"; |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 149 | case FDRState::Token::CUSTOM_EVENT_DATA: |
| 150 | return "CUSTOM_EVENT_DATA"; |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 151 | } |
| 152 | return "UNKNOWN"; |
| 153 | } |
| 154 | |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 155 | /// State transition when a NewBufferRecord is encountered. |
| 156 | Error processFDRNewBufferRecord(FDRState &State, uint8_t RecordFirstByte, |
| 157 | DataExtractor &RecordExtractor) { |
| 158 | |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 159 | if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 160 | return make_error<StringError>( |
| 161 | "Malformed log. Read New Buffer record kind out of sequence", |
| 162 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 163 | uint32_t OffsetPtr = 1; // 1 byte into record. |
| 164 | State.ThreadId = RecordExtractor.getU16(&OffsetPtr); |
| 165 | State.Expects = FDRState::Token::WALLCLOCK_RECORD; |
| 166 | return Error::success(); |
| 167 | } |
| 168 | |
| 169 | /// State transition when an EndOfBufferRecord is encountered. |
| 170 | Error processFDREndOfBufferRecord(FDRState &State, uint8_t RecordFirstByte, |
| 171 | DataExtractor &RecordExtractor) { |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 172 | if (State.Expects == FDRState::Token::NEW_BUFFER_RECORD_OR_EOF) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 173 | return make_error<StringError>( |
| 174 | "Malformed log. Received EOB message without current buffer.", |
| 175 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 176 | State.Expects = FDRState::Token::SCAN_TO_END_OF_THREAD_BUF; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 177 | return Error::success(); |
| 178 | } |
| 179 | |
| 180 | /// State transition when a NewCPUIdRecord is encountered. |
| 181 | Error processFDRNewCPUIdRecord(FDRState &State, uint8_t RecordFirstByte, |
| 182 | DataExtractor &RecordExtractor) { |
| 183 | if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE && |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 184 | State.Expects != FDRState::Token::NEW_CPU_ID_RECORD) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 185 | return make_error<StringError>( |
| 186 | "Malformed log. Read NewCPUId record kind out of sequence", |
| 187 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 188 | uint32_t OffsetPtr = 1; // Read starting after the first byte. |
| 189 | State.CPUId = RecordExtractor.getU16(&OffsetPtr); |
| 190 | State.BaseTSC = RecordExtractor.getU64(&OffsetPtr); |
| 191 | State.Expects = FDRState::Token::FUNCTION_SEQUENCE; |
| 192 | return Error::success(); |
| 193 | } |
| 194 | |
| 195 | /// State transition when a TSCWrapRecord (overflow detection) is encountered. |
| 196 | Error processFDRTSCWrapRecord(FDRState &State, uint8_t RecordFirstByte, |
| 197 | DataExtractor &RecordExtractor) { |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 198 | if (State.Expects != FDRState::Token::FUNCTION_SEQUENCE) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 199 | return make_error<StringError>( |
| 200 | "Malformed log. Read TSCWrap record kind out of sequence", |
| 201 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 202 | uint32_t OffsetPtr = 1; // Read starting after the first byte. |
| 203 | State.BaseTSC = RecordExtractor.getU64(&OffsetPtr); |
| 204 | return Error::success(); |
| 205 | } |
| 206 | |
| 207 | /// State transition when a WallTimeMarkerRecord is encountered. |
| 208 | Error processFDRWallTimeRecord(FDRState &State, uint8_t RecordFirstByte, |
| 209 | DataExtractor &RecordExtractor) { |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 210 | if (State.Expects != FDRState::Token::WALLCLOCK_RECORD) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 211 | return make_error<StringError>( |
| 212 | "Malformed log. Read Wallclock record kind out of sequence", |
| 213 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 214 | // We don't encode the wall time into any of the records. |
| 215 | // XRayRecords are concerned with the TSC instead. |
| 216 | State.Expects = FDRState::Token::NEW_CPU_ID_RECORD; |
| 217 | return Error::success(); |
| 218 | } |
| 219 | |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 220 | /// State transition when a CustomEventMarker is encountered. |
| 221 | Error processCustomEventMarker(FDRState &State, uint8_t RecordFirstByte, |
| 222 | DataExtractor &RecordExtractor, |
| 223 | size_t &RecordSize) { |
| 224 | // We can encounter a CustomEventMarker anywhere in the log, so we can handle |
Keith Wyss | 3d0bc9e | 2017-08-02 21:47:27 +0000 | [diff] [blame] | 225 | // it regardless of the expectation. However, we do set the expectation to |
| 226 | // read a set number of fixed bytes, as described in the metadata. |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 227 | uint32_t OffsetPtr = 1; // Read after the first byte. |
| 228 | uint32_t DataSize = RecordExtractor.getU32(&OffsetPtr); |
| 229 | uint64_t TSC = RecordExtractor.getU64(&OffsetPtr); |
| 230 | |
| 231 | // FIXME: Actually represent the record through the API. For now we only skip |
| 232 | // through the data. |
| 233 | (void)TSC; |
| 234 | RecordSize = 16 + DataSize; |
| 235 | return Error::success(); |
| 236 | } |
| 237 | |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 238 | /// Advances the state machine for reading the FDR record type by reading one |
Keith Wyss | e96152a | 2017-04-06 03:32:01 +0000 | [diff] [blame] | 239 | /// Metadata Record and updating the State appropriately based on the kind of |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 240 | /// record encountered. The RecordKind is encoded in the first byte of the |
| 241 | /// Record, which the caller should pass in because they have already read it |
| 242 | /// to determine that this is a metadata record as opposed to a function record. |
| 243 | Error processFDRMetadataRecord(FDRState &State, uint8_t RecordFirstByte, |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 244 | DataExtractor &RecordExtractor, |
| 245 | size_t &RecordSize) { |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 246 | // The remaining 7 bits are the RecordKind enum. |
| 247 | uint8_t RecordKind = RecordFirstByte >> 1; |
| 248 | switch (RecordKind) { |
| 249 | case 0: // NewBuffer |
| 250 | if (auto E = |
| 251 | processFDRNewBufferRecord(State, RecordFirstByte, RecordExtractor)) |
| 252 | return E; |
| 253 | break; |
| 254 | case 1: // EndOfBuffer |
| 255 | if (auto E = processFDREndOfBufferRecord(State, RecordFirstByte, |
| 256 | RecordExtractor)) |
| 257 | return E; |
| 258 | break; |
| 259 | case 2: // NewCPUId |
| 260 | if (auto E = |
| 261 | processFDRNewCPUIdRecord(State, RecordFirstByte, RecordExtractor)) |
| 262 | return E; |
| 263 | break; |
| 264 | case 3: // TSCWrap |
| 265 | if (auto E = |
| 266 | processFDRTSCWrapRecord(State, RecordFirstByte, RecordExtractor)) |
| 267 | return E; |
| 268 | break; |
| 269 | case 4: // WallTimeMarker |
| 270 | if (auto E = |
| 271 | processFDRWallTimeRecord(State, RecordFirstByte, RecordExtractor)) |
| 272 | return E; |
| 273 | break; |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 274 | case 5: // CustomEventMarker |
| 275 | if (auto E = processCustomEventMarker(State, RecordFirstByte, |
| 276 | RecordExtractor, RecordSize)) |
| 277 | return E; |
| 278 | break; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 279 | default: |
| 280 | // Widen the record type to uint16_t to prevent conversion to char. |
| 281 | return make_error<StringError>( |
| 282 | Twine("Illegal metadata record type: ") |
| 283 | .concat(Twine(static_cast<unsigned>(RecordKind))), |
| 284 | std::make_error_code(std::errc::executable_format_error)); |
| 285 | } |
| 286 | return Error::success(); |
| 287 | } |
| 288 | |
| 289 | /// Reads a function record from an FDR format log, appending a new XRayRecord |
| 290 | /// to the vector being populated and updating the State with a new value |
| 291 | /// reference value to interpret TSC deltas. |
| 292 | /// |
| 293 | /// The XRayRecord constructed includes information from the function record |
| 294 | /// processed here as well as Thread ID and CPU ID formerly extracted into |
| 295 | /// State. |
| 296 | Error processFDRFunctionRecord(FDRState &State, uint8_t RecordFirstByte, |
| 297 | DataExtractor &RecordExtractor, |
| 298 | std::vector<XRayRecord> &Records) { |
| 299 | switch (State.Expects) { |
| 300 | case FDRState::Token::NEW_BUFFER_RECORD_OR_EOF: |
| 301 | return make_error<StringError>( |
| 302 | "Malformed log. Received Function Record before new buffer setup.", |
| 303 | std::make_error_code(std::errc::executable_format_error)); |
| 304 | case FDRState::Token::WALLCLOCK_RECORD: |
| 305 | return make_error<StringError>( |
| 306 | "Malformed log. Received Function Record when expecting wallclock.", |
| 307 | std::make_error_code(std::errc::executable_format_error)); |
| 308 | case FDRState::Token::NEW_CPU_ID_RECORD: |
| 309 | return make_error<StringError>( |
| 310 | "Malformed log. Received Function Record before first CPU record.", |
| 311 | std::make_error_code(std::errc::executable_format_error)); |
| 312 | default: |
| 313 | Records.emplace_back(); |
| 314 | auto &Record = Records.back(); |
| 315 | Record.RecordType = 0; // Record is type NORMAL. |
| 316 | // Strip off record type bit and use the next three bits. |
| 317 | uint8_t RecordType = (RecordFirstByte >> 1) & 0x07; |
| 318 | switch (RecordType) { |
| 319 | case static_cast<uint8_t>(RecordTypes::ENTER): |
| 320 | Record.Type = RecordTypes::ENTER; |
| 321 | break; |
| 322 | case static_cast<uint8_t>(RecordTypes::EXIT): |
| 323 | case 2: // TAIL_EXIT is not yet defined in RecordTypes. |
| 324 | Record.Type = RecordTypes::EXIT; |
| 325 | break; |
| 326 | default: |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 327 | // Cast to an unsigned integer to not interpret the record type as a char. |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 328 | return make_error<StringError>( |
| 329 | Twine("Illegal function record type: ") |
| 330 | .concat(Twine(static_cast<unsigned>(RecordType))), |
| 331 | std::make_error_code(std::errc::executable_format_error)); |
| 332 | } |
| 333 | Record.CPU = State.CPUId; |
| 334 | Record.TId = State.ThreadId; |
Keith Wyss | 3d0bc9e | 2017-08-02 21:47:27 +0000 | [diff] [blame] | 335 | // Back up to read first 32 bits, including the 4 we pulled RecordType |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 336 | // and RecordKind out of. The remaining 28 are FunctionId. |
| 337 | uint32_t OffsetPtr = 0; |
| 338 | // Despite function Id being a signed int on XRayRecord, |
| 339 | // when it is written to an FDR format, the top bits are truncated, |
| 340 | // so it is effectively an unsigned value. When we shift off the |
| 341 | // top four bits, we want the shift to be logical, so we read as |
| 342 | // uint32_t. |
| 343 | uint32_t FuncIdBitField = RecordExtractor.getU32(&OffsetPtr); |
| 344 | Record.FuncId = FuncIdBitField >> 4; |
| 345 | // FunctionRecords have a 32 bit delta from the previous absolute TSC |
| 346 | // or TSC delta. If this would overflow, we should read a TSCWrap record |
| 347 | // with an absolute TSC reading. |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 348 | uint64_t NewTSC = State.BaseTSC + RecordExtractor.getU32(&OffsetPtr); |
| 349 | State.BaseTSC = NewTSC; |
| 350 | Record.TSC = NewTSC; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 351 | } |
| 352 | return Error::success(); |
| 353 | } |
| 354 | |
| 355 | /// Reads a log in FDR mode for version 1 of this binary format. FDR mode is |
| 356 | /// defined as part of the compiler-rt project in xray_fdr_logging.h, and such |
| 357 | /// a log consists of the familiar 32 bit XRayHeader, followed by sequences of |
| 358 | /// of interspersed 16 byte Metadata Records and 8 byte Function Records. |
| 359 | /// |
| 360 | /// The following is an attempt to document the grammar of the format, which is |
| 361 | /// parsed by this function for little-endian machines. Since the format makes |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 362 | /// use of BitFields, when we support big-endian architectures, we will need to |
Simon Pilgrim | 68168d1 | 2017-03-30 12:59:53 +0000 | [diff] [blame] | 363 | /// adjust not only the endianness parameter to llvm's RecordExtractor, but also |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 364 | /// the bit twiddling logic, which is consistent with the little-endian |
| 365 | /// convention that BitFields within a struct will first be packed into the |
| 366 | /// least significant bits the address they belong to. |
| 367 | /// |
| 368 | /// We expect a format complying with the grammar in the following pseudo-EBNF. |
| 369 | /// |
| 370 | /// FDRLog: XRayFileHeader ThreadBuffer* |
Keith Wyss | 3d0bc9e | 2017-08-02 21:47:27 +0000 | [diff] [blame] | 371 | /// XRayFileHeader: 32 bytes to identify the log as FDR with machine metadata. |
| 372 | /// Includes BufferSize |
| 373 | /// ThreadBuffer: NewBuffer WallClockTime NewCPUId FunctionSequence EOB |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 374 | /// BufSize: 8 byte unsigned integer indicating how large the buffer is. |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 375 | /// NewBuffer: 16 byte metadata record with Thread Id. |
| 376 | /// WallClockTime: 16 byte metadata record with human readable time. |
| 377 | /// NewCPUId: 16 byte metadata record with CPUId and a 64 bit TSC reading. |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 378 | /// EOB: 16 byte record in a thread buffer plus mem garbage to fill BufSize. |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 379 | /// FunctionSequence: NewCPUId | TSCWrap | FunctionRecord |
| 380 | /// TSCWrap: 16 byte metadata record with a full 64 bit TSC reading. |
| 381 | /// FunctionRecord: 8 byte record with FunctionId, entry/exit, and TSC delta. |
| 382 | Error loadFDRLog(StringRef Data, XRayFileHeader &FileHeader, |
| 383 | std::vector<XRayRecord> &Records) { |
| 384 | if (Data.size() < 32) |
| 385 | return make_error<StringError>( |
| 386 | "Not enough bytes for an XRay log.", |
| 387 | std::make_error_code(std::errc::invalid_argument)); |
| 388 | |
| 389 | // For an FDR log, there are records sized 16 and 8 bytes. |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 390 | // There actually may be no records if no non-trivial functions are |
| 391 | // instrumented. |
| 392 | if (Data.size() % 8 != 0) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 393 | return make_error<StringError>( |
| 394 | "Invalid-sized XRay data.", |
| 395 | std::make_error_code(std::errc::invalid_argument)); |
| 396 | |
| 397 | if (auto E = readBinaryFormatHeader(Data, FileHeader)) |
| 398 | return E; |
| 399 | |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 400 | uint64_t BufferSize = 0; |
| 401 | { |
| 402 | StringRef ExtraDataRef(FileHeader.FreeFormData, 16); |
| 403 | DataExtractor ExtraDataExtractor(ExtraDataRef, true, 8); |
| 404 | uint32_t ExtraDataOffset = 0; |
| 405 | BufferSize = ExtraDataExtractor.getU64(&ExtraDataOffset); |
| 406 | } |
| 407 | FDRState State{0, 0, 0, FDRState::Token::NEW_BUFFER_RECORD_OR_EOF, |
| 408 | BufferSize, 0}; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 409 | // RecordSize will tell the loop how far to seek ahead based on the record |
| 410 | // type that we have just read. |
| 411 | size_t RecordSize = 0; |
| 412 | for (auto S = Data.drop_front(32); !S.empty(); S = S.drop_front(RecordSize)) { |
| 413 | DataExtractor RecordExtractor(S, true, 8); |
| 414 | uint32_t OffsetPtr = 0; |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 415 | if (State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF) { |
| 416 | RecordSize = State.CurrentBufferSize - State.CurrentBufferConsumed; |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 417 | if (S.size() < RecordSize) { |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 418 | return make_error<StringError>( |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 419 | Twine("Incomplete thread buffer. Expected at least ") + |
| 420 | Twine(RecordSize) + " bytes but found " + Twine(S.size()), |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 421 | make_error_code(std::errc::invalid_argument)); |
| 422 | } |
| 423 | State.CurrentBufferConsumed = 0; |
| 424 | State.Expects = FDRState::Token::NEW_BUFFER_RECORD_OR_EOF; |
| 425 | continue; |
| 426 | } |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 427 | uint8_t BitField = RecordExtractor.getU8(&OffsetPtr); |
| 428 | bool isMetadataRecord = BitField & 0x01uL; |
| 429 | if (isMetadataRecord) { |
| 430 | RecordSize = 16; |
Dean Michael Berris | a7bbe44 | 2017-05-12 01:06:41 +0000 | [diff] [blame] | 431 | if (auto E = processFDRMetadataRecord(State, BitField, RecordExtractor, |
| 432 | RecordSize)) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 433 | return E; |
| 434 | } else { // Process Function Record |
| 435 | RecordSize = 8; |
| 436 | if (auto E = processFDRFunctionRecord(State, BitField, RecordExtractor, |
| 437 | Records)) |
| 438 | return E; |
| 439 | } |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 440 | State.CurrentBufferConsumed += RecordSize; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 441 | } |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 442 | |
| 443 | // Having iterated over everything we've been given, we've either consumed |
| 444 | // everything and ended up in the end state, or were told to skip the rest. |
| 445 | bool Finished = State.Expects == FDRState::Token::SCAN_TO_END_OF_THREAD_BUF && |
| 446 | State.CurrentBufferSize == State.CurrentBufferConsumed; |
| 447 | if (State.Expects != FDRState::Token::NEW_BUFFER_RECORD_OR_EOF && !Finished) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 448 | return make_error<StringError>( |
Dean Michael Berris | 60c2487 | 2017-03-29 06:10:12 +0000 | [diff] [blame] | 449 | Twine("Encountered EOF with unexpected state expectation ") + |
| 450 | fdrStateToTwine(State.Expects) + |
| 451 | ". Remaining expected bytes in thread buffer total " + |
| 452 | Twine(State.CurrentBufferSize - State.CurrentBufferConsumed), |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 453 | std::make_error_code(std::errc::executable_format_error)); |
| 454 | |
| 455 | return Error::success(); |
| 456 | } |
| 457 | |
| 458 | Error loadYAMLLog(StringRef Data, XRayFileHeader &FileHeader, |
| 459 | std::vector<XRayRecord> &Records) { |
Dean Michael Berris | f8f909f | 2017-01-10 02:38:11 +0000 | [diff] [blame] | 460 | YAMLXRayTrace Trace; |
| 461 | Input In(Data); |
| 462 | In >> Trace; |
| 463 | if (In.error()) |
| 464 | return make_error<StringError>("Failed loading YAML Data.", In.error()); |
| 465 | |
| 466 | FileHeader.Version = Trace.Header.Version; |
| 467 | FileHeader.Type = Trace.Header.Type; |
| 468 | FileHeader.ConstantTSC = Trace.Header.ConstantTSC; |
| 469 | FileHeader.NonstopTSC = Trace.Header.NonstopTSC; |
| 470 | FileHeader.CycleFrequency = Trace.Header.CycleFrequency; |
| 471 | |
| 472 | if (FileHeader.Version != 1) |
| 473 | return make_error<StringError>( |
| 474 | Twine("Unsupported XRay file version: ") + Twine(FileHeader.Version), |
| 475 | std::make_error_code(std::errc::invalid_argument)); |
| 476 | |
| 477 | Records.clear(); |
| 478 | std::transform(Trace.Records.begin(), Trace.Records.end(), |
| 479 | std::back_inserter(Records), [&](const YAMLXRayRecord &R) { |
| 480 | return XRayRecord{R.RecordType, R.CPU, R.Type, |
| 481 | R.FuncId, R.TSC, R.TId}; |
| 482 | }); |
| 483 | return Error::success(); |
| 484 | } |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 485 | } // namespace |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 486 | |
| 487 | Expected<Trace> llvm::xray::loadTraceFile(StringRef Filename, bool Sort) { |
| 488 | int Fd; |
| 489 | if (auto EC = sys::fs::openFileForRead(Filename, Fd)) { |
| 490 | return make_error<StringError>( |
| 491 | Twine("Cannot read log from '") + Filename + "'", EC); |
| 492 | } |
| 493 | |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 494 | uint64_t FileSize; |
| 495 | if (auto EC = sys::fs::file_size(Filename, FileSize)) { |
| 496 | return make_error<StringError>( |
| 497 | Twine("Cannot read log from '") + Filename + "'", EC); |
| 498 | } |
| 499 | if (FileSize < 4) { |
| 500 | return make_error<StringError>( |
| 501 | Twine("File '") + Filename + "' too small for XRay.", |
Hans Wennborg | 84da661 | 2017-01-12 18:33:14 +0000 | [diff] [blame] | 502 | std::make_error_code(std::errc::executable_format_error)); |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 505 | // Map the opened file into memory and use a StringRef to access it later. |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 506 | std::error_code EC; |
| 507 | sys::fs::mapped_file_region MappedFile( |
| 508 | Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC); |
| 509 | if (EC) { |
| 510 | return make_error<StringError>( |
| 511 | Twine("Cannot read log from '") + Filename + "'", EC); |
| 512 | } |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 513 | auto Data = StringRef(MappedFile.data(), MappedFile.size()); |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 514 | |
| 515 | // Attempt to detect the file type using file magic. We have a slight bias |
| 516 | // towards the binary format, and we do this by making sure that the first 4 |
| 517 | // bytes of the binary file is some combination of the following byte |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 518 | // patterns: (observe the code loading them assumes they're little endian) |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 519 | // |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 520 | // 0x01 0x00 0x00 0x00 - version 1, "naive" format |
| 521 | // 0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 522 | // |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 523 | // YAML files don't typically have those first four bytes as valid text so we |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 524 | // try loading assuming YAML if we don't find these bytes. |
| 525 | // |
| 526 | // Only if we can't load either the binary or the YAML format will we yield an |
| 527 | // error. |
| 528 | StringRef Magic(MappedFile.data(), 4); |
| 529 | DataExtractor HeaderExtractor(Magic, true, 8); |
| 530 | uint32_t OffsetPtr = 0; |
| 531 | uint16_t Version = HeaderExtractor.getU16(&OffsetPtr); |
| 532 | uint16_t Type = HeaderExtractor.getU16(&OffsetPtr); |
| 533 | |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 534 | enum BinaryFormatType { NAIVE_FORMAT = 0, FLIGHT_DATA_RECORDER_FORMAT = 1 }; |
| 535 | |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 536 | Trace T; |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 537 | if (Version == 1 && Type == NAIVE_FORMAT) { |
| 538 | if (auto E = |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 539 | loadNaiveFormatLog(Data, T.FileHeader, T.Records)) |
Dean Michael Berris | 4f83c4d | 2017-02-17 01:47:16 +0000 | [diff] [blame] | 540 | return std::move(E); |
| 541 | } else if (Version == 1 && Type == FLIGHT_DATA_RECORDER_FORMAT) { |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 542 | if (auto E = loadFDRLog(Data, T.FileHeader, T.Records)) |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 543 | return std::move(E); |
| 544 | } else { |
Martin Pelikan | d78db15 | 2017-09-15 04:22:16 +0000 | [diff] [blame] | 545 | if (auto E = loadYAMLLog(Data, T.FileHeader, T.Records)) |
Dean Michael Berris | d6c1865 | 2017-01-11 06:39:09 +0000 | [diff] [blame] | 546 | return std::move(E); |
| 547 | } |
| 548 | |
| 549 | if (Sort) |
| 550 | std::sort(T.Records.begin(), T.Records.end(), |
| 551 | [&](const XRayRecord &L, const XRayRecord &R) { |
| 552 | return L.TSC < R.TSC; |
| 553 | }); |
| 554 | |
| 555 | return std::move(T); |
| 556 | } |