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 IsExternal = !SrcIsInternal && !DstIsInternal;
448     bool IsIncoming = !SrcIsInternal && DstIsInternal;
449     bool IsOutgoing = SrcIsInternal && !DstIsInternal;
450     bool IsArtificial = false;
451 
452     // Ignore branches outside the current binary.
453     if (IsExternal)
454       continue;
455 
456     if (IsOutgoing) {
457       if (!PrevTrDst) {
458         // This is unpaired outgoing jump which is likely due to interrupt or
459         // incomplete LBR trace. Ignore current and subsequent entries since
460         // they are likely in different contexts.
461         break;
462       }
463 
464       if (Binary->addressIsReturn(Src)) {
465         // In a callback case, a return from internal code, say A, to external
466         // runtime can happen. The external runtime can then call back to
467         // another internal routine, say B. Making an artificial branch that
468         // looks like a return from A to B can confuse the unwinder to treat
469         // the instruction before B as the call instruction.
470         break;
471       }
472 
473       // For transition to external code, group the Source with the next
474       // availabe transition target.
475       Dst = PrevTrDst;
476       PrevTrDst = 0;
477       IsArtificial = true;
478     } else {
479       if (PrevTrDst) {
480         // If we have seen an incoming transition from external code to internal
481         // code, but not a following outgoing transition, the incoming
482         // transition is likely due to interrupt which is usually unpaired.
483         // Ignore current and subsequent entries since they are likely in
484         // different contexts.
485         break;
486       }
487 
488       if (IsIncoming) {
489         // For transition from external code (such as dynamic libraries) to
490         // the current binary, keep track of the branch target which will be
491         // grouped with the Source of the last transition from the current
492         // binary.
493         PrevTrDst = Dst;
494         continue;
495       }
496     }
497 
498     // TODO: filter out buggy duplicate branches on Skylake
499 
500     LBRStack.emplace_back(LBREntry(Src, Dst, IsArtificial));
501   }
502   TraceIt.advance();
503   return !LBRStack.empty();
504 }
505 
506 bool PerfReader::extractCallstack(TraceStream &TraceIt,
507                                   SmallVectorImpl<uint64_t> &CallStack) {
508   // The raw format of call stack is like:
509   //            4005dc      # leaf frame
510   //	          400634
511   //	          400684      # root frame
512   // It's in bottom-up order with each frame in one line.
513 
514   // Extract stack frames from sample
515   ProfiledBinary *Binary = nullptr;
516   while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().startswith(" 0x")) {
517     StringRef FrameStr = TraceIt.getCurrentLine().ltrim();
518     uint64_t FrameAddr = 0;
519     if (FrameStr.getAsInteger(16, FrameAddr)) {
520       // We might parse a non-perf sample line like empty line and comments,
521       // skip it
522       TraceIt.advance();
523       return false;
524     }
525     TraceIt.advance();
526     if (!Binary) {
527       Binary = getBinary(FrameAddr);
528       // we might have addr not match the MMAP, skip it
529       if (!Binary) {
530         if (AddrToBinaryMap.size() == 0)
531           WithColor::warning() << "No MMAP event in the perfscript, create it "
532                                   "with '--show-mmap-events'\n";
533         break;
534       }
535     }
536     // Currently intermixed frame from different binaries is not supported.
537     // Ignore bottom frames not from binary of interest.
538     if (!Binary->addressIsCode(FrameAddr))
539       break;
540 
541     // We need to translate return address to call address
542     // for non-leaf frames
543     if (!CallStack.empty()) {
544       FrameAddr = Binary->getCallAddrFromFrameAddr(FrameAddr);
545     }
546 
547     CallStack.emplace_back(FrameAddr);
548   }
549 
550   // Skip other unrelated line, find the next valid LBR line
551   // Note that even for empty call stack, we should skip the address at the
552   // bottom, otherwise the following pass may generate a truncated callstack
553   while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().startswith(" 0x")) {
554     TraceIt.advance();
555   }
556   // Filter out broken stack sample. We may not have complete frame info
557   // if sample end up in prolog/epilog, the result is dangling context not
558   // connected to entry point. This should be relatively rare thus not much
559   // impact on overall profile quality. However we do want to filter them
560   // out to reduce the number of different calling contexts. One instance
561   // of such case - when sample landed in prolog/epilog, somehow stack
562   // walking will be broken in an unexpected way that higher frames will be
563   // missing.
564   return !CallStack.empty() &&
565          !Binary->addressInPrologEpilog(CallStack.front());
566 }
567 
568 void PerfReader::parseHybridSample(TraceStream &TraceIt) {
569   // The raw hybird sample started with call stack in FILO order and followed
570   // intermediately by LBR sample
571   // e.g.
572   // 	          4005dc    # call stack leaf
573   //	          400634
574   //	          400684    # call stack root
575   // 0x4005c8/0x4005dc/P/-/-/0   0x40062f/0x4005b0/P/-/-/0 ...
576   //          ... 0x4005c8/0x4005dc/P/-/-/0    # LBR Entries
577   //
578   std::shared_ptr<HybridSample> Sample = std::make_shared<HybridSample>();
579 
580   // Parsing call stack and populate into HybridSample.CallStack
581   if (!extractCallstack(TraceIt, Sample->CallStack)) {
582     // Skip the next LBR line matched current call stack
583     if (!TraceIt.isAtEoF() && TraceIt.getCurrentLine().startswith(" 0x"))
584       TraceIt.advance();
585     return;
586   }
587   // Set the binary current sample belongs to
588   Sample->Binary = getBinary(Sample->CallStack.front());
589 
590   if (!TraceIt.isAtEoF() && TraceIt.getCurrentLine().startswith(" 0x")) {
591     // Parsing LBR stack and populate into HybridSample.LBRStack
592     if (extractLBRStack(TraceIt, Sample->LBRStack, Sample->Binary)) {
593       // Canonicalize stack leaf to avoid 'random' IP from leaf frame skew LBR
594       // ranges
595       Sample->CallStack.front() = Sample->LBRStack[0].Target;
596       // Record samples by aggregation
597       Sample->genHashCode();
598       AggregatedSamples[Hashable<PerfSample>(Sample)]++;
599     }
600   } else {
601     // LBR sample is encoded in single line after stack sample
602     exitWithError("'Hybrid perf sample is corrupted, No LBR sample line");
603   }
604 }
605 
606 void PerfReader::parseMMap2Event(TraceStream &TraceIt) {
607   // Parse a line like:
608   //  PERF_RECORD_MMAP2 2113428/2113428: [0x7fd4efb57000(0x204000) @ 0
609   //  08:04 19532229 3585508847]: r-xp /usr/lib64/libdl-2.17.so
610   constexpr static const char *const Pattern =
611       "PERF_RECORD_MMAP2 ([0-9]+)/[0-9]+: "
612       "\\[(0x[a-f0-9]+)\\((0x[a-f0-9]+)\\) @ "
613       "(0x[a-f0-9]+|0) .*\\]: [-a-z]+ (.*)";
614   // Field 0 - whole line
615   // Field 1 - PID
616   // Field 2 - base address
617   // Field 3 - mmapped size
618   // Field 4 - page offset
619   // Field 5 - binary path
620   enum EventIndex {
621     WHOLE_LINE = 0,
622     PID = 1,
623     BASE_ADDRESS = 2,
624     MMAPPED_SIZE = 3,
625     PAGE_OFFSET = 4,
626     BINARY_PATH = 5
627   };
628 
629   Regex RegMmap2(Pattern);
630   SmallVector<StringRef, 6> Fields;
631   bool R = RegMmap2.match(TraceIt.getCurrentLine(), &Fields);
632   if (!R) {
633     std::string ErrorMsg = "Cannot parse mmap event: Line" +
634                            Twine(TraceIt.getLineNumber()).str() + ": " +
635                            TraceIt.getCurrentLine().str() + " \n";
636     exitWithError(ErrorMsg);
637   }
638   MMapEvent Event;
639   Fields[PID].getAsInteger(10, Event.PID);
640   Fields[BASE_ADDRESS].getAsInteger(0, Event.BaseAddress);
641   Fields[MMAPPED_SIZE].getAsInteger(0, Event.Size);
642   Fields[PAGE_OFFSET].getAsInteger(0, Event.Offset);
643   Event.BinaryPath = Fields[BINARY_PATH];
644   updateBinaryAddress(Event);
645   if (ShowMmapEvents) {
646     outs() << "Mmap: Binary " << Event.BinaryPath << " loaded at "
647            << format("0x%" PRIx64 ":", Event.BaseAddress) << " \n";
648   }
649   TraceIt.advance();
650 }
651 
652 void PerfReader::parseEventOrSample(TraceStream &TraceIt) {
653   if (TraceIt.getCurrentLine().startswith("PERF_RECORD_MMAP2"))
654     parseMMap2Event(TraceIt);
655   else if (getPerfScriptType() == PERF_LBR_STACK)
656     parseHybridSample(TraceIt);
657   else {
658     // TODO: parse other type sample
659     TraceIt.advance();
660   }
661 }
662 
663 void PerfReader::parseAndAggregateTrace(StringRef Filename) {
664   // Trace line iterator
665   TraceStream TraceIt(Filename);
666   while (!TraceIt.isAtEoF())
667     parseEventOrSample(TraceIt);
668 }
669 
670 void PerfReader::checkAndSetPerfType(
671     cl::list<std::string> &PerfTraceFilenames) {
672   for (auto FileName : PerfTraceFilenames) {
673     PerfScriptType Type = checkPerfScriptType(FileName);
674     if (Type == PERF_INVALID)
675       exitWithError("Invalid perf script input!");
676     if (PerfType != PERF_UNKNOWN && PerfType != Type)
677       exitWithError("Inconsistent sample among different perf scripts");
678     PerfType = Type;
679   }
680 }
681 
682 void PerfReader::generateRawProfile() {
683   if (getPerfScriptType() == PERF_LBR_STACK) {
684     // Unwind samples if it's hybird sample
685     unwindSamples();
686   } else if (getPerfScriptType() == PERF_LBR) {
687     // TODO: range overlap computation for regular AutoFDO
688   }
689 }
690 
691 void PerfReader::parsePerfTraces(cl::list<std::string> &PerfTraceFilenames) {
692   // Check and set current perfscript type
693   checkAndSetPerfType(PerfTraceFilenames);
694   // Parse perf traces and do aggregation.
695   for (auto Filename : PerfTraceFilenames)
696     parseAndAggregateTrace(Filename);
697 
698   generateRawProfile();
699 }
700 
701 } // end namespace sampleprof
702 } // end namespace llvm
703