<div dir="ltr">Hello Keith,<br><br>Some of added tests do not pass on the builder:<br><a href="http://lab.llvm.org:8011/builders/llvm-clang-x86_64-expensive-checks-win/builds/4722">http://lab.llvm.org:8011/builders/llvm-clang-x86_64-expensive-checks-win/builds/4722</a><br><br>    LLVM :: tools/llvm-xray/X86/stack-empty-case.yaml<br>    LLVM :: tools/llvm-xray/X86/stack-keep-going.yaml<br><br>Please have a look?<br><br>Thanks<br><br>Galina<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 7, 2017 at 11:07 AM, Keith Wyss via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: kpw<br>
Date: Thu Sep  7 11:07:48 2017<br>
New Revision: 312733<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=312733&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=312733&view=rev</a><br>
Log:<br>
[XRay][tools] Function call stack based analysis tooling for XRay traces<br>
<br>
Second try after fixing a code san problem with iterator reference types.<br>
<br>
This change introduces a subcommand to the llvm-xray tool called<br>
"stacks" which allows for analysing XRay traces provided as inputs and<br>
accounting time to stacks instead of just individual functions. This<br>
gives us a more precise view of where in a program the latency is<br>
actually attributed.<br>
<br>
The tool uses a trie data structure to keep track of the caller-callee<br>
relationships as we process the XRay traces. In particular, we keep<br>
track of the function call stack as we enter functions. While we're<br>
doing this we're adding nodes in a trie and indicating a "calls"<br>
relatinship between the caller (current top of the stack) and the callee<br>
(the new top of the stack). When we push function ids onto the stack, we<br>
keep track of the timestamp (TSC) for the enter event.<br>
<br>
When exiting functions, we are able to account the duration by getting<br>
the difference between the timestamp of the exit event and the<br>
corresponding entry event in the stack. This works even if we somehow<br>
miss the exit events for intermediary functions (i.e. if the exit event<br>
is not cleanly associated with the enter event at the top of the stack).<br>
<br>
The output of the tool currently provides just the top N leaf functions<br>
that contribute the most latency, and the top N stacks that have the<br>
most frequency. In the future we can provide more sophisticated query<br>
mechanisms and potentially an export to database feature to make offline<br>
analysis of the stack traces possible with existing tools.<br>
<br>
Differential revision: D34863<br>
<br>
Added:<br>
    llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-empty-case.yaml<br>
    llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-keep-going.yaml<br>
    llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-multithread.<wbr>yaml<br>
    llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-simple-case.<wbr>yaml<br>
    llvm/trunk/tools/llvm-xray/<wbr>xray-stacks.cc<br>
Modified:<br>
    llvm/trunk/tools/llvm-xray/<wbr>CMakeLists.txt<br>
    llvm/trunk/tools/llvm-xray/<wbr>xray-record-yaml.h<br>
