annotate docs/XRayExample.rst @ 140:276c179585fe

Added tag LLVM5.0.1 for changeset 3a76565eade5
author mir3636
date Tue, 03 Apr 2018 19:09:39 +0900
parents 3a76565eade5
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 ===================
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 Debugging with XRay
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 ===================
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 This document shows an example of how you would go about analyzing applications
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 built with XRay instrumentation. Here we will attempt to debug ``llc``
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 compiling some sample LLVM IR generated by Clang.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 .. contents::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 :local:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 Building with XRay
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 ------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 To debug an application with XRay instrumentation, we need to build it with a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 Clang that supports the ``-fxray-instrument`` option. See `XRay <XRay.html>`_
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 for more technical details of how XRay works for background information.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 In our example, we need to add ``-fxray-instrument`` to the list of flags
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 passed to Clang when building a binary. Note that we need to link with Clang as
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 well to get the XRay runtime linked in appropriately. For building ``llc`` with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 XRay, we do something similar below for our LLVM build:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 $ mkdir -p llvm-build && cd llvm-build
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 # Assume that the LLVM sources are at ../llvm
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 $ cmake -GNinja ../llvm -DCMAKE_BUILD_TYPE=Release \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 -DCMAKE_C_FLAGS_RELEASE="-fxray-instrument" -DCMAKE_CXX_FLAGS="-fxray-instrument" \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 # Once this finishes, we should build llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 $ ninja llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 To verify that we have an XRay instrumented binary, we can use ``objdump`` to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 look for the ``xray_instr_map`` section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 $ objdump -h -j xray_instr_map ./bin/llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 ./bin/llc: file format elf64-x86-64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 Sections:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 Idx Name Size VMA LMA File off Algn
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 14 xray_instr_map 00002fc0 00000000041516c6 00000000041516c6 03d516c6 2**0
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 CONTENTS, ALLOC, LOAD, READONLY, DATA
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 Getting Traces
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 --------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 By default, XRay does not write out the trace files or patch the application
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 before main starts. If we just run ``llc`` it should just work like a normally
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 built binary. However, if we want to get a full trace of the application's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 operations (of the functions we do end up instrumenting with XRay) then we need
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 to enable XRay at application start. To do this, XRay checks the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 ``XRAY_OPTIONS`` environment variable.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 # The following doesn't create an XRay trace by default.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 $ ./bin/llc input.ll
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 # We need to set the XRAY_OPTIONS to enable some features.
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
63 $ XRAY_OPTIONS="patch_premain=true xray_mode=xray-basic verbosity=1" ./bin/llc input.ll
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 ==69819==XRay: Log file in 'xray-log.llc.m35qPB'
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 At this point we now have an XRay trace we can start analysing.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 The ``llvm-xray`` Tool
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 ----------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 Having a trace then allows us to do basic accounting of the functions that were
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 instrumented, and how much time we're spending in parts of the code. To make
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 sense of this data, we use the ``llvm-xray`` tool which has a few subcommands
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 to help us understand our trace.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 One of the simplest things we can do is to get an accounting of the functions
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 that have been instrumented. We can see an example accounting with ``llvm-xray
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 account``:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 $ llvm-xray account xray-log.llc.m35qPB -top=10 -sort=sum -sortorder=dsc -instr_map ./bin/llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 Functions with latencies: 29
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 funcid count [ min, med, 90p, 99p, max] sum function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 187 360 [ 0.000000, 0.000001, 0.000014, 0.000032, 0.000075] 0.001596 LLLexer.cpp:446:0: llvm::LLLexer::LexIdentifier()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 85 130 [ 0.000000, 0.000000, 0.000018, 0.000023, 0.000156] 0.000799 X86ISelDAGToDAG.cpp:1984:0: (anonymous namespace)::X86DAGToDAGISel::Select(llvm::SDNode*)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 138 130 [ 0.000000, 0.000000, 0.000017, 0.000155, 0.000155] 0.000774 SelectionDAGISel.cpp:2963:0: llvm::SelectionDAGISel::SelectCodeCommon(llvm::SDNode*, unsigned char const*, unsigned int)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 188 103 [ 0.000000, 0.000000, 0.000003, 0.000123, 0.000214] 0.000737 LLParser.cpp:2692:0: llvm::LLParser::ParseValID(llvm::ValID&, llvm::LLParser::PerFunctionState*)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 88 1 [ 0.000562, 0.000562, 0.000562, 0.000562, 0.000562] 0.000562 X86ISelLowering.cpp:83:0: llvm::X86TargetLowering::X86TargetLowering(llvm::X86TargetMachine const&, llvm::X86Subtarget const&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 125 102 [ 0.000001, 0.000003, 0.000010, 0.000017, 0.000049] 0.000471 Verifier.cpp:3714:0: (anonymous namespace)::Verifier::visitInstruction(llvm::Instruction&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 90 8 [ 0.000023, 0.000035, 0.000106, 0.000106, 0.000106] 0.000342 X86ISelLowering.cpp:3363:0: llvm::X86TargetLowering::LowerCall(llvm::TargetLowering::CallLoweringInfo&, llvm::SmallVectorImpl<llvm::SDValue>&) const
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 124 32 [ 0.000003, 0.000007, 0.000016, 0.000041, 0.000041] 0.000310 Verifier.cpp:1967:0: (anonymous namespace)::Verifier::visitFunction(llvm::Function const&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 123 1 [ 0.000302, 0.000302, 0.000302, 0.000302, 0.000302] 0.000302 LLVMContextImpl.cpp:54:0: llvm::LLVMContextImpl::~LLVMContextImpl()
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 139 46 [ 0.000000, 0.000002, 0.000006, 0.000008, 0.000019] 0.000138 TargetLowering.cpp:506:0: llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&, llvm::APInt&, llvm::APInt&, llvm::TargetLowering::TargetLoweringOpt&, unsigned int, bool) const
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 This shows us that for our input file, ``llc`` spent the most cumulative time
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 in the lexer (a total of 1 millisecond). If we wanted for example to work with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 this data in a spreadsheet, we can output the results as CSV using the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 ``-format=csv`` option to the command for further analysis.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 If we want to get a textual representation of the raw trace we can use the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 ``llvm-xray convert`` tool to get YAML output. The first few lines of that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 output for an example trace would look like the following:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 $ llvm-xray convert -f yaml -symbolize -instr_map=./bin/llc xray-log.llc.m35qPB
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 ---
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 header:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 version: 1
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 type: 0
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 constant-tsc: true
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 nonstop-tsc: true
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 cycle-frequency: 2601000000
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 records:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 - { type: 0, func-id: 110, function: __cxx_global_var_init.8, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426023268520 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 - { type: 0, func-id: 110, function: __cxx_global_var_init.8, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426023523052 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 - { type: 0, func-id: 164, function: __cxx_global_var_init, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426029925386 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 - { type: 0, func-id: 164, function: __cxx_global_var_init, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426030031128 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 - { type: 0, func-id: 142, function: '(anonymous namespace)::CommandLineParser::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*)', cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426046951388 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 - { type: 0, func-id: 142, function: '(anonymous namespace)::CommandLineParser::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*)', cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426047282020 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 - { type: 0, func-id: 187, function: 'llvm::LLLexer::LexIdentifier()', cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426047857332 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 - { type: 0, func-id: 187, function: 'llvm::LLLexer::LexIdentifier()', cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426047984152 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 - { type: 0, func-id: 187, function: 'llvm::LLLexer::LexIdentifier()', cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426048036584 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 - { type: 0, func-id: 187, function: 'llvm::LLLexer::LexIdentifier()', cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426048042292 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 - { type: 0, func-id: 187, function: 'llvm::LLLexer::LexIdentifier()', cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426048055056 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 - { type: 0, func-id: 187, function: 'llvm::LLLexer::LexIdentifier()', cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426048067316 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 Controlling Fidelity
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 --------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 So far in our examples, we haven't been getting full coverage of the functions
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 we have in the binary. To get that, we need to modify the compiler flags so
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 that we can instrument more (if not all) the functions we have in the binary.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 We have two options for doing that, and we explore both of these below.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 Instruction Threshold
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 `````````````````````
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 The first "blunt" way of doing this is by setting the minimum threshold for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 function bodies to 1. We can do that with the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 ``-fxray-instruction-threshold=N`` flag when building our binary. We rebuild
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 ``llc`` with this option and observe the results:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 $ rm CMakeCache.txt
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 $ cmake -GNinja ../llvm -DCMAKE_BUILD_TYPE=Release \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 -DCMAKE_C_FLAGS_RELEASE="-fxray-instrument -fxray-instruction-threshold=1" \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 -DCMAKE_CXX_FLAGS="-fxray-instrument -fxray-instruction-threshold=1"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 $ ninja llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 $ XRAY_OPTIONS="patch_premain=true" ./bin/llc input.ll
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 ==69819==XRay: Log file in 'xray-log.llc.5rqxkU'
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 $ llvm-xray account xray-log.llc.5rqxkU -top=10 -sort=sum -sortorder=dsc -instr_map ./bin/llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 Functions with latencies: 36652
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 funcid count [ min, med, 90p, 99p, max] sum function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 75 1 [ 0.672368, 0.672368, 0.672368, 0.672368, 0.672368] 0.672368 llc.cpp:271:0: main
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 78 1 [ 0.626455, 0.626455, 0.626455, 0.626455, 0.626455] 0.626455 llc.cpp:381:0: compileModule(char**, llvm::LLVMContext&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 139617 1 [ 0.472618, 0.472618, 0.472618, 0.472618, 0.472618] 0.472618 LegacyPassManager.cpp:1723:0: llvm::legacy::PassManager::run(llvm::Module&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 139610 1 [ 0.472618, 0.472618, 0.472618, 0.472618, 0.472618] 0.472618 LegacyPassManager.cpp:1681:0: llvm::legacy::PassManagerImpl::run(llvm::Module&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 139612 1 [ 0.470948, 0.470948, 0.470948, 0.470948, 0.470948] 0.470948 LegacyPassManager.cpp:1564:0: (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 139607 2 [ 0.147345, 0.315994, 0.315994, 0.315994, 0.315994] 0.463340 LegacyPassManager.cpp:1530:0: llvm::FPPassManager::runOnModule(llvm::Module&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 139605 21 [ 0.000002, 0.000002, 0.102593, 0.213336, 0.213336] 0.463331 LegacyPassManager.cpp:1491:0: llvm::FPPassManager::runOnFunction(llvm::Function&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 139563 26096 [ 0.000002, 0.000002, 0.000037, 0.000063, 0.000215] 0.225708 LegacyPassManager.cpp:1083:0: llvm::PMDataManager::findAnalysisPass(void const*, bool)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 108055 188 [ 0.000002, 0.000120, 0.001375, 0.004523, 0.062624] 0.159279 MachineFunctionPass.cpp:38:0: llvm::MachineFunctionPass::runOnFunction(llvm::Function&)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 62635 22 [ 0.000041, 0.000046, 0.000050, 0.126744, 0.126744] 0.127715 X86TargetMachine.cpp:242:0: llvm::X86TargetMachine::getSubtargetImpl(llvm::Function const&) const
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 Instrumentation Attributes
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 ``````````````````````````
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 The other way is to use configuration files for selecting which functions
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 should always be instrumented by the compiler. This gives us a way of ensuring
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 that certain functions are either always or never instrumented by not having to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 add the attribute to the source.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 To use this feature, you can define one file for the functions to always
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 instrument, and another for functions to never instrument. The format of these
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 files are exactly the same as the SanitizerLists files that control similar
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 things for the sanitizer implementations. For example, we can have two
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 different files like below:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 # always-instrument.txt
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 # always instrument functions that match the following filters:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 fun:main
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 # never-instrument.txt
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 # never instrument functions that match the following filters:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 fun:__cxx_*
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 Given the above two files we can re-build by providing those two files as
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 arguments to clang as ``-fxray-always-instrument=always-instrument.txt`` or
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 ``-fxray-never-instrument=never-instrument.txt``.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 The XRay stack tool
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 -------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 Given a trace, and optionally an instrumentation map, the ``llvm-xray stack``
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 command can be used to analyze a call stack graph constructed from the function
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 call timeline.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 The simplest way to use the command is simply to output the top stacks by call
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 count and time spent.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 $ llvm-xray stack xray-log.llc.5rqxkU -instr_map ./bin/llc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 Unique Stacks: 3069
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 Top 10 Stacks by leaf sum:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 Sum: 9633790
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 lvl function count sum
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 #0 main 1 58421550
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 #1 compileModule(char**, llvm::LLVMContext&) 1 51440360
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 #2 llvm::legacy::PassManagerImpl::run(llvm::Module&) 1 40535375
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 #3 llvm::FPPassManager::runOnModule(llvm::Module&) 2 39337525
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 #4 llvm::FPPassManager::runOnFunction(llvm::Function&) 6 39331465
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 #5 llvm::PMDataManager::verifyPreservedAnalysis(llvm::Pass*) 399 16628590
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 #6 llvm::PMTopLevelManager::findAnalysisPass(void const*) 4584 15155600
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 #7 llvm::PMDataManager::findAnalysisPass(void const*, bool) 32088 9633790
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 ..etc..
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228 In the default mode, identical stacks on different threads are independently
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 aggregated. In a multithreaded program, you may end up having identical call
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 stacks fill your list of top calls.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 To address this, you may specify the ``-aggregate-threads`` or
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 ``-per-thread-stacks`` flags. ``-per-thread-stacks`` treats the thread id as an
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 implicit root in each call stack tree, while ``-aggregate-threads`` combines
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 identical stacks from all threads.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 Flame Graph Generation
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 ----------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 The ``llvm-xray stack`` tool may also be used to generate flamegraphs for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 visualizing your instrumented invocations. The tool does not generate the graphs
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 themselves, but instead generates a format that can be used with Brendan Gregg's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 FlameGraph tool, currently available on `github
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 <https://github.com/brendangregg/FlameGraph>`_.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 To generate output for a flamegraph, a few more options are necessary.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 - ``-all-stacks`` - Emits all of the stacks instead of just the top stacks.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 - ``-stack-format`` - Choose the flamegraph output format 'flame'.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 - ``-aggregation-type`` - Choose the metric to graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 You may pipe the command output directly to the flamegraph tool to obtain an
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 svg file.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 $llvm-xray stack xray-log.llc.5rqxkU -instr_map ./bin/llc -stack-format=flame -aggregation-type=time -all-stacks | \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 /path/to/FlameGraph/flamegraph.pl > flamegraph.svg
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 If you open the svg in a browser, mouse events allow exploring the call stacks.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 Further Exploration
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 -------------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 The ``llvm-xray`` tool has a few other subcommands that are in various stages
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 of being developed. One interesting subcommand that can highlight a few
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 interesting things is the ``graph`` subcommand. Given for example the following
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 toy program that we build with XRay instrumentation, we can see how the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 generated graph may be a helpful indicator of where time is being spent for the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270 application.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 .. code-block:: c++
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 // sample.cc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 #include <iostream>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 #include <thread>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
278 [[clang::xray_always_instrument]] void f() {
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 std::cerr << '.';
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
282 [[clang::xray_always_instrument]] void g() {
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 for (int i = 0; i < 1 << 10; ++i) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 std::cerr << '-';
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 int main(int argc, char* argv[]) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 std::thread t1([] {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290 for (int i = 0; i < 1 << 10; ++i)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 f();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 std::thread t2([] {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 g();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 t1.join();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 t2.join();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 std::cerr << '\n';
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 We then build the above with XRay instrumentation:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305 $ clang++ -o sample -O3 sample.cc -std=c++11 -fxray-instrument -fxray-instruction-threshold=1
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 $ XRAY_OPTIONS="patch_premain=true" ./sample
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 We can then explore the graph rendering of the trace generated by this sample
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 application. We assume you have the graphviz toosl available in your system,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 including both ``unflatten`` and ``dot``. If you prefer rendering or exploring
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 the graph using another tool, then that should be feasible as well. ``llvm-xray
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 graph`` will create DOT format graphs which should be usable in most graph
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 rendering applications. One example invocation of the ``llvm-xray graph``
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 command should yield some interesting insights to the workings of C++
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315 applications:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317 ::
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 $ llvm-xray graph xray-log.sample.* -m sample -color-edges=sum -edge-label=sum \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320 | unflatten -f -l10 | dot -Tsvg -o sample.svg
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 Next Steps
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 ----------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 If you have some interesting analyses you'd like to implement as part of the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 llvm-xray tool, please feel free to propose them on the llvm-dev@ mailing list.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 The following are some ideas to inspire you in getting involved and potentially
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 making things better.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 - Implement a query/filtering library that allows for finding patterns in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 XRay traces.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 - A conversion from the XRay trace onto something that can be visualised
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 better by other tools (like the Chrome trace viewer for example).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 - Collecting function call stacks and how often they're encountered in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 XRay trace.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337