19f8e1810SJerin Jacob.. SPDX-License-Identifier: BSD-3-Clause 29f8e1810SJerin Jacob Copyright(C) 2020 Marvell International Ltd. 39f8e1810SJerin Jacob 49f8e1810SJerin JacobTrace Library 59f8e1810SJerin Jacob============= 69f8e1810SJerin Jacob 79f8e1810SJerin JacobOverview 89f8e1810SJerin Jacob-------- 99f8e1810SJerin Jacob 109f8e1810SJerin Jacob*Tracing* is a technique used to understand what goes on in a running software 119f8e1810SJerin Jacobsystem. The software used for tracing is called a *tracer*, which is 129f8e1810SJerin Jacobconceptually similar to a tape recorder. 139f8e1810SJerin JacobWhen recording, specific instrumentation points placed in the software source 149f8e1810SJerin Jacobcode generate events that are saved on a giant tape: a trace file. 159f8e1810SJerin JacobThe trace file then later can be opened in *trace viewers* to visualize and 169f8e1810SJerin Jacobanalyze the trace events with timestamps and multi-core views. 179f8e1810SJerin JacobSuch a mechanism will be useful for resolving a wide range of problems such as 189f8e1810SJerin Jacobmulti-core synchronization issues, latency measurements, finding out the 199f8e1810SJerin Jacobpost analysis information like CPU idle time, etc that would otherwise be 209f8e1810SJerin Jacobextremely challenging to get. 219f8e1810SJerin Jacob 229f8e1810SJerin JacobTracing is often compared to *logging*. However, tracers and loggers are two 239f8e1810SJerin Jacobdifferent tools, serving two different purposes. 249f8e1810SJerin JacobTracers are designed to record much lower-level events that occur much more 259f8e1810SJerin Jacobfrequently than log messages, often in the range of thousands per second, with 269f8e1810SJerin Jacobvery little execution overhead. 279f8e1810SJerin JacobLogging is more appropriate for a very high-level analysis of less frequent 289f8e1810SJerin Jacobevents: user accesses, exceptional conditions (errors and warnings, for 299f8e1810SJerin Jacobexample), database transactions, instant messaging communications, and such. 309f8e1810SJerin JacobSimply put, logging is one of the many use cases that can be satisfied with 319f8e1810SJerin Jacobtracing. 329f8e1810SJerin Jacob 339f8e1810SJerin JacobDPDK tracing library features 349f8e1810SJerin Jacob----------------------------- 359f8e1810SJerin Jacob 369f8e1810SJerin Jacob- A framework to add tracepoints in control and fast path APIs with minimum 379f8e1810SJerin Jacob impact on performance. 389f8e1810SJerin Jacob Typical trace overhead is ~20 cycles and instrumentation overhead is 1 cycle. 399f8e1810SJerin Jacob- Enable and disable the tracepoints at runtime. 409f8e1810SJerin Jacob- Save the trace buffer to the filesystem at any point in time. 419f8e1810SJerin Jacob- Support ``overwrite`` and ``discard`` trace mode operations. 429f8e1810SJerin Jacob- String-based tracepoint object lookup. 439f8e1810SJerin Jacob- Enable and disable a set of tracepoints based on regular expression and/or 449f8e1810SJerin Jacob globbing. 459f8e1810SJerin Jacob- Generate trace in ``Common Trace Format (CTF)``. ``CTF`` is an open-source 469f8e1810SJerin Jacob trace format and is compatible with ``LTTng``. 479f8e1810SJerin Jacob For detailed information, refer to 489f8e1810SJerin Jacob `Common Trace Format <https://diamon.org/ctf/>`_. 499f8e1810SJerin Jacob 509f8e1810SJerin JacobHow to add a tracepoint? 519f8e1810SJerin Jacob------------------------ 529f8e1810SJerin Jacob 539f8e1810SJerin JacobThis section steps you through the details of adding a simple tracepoint. 549f8e1810SJerin Jacob 55ebaee640SDavid Marchand.. _create_tracepoint_header_file: 569f8e1810SJerin Jacob 57ebaee640SDavid MarchandCreate the tracepoint header file 58ebaee640SDavid Marchand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 599f8e1810SJerin Jacob 609f8e1810SJerin Jacob.. code-block:: c 619f8e1810SJerin Jacob 629f8e1810SJerin Jacob #include <rte_trace_point.h> 639f8e1810SJerin Jacob 649f8e1810SJerin Jacob RTE_TRACE_POINT( 659f8e1810SJerin Jacob app_trace_string, 669f8e1810SJerin Jacob RTE_TRACE_POINT_ARGS(const char *str), 679f8e1810SJerin Jacob rte_trace_point_emit_string(str); 689f8e1810SJerin Jacob ) 699f8e1810SJerin Jacob 709f8e1810SJerin JacobThe above macro creates ``app_trace_string`` tracepoint. 719f8e1810SJerin JacobThe user can choose any name for the tracepoint. 729f8e1810SJerin JacobHowever, when adding a tracepoint in the DPDK library, the 739f8e1810SJerin Jacob``rte_<library_name>_trace_[<domain>_]<name>`` naming convention must be 749f8e1810SJerin Jacobfollowed. 759f8e1810SJerin JacobThe examples are ``rte_eal_trace_generic_str``, ``rte_mempool_trace_create``. 769f8e1810SJerin Jacob 779f8e1810SJerin JacobThe ``RTE_TRACE_POINT`` macro expands from above definition as the following 789f8e1810SJerin Jacobfunction template: 799f8e1810SJerin Jacob 809f8e1810SJerin Jacob.. code-block:: c 819f8e1810SJerin Jacob 829f8e1810SJerin Jacob static __rte_always_inline void 839f8e1810SJerin Jacob app_trace_string(const char *str) 849f8e1810SJerin Jacob { 859f8e1810SJerin Jacob /* Trace subsystem hooks */ 869f8e1810SJerin Jacob ... 879f8e1810SJerin Jacob rte_trace_point_emit_string(str); 889f8e1810SJerin Jacob } 899f8e1810SJerin Jacob 909f8e1810SJerin JacobThe consumer of this tracepoint can invoke 919f8e1810SJerin Jacob``app_trace_string(const char *str)`` to emit the trace event to the trace 929f8e1810SJerin Jacobbuffer. 939f8e1810SJerin Jacob 949f8e1810SJerin JacobRegister the tracepoint 959f8e1810SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~ 969f8e1810SJerin Jacob 979f8e1810SJerin Jacob.. code-block:: c 989f8e1810SJerin Jacob 99ebaee640SDavid Marchand #include <rte_trace_point_register.h> 1009f8e1810SJerin Jacob 101ebaee640SDavid Marchand #include <my_tracepoint.h> 1029f8e1810SJerin Jacob 1030fc601afSDavid Marchand RTE_TRACE_POINT_REGISTER(app_trace_string, app.trace.string) 1049f8e1810SJerin Jacob 1059f8e1810SJerin JacobThe above code snippet registers the ``app_trace_string`` tracepoint to 106ebaee640SDavid Marchandtrace library. Here, the ``my_tracepoint.h`` is the header file 107ebaee640SDavid Marchandthat the user created in the first step :ref:`create_tracepoint_header_file`. 1089f8e1810SJerin Jacob 1099f8e1810SJerin JacobThe second argument for the ``RTE_TRACE_POINT_REGISTER`` is the name for the 1109f8e1810SJerin Jacobtracepoint. This string will be used for tracepoint lookup or regular 1119f8e1810SJerin Jacobexpression and/or glob based tracepoint operations. 1129f8e1810SJerin JacobThere is no requirement for the tracepoint function and its name to be similar. 1139f8e1810SJerin JacobHowever, it is recommended to have a similar name for a better naming 1149f8e1810SJerin Jacobconvention. 1159f8e1810SJerin Jacob 1169f8e1810SJerin Jacob.. note:: 1179f8e1810SJerin Jacob 118ebaee640SDavid Marchand The ``rte_trace_point_register.h`` header must be included before any 119ebaee640SDavid Marchand inclusion of the ``rte_trace_point.h`` header. 1209f8e1810SJerin Jacob 1219f8e1810SJerin Jacob.. note:: 1229f8e1810SJerin Jacob 1230fc601afSDavid Marchand The ``RTE_TRACE_POINT_REGISTER`` defines the placeholder for the 1249f8e1810SJerin Jacob ``rte_trace_point_t`` tracepoint object. The user must export a 1259f8e1810SJerin Jacob ``__<trace_function_name>`` symbol in the library ``.map`` file for this 1269f8e1810SJerin Jacob tracepoint to be used out of the library, in shared builds. 1279f8e1810SJerin Jacob For example, ``__app_trace_string`` will be the exported symbol in the 1289f8e1810SJerin Jacob above example. 1299f8e1810SJerin Jacob 1309f8e1810SJerin JacobFast path tracepoint 1319f8e1810SJerin Jacob-------------------- 1329f8e1810SJerin Jacob 1339f8e1810SJerin JacobIn order to avoid performance impact in fast path code, the library introduced 1349f8e1810SJerin Jacob``RTE_TRACE_POINT_FP``. When adding the tracepoint in fast path code, 1359f8e1810SJerin Jacobthe user must use ``RTE_TRACE_POINT_FP`` instead of ``RTE_TRACE_POINT``. 1369f8e1810SJerin Jacob 1379f8e1810SJerin Jacob``RTE_TRACE_POINT_FP`` is compiled out by default and it can be enabled using 13889c67ae2SCiara Powerthe ``enable_trace_fp`` option for meson build. 1399f8e1810SJerin Jacob 1409f8e1810SJerin JacobEvent record mode 1419f8e1810SJerin Jacob----------------- 1429f8e1810SJerin Jacob 1439f8e1810SJerin JacobEvent record mode is an attribute of trace buffers. Trace library exposes the 1449f8e1810SJerin Jacobfollowing modes: 1459f8e1810SJerin Jacob 1469f8e1810SJerin JacobOverwrite 1479f8e1810SJerin Jacob When the trace buffer is full, new trace events overwrites the existing 1489f8e1810SJerin Jacob captured events in the trace buffer. 1499f8e1810SJerin JacobDiscard 1509f8e1810SJerin Jacob When the trace buffer is full, new trace events will be discarded. 1519f8e1810SJerin Jacob 1529f8e1810SJerin JacobThe mode can be configured either using EAL command line parameter 1539f8e1810SJerin Jacob``--trace-mode`` on application boot up or use ``rte_trace_mode_set()`` API to 1549f8e1810SJerin Jacobconfigure at runtime. 1559f8e1810SJerin Jacob 1569f8e1810SJerin JacobTrace file location 1579f8e1810SJerin Jacob------------------- 1589f8e1810SJerin Jacob 1599f8e1810SJerin JacobOn ``rte_trace_save()`` or ``rte_eal_cleanup()`` invocation, the library saves 1609f8e1810SJerin Jacobthe trace buffers to the filesystem. By default, the trace files are stored in 1619f8e1810SJerin Jacob``$HOME/dpdk-traces/rte-yyyy-mm-dd-[AP]M-hh-mm-ss/``. 1629f8e1810SJerin JacobIt can be overridden by the ``--trace-dir=<directory path>`` EAL command line 1639f8e1810SJerin Jacoboption. 1649f8e1810SJerin Jacob 1659f8e1810SJerin JacobFor more information, refer to :doc:`../linux_gsg/linux_eal_parameters` for 1669f8e1810SJerin Jacobtrace EAL command line options. 1679f8e1810SJerin Jacob 1689f8e1810SJerin JacobView and analyze the recorded events 1699f8e1810SJerin Jacob------------------------------------ 1709f8e1810SJerin Jacob 1719f8e1810SJerin JacobOnce the trace directory is available, the user can view/inspect the recorded 1729f8e1810SJerin Jacobevents. 1739f8e1810SJerin Jacob 1749f8e1810SJerin JacobThere are many tools you can use to read DPDK traces: 1759f8e1810SJerin Jacob 1769f8e1810SJerin Jacob1. ``babeltrace`` is a command-line utility that converts trace formats; it 1779f8e1810SJerin Jacobsupports the format that DPDK trace library produces, CTF, as well as a 1789f8e1810SJerin Jacobbasic text output that can be grep'ed. 1799f8e1810SJerin JacobThe babeltrace command is part of the Open Source Babeltrace project. 1809f8e1810SJerin Jacob 1819f8e1810SJerin Jacob2. ``Trace Compass`` is a graphical user interface for viewing and analyzing 1829f8e1810SJerin Jacobany type of logs or traces, including DPDK traces. 1839f8e1810SJerin Jacob 1849f8e1810SJerin JacobUse the babeltrace command-line tool 1859f8e1810SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1869f8e1810SJerin Jacob 1879f8e1810SJerin JacobThe simplest way to list all the recorded events of a trace is to pass its path 1889f8e1810SJerin Jacobto babeltrace with no options:: 1899f8e1810SJerin Jacob 1909f8e1810SJerin Jacob babeltrace </path-to-trace-events/rte-yyyy-mm-dd-[AP]M-hh-mm-ss/> 1919f8e1810SJerin Jacob 1929f8e1810SJerin Jacob``babeltrace`` finds all traces recursively within the given path and prints 1939f8e1810SJerin Jacoball their events, merging them in chronological order. 1949f8e1810SJerin Jacob 1959f8e1810SJerin JacobYou can pipe the output of the babeltrace into a tool like grep(1) for further 1969f8e1810SJerin Jacobfiltering. Below example grep the events for ``ethdev`` only:: 1979f8e1810SJerin Jacob 1989f8e1810SJerin Jacob babeltrace /tmp/my-dpdk-trace | grep ethdev 1999f8e1810SJerin Jacob 2009f8e1810SJerin JacobYou can pipe the output of babeltrace into a tool like wc(1) to count the 2019f8e1810SJerin Jacobrecorded events. Below example count the number of ``ethdev`` events:: 2029f8e1810SJerin Jacob 2039f8e1810SJerin Jacob babeltrace /tmp/my-dpdk-trace | grep ethdev | wc --lines 2049f8e1810SJerin Jacob 2059f8e1810SJerin JacobUse the tracecompass GUI tool 2069f8e1810SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2079f8e1810SJerin Jacob 2089f8e1810SJerin Jacob``Tracecompass`` is another tool to view/analyze the DPDK traces which gives 2099f8e1810SJerin Jacoba graphical view of events. Like ``babeltrace``, tracecompass also provides 2109f8e1810SJerin Jacoban interface to search for a particular event. 2119f8e1810SJerin JacobTo use ``tracecompass``, following are the minimum required steps: 2129f8e1810SJerin Jacob 2139f8e1810SJerin Jacob- Install ``tracecompass`` to the localhost. Variants are available for Linux, 2149f8e1810SJerin Jacob Windows, and OS-X. 2159f8e1810SJerin Jacob- Launch ``tracecompass`` which will open a graphical window with trace 2169f8e1810SJerin Jacob management interfaces. 2179f8e1810SJerin Jacob- Open a trace using ``File->Open Trace`` option and select metadata file which 2189f8e1810SJerin Jacob is to be viewed/analyzed. 2199f8e1810SJerin Jacob 2209f8e1810SJerin JacobFor more details, refer 2219f8e1810SJerin Jacob`Trace Compass <https://www.eclipse.org/tracecompass/>`_. 2229f8e1810SJerin Jacob 2239f8e1810SJerin JacobQuick start 2249f8e1810SJerin Jacob----------- 2259f8e1810SJerin Jacob 2269f8e1810SJerin JacobThis section steps you through the details of generating trace and viewing it. 2279f8e1810SJerin Jacob 2289f8e1810SJerin Jacob- Start the dpdk-test:: 2299f8e1810SJerin Jacob 2309f8e1810SJerin Jacob echo "quit" | ./build/app/test/dpdk-test --no-huge --trace=.* 2319f8e1810SJerin Jacob 2329f8e1810SJerin Jacob- View the traces with babeltrace viewer:: 2339f8e1810SJerin Jacob 2349f8e1810SJerin Jacob babeltrace $HOME/dpdk-traces/rte-yyyy-mm-dd-[AP]M-hh-mm-ss/ 2359f8e1810SJerin Jacob 2369f8e1810SJerin JacobImplementation details 2379f8e1810SJerin Jacob---------------------- 2389f8e1810SJerin Jacob 2399f8e1810SJerin JacobAs DPDK trace library is designed to generate traces that uses ``Common Trace 2409f8e1810SJerin JacobFormat (CTF)``. ``CTF`` specification consists of the following units to create 2419f8e1810SJerin Jacoba trace. 2429f8e1810SJerin Jacob 2439f8e1810SJerin Jacob- ``Stream`` Sequence of packets. 2449f8e1810SJerin Jacob- ``Packet`` Header and one or more events. 2459f8e1810SJerin Jacob- ``Event`` Header and payload. 2469f8e1810SJerin Jacob 2479f8e1810SJerin JacobFor detailed information, refer to 2489f8e1810SJerin Jacob`Common Trace Format <https://diamon.org/ctf/>`_. 2499f8e1810SJerin Jacob 2509f8e1810SJerin JacobThe implementation details broadly divided into the following areas: 2519f8e1810SJerin Jacob 2529f8e1810SJerin JacobTrace metadata creation 2539f8e1810SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~ 2549f8e1810SJerin Jacob 2559f8e1810SJerin JacobBased on the ``CTF`` specification, one of a CTF trace's streams is mandatory: 2569f8e1810SJerin Jacobthe metadata stream. It contains exactly what you would expect: data about the 2579f8e1810SJerin Jacobtrace itself. The metadata stream contains a textual description of the binary 2589f8e1810SJerin Jacoblayouts of all the other streams. 2599f8e1810SJerin Jacob 2609f8e1810SJerin JacobThis description is written using the Trace Stream Description Language (TSDL), 2619f8e1810SJerin Jacoba declarative language that exists only in the realm of CTF. 2629f8e1810SJerin JacobThe purpose of the metadata stream is to make CTF readers know how to parse a 2639f8e1810SJerin Jacobtrace's binary streams of events without CTF specifying any fixed layout. 2649f8e1810SJerin JacobThe only stream layout known in advance is, in fact, the metadata stream's one. 2659f8e1810SJerin Jacob 2669f8e1810SJerin JacobThe internal ``trace_metadata_create()`` function generates the metadata. 2679f8e1810SJerin Jacob 2689f8e1810SJerin JacobTrace memory 2699f8e1810SJerin Jacob~~~~~~~~~~~~ 2709f8e1810SJerin Jacob 2719f8e1810SJerin JacobThe trace memory will be allocated through an internal function 2729f8e1810SJerin Jacob``__rte_trace_mem_per_thread_alloc()``. The trace memory will be allocated 2739f8e1810SJerin Jacobper thread to enable lock less trace-emit function. 274*d6fd5a01SDavid Marchand 275*d6fd5a01SDavid MarchandFor non lcore threads, the trace memory is allocated on the first trace 276*d6fd5a01SDavid Marchandemission. 277*d6fd5a01SDavid Marchand 278*d6fd5a01SDavid MarchandFor lcore threads, if trace points are enabled through a EAL option, the trace 279*d6fd5a01SDavid Marchandmemory is allocated when the threads are known of DPDK 280*d6fd5a01SDavid Marchand(``rte_eal_init`` for EAL lcores, ``rte_thread_register`` for non-EAL lcores). 281*d6fd5a01SDavid MarchandOtherwise, when trace points are enabled later in the life of the application, 282*d6fd5a01SDavid Marchandthe behavior is the same as non lcore threads and the trace memory is allocated 283*d6fd5a01SDavid Marchandon the first trace emission. 2849f8e1810SJerin Jacob 2859f8e1810SJerin JacobTrace memory layout 2869f8e1810SJerin Jacob~~~~~~~~~~~~~~~~~~~ 2879f8e1810SJerin Jacob 2889f8e1810SJerin Jacob.. _table_trace_mem_layout: 2899f8e1810SJerin Jacob 2909f8e1810SJerin Jacob.. table:: Trace memory layout. 2919f8e1810SJerin Jacob 2929f8e1810SJerin Jacob +-------------------+ 2939f8e1810SJerin Jacob | packet.header | 2949f8e1810SJerin Jacob +-------------------+ 2959f8e1810SJerin Jacob | packet.context | 2969f8e1810SJerin Jacob +-------------------+ 2979f8e1810SJerin Jacob | trace 0 header | 2989f8e1810SJerin Jacob +-------------------+ 2999f8e1810SJerin Jacob | trace 0 payload | 3009f8e1810SJerin Jacob +-------------------+ 3019f8e1810SJerin Jacob | trace 1 header | 3029f8e1810SJerin Jacob +-------------------+ 3039f8e1810SJerin Jacob | trace 1 payload | 3049f8e1810SJerin Jacob +-------------------+ 3059f8e1810SJerin Jacob | trace N header | 3069f8e1810SJerin Jacob +-------------------+ 3079f8e1810SJerin Jacob | trace N payload | 3089f8e1810SJerin Jacob +-------------------+ 3099f8e1810SJerin Jacob 3109f8e1810SJerin Jacobpacket.header 3119f8e1810SJerin Jacob^^^^^^^^^^^^^ 3129f8e1810SJerin Jacob 3139f8e1810SJerin Jacob.. _table_packet_header: 3149f8e1810SJerin Jacob 3159f8e1810SJerin Jacob.. table:: Packet header layout. 3169f8e1810SJerin Jacob 3179f8e1810SJerin Jacob +-------------------+ 3189f8e1810SJerin Jacob | uint32_t magic | 3199f8e1810SJerin Jacob +-------------------+ 3209f8e1810SJerin Jacob | rte_uuid_t uuid | 3219f8e1810SJerin Jacob +-------------------+ 3229f8e1810SJerin Jacob 3239f8e1810SJerin Jacobpacket.context 3249f8e1810SJerin Jacob^^^^^^^^^^^^^^ 3259f8e1810SJerin Jacob 3269f8e1810SJerin Jacob.. _table_packet_context: 3279f8e1810SJerin Jacob 3289f8e1810SJerin Jacob.. table:: Packet context layout. 3299f8e1810SJerin Jacob 3309f8e1810SJerin Jacob +----------------------+ 3319f8e1810SJerin Jacob | uint32_t thread_id | 3329f8e1810SJerin Jacob +----------------------+ 3339f8e1810SJerin Jacob | char thread_name[32] | 3349f8e1810SJerin Jacob +----------------------+ 3359f8e1810SJerin Jacob 3369f8e1810SJerin Jacobtrace.header 3379f8e1810SJerin Jacob^^^^^^^^^^^^ 3389f8e1810SJerin Jacob 3399f8e1810SJerin Jacob.. _table_trace_header: 3409f8e1810SJerin Jacob 3419f8e1810SJerin Jacob.. table:: Trace header layout. 3429f8e1810SJerin Jacob 3439f8e1810SJerin Jacob +----------------------+ 3449f8e1810SJerin Jacob | event_id [63:48] | 3459f8e1810SJerin Jacob +----------------------+ 3469f8e1810SJerin Jacob | timestamp [47:0] | 3479f8e1810SJerin Jacob +----------------------+ 3489f8e1810SJerin Jacob 3499f8e1810SJerin JacobThe trace header is 64 bits, it consists of 48 bits of timestamp and 16 bits 3509f8e1810SJerin Jacobevent ID. 3519f8e1810SJerin Jacob 3529f8e1810SJerin JacobThe ``packet.header`` and ``packet.context`` will be written in the slow path 3539f8e1810SJerin Jacobat the time of trace memory creation. The ``trace.header`` and trace payload 3549f8e1810SJerin Jacobwill be emitted when the tracepoint function is invoked. 355