<br>
Added: llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-empty-case.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-xray/X86/stack-empty-case.yaml?rev=312733&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/tools/<wbr>llvm-xray/X86/stack-empty-<wbr>case.yaml?rev=312733&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-empty-case.yaml (added)<br>
+++ llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-empty-case.yaml Thu Sep  7 11:07:48 2017<br>
@@ -0,0 +1,13 @@<br>
+#RUN: (llvm-xray stack %s 2>&1 || echo "Checking Command Failed") | FileCheck %s<br>
+---<br>
+header:<br>
+  version: 1<br>
+  type: 0<br>
+  constant-tsc: true<br>
+  nonstop-tsc: true<br>
+  cycle-frequency: 2601000000<br>
+records:<br>
+...<br>
+# CHECK:     llvm-xray: No instrumented calls were accounted in the input file.<br>
+# CHECK:     Checking Command Failed<br>
+# CHECK-NOT: {{[0-9A-Z]+}}<br>
<br>
Added: llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-keep-going.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-xray/X86/stack-keep-going.yaml?rev=312733&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/tools/<wbr>llvm-xray/X86/stack-keep-<wbr>going.yaml?rev=312733&view=<wbr>auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-keep-going.yaml (added)<br>
+++ llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-keep-going.yaml Thu Sep  7 11:07:48 2017<br>
@@ -0,0 +1,28 @@<br>
+#RUN: (llvm-xray stack %s 2>&1 1>&- || echo "Check Command Failed") | FileCheck --check-prefix HALT %s<br>
+#RUN: (llvm-xray stack -k %s 2>&1 && echo "Check Command Succeeded") | FileCheck --check-prefix KEEP-GOING-SUCCEEDS %s<br>
+#RUN: llvm-xray stack -k %s | FileCheck --check-prefix KEEP-GOING %s<br>
+---<br>
+header:<br>
+  version: 1<br>
+  type: 0<br>
+  constant-tsc: true<br>
+  nonstop-tsc: true<br>
+  cycle-frequency: 2601000000<br>
+records:<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10001 }<br>
+  - { type: 1, func-id: 4, cpu: 1, thread: 111, kind: function-exit, tsc: 10301 }<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10401 }<br>
+  - { type: 0, func-id: 2, cpu: 1, thread: 111, kind: function-enter, tsc: 10501 }<br>
+  - { type: 0, func-id: 3, cpu: 1, thread: 111, kind: function-enter, tsc: 10601 }<br>
+  - { type: 1, func-id: 3, cpu: 1, thread: 111, kind: function-exit, tsc: 10701 }<br>
+  - { type: 1, func-id: 2, cpu: 1, thread: 111, kind: function-exit, tsc: 10751 }<br>
+  - { type: 1, func-id: 1, cpu: 1, thread: 111, kind: function-exit, tsc: 10775 }<br>
+...<br>
+<br>
+#HALT: llvm-xray: Found record {FuncId: "#4", ThreadId: "111", RecordType: "Fn Exit"} with no matching function entry<br>
+#HALT: Check Command Failed<br>
+#KEEP-GOING-SUCCEEDS: Found record {FuncId: "#4", ThreadId: "111", RecordType: "Fn Exit"} with no matching function entry<br>
+#KEEP-GOING-SUCCEEDS: Check Command Succeeded<br>
+#KEEP-GOING: Unique Stacks: 2<br>
+# Note the interesting case here that the stack { fn-1 } is a prefix of { fn-1, fn-2, fn-3 } but they<br>
+# are still counted as unique stacks.<br>
<br>
Added: llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-multithread.<wbr>yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-xray/X86/stack-multithread.yaml?rev=312733&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/tools/<wbr>llvm-xray/X86/stack-<wbr>multithread.yaml?rev=312733&<wbr>view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-multithread.<wbr>yaml (added)<br>
+++ llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-multithread.<wbr>yaml Thu Sep  7 11:07:48 2017<br>
@@ -0,0 +1,83 @@<br>
+#RUN: llvm-xray stack -per-thread-stacks %s | FileCheck %s --check-prefix PER-THREAD<br>
+#RUN: llvm-xray stack -aggregate-threads %s | FileCheck %s --check-prefix AGGREGATE<br>
+<br>
+---<br>
+header:<br>
+  version: 1<br>
+  type: 0<br>
+  constant-tsc: true<br>
+  nonstop-tsc: true<br>
+  cycle-frequency: 2601000000<br>
+records:<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10001 }<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10100 }<br>
+  - { type: 1, func-id: 1, cpu: 1, thread: 111, kind: function-exit, tsc: 10101 }<br>
+  - { type: 1, func-id: 1, cpu: 1, thread: 111, kind: function-exit, tsc: 10301 }<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10401 }<br>
+  - { type: 0, func-id: 2, cpu: 1, thread: 111, kind: function-enter, tsc: 10501 }<br>
+  - { type: 0, func-id: 3, cpu: 1, thread: 111, kind: function-enter, tsc: 10601 }<br>
+  - { type: 1, func-id: 3, cpu: 1, thread: 111, kind: function-exit, tsc: 10701 }<br>
+  - { type: 1, func-id: 2, cpu: 1, thread: 111, kind: function-exit, tsc: 10751 }<br>
+  - { type: 1, func-id: 1, cpu: 1, thread: 111, kind: function-exit, tsc: 10775 }<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 123, kind: function-enter, tsc: 10401 }<br>
+  - { type: 0, func-id: 2, cpu: 1, thread: 123, kind: function-enter, tsc: 10501 }<br>
+  - { type: 0, func-id: 3, cpu: 1, thread: 123, kind: function-enter, tsc: 10701 }<br>
+  - { type: 1, func-id: 3, cpu: 1, thread: 123, kind: function-exit, tsc: 10801 }<br>
+  - { type: 1, func-id: 2, cpu: 1, thread: 123, kind: function-exit, tsc: 10951 }<br>
+  - { type: 1, func-id: 1, cpu: 1, thread: 123, kind: function-exit, tsc: 11075 }<br>
+  - { type: 0, func-id: 2, cpu: 1, thread: 200, kind: function-enter, tsc: 0 }<br>
+  - { type: 0, func-id: 3, cpu: 1, thread: 200, kind: function-enter, tsc: 10 }<br>
+  - { type: 1, func-id: 3, cpu: 1, thread: 200, kind: function-exit, tsc: 20 }<br>
+  - { type: 1, func-id: 2, cpu: 1, thread: 200, kind: function-exit, tsc: 30 }<br>
+...<br>
+# PER-THREAD: Thread 123<br>
+# PER-THREAD: Unique Stacks: 1<br>
+# PER-THREAD: Top 10 Stacks by leaf sum:<br>
+# PER-THREAD: Sum: 100<br>
+# PER-THREAD: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# PER-THREAD: #0    #1{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}674<br>
+# PER-THREAD: #1    #2{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}450<br>
+# PER-THREAD: #2    #3{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}100<br>
+# PER-THREAD: Top 10 Stacks by leaf count:<br>
+# PER-THREAD: #0    #1{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}674<br>
+# PER-THREAD: #1    #2{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}450<br>
+# PER-THREAD: #2    #3{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}100<br>
+# PER-THREAD: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+<br>
+# AGGREGATE: Unique Stacks: 3<br>
+# AGGREGATE: Top 10 Stacks by leaf sum:<br>
+# AGGREGATE: Sum: 200<br>
+<br>
+# AGGREGATE: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# AGGREGATE: #0    #1{{[[:space:]]+}}3{{[[:space:<wbr>]]+}}1348<br>
+# AGGREGATE: #1    #2{{[[:space:]]+}}2{{[[:space:<wbr>]]+}}700<br>
+# AGGREGATE: #2    #3{{[[:space:]]+}}2{{[[:space:<wbr>]]+}}200<br>
+<br>
+# AGGREGATE: Sum: 10<br>
+# AGGREGATE: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# AGGREGATE: #0    #2{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}30<br>
+# AGGREGATE: #1    #3{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}10<br>
+<br>
+# AGGREGATE: Sum: 1<br>
+# AGGREGATE: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# AGGREGATE: #0    #1{{[[:space:]]+}}2{{[[:space:<wbr>]]+}}674<br>
+# AGGREGATE: #1    #1{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}1<br>
+<br>
+<br>
+# AGGREGATE: Top 10 Stacks by leaf count:<br>
+<br>
+# AGGREGATE: Count: 2<br>
+# AGGREGATE: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# AGGREGATE: #0    #1{{[[:space:]]+}}3{{[[:space:<wbr>]]+}}1348<br>
+# AGGREGATE: #1    #2{{[[:space:]]+}}2{{[[:space:<wbr>]]+}}700<br>
+# AGGREGATE: #2    #3{{[[:space:]]+}}2{{[[:space:<wbr>]]+}}200<br>
+<br>
+# AGGREGATE: Count: 1<br>
+# AGGREGATE: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# AGGREGATE: #0    #2{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}30<br>
+# AGGREGATE: #1    #3{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}10<br>
+<br>
+# AGGREGATE: Count: 1<br>
+# AGGREGATE: lvl   function{{[[:space:]]+}}count{<wbr>{[[:space:]]+}}sum<br>
+# AGGREGATE: #0    #1{{[[:space:]]+}}2{{[[:space:<wbr>]]+}}674<br>
+# AGGREGATE: #1    #1{{[[:space:]]+}}1{{[[:space:<wbr>]]+}}1<br>
<br>
Added: llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-simple-case.<wbr>yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-xray/X86/stack-simple-case.yaml?rev=312733&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/tools/<wbr>llvm-xray/X86/stack-simple-<wbr>case.yaml?rev=312733&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-simple-case.<wbr>yaml (added)<br>
+++ llvm/trunk/test/tools/llvm-<wbr>xray/X86/stack-simple-case.<wbr>yaml Thu Sep  7 11:07:48 2017<br>
@@ -0,0 +1,13 @@<br>
+#RUN: llvm-xray stack %s | FileCheck %s<br>
+---<br>
+header:<br>
+  version: 1<br>
+  type: 0<br>
+  constant-tsc: true<br>
+  nonstop-tsc: true<br>
+  cycle-frequency: 2601000000<br>
+records:<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-enter, tsc: 10001 }<br>
+  - { type: 0, func-id: 1, cpu: 1, thread: 111, kind: function-exit, tsc: 10100 }<br>
+...<br>
+#CHECK: Unique Stacks: 1<br>
<br>
Modified: llvm/trunk/tools/llvm-xray/<wbr>CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-xray/CMakeLists.txt?rev=312733&r1=312732&r2=312733&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/tools/llvm-<wbr>xray/CMakeLists.txt?rev=<wbr>312733&r1=312732&r2=312733&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/tools/llvm-xray/<wbr>CMakeLists.txt (original)<br>
+++ llvm/trunk/tools/llvm-xray/<wbr>CMakeLists.txt Thu Sep  7 11:07:48 2017<br>
@@ -15,6 +15,7 @@ set(LLVM_XRAY_TOOLS<br>
   xray-extract.cc<br>
   xray-graph.cc<br>
   xray-graph-diff.cc<br>
+  xray-stacks.cc<br>
   xray-registry.cc)<br>
