1 //===-- PerfReader.cpp - perfscript reader ---------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 #include "PerfReader.h" 9 #include "ProfileGenerator.h" 10 #include "llvm/Support/FileSystem.h" 11 12 static cl::opt<bool> ShowMmapEvents("show-mmap-events", cl::ReallyHidden, 13 cl::init(false), cl::ZeroOrMore, 14 cl::desc("Print binary load events.")); 15 16 static cl::opt<bool> ShowUnwinderOutput("show-unwinder-output", 17 cl::ReallyHidden, cl::init(false), 18 cl::ZeroOrMore, 19 cl::desc("Print unwinder output")); 20 21 extern cl::opt<bool> ShowDisassemblyOnly; 22 extern cl::opt<bool> ShowSourceLocations; 23 24 namespace llvm { 25 namespace sampleprof { 26 27 void VirtualUnwinder::unwindCall(UnwindState &State) { 28 // The 2nd frame after leaf could be missing if stack sample is 29 // taken when IP is within prolog/epilog, as frame chain isn't 30 // setup yet. Fill in the missing frame in that case. 31 // TODO: Currently we just assume all the addr that can't match the 32 // 2nd frame is in prolog/epilog. In the future, we will switch to 33 // pro/epi tracker(Dwarf CFI) for the precise check. 34 uint64_t Source = State.getCurrentLBRSource(); 35 auto *ParentFrame = State.getParentFrame(); 36 if (ParentFrame == State.getDummyRootPtr() || 37 ParentFrame->Address != Source) { 38 State.switchToFrame(Source); 39 } else { 40 State.popFrame(); 41 } 42 State.InstPtr.update(Source); 43 } 44 45 void VirtualUnwinder::unwindLinear(UnwindState &State, uint64_t Repeat) { 46 InstructionPointer &IP = State.InstPtr; 47 uint64_t Target = State.getCurrentLBRTarget(); 48 uint64_t End = IP.Address; 49 if (Binary->usePseudoProbes()) { 50 // We don't need to top frame probe since it should be extracted 51 // from the range. 52 // The outcome of the virtual unwinding with pseudo probes is a 53 // map from a context key to the address range being unwound. 54 // This means basically linear unwinding is not needed for pseudo 55 // probes. The range will be simply recorded here and will be 56 // converted to a list of pseudo probes to report in ProfileGenerator. 57 State.getParentFrame()->recordRangeCount(Target, End, Repeat); 58 } else { 59 // Unwind linear execution part 60 uint64_t LeafAddr = State.CurrentLeafFrame->Address; 61 while (IP.Address >= Target) { 62 uint64_t PrevIP = IP.Address; 63 IP.backward(); 64 // Break into segments for implicit call/return due to inlining 65 bool SameInlinee = Binary->inlineContextEqual(PrevIP, IP.Address); 66 if (!SameInlinee || PrevIP == Target) { 67 State.switchToFrame(LeafAddr); 68 State.CurrentLeafFrame->recordRangeCount(PrevIP, End, Repeat); 69 End = IP.Address; 70 } 71 LeafAddr = IP.Address; 72 } 73 } 74 } 75 76 void VirtualUnwinder::unwindReturn(UnwindState &State) { 77 // Add extra frame as we unwind through the return 78 const LBREntry &LBR = State.getCurrentLBR(); 79 uint64_t CallAddr = Binary->getCallAddrFromFrameAddr(LBR.Target); 80 State.switchToFrame(CallAddr); 81 State.pushFrame(LBR.Source); 82 State.InstPtr.update(LBR.Source); 83 } 84 85 void VirtualUnwinder::unwindBranchWithinFrame(UnwindState &State) { 86 // TODO: Tolerate tail call for now, as we may see tail call from libraries. 87 // This is only for intra function branches, excluding tail calls. 88 uint64_t Source = State.getCurrentLBRSource(); 89 State.switchToFrame(Source); 90 State.InstPtr.update(Source); 91 } 92 93 std::shared_ptr<StringBasedCtxKey> FrameStack::getContextKey() { 94 std::shared_ptr<StringBasedCtxKey> KeyStr = 95 std::make_shared<StringBasedCtxKey>(); 96 KeyStr->Context = 97 Binary->getExpandedContextStr(Stack, KeyStr->WasLeafInlined); 98 if (KeyStr->Context.empty()) 99 return nullptr; 100 KeyStr->genHashCode(); 101 return KeyStr; 102 } 103 104 std::shared_ptr<ProbeBasedCtxKey> ProbeStack::getContextKey() { 105 std::shared_ptr<ProbeBasedCtxKey> ProbeBasedKey = 106 std::make_shared<ProbeBasedCtxKey>(); 107 for (auto CallProbe : Stack) { 108 ProbeBasedKey->Probes.emplace_back(CallProbe); 109 } 110 CSProfileGenerator::compressRecursionContext<const PseudoProbe *>( 111 ProbeBasedKey->Probes); 112 ProbeBasedKey->genHashCode(); 113 return ProbeBasedKey; 114 } 115 116 template <typename T> 117 void VirtualUnwinder::collectSamplesFromFrame(UnwindState::ProfiledFrame *Cur, 118 T &Stack) { 119 if (Cur->RangeSamples.empty() && Cur->BranchSamples.empty()) 120 return; 121 122 std::shared_ptr<ContextKey> Key = Stack.getContextKey(); 123 if (Key == nullptr) 124 return; 125 auto Ret = CtxCounterMap->emplace(Hashable<ContextKey>(Key), SampleCounter()); 126 SampleCounter &SCounter = Ret.first->second; 127 for (auto &Item : Cur->RangeSamples) { 128 uint64_t StartOffset = Binary->virtualAddrToOffset(std::get<0>(Item)); 129 uint64_t EndOffset = Binary->virtualAddrToOffset(std::get<1>(Item)); 130 SCounter.recordRangeCount(StartOffset, EndOffset, std::get<2>(Item)); 131 } 132 133 for (auto &Item : Cur->BranchSamples) { 134 uint64_t SourceOffset = Binary->virtualAddrToOffset(std::get<0>(Item)); 135 uint64_t TargetOffset = Binary->virtualAddrToOffset(std::get<1>(Item)); 136 SCounter.recordBranchCount(SourceOffset, TargetOffset, std::get<2>(Item)); 137 } 138 } 139 140 template <typename T> 141 void VirtualUnwinder::collectSamplesFromFrameTrie( 142 UnwindState::ProfiledFrame *Cur, T &Stack) { 143 if (!Cur->isDummyRoot()) { 144 if (!Stack.pushFrame(Cur)) { 145 // Process truncated context 146 // Start a new traversal ignoring its bottom context 147 T EmptyStack(Binary); 148 collectSamplesFromFrame(Cur, EmptyStack); 149 for (const auto &Item : Cur->Children) { 150 collectSamplesFromFrameTrie(Item.second.get(), EmptyStack); 151 } 152 return; 153 } 154 } 155 156 collectSamplesFromFrame(Cur, Stack); 157 // Process children frame 158 for (const auto &Item : Cur->Children) { 159 collectSamplesFromFrameTrie(Item.second.get(), Stack); 160 } 161 // Recover the call stack 162 Stack.popFrame(); 163 } 164 165 void VirtualUnwinder::collectSamplesFromFrameTrie( 166 UnwindState::ProfiledFrame *Cur) { 167 if (Binary->usePseudoProbes()) { 168 ProbeStack Stack(Binary); 169 collectSamplesFromFrameTrie<ProbeStack>(Cur, Stack); 170 } else { 171 FrameStack Stack(Binary); 172 collectSamplesFromFrameTrie<FrameStack>(Cur, Stack); 173 } 174 } 175 176 void VirtualUnwinder::recordBranchCount(const LBREntry &Branch, 177 UnwindState &State, uint64_t Repeat) { 178 if (Branch.IsArtificial) 179 return; 180 181 if (Binary->usePseudoProbes()) { 182 // Same as recordRangeCount, We don't need to top frame probe since we will 183 // extract it from branch's source address 184 State.getParentFrame()->recordBranchCount(Branch.Source, Branch.Target, 185 Repeat); 186 } else { 187 State.CurrentLeafFrame->recordBranchCount(Branch.Source, Branch.Target, 188 Repeat); 189 } 190 } 191 192 bool VirtualUnwinder::unwind(const HybridSample *Sample, uint64_t Repeat) { 193 // Capture initial state as starting point for unwinding. 194 UnwindState State(Sample); 195 196 // Sanity check - making sure leaf of LBR aligns with leaf of stack sample 197 // Stack sample sometimes can be unreliable, so filter out bogus ones. 198 if (!State.validateInitialState()) 199 return false; 200 201 // Also do not attempt linear unwind for the leaf range as it's incomplete. 202 bool IsLeaf = true; 203 204 // Now process the LBR samples in parrallel with stack sample 205 // Note that we do not reverse the LBR entry order so we can 206 // unwind the sample stack as we walk through LBR entries. 207 while (State.hasNextLBR()) { 208 State.checkStateConsistency(); 209 210 // Unwind implicit calls/returns from inlining, along the linear path, 211 // break into smaller sub section each with its own calling context. 212 if (!IsLeaf) { 213 unwindLinear(State, Repeat); 214 } 215 IsLeaf = false; 216 217 // Save the LBR branch before it gets unwound. 218 const LBREntry &Branch = State.getCurrentLBR(); 219 220 if (isCallState(State)) { 221 // Unwind calls - we know we encountered call if LBR overlaps with 222 // transition between leaf the 2nd frame. Note that for calls that 223 // were not in the original stack sample, we should have added the 224 // extra frame when processing the return paired with this call. 225 unwindCall(State); 226 } else if (isReturnState(State)) { 227 // Unwind returns - check whether the IP is indeed at a return instruction 228 unwindReturn(State); 229 } else { 230 // Unwind branches - for regular intra function branches, we only 231 // need to record branch with context. 232 unwindBranchWithinFrame(State); 233 } 234 State.advanceLBR(); 235 // Record `branch` with calling context after unwinding. 236 recordBranchCount(Branch, State, Repeat); 237 } 238 // As samples are aggregated on trie, record them into counter map 239 collectSamplesFromFrameTrie(State.getDummyRootPtr()); 240 241 return true; 242 } 243 244 void PerfReader::validateCommandLine( 245 cl::list<std::string> &BinaryFilenames, 246 cl::list<std::string> &PerfTraceFilenames) { 247 // Allow the invalid perfscript if we only use to show binary disassembly 248 if (!ShowDisassemblyOnly) { 249 for (auto &File : PerfTraceFilenames) { 250 if (!llvm::sys::fs::exists(File)) { 251 std::string Msg = "Input perf script(" + File + ") doesn't exist!"; 252 exitWithError(Msg); 253 } 254 } 255 } 256 if (BinaryFilenames.size() > 1) { 257 // TODO: remove this if everything is ready to support multiple binaries. 258 exitWithError( 259 "Currently only support one input binary, multiple binaries' " 260 "profile will be merged in one profile and make profile " 261 "summary info inaccurate. Please use `llvm-perfdata` to merge " 262 "profiles from multiple binaries."); 263 } 264 for (auto &Binary : BinaryFilenames) { 265 if (!llvm::sys::fs::exists(Binary)) { 266 std::string Msg = "Input binary(" + Binary + ") doesn't exist!"; 267 exitWithError(Msg); 268 } 269 } 270 if (CSProfileGenerator::MaxCompressionSize < -1) { 271 exitWithError("Value of --compress-recursion should >= -1"); 272 } 273 if (ShowSourceLocations && !ShowDisassemblyOnly) { 274 exitWithError("--show-source-locations should work together with " 275 "--show-disassembly-only!"); 276 } 277 } 278 279 PerfReader::PerfReader(cl::list<std::string> &BinaryFilenames, 280 cl::list<std::string> &PerfTraceFilenames) { 281 validateCommandLine(BinaryFilenames, PerfTraceFilenames); 282 // Load the binaries. 283 for (auto Filename : BinaryFilenames) 284 loadBinary(Filename, /*AllowNameConflict*/ false); 285 } 286 287 ProfiledBinary &PerfReader::loadBinary(const StringRef BinaryPath, 288 bool AllowNameConflict) { 289 // The binary table is currently indexed by the binary name not the full 290 // binary path. This is because the user-given path may not match the one 291 // that was actually executed. 292 StringRef BinaryName = llvm::sys::path::filename(BinaryPath); 293 294 // Call to load the binary in the ctor of ProfiledBinary. 295 auto Ret = BinaryTable.insert({BinaryName, ProfiledBinary(BinaryPath)}); 296 297 if (!Ret.second && !AllowNameConflict) { 298 std::string ErrorMsg = "Binary name conflict: " + BinaryPath.str() + 299 " and " + Ret.first->second.getPath().str() + " \n"; 300 exitWithError(ErrorMsg); 301 } 302 303 return Ret.first->second; 304 } 305 306 void PerfReader::updateBinaryAddress(const MMapEvent &Event) { 307 // Load the binary. 308 StringRef BinaryPath = Event.BinaryPath; 309 StringRef BinaryName = llvm::sys::path::filename(BinaryPath); 310 311 auto I = BinaryTable.find(BinaryName); 312 // Drop the event which doesn't belong to user-provided binaries 313 // or if its image is loaded at the same address 314 if (I == BinaryTable.end() || Event.BaseAddress == I->second.getBaseAddress()) 315 return; 316 317 ProfiledBinary &Binary = I->second; 318 319 // A binary image could be uploaded and then reloaded at different 320 // place, so update the address map here 321 AddrToBinaryMap.erase(Binary.getBaseAddress()); 322 AddrToBinaryMap[Event.BaseAddress] = &Binary; 323 324 // Update binary load address. 325 Binary.setBaseAddress(Event.BaseAddress); 326 } 327 328 ProfiledBinary *PerfReader::getBinary(uint64_t Address) { 329 auto Iter = AddrToBinaryMap.lower_bound(Address); 330 if (Iter == AddrToBinaryMap.end() || Iter->first != Address) { 331 if (Iter == AddrToBinaryMap.begin()) 332 return nullptr; 333 Iter--; 334 } 335 return Iter->second; 336 } 337 338 // Use ordered map to make the output deterministic 339 using OrderedCounterForPrint = std::map<std::string, RangeSample>; 340 341 static void printSampleCounter(OrderedCounterForPrint &OrderedCounter) { 342 for (auto Range : OrderedCounter) { 343 outs() << Range.first << "\n"; 344 for (auto I : Range.second) { 345 outs() << " (" << format("%" PRIx64, I.first.first) << ", " 346 << format("%" PRIx64, I.first.second) << "): " << I.second << "\n"; 347 } 348 } 349 } 350 351 static std::string getContextKeyStr(ContextKey *K, 352 const ProfiledBinary *Binary) { 353 std::string ContextStr; 354 if (const auto *CtxKey = dyn_cast<StringBasedCtxKey>(K)) { 355 return CtxKey->Context; 356 } else if (const auto *CtxKey = dyn_cast<ProbeBasedCtxKey>(K)) { 357 SmallVector<std::string, 16> ContextStack; 358 for (const auto *Probe : CtxKey->Probes) { 359 Binary->getInlineContextForProbe(Probe, ContextStack, true); 360 } 361 for (const auto &Context : ContextStack) { 362 if (ContextStr.size()) 363 ContextStr += " @ "; 364 ContextStr += Context; 365 } 366 } 367 return ContextStr; 368 } 369 370 static void printRangeCounter(ContextSampleCounterMap &Counter, 371 const ProfiledBinary *Binary) { 372 OrderedCounterForPrint OrderedCounter; 373 for (auto &CI : Counter) { 374 OrderedCounter[getContextKeyStr(CI.first.getPtr(), Binary)] = 375 CI.second.RangeCounter; 376 } 377 printSampleCounter(OrderedCounter); 378 } 379 380 static void printBranchCounter(ContextSampleCounterMap &Counter, 381 const ProfiledBinary *Binary) { 382 OrderedCounterForPrint OrderedCounter; 383 for (auto &CI : Counter) { 384 OrderedCounter[getContextKeyStr(CI.first.getPtr(), Binary)] = 385 CI.second.BranchCounter; 386 } 387 printSampleCounter(OrderedCounter); 388 } 389 390 void PerfReader::printUnwinderOutput() { 391 for (auto I : BinarySampleCounters) { 392 const ProfiledBinary *Binary = I.first; 393 outs() << "Binary(" << Binary->getName().str() << ")'s Range Counter:\n"; 394 printRangeCounter(I.second, Binary); 395 outs() << "\nBinary(" << Binary->getName().str() << ")'s Branch Counter:\n"; 396 printBranchCounter(I.second, Binary); 397 } 398 } 399 400 void PerfReader::unwindSamples() { 401 for (const auto &Item : AggregatedSamples) { 402 const HybridSample *Sample = dyn_cast<HybridSample>(Item.first.getPtr()); 403 VirtualUnwinder Unwinder(&BinarySampleCounters[Sample->Binary], 404 Sample->Binary); 405 Unwinder.unwind(Sample, Item.second); 406 } 407 408 if (ShowUnwinderOutput) 409 printUnwinderOutput(); 410 } 411 412 bool PerfReader::extractLBRStack(TraceStream &TraceIt, 413 SmallVectorImpl<LBREntry> &LBRStack, 414 ProfiledBinary *Binary) { 415 // The raw format of LBR stack is like: 416 // 0x4005c8/0x4005dc/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 ... 417 // ... 0x4005c8/0x4005dc/P/-/-/0 418 // It's in FIFO order and seperated by whitespace. 419 SmallVector<StringRef, 32> Records; 420 TraceIt.getCurrentLine().split(Records, " "); 421 422 // Extract leading instruction pointer if present, use single 423 // list to pass out as reference. 424 size_t Index = 0; 425 if (!Records.empty() && Records[0].find('/') == StringRef::npos) { 426 Index = 1; 427 } 428 // Now extract LBR samples - note that we do not reverse the 429 // LBR entry order so we can unwind the sample stack as we walk 430 // through LBR entries. 431 uint64_t PrevTrDst = 0; 432 433 while (Index < Records.size()) { 434 auto &Token = Records[Index++]; 435 if (Token.size() == 0) 436 continue; 437 438 SmallVector<StringRef, 8> Addresses; 439 Token.split(Addresses, "/"); 440 uint64_t Src; 441 uint64_t Dst; 442 Addresses[0].substr(2).getAsInteger(16, Src); 443 Addresses[1].substr(2).getAsInteger(16, Dst); 444 445 bool SrcIsInternal = Binary->addressIsCode(Src); 446 bool DstIsInternal = Binary->addressIsCode(Dst); 447 bool IsArtificial = false; 448 // Ignore branches outside the current binary. 449 if (!SrcIsInternal && !DstIsInternal) 450 continue; 451 if (!SrcIsInternal && DstIsInternal) { 452 // For transition from external code (such as dynamic libraries) to 453 // the current binary, keep track of the branch target which will be 454 // grouped with the Source of the last transition from the current 455 // binary. 456 PrevTrDst = Dst; 457 continue; 458 } 459 if (SrcIsInternal && !DstIsInternal) { 460 // For transition to external code, group the Source with the next 461 // availabe transition target. 462 if (!PrevTrDst) 463 continue; 464 Dst = PrevTrDst; 465 PrevTrDst = 0; 466 IsArtificial = true; 467 } 468 // TODO: filter out buggy duplicate branches on Skylake 469 470 LBRStack.emplace_back(LBREntry(Src, Dst, IsArtificial)); 471 } 472 TraceIt.advance(); 473 return !LBRStack.empty(); 474 } 475 476 bool PerfReader::extractCallstack(TraceStream &TraceIt, 477 SmallVectorImpl<uint64_t> &CallStack) { 478 // The raw format of call stack is like: 479 // 4005dc # leaf frame 480 // 400634 481 // 400684 # root frame 482 // It's in bottom-up order with each frame in one line. 483 484 // Extract stack frames from sample 485 ProfiledBinary *Binary = nullptr; 486 while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().startswith(" 0x")) { 487 StringRef FrameStr = TraceIt.getCurrentLine().ltrim(); 488 uint64_t FrameAddr = 0; 489 if (FrameStr.getAsInteger(16, FrameAddr)) { 490 // We might parse a non-perf sample line like empty line and comments, 491 // skip it 492 TraceIt.advance(); 493 return false; 494 } 495 TraceIt.advance(); 496 if (!Binary) { 497 Binary = getBinary(FrameAddr); 498 // we might have addr not match the MMAP, skip it 499 if (!Binary) { 500 if (AddrToBinaryMap.size() == 0) 501 WithColor::warning() << "No MMAP event in the perfscript, create it " 502 "with '--show-mmap-events'\n"; 503 break; 504 } 505 } 506 // Currently intermixed frame from different binaries is not supported. 507 // Ignore bottom frames not from binary of interest. 508 if (!Binary->addressIsCode(FrameAddr)) 509 break; 510 511 // We need to translate return address to call address 512 // for non-leaf frames 513 if (!CallStack.empty()) { 514 FrameAddr = Binary->getCallAddrFromFrameAddr(FrameAddr); 515 } 516 517 CallStack.emplace_back(FrameAddr); 518 } 519 520 // Skip other unrelated line, find the next valid LBR line 521 // Note that even for empty call stack, we should skip the address at the 522 // bottom, otherwise the following pass may generate a truncated callstack 523 while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().startswith(" 0x")) { 524 TraceIt.advance(); 525 } 526 // Filter out broken stack sample. We may not have complete frame info 527 // if sample end up in prolog/epilog, the result is dangling context not 528 // connected to entry point. This should be relatively rare thus not much 529 // impact on overall profile quality. However we do want to filter them 530 // out to reduce the number of different calling contexts. One instance 531 // of such case - when sample landed in prolog/epilog, somehow stack 532 // walking will be broken in an unexpected way that higher frames will be 533 // missing. 534 return !CallStack.empty() && 535 !Binary->addressInPrologEpilog(CallStack.front()); 536 } 537 538 void PerfReader::parseHybridSample(TraceStream &TraceIt) { 539 // The raw hybird sample started with call stack in FILO order and followed 540 // intermediately by LBR sample 541 // e.g. 542 // 4005dc # call stack leaf 543 // 400634 544 // 400684 # call stack root 545 // 0x4005c8/0x4005dc/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 ... 546 // ... 0x4005c8/0x4005dc/P/-/-/0 # LBR Entries 547 // 548 std::shared_ptr<HybridSample> Sample = std::make_shared<HybridSample>(); 549 550 // Parsing call stack and populate into HybridSample.CallStack 551 if (!extractCallstack(TraceIt, Sample->CallStack)) { 552 // Skip the next LBR line matched current call stack 553 if (!TraceIt.isAtEoF() && TraceIt.getCurrentLine().startswith(" 0x")) 554 TraceIt.advance(); 555 return; 556 } 557 // Set the binary current sample belongs to 558 Sample->Binary = getBinary(Sample->CallStack.front()); 559 560 if (!TraceIt.isAtEoF() && TraceIt.getCurrentLine().startswith(" 0x")) { 561 // Parsing LBR stack and populate into HybridSample.LBRStack 562 if (extractLBRStack(TraceIt, Sample->LBRStack, Sample->Binary)) { 563 // Canonicalize stack leaf to avoid 'random' IP from leaf frame skew LBR 564 // ranges 565 Sample->CallStack.front() = Sample->LBRStack[0].Target; 566 // Record samples by aggregation 567 Sample->genHashCode(); 568 AggregatedSamples[Hashable<PerfSample>(Sample)]++; 569 } 570 } else { 571 // LBR sample is encoded in single line after stack sample 572 exitWithError("'Hybrid perf sample is corrupted, No LBR sample line"); 573 } 574 } 575 576 void PerfReader::parseMMap2Event(TraceStream &TraceIt) { 577 // Parse a line like: 578 // PERF_RECORD_MMAP2 2113428/2113428: [0x7fd4efb57000(0x204000) @ 0 579 // 08:04 19532229 3585508847]: r-xp /usr/lib64/libdl-2.17.so 580 constexpr static const char *const Pattern = 581 "PERF_RECORD_MMAP2 ([0-9]+)/[0-9]+: " 582 "\\[(0x[a-f0-9]+)\\((0x[a-f0-9]+)\\) @ " 583 "(0x[a-f0-9]+|0) .*\\]: [-a-z]+ (.*)"; 584 // Field 0 - whole line 585 // Field 1 - PID 586 // Field 2 - base address 587 // Field 3 - mmapped size 588 // Field 4 - page offset 589 // Field 5 - binary path 590 enum EventIndex { 591 WHOLE_LINE = 0, 592 PID = 1, 593 BASE_ADDRESS = 2, 594 MMAPPED_SIZE = 3, 595 PAGE_OFFSET = 4, 596 BINARY_PATH = 5 597 }; 598 599 Regex RegMmap2(Pattern); 600 SmallVector<StringRef, 6> Fields; 601 bool R = RegMmap2.match(TraceIt.getCurrentLine(), &Fields); 602 if (!R) { 603 std::string ErrorMsg = "Cannot parse mmap event: Line" + 604 Twine(TraceIt.getLineNumber()).str() + ": " + 605 TraceIt.getCurrentLine().str() + " \n"; 606 exitWithError(ErrorMsg); 607 } 608 MMapEvent Event; 609 Fields[PID].getAsInteger(10, Event.PID); 610 Fields[BASE_ADDRESS].getAsInteger(0, Event.BaseAddress); 611 Fields[MMAPPED_SIZE].getAsInteger(0, Event.Size); 612 Fields[PAGE_OFFSET].getAsInteger(0, Event.Offset); 613 Event.BinaryPath = Fields[BINARY_PATH]; 614 updateBinaryAddress(Event); 615 if (ShowMmapEvents) { 616 outs() << "Mmap: Binary " << Event.BinaryPath << " loaded at " 617 << format("0x%" PRIx64 ":", Event.BaseAddress) << " \n"; 618 } 619 TraceIt.advance(); 620 } 621 622 void PerfReader::parseEventOrSample(TraceStream &TraceIt) { 623 if (TraceIt.getCurrentLine().startswith("PERF_RECORD_MMAP2")) 624 parseMMap2Event(TraceIt); 625 else if (getPerfScriptType() == PERF_LBR_STACK) 626 parseHybridSample(TraceIt); 627 else { 628 // TODO: parse other type sample 629 TraceIt.advance(); 630 } 631 } 632 633 void PerfReader::parseAndAggregateTrace(StringRef Filename) { 634 // Trace line iterator 635 TraceStream TraceIt(Filename); 636 while (!TraceIt.isAtEoF()) 637 parseEventOrSample(TraceIt); 638 } 639 640 void PerfReader::checkAndSetPerfType( 641 cl::list<std::string> &PerfTraceFilenames) { 642 for (auto FileName : PerfTraceFilenames) { 643 PerfScriptType Type = checkPerfScriptType(FileName); 644 if (Type == PERF_INVALID) 645 exitWithError("Invalid perf script input!"); 646 if (PerfType != PERF_UNKNOWN && PerfType != Type) 647 exitWithError("Inconsistent sample among different perf scripts"); 648 PerfType = Type; 649 } 650 } 651 652 void PerfReader::generateRawProfile() { 653 if (getPerfScriptType() == PERF_LBR_STACK) { 654 // Unwind samples if it's hybird sample 655 unwindSamples(); 656 } else if (getPerfScriptType() == PERF_LBR) { 657 // TODO: range overlap computation for regular AutoFDO 658 } 659 } 660 661 void PerfReader::parsePerfTraces(cl::list<std::string> &PerfTraceFilenames) { 662 // Check and set current perfscript type 663 checkAndSetPerfType(PerfTraceFilenames); 664 // Parse perf traces and do aggregation. 665 for (auto Filename : PerfTraceFilenames) 666 parseAndAggregateTrace(Filename); 667 668 generateRawProfile(); 669 } 670 671 } // end namespace sampleprof 672 } // end namespace llvm 673