1 //===- xray-stacks.cpp: XRay Function Call Stack Accounting ---------------===//
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 //
9 // This file implements stack-based accounting. It takes XRay traces, and
10 // collates statistics across these traces to show a breakdown of time spent
11 // at various points of the stack to provide insight into which functions
12 // spend the most time in terms of a call stack. We provide a few
13 // sorting/filtering options for zero'ing in on the useful stacks.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include <forward_list>
18 #include <numeric>
19 
20 #include "func-id-helper.h"
21 #include "trie-node.h"
22 #include "xray-registry.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Errc.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/FormatAdapters.h"
28 #include "llvm/Support/FormatVariadic.h"
29 #include "llvm/XRay/Graph.h"
30 #include "llvm/XRay/InstrumentationMap.h"
31 #include "llvm/XRay/Trace.h"
32 
33 using namespace llvm;
34 using namespace llvm::xray;
35 
36 static cl::SubCommand Stack("stack", "Call stack accounting");
37 static cl::list<std::string> StackInputs(cl::Positional,
38                                          cl::desc("<xray trace>"), cl::Required,
39                                          cl::sub(Stack), cl::OneOrMore);
40 
41 static cl::opt<bool>
42     StackKeepGoing("keep-going", cl::desc("Keep going on errors encountered"),
43                    cl::sub(Stack), cl::init(false));
44 static cl::alias StackKeepGoing2("k", cl::aliasopt(StackKeepGoing),
45                                  cl::desc("Alias for -keep-going"));
46 
47 // TODO: Does there need to be an option to deduce tail or sibling calls?
48 
49 static cl::opt<std::string> StacksInstrMap(
50     "instr_map",
51     cl::desc("instrumentation map used to identify function ids. "
52              "Currently supports elf file instrumentation maps."),
53     cl::sub(Stack), cl::init(""));
54 static cl::alias StacksInstrMap2("m", cl::aliasopt(StacksInstrMap),
55                                  cl::desc("Alias for -instr_map"));
56 
57 static cl::opt<bool>
58     SeparateThreadStacks("per-thread-stacks",
59                          cl::desc("Report top stacks within each thread id"),
60                          cl::sub(Stack), cl::init(false));
61 
62 static cl::opt<bool>
63     AggregateThreads("aggregate-threads",
64                      cl::desc("Aggregate stack times across threads"),
65                      cl::sub(Stack), cl::init(false));
66 
67 static cl::opt<bool>
68     DumpAllStacks("all-stacks",
69                   cl::desc("Dump sum of timings for all stacks. "
70                            "By default separates stacks per-thread."),
71                   cl::sub(Stack), cl::init(false));
72 static cl::alias DumpAllStacksShort("all", cl::aliasopt(DumpAllStacks),
73                                     cl::desc("Alias for -all-stacks"));
74 
75 // TODO(kpw): Add other interesting formats. Perhaps chrome trace viewer format
76 // possibly with aggregations or just a linear trace of timings.
77 enum StackOutputFormat { HUMAN, FLAMETOOL };
78 
79 static cl::opt<StackOutputFormat> StacksOutputFormat(
80     "stack-format",
81     cl::desc("The format that output stacks should be "
82              "output in. Only applies with all-stacks."),
83     cl::values(
84         clEnumValN(HUMAN, "human",
85                    "Human readable output. Only valid without -all-stacks."),
86         clEnumValN(FLAMETOOL, "flame",
87                    "Format consumable by Brendan Gregg's FlameGraph tool. "
88                    "Only valid with -all-stacks.")),
89     cl::sub(Stack), cl::init(HUMAN));
90 
91 // Types of values for each stack in a CallTrie.
92 enum class AggregationType {
93   TOTAL_TIME,      // The total time spent in a stack and its callees.
94   INVOCATION_COUNT // The number of times the stack was invoked.
95 };
96 
97 static cl::opt<AggregationType> RequestedAggregation(
98     "aggregation-type",
99     cl::desc("The type of aggregation to do on call stacks."),
100     cl::values(
101         clEnumValN(
102             AggregationType::TOTAL_TIME, "time",
103             "Capture the total time spent in an all invocations of a stack."),
104         clEnumValN(AggregationType::INVOCATION_COUNT, "count",
105                    "Capture the number of times a stack was invoked. "
106                    "In flamegraph mode, this count also includes invocations "
107                    "of all callees.")),
108     cl::sub(Stack), cl::init(AggregationType::TOTAL_TIME));
109 
110 /// A helper struct to work with formatv and XRayRecords. Makes it easier to
111 /// use instrumentation map names or addresses in formatted output.
112 struct format_xray_record : public FormatAdapter<XRayRecord> {
113   explicit format_xray_record(XRayRecord record,
114                               const FuncIdConversionHelper &conv)
115       : FormatAdapter<XRayRecord>(std::move(record)), Converter(&conv) {}
116   void format(raw_ostream &Stream, StringRef Style) override {
117     Stream << formatv(
118         "{FuncId: \"{0}\", ThreadId: \"{1}\", RecordType: \"{2}\"}",
119         Converter->SymbolOrNumber(Item.FuncId), Item.TId,
120         DecodeRecordType(Item.RecordType));
121   }
122 
123 private:
124   Twine DecodeRecordType(uint16_t recordType) {
125     switch (recordType) {
126     case 0:
127       return Twine("Fn Entry");
128     case 1:
129       return Twine("Fn Exit");
130     default:
131       // TODO: Add Tail exit when it is added to llvm/XRay/XRayRecord.h
132       return Twine("Unknown");
133     }
134   }
135 
136   const FuncIdConversionHelper *Converter;
137 };
138 
139 /// The stack command will take a set of XRay traces as arguments, and collects
140 /// information about the stacks of instrumented functions that appear in the
141 /// traces. We track the following pieces of information:
142 ///
143 ///   - Total time: amount of time/cycles accounted for in the traces.
144 ///   - Stack count: number of times a specific stack appears in the
145 ///     traces. Only instrumented functions show up in stacks.
146 ///   - Cumulative stack time: amount of time spent in a stack accumulated
147 ///     across the invocations in the traces.
148 ///   - Cumulative local time: amount of time spent in each instrumented
149 ///     function showing up in a specific stack, accumulated across the traces.
150 ///
151 /// Example output for the kind of data we'd like to provide looks like the
152 /// following:
153 ///
154 ///   Total time: 3.33234 s
155 ///   Stack ID: ...
156 ///   Stack Count: 2093
157 ///   #     Function                  Local Time     (%)      Stack Time     (%)
158 ///   0     main                         2.34 ms   0.07%      3.33234  s    100%
159 ///   1     foo()                     3.30000  s  99.02%         3.33  s  99.92%
160 ///   2     bar()                          30 ms   0.90%           30 ms   0.90%
161 ///
162 /// We can also show distributions of the function call durations with
163 /// statistics at each level of the stack. This works by doing the following
164 /// algorithm:
165 ///
166 ///   1. When unwinding, record the duration of each unwound function associated
167 ///   with the path up to which the unwinding stops. For example:
168 ///
169 ///        Step                         Duration (? means has start time)
170 ///
171 ///        push a <start time>           a = ?
172 ///        push b <start time>           a = ?, a->b = ?
173 ///        push c <start time>           a = ?, a->b = ?, a->b->c = ?
174 ///        pop  c <end time>             a = ?, a->b = ?, emit duration(a->b->c)
175 ///        pop  b <end time>             a = ?, emit duration(a->b)
176 ///        push c <start time>           a = ?, a->c = ?
177 ///        pop  c <end time>             a = ?, emit duration(a->c)
178 ///        pop  a <end time>             emit duration(a)
179 ///
180 ///   2. We then account for the various stacks we've collected, and for each of
181 ///      them will have measurements that look like the following (continuing
182 ///      with the above simple example):
183 ///
184 ///        c : [<id("a->b->c"), [durations]>, <id("a->c"), [durations]>]
185 ///        b : [<id("a->b"), [durations]>]
186 ///        a : [<id("a"), [durations]>]
187 ///
188 ///      This allows us to compute, for each stack id, and each function that
189 ///      shows up in the stack,  some important statistics like:
190 ///
191 ///        - median
192 ///        - 99th percentile
193 ///        - mean + stddev
194 ///        - count
195 ///
196 ///   3. For cases where we don't have durations for some of the higher levels
197 ///   of the stack (perhaps instrumentation wasn't activated when the stack was
198 ///   entered), we can mark them appropriately.
199 ///
200 ///  Computing this data also allows us to implement lookup by call stack nodes,
201 ///  so that we can find functions that show up in multiple stack traces and
202 ///  show the statistical properties of that function in various contexts. We
203 ///  can compute information similar to the following:
204 ///
205 ///    Function: 'c'
206 ///    Stacks: 2 / 2
207 ///    Stack ID: ...
208 ///    Stack Count: ...
209 ///    #     Function  ...
210 ///    0     a         ...
211 ///    1     b         ...
212 ///    2     c         ...
213 ///
214 ///    Stack ID: ...
215 ///    Stack Count: ...
216 ///    #     Function  ...
217 ///    0     a         ...
218 ///    1     c         ...
219 ///    ----------------...
220 ///
221 ///    Function: 'b'
222 ///    Stacks:  1 / 2
223 ///    Stack ID: ...
224 ///    Stack Count: ...
225 ///    #     Function  ...
226 ///    0     a         ...
227 ///    1     b         ...
228 ///    2     c         ...
229 ///
230 ///
231 /// To do this we require a Trie data structure that will allow us to represent
232 /// all the call stacks of instrumented functions in an easily traversible
233 /// manner when we do the aggregations and lookups. For instrumented call
234 /// sequences like the following:
235 ///
236 ///   a()
237 ///    b()
238 ///     c()
239 ///     d()
240 ///    c()
241 ///
242 /// We will have a representation like so:
243 ///
244 ///   a -> b -> c
245 ///   |    |
246 ///   |    +--> d
247 ///   |
248 ///   +--> c
249 ///
250 /// We maintain a sequence of durations on the leaves and in the internal nodes
251 /// as we go through and process every record from the XRay trace. We also
252 /// maintain an index of unique functions, and provide a means of iterating
253 /// through all the instrumented call stacks which we know about.
254 
255 namespace {
256 struct StackDuration {
257   llvm::SmallVector<int64_t, 4> TerminalDurations;
258   llvm::SmallVector<int64_t, 4> IntermediateDurations;
259 };
260 } // namespace
261 
262 static StackDuration mergeStackDuration(const StackDuration &Left,
263                                         const StackDuration &Right) {
264   StackDuration Data{};
265   Data.TerminalDurations.reserve(Left.TerminalDurations.size() +
266                                  Right.TerminalDurations.size());
267   Data.IntermediateDurations.reserve(Left.IntermediateDurations.size() +
268                                      Right.IntermediateDurations.size());
269   // Aggregate the durations.
270   for (auto duration : Left.TerminalDurations)
271     Data.TerminalDurations.push_back(duration);
272   for (auto duration : Right.TerminalDurations)
273     Data.TerminalDurations.push_back(duration);
274 
275   for (auto duration : Left.IntermediateDurations)
276     Data.IntermediateDurations.push_back(duration);
277   for (auto duration : Right.IntermediateDurations)
278     Data.IntermediateDurations.push_back(duration);
279   return Data;
280 }
281 
282 using StackTrieNode = TrieNode<StackDuration>;
283 
284 template <AggregationType AggType>
285 static std::size_t GetValueForStack(const StackTrieNode *Node);
286 
287 // When computing total time spent in a stack, we're adding the timings from
288 // its callees and the timings from when it was a leaf.
289 template <>
290 std::size_t
291 GetValueForStack<AggregationType::TOTAL_TIME>(const StackTrieNode *Node) {
292   auto TopSum = std::accumulate(Node->ExtraData.TerminalDurations.begin(),
293                                 Node->ExtraData.TerminalDurations.end(), 0uLL);
294   return std::accumulate(Node->ExtraData.IntermediateDurations.begin(),
295                          Node->ExtraData.IntermediateDurations.end(), TopSum);
296 }
297 
298 // Calculates how many times a function was invoked.
299 // TODO: Hook up option to produce stacks
300 template <>
301 std::size_t
302 GetValueForStack<AggregationType::INVOCATION_COUNT>(const StackTrieNode *Node) {
303   return Node->ExtraData.TerminalDurations.size() +
304          Node->ExtraData.IntermediateDurations.size();
305 }
306 
307 // Make sure there are implementations for each enum value.
308 template <AggregationType T> struct DependentFalseType : std::false_type {};
309 
310 template <AggregationType AggType>
311 std::size_t GetValueForStack(const StackTrieNode *Node) {
312   static_assert(DependentFalseType<AggType>::value,
313                 "No implementation found for aggregation type provided.");
314   return 0;
315 }
316 
317 class StackTrie {
318   // Avoid the magic number of 4 propagated through the code with an alias.
319   // We use this SmallVector to track the root nodes in a call graph.
320   using RootVector = SmallVector<StackTrieNode *, 4>;
321 
322   // We maintain pointers to the roots of the tries we see.
323   DenseMap<uint32_t, RootVector> Roots;
324 
325   // We make sure all the nodes are accounted for in this list.
326   std::forward_list<StackTrieNode> NodeStore;
327 
328   // A map of thread ids to pairs call stack trie nodes and their start times.
329   DenseMap<uint32_t, SmallVector<std::pair<StackTrieNode *, uint64_t>, 8>>
330       ThreadStackMap;
331 
332   StackTrieNode *createTrieNode(uint32_t ThreadId, int32_t FuncId,
333                                 StackTrieNode *Parent) {
334     NodeStore.push_front(StackTrieNode{FuncId, Parent, {}, {{}, {}}});
335     auto I = NodeStore.begin();
336     auto *Node = &*I;
337     if (!Parent)
338       Roots[ThreadId].push_back(Node);
339     return Node;
340   }
341 
342   StackTrieNode *findRootNode(uint32_t ThreadId, int32_t FuncId) {
343     const auto &RootsByThread = Roots[ThreadId];
344     auto I = find_if(RootsByThread,
345                      [&](StackTrieNode *N) { return N->FuncId == FuncId; });
346     return (I == RootsByThread.end()) ? nullptr : *I;
347   }
348 
349 public:
350   enum class AccountRecordStatus {
351     OK,              // Successfully processed
352     ENTRY_NOT_FOUND, // An exit record had no matching call stack entry
353     UNKNOWN_RECORD_TYPE
354   };
355 
356   struct AccountRecordState {
357     // We keep track of whether the call stack is currently unwinding.
358     bool wasLastRecordExit;
359 
360     static AccountRecordState CreateInitialState() { return {false}; }
361   };
362 
363   AccountRecordStatus accountRecord(const XRayRecord &R,
364                                     AccountRecordState *state) {
365     auto &TS = ThreadStackMap[R.TId];
366     switch (R.Type) {
367     case RecordTypes::CUSTOM_EVENT:
368     case RecordTypes::TYPED_EVENT:
369       return AccountRecordStatus::OK;
370     case RecordTypes::ENTER:
371     case RecordTypes::ENTER_ARG: {
372       state->wasLastRecordExit = false;
373       // When we encounter a new function entry, we want to record the TSC for
374       // that entry, and the function id. Before doing so we check the top of
375       // the stack to see if there are callees that already represent this
376       // function.
377       if (TS.empty()) {
378         auto *Root = findRootNode(R.TId, R.FuncId);
379         TS.emplace_back(Root ? Root : createTrieNode(R.TId, R.FuncId, nullptr),
380                         R.TSC);
381         return AccountRecordStatus::OK;
382       }
383 
384       auto &Top = TS.back();
385       auto I = find_if(Top.first->Callees,
386                        [&](StackTrieNode *N) { return N->FuncId == R.FuncId; });
387       if (I == Top.first->Callees.end()) {
388         // We didn't find the callee in the stack trie, so we're going to
389         // add to the stack then set up the pointers properly.
390         auto N = createTrieNode(R.TId, R.FuncId, Top.first);
391         Top.first->Callees.emplace_back(N);
392 
393         // Top may be invalidated after this statement.
394         TS.emplace_back(N, R.TSC);
395       } else {
396         // We found the callee in the stack trie, so we'll use that pointer
397         // instead, add it to the stack associated with the TSC.
398         TS.emplace_back(*I, R.TSC);
399       }
400       return AccountRecordStatus::OK;
401     }
402     case RecordTypes::EXIT:
403     case RecordTypes::TAIL_EXIT: {
404       bool wasLastRecordExit = state->wasLastRecordExit;
405       state->wasLastRecordExit = true;
406       // The exit case is more interesting, since we want to be able to deduce
407       // missing exit records. To do that properly, we need to look up the stack
408       // and see whether the exit record matches any of the entry records. If it
409       // does match, we attempt to record the durations as we pop the stack to
410       // where we see the parent.
411       if (TS.empty()) {
412         // Short circuit, and say we can't find it.
413 
414         return AccountRecordStatus::ENTRY_NOT_FOUND;
415       }
416 
417       auto FunctionEntryMatch = find_if(
418           reverse(TS), [&](const std::pair<StackTrieNode *, uint64_t> &E) {
419             return E.first->FuncId == R.FuncId;
420           });
421       auto status = AccountRecordStatus::OK;
422       if (FunctionEntryMatch == TS.rend()) {
423         status = AccountRecordStatus::ENTRY_NOT_FOUND;
424       } else {
425         // Account for offset of 1 between reverse and forward iterators. We
426         // want the forward iterator to include the function that is exited.
427         ++FunctionEntryMatch;
428       }
429       auto I = FunctionEntryMatch.base();
430       for (auto &E : make_range(I, TS.end() - 1))
431         E.first->ExtraData.IntermediateDurations.push_back(
432             std::max(E.second, R.TSC) - std::min(E.second, R.TSC));
433       auto &Deepest = TS.back();
434       if (wasLastRecordExit)
435         Deepest.first->ExtraData.IntermediateDurations.push_back(
436             std::max(Deepest.second, R.TSC) - std::min(Deepest.second, R.TSC));
437       else
438         Deepest.first->ExtraData.TerminalDurations.push_back(
439             std::max(Deepest.second, R.TSC) - std::min(Deepest.second, R.TSC));
440       TS.erase(I, TS.end());
441       return status;
442     }
443     }
444     return AccountRecordStatus::UNKNOWN_RECORD_TYPE;
445   }
446 
447   bool isEmpty() const { return Roots.empty(); }
448 
449   void printStack(raw_ostream &OS, const StackTrieNode *Top,
450                   FuncIdConversionHelper &FN) {
451     // Traverse the pointers up to the parent, noting the sums, then print
452     // in reverse order (callers at top, callees down bottom).
453     SmallVector<const StackTrieNode *, 8> CurrentStack;
454     for (auto *F = Top; F != nullptr; F = F->Parent)
455       CurrentStack.push_back(F);
456     int Level = 0;
457     OS << formatv("{0,-5} {1,-60} {2,+12} {3,+16}\n", "lvl", "function",
458                   "count", "sum");
459     for (auto *F : reverse(drop_begin(CurrentStack))) {
460       auto Sum = std::accumulate(F->ExtraData.IntermediateDurations.begin(),
461                                  F->ExtraData.IntermediateDurations.end(), 0LL);
462       auto FuncId = FN.SymbolOrNumber(F->FuncId);
463       OS << formatv("#{0,-4} {1,-60} {2,+12} {3,+16}\n", Level++,
464                     FuncId.size() > 60 ? FuncId.substr(0, 57) + "..." : FuncId,
465                     F->ExtraData.IntermediateDurations.size(), Sum);
466     }
467     auto *Leaf = *CurrentStack.begin();
468     auto LeafSum =
469         std::accumulate(Leaf->ExtraData.TerminalDurations.begin(),
470                         Leaf->ExtraData.TerminalDurations.end(), 0LL);
471     auto LeafFuncId = FN.SymbolOrNumber(Leaf->FuncId);
472     OS << formatv("#{0,-4} {1,-60} {2,+12} {3,+16}\n", Level++,
473                   LeafFuncId.size() > 60 ? LeafFuncId.substr(0, 57) + "..."
474                                          : LeafFuncId,
475                   Leaf->ExtraData.TerminalDurations.size(), LeafSum);
476     OS << "\n";
477   }
478 
479   /// Prints top stacks for each thread.
480   void printPerThread(raw_ostream &OS, FuncIdConversionHelper &FN) {
481     for (auto iter : Roots) {
482       OS << "Thread " << iter.first << ":\n";
483       print(OS, FN, iter.second);
484       OS << "\n";
485     }
486   }
487 
488   /// Prints timing sums for each stack in each threads.
489   template <AggregationType AggType>
490   void printAllPerThread(raw_ostream &OS, FuncIdConversionHelper &FN,
491                          StackOutputFormat format) {
492     for (auto iter : Roots) {
493       uint32_t threadId = iter.first;
494       RootVector &perThreadRoots = iter.second;
495       bool reportThreadId = true;
496       printAll<AggType>(OS, FN, perThreadRoots, threadId, reportThreadId);
497     }
498   }
499 
500   /// Prints top stacks from looking at all the leaves and ignoring thread IDs.
501   /// Stacks that consist of the same function IDs but were called in different
502   /// thread IDs are not considered unique in this printout.
503   void printIgnoringThreads(raw_ostream &OS, FuncIdConversionHelper &FN) {
504     RootVector RootValues;
505 
506     // Function to pull the values out of a map iterator.
507     using RootsType = decltype(Roots.begin())::value_type;
508     auto MapValueFn = [](const RootsType &Value) { return Value.second; };
509 
510     for (const auto &RootNodeRange :
511          make_range(map_iterator(Roots.begin(), MapValueFn),
512                     map_iterator(Roots.end(), MapValueFn))) {
513       for (auto *RootNode : RootNodeRange)
514         RootValues.push_back(RootNode);
515     }
516 
517     print(OS, FN, RootValues);
518   }
519 
520   /// Creates a merged list of Tries for unique stacks that disregards their
521   /// thread IDs.
522   RootVector mergeAcrossThreads(std::forward_list<StackTrieNode> &NodeStore) {
523     RootVector MergedByThreadRoots;
524     for (auto MapIter : Roots) {
525       const auto &RootNodeVector = MapIter.second;
526       for (auto *Node : RootNodeVector) {
527         auto MaybeFoundIter =
528             find_if(MergedByThreadRoots, [Node](StackTrieNode *elem) {
529               return Node->FuncId == elem->FuncId;
530             });
531         if (MaybeFoundIter == MergedByThreadRoots.end()) {
532           MergedByThreadRoots.push_back(Node);
533         } else {
534           MergedByThreadRoots.push_back(mergeTrieNodes(
535               **MaybeFoundIter, *Node, nullptr, NodeStore, mergeStackDuration));
536           MergedByThreadRoots.erase(MaybeFoundIter);
537         }
538       }
539     }
540     return MergedByThreadRoots;
541   }
542 
543   /// Print timing sums for all stacks merged by Thread ID.
544   template <AggregationType AggType>
545   void printAllAggregatingThreads(raw_ostream &OS, FuncIdConversionHelper &FN,
546                                   StackOutputFormat format) {
547     std::forward_list<StackTrieNode> AggregatedNodeStore;
548     RootVector MergedByThreadRoots = mergeAcrossThreads(AggregatedNodeStore);
549     bool reportThreadId = false;
550     printAll<AggType>(OS, FN, MergedByThreadRoots,
551                       /*threadId*/ 0, reportThreadId);
552   }
553 
554   /// Merges the trie by thread id before printing top stacks.
555   void printAggregatingThreads(raw_ostream &OS, FuncIdConversionHelper &FN) {
556     std::forward_list<StackTrieNode> AggregatedNodeStore;
557     RootVector MergedByThreadRoots = mergeAcrossThreads(AggregatedNodeStore);
558     print(OS, FN, MergedByThreadRoots);
559   }
560 
561   // TODO: Add a format option when more than one are supported.
562   template <AggregationType AggType>
563   void printAll(raw_ostream &OS, FuncIdConversionHelper &FN,
564                 RootVector RootValues, uint32_t ThreadId, bool ReportThread) {
565     SmallVector<const StackTrieNode *, 16> S;
566     for (const auto *N : RootValues) {
567       S.clear();
568       S.push_back(N);
569       while (!S.empty()) {
570         auto *Top = S.pop_back_val();
571         printSingleStack<AggType>(OS, FN, ReportThread, ThreadId, Top);
572         for (const auto *C : Top->Callees)
573           S.push_back(C);
574       }
575     }
576   }
577 
578   /// Prints values for stacks in a format consumable for the flamegraph.pl
579   /// tool. This is a line based format that lists each level in the stack
580   /// hierarchy in a semicolon delimited form followed by a space and a numeric
581   /// value. If breaking down by thread, the thread ID will be added as the
582   /// root level of the stack.
583   template <AggregationType AggType>
584   void printSingleStack(raw_ostream &OS, FuncIdConversionHelper &Converter,
585                         bool ReportThread, uint32_t ThreadId,
586                         const StackTrieNode *Node) {
587     if (ReportThread)
588       OS << "thread_" << ThreadId << ";";
589     SmallVector<const StackTrieNode *, 5> lineage{};
590     lineage.push_back(Node);
591     while (lineage.back()->Parent != nullptr)
592       lineage.push_back(lineage.back()->Parent);
593     while (!lineage.empty()) {
594       OS << Converter.SymbolOrNumber(lineage.back()->FuncId) << ";";
595       lineage.pop_back();
596     }
597     OS << " " << GetValueForStack<AggType>(Node) << "\n";
598   }
599 
600   void print(raw_ostream &OS, FuncIdConversionHelper &FN,
601              RootVector RootValues) {
602     // Go through each of the roots, and traverse the call stack, producing the
603     // aggregates as you go along. Remember these aggregates and stacks, and
604     // show summary statistics about:
605     //
606     //   - Total number of unique stacks
607     //   - Top 10 stacks by count
608     //   - Top 10 stacks by aggregate duration
609     SmallVector<std::pair<const StackTrieNode *, uint64_t>, 11>
610         TopStacksByCount;
611     SmallVector<std::pair<const StackTrieNode *, uint64_t>, 11> TopStacksBySum;
612     auto greater_second =
613         [](const std::pair<const StackTrieNode *, uint64_t> &A,
614            const std::pair<const StackTrieNode *, uint64_t> &B) {
615           return A.second > B.second;
616         };
617     uint64_t UniqueStacks = 0;
618     for (const auto *N : RootValues) {
619       SmallVector<const StackTrieNode *, 16> S;
620       S.emplace_back(N);
621 
622       while (!S.empty()) {
623         auto *Top = S.pop_back_val();
624 
625         // We only start printing the stack (by walking up the parent pointers)
626         // when we get to a leaf function.
627         if (!Top->ExtraData.TerminalDurations.empty()) {
628           ++UniqueStacks;
629           auto TopSum =
630               std::accumulate(Top->ExtraData.TerminalDurations.begin(),
631                               Top->ExtraData.TerminalDurations.end(), 0uLL);
632           {
633             auto E = std::make_pair(Top, TopSum);
634             TopStacksBySum.insert(
635                 llvm::lower_bound(TopStacksBySum, E, greater_second), E);
636             if (TopStacksBySum.size() == 11)
637               TopStacksBySum.pop_back();
638           }
639           {
640             auto E =
641                 std::make_pair(Top, Top->ExtraData.TerminalDurations.size());
642             TopStacksByCount.insert(
643                 llvm::lower_bound(TopStacksByCount, E, greater_second), E);
644             if (TopStacksByCount.size() == 11)
645               TopStacksByCount.pop_back();
646           }
647         }
648         for (const auto *C : Top->Callees)
649           S.push_back(C);
650       }
651     }
652 
653     // Now print the statistics in the end.
654     OS << "\n";
655     OS << "Unique Stacks: " << UniqueStacks << "\n";
656     OS << "Top 10 Stacks by leaf sum:\n\n";
657     for (const auto &P : TopStacksBySum) {
658       OS << "Sum: " << P.second << "\n";
659       printStack(OS, P.first, FN);
660     }
661     OS << "\n";
662     OS << "Top 10 Stacks by leaf count:\n\n";
663     for (const auto &P : TopStacksByCount) {
664       OS << "Count: " << P.second << "\n";
665       printStack(OS, P.first, FN);
666     }
667     OS << "\n";
668   }
669 };
670 
671 static std::string CreateErrorMessage(StackTrie::AccountRecordStatus Error,
672                                       const XRayRecord &Record,
673                                       const FuncIdConversionHelper &Converter) {
674   switch (Error) {
675   case StackTrie::AccountRecordStatus::ENTRY_NOT_FOUND:
676     return std::string(
677         formatv("Found record {0} with no matching function entry\n",
678                 format_xray_record(Record, Converter)));
679   default:
680     return std::string(formatv("Unknown error type for record {0}\n",
681                                format_xray_record(Record, Converter)));
682   }
683 }
684 
685 static CommandRegistration Unused(&Stack, []() -> Error {
686   // Load each file provided as a command-line argument. For each one of them
687   // account to a single StackTrie, and just print the whole trie for now.
688   StackTrie ST;
689   InstrumentationMap Map;
690   if (!StacksInstrMap.empty()) {
691     auto InstrumentationMapOrError = loadInstrumentationMap(StacksInstrMap);
692     if (!InstrumentationMapOrError)
693       return joinErrors(
694           make_error<StringError>(
695               Twine("Cannot open instrumentation map: ") + StacksInstrMap,
696               std::make_error_code(std::errc::invalid_argument)),
697           InstrumentationMapOrError.takeError());
698     Map = std::move(*InstrumentationMapOrError);
699   }
700 
701   if (SeparateThreadStacks && AggregateThreads)
702     return make_error<StringError>(
703         Twine("Can't specify options for per thread reporting and reporting "
704               "that aggregates threads."),
705         std::make_error_code(std::errc::invalid_argument));
706 
707   if (!DumpAllStacks && StacksOutputFormat != HUMAN)
708     return make_error<StringError>(
709         Twine("Can't specify a non-human format without -all-stacks."),
710         std::make_error_code(std::errc::invalid_argument));
711 
712   if (DumpAllStacks && StacksOutputFormat == HUMAN)
713     return make_error<StringError>(
714         Twine("You must specify a non-human format when reporting with "
715               "-all-stacks."),
716         std::make_error_code(std::errc::invalid_argument));
717 
718   symbolize::LLVMSymbolizer Symbolizer;
719   FuncIdConversionHelper FuncIdHelper(StacksInstrMap, Symbolizer,
720                                       Map.getFunctionAddresses());
721   // TODO: Someday, support output to files instead of just directly to
722   // standard output.
723   for (const auto &Filename : StackInputs) {
724     auto TraceOrErr = loadTraceFile(Filename);
725     if (!TraceOrErr) {
726       if (!StackKeepGoing)
727         return joinErrors(
728             make_error<StringError>(
729                 Twine("Failed loading input file '") + Filename + "'",
730                 std::make_error_code(std::errc::invalid_argument)),
731             TraceOrErr.takeError());
732       logAllUnhandledErrors(TraceOrErr.takeError(), errs());
733       continue;
734     }
735     auto &T = *TraceOrErr;
736     StackTrie::AccountRecordState AccountRecordState =
737         StackTrie::AccountRecordState::CreateInitialState();
738     for (const auto &Record : T) {
739       auto error = ST.accountRecord(Record, &AccountRecordState);
740       if (error != StackTrie::AccountRecordStatus::OK) {
741         if (!StackKeepGoing)
742           return make_error<StringError>(
743               CreateErrorMessage(error, Record, FuncIdHelper),
744               make_error_code(errc::illegal_byte_sequence));
745         errs() << CreateErrorMessage(error, Record, FuncIdHelper);
746       }
747     }
748   }
749   if (ST.isEmpty()) {
750     return make_error<StringError>(
751         "No instrumented calls were accounted in the input file.",
752         make_error_code(errc::result_out_of_range));
753   }
754 
755   // Report the stacks in a long form mode for another tool to analyze.
756   if (DumpAllStacks) {
757     if (AggregateThreads) {
758       switch (RequestedAggregation) {
759       case AggregationType::TOTAL_TIME:
760         ST.printAllAggregatingThreads<AggregationType::TOTAL_TIME>(
761             outs(), FuncIdHelper, StacksOutputFormat);
762         break;
763       case AggregationType::INVOCATION_COUNT:
764         ST.printAllAggregatingThreads<AggregationType::INVOCATION_COUNT>(
765             outs(), FuncIdHelper, StacksOutputFormat);
766         break;
767       }
768     } else {
769       switch (RequestedAggregation) {
770       case AggregationType::TOTAL_TIME:
771         ST.printAllPerThread<AggregationType::TOTAL_TIME>(outs(), FuncIdHelper,
772                                                           StacksOutputFormat);
773         break;
774       case AggregationType::INVOCATION_COUNT:
775         ST.printAllPerThread<AggregationType::INVOCATION_COUNT>(
776             outs(), FuncIdHelper, StacksOutputFormat);
777         break;
778       }
779     }
780     return Error::success();
781   }
782 
783   // We're only outputting top stacks.
784   if (AggregateThreads) {
785     ST.printAggregatingThreads(outs(), FuncIdHelper);
786   } else if (SeparateThreadStacks) {
787     ST.printPerThread(outs(), FuncIdHelper);
788   } else {
789     ST.printIgnoringThreads(outs(), FuncIdHelper);
790   }
791   return Error::success();
792 });
793