blob: ebf0256783057bc95a5686b99f21528ed3b9ae51 [file] [log] [blame]
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +00001====================
2XRay Instrumentation
3====================
4
5:Version: 1 as of 2016-11-08
6
7.. contents::
8 :local:
9
10
11Introduction
12============
13
14XRay is a function call tracing system which combines compiler-inserted
15instrumentation points and a runtime library that can dynamically enable and
16disable the instrumentation.
17
18More high level information about XRay can be found in the `XRay whitepaper`_.
19
20This document describes how to use XRay as implemented in LLVM.
21
22XRay in LLVM
23============
24
25XRay consists of three main parts:
26
27- Compiler-inserted instrumentation points.
28- A runtime library for enabling/disabling tracing at runtime.
29- A suite of tools for analysing the traces.
30
Dean Michael Berris352e7602017-02-28 22:01:26 +000031 **NOTE:** As of February 27, 2017 , XRay is only available for the following
32 architectures running Linux: x86_64, arm7 (no thumb), aarch64, powerpc64le,
33 mips, mipsel, mips64, mips64el.
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +000034
35The compiler-inserted instrumentation points come in the form of nop-sleds in
36the final generated binary, and an ELF section named ``xray_instr_map`` which
37contains entries pointing to these instrumentation points. The runtime library
38relies on being able to access the entries of the ``xray_instr_map``, and
39overwrite the instrumentation points at runtime.
40
41Using XRay
42==========
43
44You can use XRay in a couple of ways:
45
46- Instrumenting your C/C++/Objective-C/Objective-C++ application.
47- Generating LLVM IR with the correct function attributes.
48
49The rest of this section covers these main ways and later on how to customise
50what XRay does in an XRay-instrumented binary.
51
52Instrumenting your C/C++/Objective-C Application
53------------------------------------------------
54
55The easiest way of getting XRay instrumentation for your application is by
56enabling the ``-fxray-instrument`` flag in your clang invocation.
57
58For example:
59
60::
61
62 clang -fxray-instrument ..
63
64By default, functions that have at least 200 instructions will get XRay
65instrumentation points. You can tweak that number through the
66``-fxray-instruction-threshold=`` flag:
67
68::
69
70 clang -fxray-instrument -fxray-instruction-threshold=1 ..
71
72You can also specifically instrument functions in your binary to either always
73or never be instrumented using source-level attributes. You can do it using the
74GCC-style attributes or C++11-style attributes.
75
76.. code-block:: c++
77
Fangrui Songcc1328c2017-11-27 16:59:26 +000078 [[clang::xray_always_instrument]] void always_instrumented();
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +000079
80 [[clang::xray_never_instrument]] void never_instrumented();
81
Fangrui Songcc1328c2017-11-27 16:59:26 +000082 void alt_always_instrumented() __attribute__((xray_always_instrument));
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +000083
84 void alt_never_instrumented() __attribute__((xray_never_instrument));
85
86When linking a binary, you can either manually link in the `XRay Runtime
87Library`_ or use ``clang`` to link it in automatically with the
Dean Michael Berris352e7602017-02-28 22:01:26 +000088``-fxray-instrument`` flag. Alternatively, you can statically link-in the XRay
89runtime library from compiler-rt -- those archive files will take the name of
90`libclang_rt.xray-{arch}` where `{arch}` is the mnemonic supported by clang
91(x86_64, arm7, etc.).
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +000092
93LLVM Function Attribute
94-----------------------
95
96If you're using LLVM IR directly, you can add the ``function-instrument``
97string attribute to your functions, to get the similar effect that the
98C/C++/Objective-C source-level attributes would get:
99
100.. code-block:: llvm
101
102 define i32 @always_instrument() uwtable "function-instrument"="xray-always" {
Dean Michael Berris0f1ddfa2016-11-09 02:12:13 +0000103 ; ...
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000104 }
105
106 define i32 @never_instrument() uwtable "function-instrument"="xray-never" {
Dean Michael Berris0f1ddfa2016-11-09 02:12:13 +0000107 ; ...
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000108 }
109
110You can also set the ``xray-instruction-threshold`` attribute and provide a
111numeric string value for how many instructions should be in the function before
112it gets instrumented.
113
114.. code-block:: llvm
115
116 define i32 @maybe_instrument() uwtable "xray-instruction-threshold"="2" {
Dean Michael Berris0f1ddfa2016-11-09 02:12:13 +0000117 ; ...
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000118 }
119
120XRay Runtime Library
121--------------------
122
123The XRay Runtime Library is part of the compiler-rt project, which implements
124the runtime components that perform the patching and unpatching of inserted
125instrumentation points. When you use ``clang`` to link your binaries and the
126``-fxray-instrument`` flag, it will automatically link in the XRay runtime.
127
128The default implementation of the XRay runtime will enable XRay instrumentation
129before ``main`` starts, which works for applications that have a short
130lifetime. This implementation also records all function entry and exit events
131which may result in a lot of records in the resulting trace.
132
133Also by default the filename of the XRay trace is ``xray-log.XXXXXX`` where the
134``XXXXXX`` part is randomly generated.
135
136These options can be controlled through the ``XRAY_OPTIONS`` environment
137variable, where we list down the options and their defaults below.
138
139+-------------------+-----------------+---------------+------------------------+
140| Option | Type | Default | Description |
141+===================+=================+===============+========================+
Dean Michael Berris352e7602017-02-28 22:01:26 +0000142| patch_premain | ``bool`` | ``false`` | Whether to patch |
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000143| | | | instrumentation points |
144| | | | before main. |
145+-------------------+-----------------+---------------+------------------------+
Dean Michael Berrisbf77c232017-12-05 12:43:12 +0000146| xray_mode | ``const char*`` | ``""`` | Default mode to |
147| | | | install and initialize |
148| | | | before ``main``. |
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000149+-------------------+-----------------+---------------+------------------------+
150| xray_logfile_base | ``const char*`` | ``xray-log.`` | Filename base for the |
151| | | | XRay logfile. |
152+-------------------+-----------------+---------------+------------------------+
Dean Michael Berrisbf77c232017-12-05 12:43:12 +0000153| xray_naive_log | ``bool`` | ``false`` | **DEPRECATED:** Use |
154| | | | xray_mode=xray-basic |
155| | | | instead. Whether to |
156| | | | install the basic log |
157| | | | the naive log |
158| | | | implementation. |
159+-------------------+-----------------+---------------+------------------------+
160| xray_fdr_log | ``bool`` | ``false`` | **DEPRECATED:** Use |
161| | | | xray_mode=xray-fdr |
162| | | | instead. Whether to |
163| | | | install the Flight |
164| | | | Data Recorder |
Dean Michael Berris352e7602017-02-28 22:01:26 +0000165| | | | (FDR) mode. |
166+-------------------+-----------------+---------------+------------------------+
Dean Michael Berriseec462f2017-12-13 06:37:13 +0000167| verbosity | ``int`` | ``0`` | Runtime verbosity |
168| | | | level. |
169+-------------------+-----------------+---------------+------------------------+
Dean Michael Berris352e7602017-02-28 22:01:26 +0000170
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000171
172If you choose to not use the default logging implementation that comes with the
173XRay runtime and/or control when/how the XRay instrumentation runs, you may use
174the XRay APIs directly for doing so. To do this, you'll need to include the
175``xray_interface.h`` from the compiler-rt ``xray`` directory. The important API
176functions we list below:
177
178- ``__xray_set_handler(void (*entry)(int32_t, XRayEntryType))``: Install your
179 own logging handler for when an event is encountered. See
180 ``xray/xray_interface.h`` for more details.
181- ``__xray_remove_handler()``: Removes whatever the installed handler is.
182- ``__xray_patch()``: Patch all the instrumentation points defined in the
183 binary.
184- ``__xray_unpatch()``: Unpatch the instrumentation points defined in the
185 binary.
186
Dean Michael Berris6eec7d42016-11-16 02:18:23 +0000187There are some requirements on the logging handler to be installed for the
188thread-safety of operations to be performed by the XRay runtime library:
189
190- The function should be thread-safe, as multiple threads may be invoking the
191 function at the same time. If the logging function needs to do
192 synchronisation, it must do so internally as XRay does not provide any
193 synchronisation guarantees outside from the atomicity of updates to the
194 pointer.
195- The pointer provided to ``__xray_set_handler(...)`` must be live even after
196 calls to ``__xray_remove_handler()`` and ``__xray_unpatch()`` have succeeded.
197 XRay cannot guarantee that all threads that have ever gotten a copy of the
198 pointer will not invoke the function.
199
Dean Michael Berris352e7602017-02-28 22:01:26 +0000200Flight Data Recorder Mode
201-------------------------
202
203XRay supports a logging mode which allows the application to only capture a
204fixed amount of memory's worth of events. Flight Data Recorder (FDR) mode works
205very much like a plane's "black box" which keeps recording data to memory in a
206fixed-size circular queue of buffers, and have the data available
207programmatically until the buffers are finalized and flushed. To use FDR mode
208on your application, you may set the ``xray_fdr_log`` option to ``true`` in the
209``XRAY_OPTIONS`` environment variable (while also optionally setting the
210``xray_naive_log`` to ``false``).
211
Keith Wyss3d0bc9e2017-08-02 21:47:27 +0000212When the buffers are flushed to disk, the result is a binary trace format
213described by `XRay FDR format <XRayFDRFormat.html>`_
214
Dean Michael Berris352e7602017-02-28 22:01:26 +0000215When FDR mode is on, it will keep writing and recycling memory buffers until
216the logging implementation is finalized -- at which point it can be flushed and
217re-initialised later. To do this programmatically, we follow the workflow
218provided below:
219
220.. code-block:: c++
221
222 // Patch the sleds, if we haven't yet.
223 auto patch_status = __xray_patch();
224
225 // Maybe handle the patch_status errors.
226
227 // When we want to flush the log, we need to finalize it first, to give
228 // threads a chance to return buffers to the queue.
229 auto finalize_status = __xray_log_finalize();
230 if (finalize_status != XRAY_LOG_FINALIZED) {
231 // maybe retry, or bail out.
232 }
233
234 // At this point, we are sure that the log is finalized, so we may try
235 // flushing the log.
236 auto flush_status = __xray_log_flushLog();
237 if (flush_status != XRAY_LOG_FLUSHED) {
238 // maybe retry, or bail out.
239 }
240
241The default settings for the FDR mode implementation will create logs named
242similarly to the naive log implementation, but will have a different log
243format. All the trace analysis tools (and the trace reading library) will
244support all versions of the FDR mode format as we add more functionality and
245record types in the future.
246
247 **NOTE:** We do not however promise perpetual support for when we update the
248 log versions we support going forward. Deprecation of the formats will be
249 announced and discussed on the developers mailing list.
250
251XRay allows for replacing the default FDR mode logging implementation using the
252following API:
253
254- ``__xray_set_log_impl(...)``: This function takes a struct of type
255 ``XRayLogImpl``, which is defined in ``xray/xray_log_interface.h``, part of
256 the XRay compiler-rt installation.
Dean Michael Berrisbf77c232017-12-05 12:43:12 +0000257- ``__xray_log_register_mode(...)``: Register a logging implementation against
258 a string Mode. The implementation is an instance of ``XRayLogImpl`` defined
259 in ``xray/xray_log_interface.h``.
260- ``__xray_log_select_mode(...)``: Select the mode to install, associated with
261 a string Mode. Only implementations registered with
262 ``__xray_log_register_mode(...)`` can be chosen with this function. When
263 successful, has the same effects as calling ``__xray_set_log_impl(...)`` with
264 the registered logging implementation.
Dean Michael Berris352e7602017-02-28 22:01:26 +0000265- ``__xray_log_init(...)``: This function allows for initializing and
266 re-initializing an installed logging implementation. See
267 ``xray/xray_log_interface.h`` for details, part of the XRay compiler-rt
268 installation.
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000269
270Trace Analysis Tools
271--------------------
272
273We currently have the beginnings of a trace analysis tool in LLVM, which can be
274found in the ``tools/llvm-xray`` directory. The ``llvm-xray`` tool currently
275supports the following subcommands:
276
277- ``extract``: Extract the instrumentation map from a binary, and return it as
278 YAML.
Dean Michael Berris352e7602017-02-28 22:01:26 +0000279- ``account``: Performs basic function call accounting statistics with various
280 options for sorting, and output formats (supports CSV, YAML, and
281 console-friendly TEXT).
Dean Michael Berris98502762017-11-30 05:35:51 +0000282- ``convert``: Converts an XRay log file from one format to another. We can
283 convert from binary XRay traces (both naive and FDR mode) to YAML,
284 `flame-graph <https://github.com/brendangregg/FlameGraph>`_ friendly text
285 formats, as well as `Chrome Trace Viewer (catapult)
286 <https://github.com/catapult-project/catapult>` formats.
Dean Michael Berris352e7602017-02-28 22:01:26 +0000287- ``graph``: Generates a DOT graph of the function call relationships between
288 functions found in an XRay trace.
Keith Wyssb2f894f2017-10-19 22:35:09 +0000289- ``stack``: Reconstructs function call stacks from a timeline of function
290 calls in an XRay trace.
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000291
Dean Michael Berris352e7602017-02-28 22:01:26 +0000292These subcommands use various library components found as part of the XRay
293libraries, distributed with the LLVM distribution. These are:
294
295- ``llvm/XRay/Trace.h`` : A trace reading library for conveniently loading
296 an XRay trace of supported forms, into a convenient in-memory representation.
297 All the analysis tools that deal with traces use this implementation.
298- ``llvm/XRay/Graph.h`` : A semi-generic graph type used by the graph
299 subcommand to conveniently represent a function call graph with statistics
300 associated with edges and vertices.
301- ``llvm/XRay/InstrumentationMap.h``: A convenient tool for analyzing the
302 instrumentation map in XRay-instrumented object files and binaries. The
Keith Wyssb2f894f2017-10-19 22:35:09 +0000303 ``extract`` and ``stack`` subcommands uses this particular library.
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000304
305Future Work
306===========
307
308There are a number of ongoing efforts for expanding the toolset building around
309the XRay instrumentation system.
310
Keith Wyssb2f894f2017-10-19 22:35:09 +0000311Trace Analysis Tools
312--------------------
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000313
Keith Wyssb2f894f2017-10-19 22:35:09 +0000314- Work is in progress to integrate with or develop tools to visualize findings
315 from an XRay trace. Particularly, the ``stack`` tool is being expanded to
316 output formats that allow graphing and exploring the duration of time in each
317 call stack.
318- With a large instrumented binary, the size of generated XRay traces can
319 quickly become unwieldy. We are working on integrating pruning techniques and
320 heuristics for the analysis tools to sift through the traces and surface only
321 relevant information.
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000322
323More Platforms
324--------------
325
Dean Michael Berris352e7602017-02-28 22:01:26 +0000326We're looking forward to contributions to port XRay to more architectures and
327operating systems.
Dean Michael Berrisf3da16b2016-11-09 00:24:58 +0000328
329.. References...
330
331.. _`XRay whitepaper`: http://research.google.com/pubs/pub45287.html
332