| 1 |
Perfetto Tracing |
| 2 |
================ |
| 3 |
|
| 4 |
Mesa has experimental support for `Perfetto <https://perfetto.dev>`__ for |
| 5 |
GPU performance monitoring. Perfetto supports multiple |
| 6 |
`producers <https://perfetto.dev/docs/concepts/service-model>`__ each with |
| 7 |
one or more data-sources. Perfetto already provides various producers and |
| 8 |
data-sources for things like: |
| 9 |
|
| 10 |
- CPU scheduling events (``linux.ftrace``) |
| 11 |
- CPU frequency scaling (``linux.ftrace``) |
| 12 |
- System calls (``linux.ftrace``) |
| 13 |
- Process memory utilization (``linux.process_stats``) |
| 14 |
|
| 15 |
As well as various domain specific producers. |
| 16 |
|
| 17 |
The mesa Perfetto support adds additional producers, to allow for visualizing |
| 18 |
GPU performance (frequency, utilization, performance counters, etc) on the |
| 19 |
same timeline, to better understand and tune/debug system level performance: |
| 20 |
|
| 21 |
- pps-producer: A systemwide daemon that can collect global performance |
| 22 |
counters. |
| 23 |
- mesa: Per-process producer within mesa to capture render-stage traces |
| 24 |
on the GPU timeline, track events on the CPU timeline, etc. |
| 25 |
|
| 26 |
The exact supported features vary per driver: |
| 27 |
|
| 28 |
.. list-table:: Supported data-sources |
| 29 |
:header-rows: 1 |
| 30 |
|
| 31 |
* - Driver |
| 32 |
- PPS Counters |
| 33 |
- Render Stages |
| 34 |
* - Freedreno |
| 35 |
- ``gpu.counters.msm`` |
| 36 |
- ``gpu.renderstages.msm`` |
| 37 |
* - Turnip |
| 38 |
- ``gpu.counters.msm`` |
| 39 |
- ``gpu.renderstages.msm`` |
| 40 |
* - Intel |
| 41 |
- ``gpu.counters.i915`` |
| 42 |
- ``gpu.renderstages.intel`` |
| 43 |
* - Panfrost |
| 44 |
- ``gpu.counters.panfrost`` |
| 45 |
- |
| 46 |
* - PanVK |
| 47 |
- ``gpu.counters.panfrost`` |
| 48 |
- ``gpu.renderstages.panfrost`` |
| 49 |
* - V3D |
| 50 |
- ``gpu.counters.v3d`` |
| 51 |
- |
| 52 |
* - V3DV |
| 53 |
- ``gpu.counters.v3d`` |
| 54 |
- |
| 55 |
|
| 56 |
Run |
| 57 |
--- |
| 58 |
|
| 59 |
To capture a trace with Perfetto you need to take the following steps: |
| 60 |
|
| 61 |
1. Build Mesa with perfetto enabled. |
| 62 |
|
| 63 |
.. code-block:: sh |
| 64 |
|
| 65 |
# Configure Mesa with perfetto |
| 66 |
mesa $ meson . build -Dperfetto=true -Dvulkan-drivers=intel,broadcom -Dgallium-drivers= |
| 67 |
# Build mesa |
| 68 |
mesa $ meson compile -C build |
| 69 |
|
| 70 |
2. Build Perfetto from sources available at ``subprojects/perfetto``. |
| 71 |
|
| 72 |
.. code-block:: sh |
| 73 |
|
| 74 |
# Within the Mesa repo, build perfetto |
| 75 |
mesa $ cd subprojects/perfetto |
| 76 |
perfetto $ ./tools/install-build-deps |
| 77 |
perfetto $ ./tools/gn gen --args='is_debug=false' out/linux |
| 78 |
perfetto $ ./tools/ninja -C out/linux |
| 79 |
|
| 80 |
# Example arm64 cross compile instead |
| 81 |
perfetto $ ./tools/install-build-deps --linux-arm |
| 82 |
perfetto $ ./tools/gn gen --args='is_debug=false target_cpu="arm64"' out/linux-arm64 |
| 83 |
|
| 84 |
More build options can be found in `this guide <https://perfetto.dev/docs/quickstart/linux-tracing>`__. |
| 85 |
|
| 86 |
3. Select a `trace config <https://perfetto.dev/docs/concepts/config>`__, likely |
| 87 |
``src/tool/pps/cfg/system.cfg`` which does whole-system including GPU |
| 88 |
profiling for any supported GPUs). Other configs are available in that |
| 89 |
directory for CPU-only or GPU-only tracing, and more examples of config files |
| 90 |
can be found in ``subprojects/perfetto/test/configs``. |
| 91 |
|
| 92 |
4. Start the PPS producer to capture GPU performance counters. |
| 93 |
|
| 94 |
.. code-block:: sh |
| 95 |
|
| 96 |
mesa $ sudo meson devenv -C build pps-producer |
| 97 |
|
| 98 |
5. Start your application (and any other GPU-using system components) you want |
| 99 |
to trace using the perfetto-enabled Mesa build. |
| 100 |
|
| 101 |
.. code-block:: sh |
| 102 |
|
| 103 |
mesa $ meson devenv -C build vkcube |
| 104 |
|
| 105 |
6. Capture a perfetto trace using ``tracebox``. |
| 106 |
|
| 107 |
.. code-block:: sh |
| 108 |
|
| 109 |
mesa $ sudo ./subprojects/perfetto/out/linux/tracebox --system-sockets --txt -c src/tool/pps/cfg/system.cfg -o vkcube.trace |
| 110 |
|
| 111 |
7. Go to `ui.perfetto.dev <https://ui.perfetto.dev>`__ and upload |
| 112 |
``vkcube.trace`` by clicking on **Open trace file**. |
| 113 |
|
| 114 |
8. Alternatively you can open the trace in `AGI <https://gpuinspector.dev/>`__ |
| 115 |
(which despite the name can be used to view non-android traces). |
| 116 |
|
| 117 |
CPU Tracing |
| 118 |
~~~~~~~~~~~ |
| 119 |
|
| 120 |
Mesa's CPU tracepoints (``MESA_TRACE_*``) use Perfetto track events when |
| 121 |
Perfetto is enabled. They use ``mesa.default`` and ``mesa.slow`` categories. |
| 122 |
|
| 123 |
Currently, only EGL and the following drivers have CPU tracepoints. |
| 124 |
|
| 125 |
- Freedreno |
| 126 |
- Panfrost |
| 127 |
- Turnip |
| 128 |
- V3D |
| 129 |
- VC4 |
| 130 |
- V3DV |
| 131 |
|
| 132 |
Render stage data sources |
| 133 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 134 |
|
| 135 |
The render stage data sources are the driver-specific traces of command buffer |
| 136 |
execution on the GPU. |
| 137 |
|
| 138 |
The Vulkan API gives the application control over recording of command buffers |
| 139 |
as well as when they are submitted to the hardware, and command buffers can be |
| 140 |
recorded once and reused repeatedly. Trace commands are normally only recorded |
| 141 |
into a command buffer when a perfetto trace is active. Most applications don't |
| 142 |
reuse command buffers, so you'll see traces appear shortly after the trace was |
| 143 |
started, but if you have one of the rare applications that reuses command |
| 144 |
buffers, you'll need to set the :envvar:`MESA_GPU_TRACES` environment variable |
| 145 |
before starting a Vulkan application : |
| 146 |
|
| 147 |
.. code-block:: sh |
| 148 |
|
| 149 |
MESA_GPU_TRACES=perfetto ./build/my_vulkan_app |
| 150 |
|
| 151 |
Driver Specifics |
| 152 |
~~~~~~~~~~~~~~~~ |
| 153 |
|
| 154 |
Below is driver specific information/instructions for the PPS producer. |
| 155 |
|
| 156 |
Freedreno / Turnip |
| 157 |
^^^^^^^^^^^^^^^^^^ |
| 158 |
|
| 159 |
The Freedreno PPS driver needs root access to read system-wide |
| 160 |
performance counters, so you can simply run it with sudo: |
| 161 |
|
| 162 |
.. code-block:: sh |
| 163 |
|
| 164 |
sudo ./build/src/tool/pps/pps-producer |
| 165 |
|
| 166 |
Intel |
| 167 |
^^^^^ |
| 168 |
|
| 169 |
The Intel PPS driver needs root access to read system-wide |
| 170 |
`RenderBasic <https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2023-0/gpu-metrics-reference.html>`__ |
| 171 |
performance counters, so you can simply run it with sudo: |
| 172 |
|
| 173 |
.. code-block:: sh |
| 174 |
|
| 175 |
sudo ./build/src/tool/pps/pps-producer |
| 176 |
|
| 177 |
Another option to enable access wide data without root permissions would be running the following: |
| 178 |
|
| 179 |
.. code-block:: sh |
| 180 |
|
| 181 |
sudo sysctl dev.i915.perf_stream_paranoid=0 |
| 182 |
|
| 183 |
Alternatively using the ``CAP_PERFMON`` permission on the binary should work too. |
| 184 |
|
| 185 |
A particular metric set can also be selected to capture a different |
| 186 |
set of HW counters : |
| 187 |
|
| 188 |
.. code-block:: sh |
| 189 |
|
| 190 |
INTEL_PERFETTO_METRIC_SET=RasterizerAndPixelBackend ./build/src/tool/pps/pps-producer |
| 191 |
|
| 192 |
Vulkan applications can also be instrumented to be Perfetto producers. |
| 193 |
To enable this for given application, set the environment variable as |
| 194 |
follow : |
| 195 |
|
| 196 |
.. code-block:: sh |
| 197 |
|
| 198 |
PERFETTO_TRACE=1 my_vulkan_app |
| 199 |
|
| 200 |
Panfrost |
| 201 |
^^^^^^^^ |
| 202 |
|
| 203 |
The Panfrost PPS driver uses unstable ioctls that behave correctly on |
| 204 |
kernel version `5.4.23+ <https://lwn.net/Articles/813601/>`__ and |
| 205 |
`5.5.7+ <https://lwn.net/Articles/813600/>`__. |
| 206 |
|
| 207 |
To run the producer, follow these two simple steps: |
| 208 |
|
| 209 |
1. Enable Panfrost unstable ioctls via kernel parameter: |
| 210 |
|
| 211 |
.. code-block:: sh |
| 212 |
|
| 213 |
modprobe panfrost unstable_ioctls=1 |
| 214 |
|
| 215 |
Alternatively you could add ``panfrost.unstable_ioctls=1`` to your kernel command line, or ``echo 1 > /sys/module/panfrost/parameters/unstable_ioctls``. |
| 216 |
|
| 217 |
2. Run the producer: |
| 218 |
|
| 219 |
.. code-block:: sh |
| 220 |
|
| 221 |
./build/pps-producer |
| 222 |
|
| 223 |
V3D / V3DV |
| 224 |
^^^^^^^^^^ |
| 225 |
|
| 226 |
As we can only have one performance monitor active at a given time, we can only monitor |
| 227 |
32 performance counters. There is a need to define the performance counters of interest |
| 228 |
for pps_producer using the environment variable ``V3D_DS_COUNTER``. |
| 229 |
|
| 230 |
.. code-block:: sh |
| 231 |
|
| 232 |
V3D_DS_COUNTER=cycle-count,CLE-bin-thread-active-cycles,CLE-render-thread-active-cycles,QPU-total-uniform-cache-hit ./src/tool/pps/pps-producer |
| 233 |
|
| 234 |
Troubleshooting |
| 235 |
--------------- |
| 236 |
|
| 237 |
Missing counter names |
| 238 |
~~~~~~~~~~~~~~~~~~~~~ |
| 239 |
|
| 240 |
If the trace viewer shows a list of counters with a description like |
| 241 |
``gpu_counter(#)`` instead of their proper names, maybe you had a data loss due |
| 242 |
to the trace buffer being full and wrapped. |
| 243 |
|
| 244 |
In order to prevent this loss of data you can tweak the trace config file in |
| 245 |
two different ways: |
| 246 |
|
| 247 |
- Increase the size of the buffer in use: |
| 248 |
|
| 249 |
.. code-block:: javascript |
| 250 |
|
| 251 |
buffers { |
| 252 |
size_kb: 2048, |
| 253 |
fill_policy: RING_BUFFER, |
| 254 |
} |
| 255 |
|
| 256 |
- Periodically flush the trace buffer into the output file: |
| 257 |
|
| 258 |
.. code-block:: javascript |
| 259 |
|
| 260 |
write_into_file: true |
| 261 |
file_write_period_ms: 250 |
| 262 |
|
| 263 |
|
| 264 |
- Discard new traces when the buffer fills: |
| 265 |
|
| 266 |
.. code-block:: javascript |
| 267 |
|
| 268 |
buffers { |
| 269 |
size_kb: 2048, |
| 270 |
fill_policy: DISCARD, |
| 271 |
} |