<br>
 add_llvm_tool(llvm-xray llvm-xray.cc ${LLVM_XRAY_TOOLS})<br>
<br>
Modified: llvm/trunk/tools/llvm-xray/<wbr>xray-record-yaml.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-xray/xray-record-yaml.h?rev=312733&r1=312732&r2=312733&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/tools/llvm-<wbr>xray/xray-record-yaml.h?rev=<wbr>312733&r1=312732&r2=312733&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/tools/llvm-xray/<wbr>xray-record-yaml.h (original)<br>
+++ llvm/trunk/tools/llvm-xray/<wbr>xray-record-yaml.h Thu Sep  7 11:07:48 2017<br>
@@ -97,6 +97,6 @@ template <> struct MappingTraits<xray::Y<br>
 } // namespace yaml<br>
 } // namespace llvm<br>
<br>
-LLVM_YAML_IS_SEQUENCE_VECTOR(<wbr>xray::YAMLXRayRecord)<br>
+LLVM_YAML_IS_SEQUENCE_VECTOR(<wbr>xray::YAMLXRayRecord)<br>
<br>
 #endif // LLVM_TOOLS_LLVM_XRAY_XRAY_<wbr>RECORD_YAML_H<br>
<br>
Added: llvm/trunk/tools/llvm-xray/<wbr>xray-stacks.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-xray/xray-stacks.cc?rev=312733&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/tools/llvm-<wbr>xray/xray-stacks.cc?rev=<wbr>312733&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/tools/llvm-xray/<wbr>xray-stacks.cc (added)<br>
+++ llvm/trunk/tools/llvm-xray/<wbr>xray-stacks.cc Thu Sep  7 11:07:48 2017<br>
@@ -0,0 +1,634 @@<br>
+//===- xray-stacks.cc - XRay Function Call Stack Accounting ---------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===------------------------<wbr>------------------------------<wbr>----------------===//<br>
+//<br>
+// This file implements stack-based accounting. It takes XRay traces, and<br>
+// collates statistics across these traces to show a breakdown of time spent<br>
+// at various points of the stack to provide insight into which functions<br>
+// spend the most time in terms of a call stack. We provide a few<br>
+// sorting/filtering options for zero'ing in on the useful stacks.<br>
+//<br>
+//===------------------------<wbr>------------------------------<wbr>----------------===//<br>
+<br>
+#include <forward_list><br>
+#include <numeric><br>
+<br>
+#include "func-id-helper.h"<br>
+#include "xray-registry.h"<br>
+#include "llvm/ADT/StringExtras.h"<br>
+#include "llvm/Support/CommandLine.h"<br>
+#include "llvm/Support/Errc.h"<br>
+#include "llvm/Support/ErrorHandling.h"<br>
+#include "llvm/Support/FormatAdapters.<wbr>h"<br>
+#include "llvm/Support/FormatVariadic.<wbr>h"<br>
+#include "llvm/XRay/Graph.h"<br>
+#include "llvm/XRay/InstrumentationMap.<wbr>h"<br>
+#include "llvm/XRay/Trace.h"<br>
+<br>
+using namespace llvm;<br>
+using namespace llvm::xray;<br>
+<br>
+static cl::SubCommand Stack("stack", "Call stack accounting");<br>
+static cl::list<std::string> StackInputs(cl::Positional,<br>
+                                         cl::desc("<xray trace>"), cl::Required,<br>
+                                         cl::sub(Stack), cl::OneOrMore);<br>
+<br>
+static cl::opt<bool><br>
+    StackKeepGoing("keep-going", cl::desc("Keep going on errors encountered"),<br>
+                   cl::sub(Stack), cl::init(false));<br>
+static cl::alias StackKeepGoing2("k", cl::aliasopt(StackKeepGoing),<br>
+                                 cl::desc("Alias for -keep-going"),<br>
+                                 cl::sub(Stack));<br>
+<br>
+// TODO: Does there need to be an option to deduce tail or sibling calls?<br>
+<br>
+static cl::opt<std::string> StacksInstrMap(<br>
+    "instr_map",<br>
+    cl::desc("instrumentation map used to identify function ids. "<br>
+             "Currently supports elf file instrumentation maps."),<br>
+    cl::sub(Stack), cl::init(""));<br>
+static cl::alias StacksInstrMap2("m", cl::aliasopt(StacksInstrMap),<br>
+                                 cl::desc("Alias for -instr_map"),<br>
+                                 cl::sub(Stack));<br>
+<br>
+static cl::opt<bool><br>
+    SeparateThreadStacks("per-<wbr>thread-stacks",<br>
+                         cl::desc("Report top stacks within each thread id"),<br>
+                         cl::sub(Stack), cl::init(false));<br>
+<br>
+static cl::opt<bool><br>
+    AggregateThreads("aggregate-<wbr>threads",<br>
+                     cl::desc("Aggregate stack times across threads"),<br>
+                     cl::sub(Stack), cl::init(false));<br>
+<br>
+/// A helper struct to work with formatv and XRayRecords. Makes it easier to use<br>
+/// instrumentation map names or addresses in formatted output.<br>
+struct format_xray_record : public FormatAdapter<XRayRecord> {<br>
+  explicit format_xray_record(XRayRecord record,<br>
+                              const FuncIdConversionHelper &conv)<br>
+      : FormatAdapter<XRayRecord>(std:<wbr>:move(record)), Converter(&conv) {}<br>
+  void format(raw_ostream &Stream, StringRef Style) override {<br>
+    Stream << formatv(<br>
+        "{FuncId: \"{0}\", ThreadId: \"{1}\", RecordType: \"{2}\"}",<br>
+        Converter->SymbolOrNumber(<wbr>Item.FuncId), Item.TId,<br>
+        DecodeRecordType(Item.<wbr>RecordType));<br>
+  }<br>
+<br>
+private:<br>
+  Twine DecodeRecordType(uint16_t recordType) {<br>
+    switch (recordType) {<br>
+    case 0:<br>
+      return Twine("Fn Entry");<br>
+    case 1:<br>
+      return Twine("Fn Exit");<br>
+    default:<br>
+      // TODO: Add Tail exit when it is added to llvm/XRay/XRayRecord.h<br>
+      return Twine("Unknown");<br>
+    }<br>
+  }<br>
+<br>
+  const FuncIdConversionHelper *Converter;<br>
+};<br>
+<br>
+/// The stack command will take a set of XRay traces as arguments, and collects<br>
+/// information about the stacks of instrumented functions that appear in the<br>
+/// traces. We track the following pieces of information:<br>
+///<br>
+///   - Total time: amount of time/cycles accounted for in the traces.<br>
+///   - Stack count: number of times a specific stack appears in the<br>
+///     traces. Only instrumented functions show up in stacks.<br>
+///   - Cumulative stack time: amount of time spent in a stack accumulated<br>
+///     across the invocations in the traces.<br>
+///   - Cumulative local time: amount of time spent in each instrumented<br>
+///     function showing up in a specific stack, accumulated across the traces.<br>
+///<br>
+/// Example output for the kind of data we'd like to provide looks like the<br>
+/// following:<br>
+///<br>
+///   Total time: 3.33234 s<br>
+///   Stack ID: ...<br>
+///   Stack Count: 2093<br>
+///   #     Function                  Local Time     (%)      Stack Time     (%)<br>
+///   0     main                         2.34 ms   0.07%      3.33234  s    100%<br>
+///   1     foo()                     3.30000  s  99.02%         3.33  s  99.92%<br>
+///   2     bar()                          30 ms   0.90%           30 ms   0.90%<br>
+///<br>
+/// We can also show distributions of the function call durations with<br>
+/// statistics at each level of the stack. This works by doing the following<br>
+/// algorithm:<br>
+///<br>
+///   1. When unwinding, record the duration of each unwound function associated<br>
+///   with the path up to which the unwinding stops. For example:<br>
+///<br>
+///        Step                         Duration (? means has start time)<br>
+///<br>
+///        push a <start time>           a = ?<br>
+///        push b <start time>           a = ?, a->b = ?<br>
+///        push c <start time>           a = ?, a->b = ?, a->b->c = ?<br>
+///        pop  c <end time>             a = ?, a->b = ?, emit duration(a->b->c)<br>
+///        pop  b <end time>             a = ?, emit duration(a->b)<br>
+///        push c <start time>           a = ?, a->c = ?<br>
+///        pop  c <end time>             a = ?, emit duration(a->c)<br>
+///        pop  a <end time>             emit duration(a)<br>
+///<br>
+///   2. We then account for the various stacks we've collected, and for each of<br>
+///      them will have measurements that look like the following (continuing<br>
+///      with the above simple example):<br>
+///<br>
+///        c : [<id("a->b->c"), [durations]>, <id("a->c"), [durations]>]<br>
+///        b : [<id("a->b"), [durations]>]<br>
+///        a : [<id("a"), [durations]>]<br>
+///<br>
+///      This allows us to compute, for each stack id, and each function that<br>
+///      shows up in the stack,  some important statistics like:<br>
+///<br>
+///        - median<br>
+///        - 99th percentile<br>
+///        - mean + stddev<br>
+///        - count<br>
+///<br>
+///   3. For cases where we don't have durations for some of the higher levels<br>
+///   of the stack (perhaps instrumentation wasn't activated when the stack was<br>
+///   entered), we can mark them appropriately.<br>
+///<br>
+///  Computing this data also allows us to implement lookup by call stack nodes,<br>
+///  so that we can find functions that show up in multiple stack traces and<br>
+///  show the statistical properties of that function in various contexts. We<br>
+///  can compute information similar to the following:<br>
+///<br>
+///    Function: 'c'<br>
+///    Stacks: 2 / 2<br>
+///    Stack ID: ...<br>
+///    Stack Count: ...<br>
+///    #     Function  ...<br>
+///    0     a         ...<br>
+///    1     b         ...<br>
+///    2     c         ...<br>
+///<br>
+///    Stack ID: ...<br>
+///    Stack Count: ...<br>
+///    #     Function  ...<br>
+///    0     a         ...<br>
+///    1     c         ...<br>
+///    ----------------...<br>
+///<br>
+///    Function: 'b'<br>
+///    Stacks:  1 / 2<br>
+///    Stack ID: ...<br>
+///    Stack Count: ...<br>
+///    #     Function  ...<br>
+///    0     a         ...<br>
+///    1     b         ...<br>
+///    2     c         ...<br>
+///<br>
+///<br>
+/// To do this we require a Trie data structure that will allow us to represent<br>
+/// all the call stacks of instrumented functions in an easily traversible<br>
+/// manner when we do the aggregations and lookups. For instrumented call<br>
+/// sequences like the following:<br>
+///<br>
+///   a()<br>
+///    b()<br>
+///     c()<br>
+///     d()<br>
+///    c()<br>
+///<br>
+/// We will have a representation like so:<br>
+///<br>
+///   a -> b -> c<br>
+///   |    |<br>
+///   |    +--> d<br>
+///   |<br>
+///   +--> c<br>
+///<br>
+/// We maintain a sequence of durations on the leaves and in the internal nodes<br>
+/// as we go through and process every record from the XRay trace. We also<br>
+/// maintain an index of unique functions, and provide a means of iterating<br>
+/// through all the instrumented call stacks which we know about.<br>
+<br>
+struct TrieNode {<br>
+  int32_t FuncId;<br>
+  TrieNode *Parent;<br>
+  SmallVector<TrieNode *, 4> Callees;<br>
+  // Separate durations depending on whether the node is the deepest node in the<br>
+  // stack.<br>
+  SmallVector<int64_t, 4> TerminalDurations;<br>
+  SmallVector<int64_t, 4> IntermediateDurations;<br>
+};<br>
+<br>
+/// Merges together two TrieNodes with like function ids, aggregating their<br>
+/// callee lists and durations. The caller must provide storage where new merged<br>
+/// nodes can be allocated in the form of a linked list.<br>
+TrieNode *mergeTrieNodes(const TrieNode &Left, const TrieNode &Right,<br>
+                         TrieNode *NewParent,<br>
+                         std::forward_list<TrieNode> &NodeStore) {<br>
+  assert(Left.FuncId == Right.FuncId);<br>
+  NodeStore.push_front(TrieNode{<wbr>Left.FuncId, NewParent, {}, {}, {}});<br>
+  auto I = NodeStore.begin();<br>
+  auto *Node = &*I;<br>
+<br>
+  // Build a map of callees from the left side.<br>
+  DenseMap<int32_t, TrieNode *> LeftCalleesByFuncId;<br>
+  for (auto *Callee : Left.Callees) {<br>
+    LeftCalleesByFuncId[Callee-><wbr>FuncId] = Callee;<br>
+  }<br>
+<br>
+  // Iterate through the right side, either merging with the map values or<br>
+  // directly adding to the Callees vector. The iteration also removes any<br>
+  // merged values from the left side map.<br>
+  for (auto *Callee : Right.Callees) {<br>
+    auto iter = LeftCalleesByFuncId.find(<wbr>Callee->FuncId);<br>
+    if (iter != LeftCalleesByFuncId.end()) {<br>
+      Node->Callees.push_back(<br>
+          mergeTrieNodes(*(iter->second)<wbr>, *Callee, Node, NodeStore));<br>
+      LeftCalleesByFuncId.erase(<wbr>iter);<br>
+    } else {<br>
+      Node->Callees.push_back(<wbr>Callee);<br>
+    }<br>
+  }<br>
+<br>
+  // Add any callees that weren't found in the right side.<br>
+  for (auto MapPairIter : LeftCalleesByFuncId) {<br>
+    Node->Callees.push_back(<wbr>MapPairIter.second);<br>
+  }<br>
+<br>
+  // Aggregate the durations.<br>
+  for (auto duration : Left.TerminalDurations) {<br>
+    Node->TerminalDurations.push_<wbr>back(duration);<br>
+  }<br>
+  for (auto duration : Right.TerminalDurations) {<br>
+    Node->TerminalDurations.push_<wbr>back(duration);<br>
+  }<br>
+  for (auto duration : Left.IntermediateDurations) {<br>
+    Node->IntermediateDurations.<wbr>push_back(duration);<br>
+  }<br>
+  for (auto duration : Right.IntermediateDurations) {<br>
+    Node->IntermediateDurations.<wbr>push_back(duration);<br>
+  }<br>
+<br>
+  return Node;<br>
+}<br>
+<br>
+class StackTrie {<br>
+<br>
+  // We maintain pointers to the roots of the tries we see.<br>
+  DenseMap<uint32_t, SmallVector<TrieNode *, 4>> Roots;<br>
+<br>
+  // We make sure all the nodes are accounted for in this list.<br>
+  std::forward_list<TrieNode> NodeStore;<br>
+<br>
+  // A map of thread ids to pairs call stack trie nodes and their start times.<br>
+  DenseMap<uint32_t, SmallVector<std::pair<TrieNode *, uint64_t>, 8>><br>
+      ThreadStackMap;<br>
+<br>
+  TrieNode *createTrieNode(uint32_t ThreadId, int32_t FuncId,<br>
+                           TrieNode *Parent) {<br>
+    NodeStore.push_front(TrieNode{<wbr>FuncId, Parent, {}, {}, {}});<br>
+    auto I = NodeStore.begin();<br>
+    auto *Node = &*I;<br>
+    if (!Parent)<br>
+      Roots[ThreadId].push_back(<wbr>Node);<br>
+    return Node;<br>
+  }<br>
+<br>
+  TrieNode *findRootNode(uint32_t ThreadId, int32_t FuncId) {<br>
+    const auto &RootsByThread = Roots[ThreadId];<br>
+    auto I = find_if(RootsByThread,<br>
+                     [&](TrieNode *N) { return N->FuncId == FuncId; });<br>
+    return (I == RootsByThread.end()) ? nullptr : *I;<br>
+  }<br>
+<br>
+public:<br>
+  enum class AccountRecordStatus {<br>
+    OK,              // Successfully processed<br>
+    ENTRY_NOT_FOUND, // An exit record had no matching call stack entry<br>
+    UNKNOWN_RECORD_TYPE<br>
+  };<br>
+<br>
+  struct AccountRecordState {<br>
+    // We keep track of whether the call stack is currently unwinding.<br>
+    bool wasLastRecordExit;<br>
+<br>
+    static AccountRecordState CreateInitialState() { return {false}; }<br>
+  };<br>
+<br>
+  AccountRecordStatus accountRecord(const XRayRecord &R,<br>
+                                    AccountRecordState *state) {<br>
+    auto &TS = ThreadStackMap[R.TId];<br>
+    switch (R.Type) {<br>
+    case RecordTypes::ENTER: {<br>
+      state->wasLastRecordExit = false;<br>
+      // When we encounter a new function entry, we want to record the TSC for<br>
+      // that entry, and the function id. Before doing so we check the top of<br>
+      // the stack to see if there are callees that already represent this<br>
+      // function.<br>
+      if (TS.empty()) {<br>
+        auto *Root = findRootNode(R.TId, R.FuncId);<br>
+        TS.emplace_back(Root ? Root : createTrieNode(R.TId, R.FuncId, nullptr),<br>
+                        R.TSC);<br>
+        return AccountRecordStatus::OK;<br>
+      }<br>
+<br>
+      auto &Top = TS.back();<br>
+      auto I = find_if(Top.first->Callees,<br>
+                       [&](TrieNode *N) { return N->FuncId == R.FuncId; });<br>
+      if (I == Top.first->Callees.end()) {<br>
+        // We didn't find the callee in the stack trie, so we're going to<br>
+        // add to the stack then set up the pointers properly.<br>
+        auto N = createTrieNode(R.TId, R.FuncId, Top.first);<br>
+        Top.first->Callees.emplace_<wbr>back(N);<br>
+<br>
+        // Top may be invalidated after this statement.<br>
+        TS.emplace_back(N, R.TSC);<br>
+      } else {<br>
+        // We found the callee in the stack trie, so we'll use that pointer<br>
+        // instead, add it to the stack associated with the TSC.<br>
+        TS.emplace_back(*I, R.TSC);<br>
+      }<br>
+      return AccountRecordStatus::OK;<br>
+    }<br>
+    case RecordTypes::EXIT: {<br>
+      bool wasLastRecordExit = state->wasLastRecordExit;<br>
+      state->wasLastRecordExit = true;<br>
+      // The exit case is more interesting, since we want to be able to deduce<br>
+      // missing exit records. To do that properly, we need to look up the stack<br>
+      // and see whether the exit record matches any of the entry records. If it<br>
+      // does match, we attempt to record the durations as we pop the stack to<br>
+      // where we see the parent.<br>
+      if (TS.empty()) {<br>
+        // Short circuit, and say we can't find it.<br>
+<br>
+        return AccountRecordStatus::ENTRY_<wbr>NOT_FOUND;<br>
+      }<br>
+<br>
+      auto FunctionEntryMatch =<br>
+          find_if(reverse(TS), [&](const std::pair<TrieNode *, uint64_t> &E) {<br>
+            return E.first->FuncId == R.FuncId;<br>
+          });<br>
+      auto status = AccountRecordStatus::OK;<br>
+      if (FunctionEntryMatch == TS.rend()) {<br>
+        status = AccountRecordStatus::ENTRY_<wbr>NOT_FOUND;<br>
+      } else {<br>
+        // Account for offset of 1 between reverse and forward iterators. We<br>
+        // want the forward iterator to include the function that is exited.<br>
+        ++FunctionEntryMatch;<br>
+      }<br>
+      auto I = FunctionEntryMatch.base();<br>
+      for (auto &E : make_range(I, TS.end() - 1))<br>
+        E.first-><wbr>IntermediateDurations.push_<wbr>back(std::max(E.second, R.TSC) -<br>
+                                                 std::min(E.second, R.TSC));<br>
+      auto &Deepest = TS.back();<br>
+      if (wasLastRecordExit)<br>
+        Deepest.first-><wbr>IntermediateDurations.push_<wbr>back(<br>
+            std::max(Deepest.second, R.TSC) - std::min(Deepest.second, R.TSC));<br>
+      else<br>
+        Deepest.first-><wbr>TerminalDurations.push_back(<br>
+            std::max(Deepest.second, R.TSC) - std::min(Deepest.second, R.TSC));<br>
+      TS.erase(I, TS.end());<br>
+      return status;<br>
+    }<br>
+    }<br>
+    return AccountRecordStatus::UNKNOWN_<wbr>RECORD_TYPE;<br>
+  }<br>
+<br>
+  bool isEmpty() const { return Roots.empty(); }<br>
+<br>
+  void printStack(raw_ostream &OS, const TrieNode *Top,<br>
+                  FuncIdConversionHelper &FN) {<br>
+    // Traverse the pointers up to the parent, noting the sums, then print<br>
+    // in reverse order (callers at top, callees down bottom).<br>
+    SmallVector<const TrieNode *, 8> CurrentStack;<br>
+    for (auto *F = Top; F != nullptr; F = F->Parent)<br>
+      CurrentStack.push_back(F);<br>
+    int Level = 0;<br>
+    OS << formatv("{0,-5} {1,-60} {2,+12} {3,+16}\n", "lvl", "function",<br>
+                  "count", "sum");<br>
+    for (auto *F :<br>
+         reverse(make_range(<wbr>CurrentStack.begin() + 1, CurrentStack.end()))) {<br>
+      auto Sum = std::accumulate(F-><wbr>IntermediateDurations.begin(),<br>
+                                 F->IntermediateDurations.end()<wbr>, 0LL);<br>
+      auto FuncId = FN.SymbolOrNumber(F->FuncId);<br>
+      OS << formatv("#{0,-4} {1,-60} {2,+12} {3,+16}\n", Level++,<br>
+                    FuncId.size() > 60 ? FuncId.substr(0, 57) + "..." : FuncId,<br>
+                    F->IntermediateDurations.size(<wbr>), Sum);<br>
+    }<br>
+    auto *Leaf = *CurrentStack.begin();<br>
+    auto LeafSum = std::accumulate(Leaf-><wbr>TerminalDurations.begin(),<br>
+                                   Leaf->TerminalDurations.end(), 0LL);<br>
+    auto LeafFuncId = FN.SymbolOrNumber(Leaf-><wbr>FuncId);<br>
+    OS << formatv("#{0,-4} {1,-60} {2,+12} {3,+16}\n", Level++,<br>
+                  LeafFuncId.size() > 60 ? LeafFuncId.substr(0, 57) + "..."<br>
+                                         : LeafFuncId,<br>
+                  Leaf->TerminalDurations.size()<wbr>, LeafSum);<br>
+    OS << "\n";<br>
+  }<br>
+<br>
+  /// Prints top stacks for each thread.<br>
+  void printPerThread(raw_ostream &OS, FuncIdConversionHelper &FN) {<br>
+    for (auto iter : Roots) {<br>
+      OS << "Thread " << iter.first << ":\n";<br>
+      print(OS, FN, iter.second);<br>
+      OS << "\n";<br>
+    }<br>
+  }<br>
+<br>
+  /// Prints top stacks from looking at all the leaves and ignoring thread IDs.<br>
+  /// Stacks that consist of the same function IDs but were called in different<br>
+  /// thread IDs are not considered unique in this printout.<br>
+  void printIgnoringThreads(raw_<wbr>ostream &OS, FuncIdConversionHelper &FN) {<br>
+    SmallVector<TrieNode *, 4> RootValues;<br>
+<br>
+    // Function to pull the values out of a map iterator.<br>
+    using RootsType = decltype(Roots.begin())::<wbr>value_type;<br>
+    auto MapValueFn = [](const RootsType &Value) { return Value.second; };<br>
+<br>
+    for (const auto &RootNodeRange :<br>
+         make_range(map_iterator(Roots.<wbr>begin(), MapValueFn),<br>
+                    map_iterator(Roots.end(), MapValueFn))) {<br>
+      for (auto *RootNode : RootNodeRange)<br>
+        RootValues.push_back(RootNode)<wbr>;<br>
+    }<br>
+<br>
+    print(OS, FN, RootValues);<br>
+  }<br>
+<br>
+  /// Merges the trie by thread id before printing top stacks.<br>
+  void printAggregatingThreads(raw_<wbr>ostream &OS, FuncIdConversionHelper &FN) {<br>
+    std::forward_list<TrieNode> AggregatedNodeStore;<br>
+    SmallVector<TrieNode *, 4> RootValues;<br>
+    for (auto MapIter : Roots) {<br>
+      const auto &RootNodeVector = MapIter.second;<br>
+      for (auto *Node : RootNodeVector) {<br>
+        auto MaybeFoundIter = find_if(RootValues, [Node](TrieNode *elem) {<br>
+          return Node->FuncId == elem->FuncId;<br>
+        });<br>
+        if (MaybeFoundIter == RootValues.end()) {<br>
+          RootValues.push_back(Node);<br>
+        } else {<br>
+          RootValues.push_back(<wbr>mergeTrieNodes(**<wbr>MaybeFoundIter, *Node, nullptr,<br>
+                                              AggregatedNodeStore));<br>
+          RootValues.erase(<wbr>MaybeFoundIter);<br>
+        }<br>
+      }<br>
+    }<br>
+    print(OS, FN, RootValues);<br>
+  }<br>
+<br>
+  void print(raw_ostream &OS, FuncIdConversionHelper &FN,<br>
+             SmallVector<TrieNode *, 4> RootValues) {<br>
+    // Go through each of the roots, and traverse the call stack, producing the<br>
+    // aggregates as you go along. Remember these aggregates and stacks, and<br>
+    // show summary statistics about:<br>
+    //<br>
+    //   - Total number of unique stacks<br>
+    //   - Top 10 stacks by count<br>
+    //   - Top 10 stacks by aggregate duration<br>
+    SmallVector<std::pair<const TrieNode *, uint64_t>, 11> TopStacksByCount;<br>
+    SmallVector<std::pair<const TrieNode *, uint64_t>, 11> TopStacksBySum;<br>
+    auto greater_second = [](const std::pair<const TrieNode *, uint64_t> &A,<br>
+                             const std::pair<const TrieNode *, uint64_t> &B) {<br>
+      return A.second > B.second;<br>
+    };<br>
+    uint64_t UniqueStacks = 0;<br>
+    for (const auto *N : RootValues) {<br>
+      SmallVector<const TrieNode *, 16> S;<br>
+      S.emplace_back(N);<br>
+<br>
+      while (!S.empty()) {<br>
+        auto Top = S.pop_back_val();<br>
+<br>
+        // We only start printing the stack (by walking up the parent pointers)<br>
+        // when we get to a leaf function.<br>
+        if (!Top->TerminalDurations.<wbr>empty()) {<br>
+          ++UniqueStacks;<br>
+          auto TopSum = std::accumulate(Top-><wbr>TerminalDurations.begin(),<br>
+                                        Top->TerminalDurations.end(), 0uLL);<br>
+          {<br>
+            auto E = std::make_pair(Top, TopSum);<br>
+            TopStacksBySum.insert(std::<wbr>lower_bound(TopStacksBySum.<wbr>begin(),<br>
+                                                   TopStacksBySum.end(), E,<br>
+                                                   greater_second),<br>
+                                  E);<br>
+            if (TopStacksBySum.size() == 11)<br>
+              TopStacksBySum.pop_back();<br>
+          }<br>
+          {<br>
+            auto E = std::make_pair(Top, Top->TerminalDurations.size())<wbr>;<br>
+            TopStacksByCount.insert(std::<wbr>lower_bound(TopStacksByCount.<wbr>begin(),<br>
+                                                     TopStacksByCount.end(), E,<br>
+                                                     greater_second),<br>
+                                    E);<br>
+            if (TopStacksByCount.size() == 11)<br>
+              TopStacksByCount.pop_back();<br>
+          }<br>
+        }<br>
+        for (const auto *C : Top->Callees)<br>
+          S.push_back(C);<br>
+      }<br>
+    }<br>
+<br>
+    // Now print the statistics in the end.<br>
+    OS << "\n";<br>
+    OS << "Unique Stacks: " << UniqueStacks << "\n";<br>
+    OS << "Top 10 Stacks by leaf sum:\n\n";<br>
+    for (const auto &P : TopStacksBySum) {<br>
+      OS << "Sum: " << P.second << "\n";<br>
+      printStack(OS, P.first, FN);<br>
+    }<br>
+    OS << "\n";<br>
+    OS << "Top 10 Stacks by leaf count:\n\n";<br>
+    for (const auto &P : TopStacksByCount) {<br>
+      OS << "Count: " << P.second << "\n";<br>
+      printStack(OS, P.first, FN);<br>
+    }<br>
+    OS << "\n";<br>
+  }<br>
+};<br>
+<br>
+std::string CreateErrorMessage(StackTrie::<wbr>AccountRecordStatus Error,<br>
+                               const XRayRecord &Record,<br>
+                               const FuncIdConversionHelper &Converter) {<br>
+  switch (Error) {<br>
+  case StackTrie::<wbr>AccountRecordStatus::ENTRY_<wbr>NOT_FOUND:<br>
+    return formatv("Found record {0} with no matching function entry\n",<br>
+                   format_xray_record(Record, Converter));<br>
+  default:<br>
+    return formatv("Unknown error type for record {0}\n",<br>
+                   format_xray_record(Record, Converter));<br>
+  }<br>
+}<br>
+<br>
+static CommandRegistration Unused(&Stack, []() -> Error {<br>
+  // Load each file provided as a command-line argument. For each one of them<br>
+  // account to a single StackTrie, and just print the whole trie for now.<br>
+  StackTrie ST;<br>
+  InstrumentationMap Map;<br>
+  if (!StacksInstrMap.empty()) {<br>
+    auto InstrumentationMapOrError = loadInstrumentationMap(<wbr>StacksInstrMap);<br>
+    if (!InstrumentationMapOrError)<br>
+      return joinErrors(<br>
+          make_error<StringError>(<br>
+              Twine("Cannot open instrumentation map: ") + StacksInstrMap,<br>
+              std::make_error_code(std::<wbr>errc::invalid_argument)),<br>
+          InstrumentationMapOrError.<wbr>takeError());<br>
+    Map = std::move(*<wbr>InstrumentationMapOrError);<br>
+  }<br>
+<br>
+  if (SeparateThreadStacks && AggregateThreads)<br>
+    return make_error<StringError>(<br>
+        Twine("Can't specify options for per thread reporting and reporting "<br>
+              "that aggregates threads."),<br>
+        std::make_error_code(std::<wbr>errc::invalid_argument));<br>
+<br>
+  symbolize::LLVMSymbolizer::<wbr>Options Opts(<br>
+      symbolize::FunctionNameKind::<wbr>LinkageName, true, true, false, "");<br>
+  symbolize::LLVMSymbolizer Symbolizer(Opts);<br>
+  FuncIdConversionHelper FuncIdHelper(StacksInstrMap, Symbolizer,<br>
+                                      Map.getFunctionAddresses());<br>
+  // TODO: Someday, support output to files instead of just directly to<br>
+  // standard output.<br>
+  for (const auto &Filename : StackInputs) {<br>
+    auto TraceOrErr = loadTraceFile(Filename);<br>
+    if (!TraceOrErr) {<br>
+      if (!StackKeepGoing)<br>
+        return joinErrors(<br>
+            make_error<StringError>(<br>
+                Twine("Failed loading input file '") + Filename + "'",<br>
+                std::make_error_code(std::<wbr>errc::invalid_argument)),<br>
+            TraceOrErr.takeError());<br>
+      logAllUnhandledErrors(<wbr>TraceOrErr.takeError(), errs(), "");<br>
+      continue;<br>
+    }<br>
+    auto &T = *TraceOrErr;<br>
+    StackTrie::AccountRecordState AccountRecordState =<br>
+        StackTrie::AccountRecordState:<wbr>:CreateInitialState();<br>
+    for (const auto &Record : T) {<br>
+      auto error = ST.accountRecord(Record, &AccountRecordState);<br>
+      if (error != StackTrie::<wbr>AccountRecordStatus::OK) {<br>
+        if (!StackKeepGoing)<br>
+          return make_error<StringError>(<br>
+              CreateErrorMessage(error, Record, FuncIdHelper),<br>
+              make_error_code(errc::illegal_<wbr>byte_sequence));<br>
+        errs() << CreateErrorMessage(error, Record, FuncIdHelper);<br>
+      }<br>
+    }<br>
+  }<br>
+  if (ST.isEmpty()) {<br>
+    return make_error<StringError>(<br>
+        "No instrumented calls were accounted in the input file.",<br>
+        make_error_code(errc::result_<wbr>out_of_range));<br>
+  }<br>
+  if (AggregateThreads) {<br>
+    ST.printAggregatingThreads(<wbr>outs(), FuncIdHelper);<br>
+  } else if (SeparateThreadStacks) {<br>
+    ST.printPerThread(outs(), FuncIdHelper);<br>
+  } else {<br>
+    ST.printIgnoringThreads(outs()<wbr>, FuncIdHelper);<br>
+  }<br>
+  return Error::success();<br>
+});<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>