Words from the Inside Uninteresting things from an uninteresting developer https://blogs.igalia.com/jasuarez/ Mon, 06 Sep 2021 09:44:37 +0200 Mon, 06 Sep 2021 09:44:37 +0200 Jekyll v4.2.0 Implementing Performance Counters in V3D driver <p>Let me talk here about how we implemented the support for performance counters in the <a href="https://www.mesa3d.org/">Mesa</a> <a href="https://docs.mesa3d.org/drivers/v3d.html">V3D driver</a>, the OpenGL driver used by the <a href="https://www.raspberrypi.org/products/raspberry-pi-4-model-b/">Raspberry Pi 4</a>. For reference, the implementation is very similar to the one already available (not done by me, by the way) for the <a href="https://docs.mesa3d.org/drivers/vc4.html">VC4</a>, OpenGL driver for the <a href="https://www.raspberrypi.org/products/raspberry-pi-3-model-b-plus/">Raspberry Pi 3</a> and prior devices, also part of Mesa. If you are already familiar with how this is implemented in VC4, then this will mostly be a refresher.</p> <p>First of all, what are these performance counters? Most of the processors nowadays contain some hardware facilities to get measurements about what is happening inside the processor. And of course graphics processors aren’t different. In this case, the graphics chips used by Raspberry Pi devices (manufactured by Broadcom) can record a bunch of different graphics-related parameters: how many quads are passing or failing depth/stencil tests, how many clock cycles are spent on doing vertex/fragment shading, hits/misses in the GPU cache, and many others values. In fact, with the V3D driver it is possible to measure around 87 different parameters, and up to 32 of them simultaneously. Quite a few less in VC4, though. But still a lot.</p> <p>On a hardware level, using these counters is just a matter of writing and reading some GPU registers. First, write the registers to select what we want to measure, then a few more to start to measure, and finally read other registers containing the results. But of course, much like we don’t expect users to write GPU assembly code, we don’t expect users to write registers in the GPU directly. Moreover, even the Mesa drivers such as V3D can’t interact directly with the hardware; rather, this is done through the kernel, the one that can use the hardware directly, through the DRM subsystem in the kernel. For the case of V3D (and same applies to VC4, and in general to any other driver), we have a driver in user-space (whether the OpenGL driver, V3D, or the Vulkan driver, V3DV), and a kernel driver in the kernel-space, unsurprisingly also called V3D. The user-space driver is in charge of translating all the commands and options created with the OpenGL API or other API to batches of commands to be executed by the GPU, which are submitted to the kernel driver as DRM jobs. The kernel does the proper actions to send these to the GPU to execute them, including touching the proper registers. Thus, if we want to implement support for the performance counters, we need to modify the code in two places: the kernel and the (user-space) driver.</p> <h2 id="implementation-in-the-kernel">Implementation in the kernel</h2> <p>Here we need to think about how to deal with the GPU and the registers to make the performance counters work, as well as the API we provide to user-space to use them. As mentioned before, the approach we are following here is the same as the one used in the VC4 driver: <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_perfmon.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662">performance counters monitors</a>. That is, the user-space driver <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n53">creates</a> one or more monitors, specifying for each monitor <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n281">what counters it is interested in</a> (up to 32 simultaneously, the hardware limit). The kernel returns a <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n375">unique identifier</a> for each monitor, which can be used later to do the measurement, <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n57">query the results</a>, and finally <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n55">destroy it</a> when done.</p> <p>In this case, there isn’t an explicit start/stop the measurement. Rather, every time the driver wants to measure a job, it includes <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n141">the</a> <a href="https://cgit.freedesktop.org/drm/drm/tree/include/uapi/drm/v3d_drm.h?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n278">identifier</a> of the monitor it wants to use for that job, if any. Before submitting a job to the GPU, the kernel <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_sched.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n133">checks</a> if the job has a monitor identifier attached. If so, then it needs to check if the previous job executed by the GPU was also using the same monitor identifier, in which case it doesn’t need to do anything other than send the job to the GPU, as the performance counters required are already enabled. If the <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_sched.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n69">monitor is different</a>, then it needs first to <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_perfmon.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n74">read the current counter values</a> (through proper GPU registers), adding them to the current monitor, <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_perfmon.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n76">stop the measurement</a>, <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_perfmon.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n35">configure the counters</a> for the new monitor, <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_perfmon.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n53">start the measurement again</a>, and finally <a href="https://cgit.freedesktop.org/drm/drm/tree/drivers/gpu/drm/v3d/v3d_sched.c?id=26a4dc29b74a137f45665089f6d3d633fcc9b662#n147">submit</a> the new job to the GPU. In this process, if it turns out there wasn’t a monitor under execution before, then it only needs to execute the last steps.</p> <p>The reason to do all this is that multiple applications can be executing at the same time, some using (different) performance counters, and most of them probably not using performance counters at all. But the performance counter values of one application shouldn’t affect any other application so we need to make sure we don’t mix up the counters between applications. Keeping the values in their respective monitors helps to accomplish this. There is still a small requirement in the user-space driver to help with accomplishing this, but in general, this is how we avoid the mixing.</p> <p>If you want to take a look at the full implementation, it is available in a <a href="https://cgit.freedesktop.org/drm/drm/commit/?id=26a4dc29b74a137f456">single commit</a>.</p> <h2 id="implementation-in-the-driver">Implementation in the driver</h2> <p>Once we have a way to create and manage the monitors, using them in the driver is quite easy: as mentioned before, we only need to <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L222">create a monitor</a> with the counters we are interested in and <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_job.c#L507">attach it</a> to the job to be submitted to the kernel. In order to make things easier, we keep a <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_context.h#L307">mirror-like version</a> of the monitor inside the driver.</p> <p>This approach is adequate when you are developing the driver, and you can add code directly on it to check performance. But what about the final user, who is writing an OpenGL application and wants to check how to improve its performance, or check any bottleneck on it? We want the user to have a way to use OpenGL for this.</p> <p>Fortunately, there is in fact a way to do this through OpenGL: the <a href="https://www.khronos.org/registry/OpenGL/extensions/AMD/AMD_performance_monitor.txt">GL_AMD_performance_monitor</a> extension. This OpenGL extension provides an API to query what counters the hardware supports, to create monitors, to start and stop them, and to retrieve the values. It looks very similar to what we have described so far, except for an important difference: the user needs to start and stop the monitors explicitly. We will explain later why this is necessary. But the key point here is that when we start a monitor, this means that from that moment on, until stopping it, any job created and submitted to the kernel will have the identifier of that monitor attached. This implies that only <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L209">one monitor</a> can be enabled in the application at the same time. But this isn’t a problem, as this restriction is part of the extension.</p> <p>Our driver does not implement this API directly, but through <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query.c">“queries”</a>, which are used then by the Gallium subsystem in Mesa to implement the extension. For reference, the V3D driver (as well as the VC4) is implemented as part of the Gallium subsystem. The <a href="https://docs.mesa3d.org/gallium/index.html">Gallium</a> part basically handles all the hardware-independent OpenGL functionality, and just requires the driver hook functions to be implemented by the driver. If the driver implements the proper functions, then Gallium exposes the right extension (in this case, the GL_AMD_performance_monitor extension).</p> <p>For our case, it requires the driver to implement functions to return which <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L160">counters are available</a>, to <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L310">create</a> or <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L183">destroy</a> a query (in this case, the query is the same as the monitor), <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L202">start</a> and <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L244">stop</a> the query, and once it is finished, <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L272">to get the results back</a>.</p> <p>At this point, I would like to explain a bit better what it implies to stop the monitor and get the results back. As explained earlier, stopping the monitor or query means that from that moment on, any new job submitted to the kernel (and thus to the GPU) won’t contain a performance monitor identifier attached, and hence won’t be measured. But it is important to know that the driver submits jobs to the kernel to be executed at its own pace, but these aren’t executed immediatly; the GPU needs time to execute the jobs, and so the kernel puts the arriving jobs in a queue, to be submitted to the GPU. This means when the user stops the monitor, there could be still jobs in the queue that haven’t been executed yet and are thus pending to be measured.</p> <p>And how do we know that the jobs have been executed by the GPU? The hook function to implement getting the query results has a <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L272">“wait” parameter</a>, which tells if the function <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L282">needs to wait</a> for all the pending jobs to be measured to be executed or not. If it doesn’t but there are pending jobs, then it just returns telling the caller this fact. This allows to do other work meanwhile and query again later, instead of becoming blocked waiting for all the jobs to be executed. This is implemented through sync objects. Every time a job is sent to the kernel, there’s a <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_context.h#L516">sync object</a> that is used to signal when the job has finished executing. This is mainly used to have a way to synchronize the jobs. In our case, when the user finalizes the query we <a href="https://gitlab.freedesktop.org/mesa/mesa/-/blob/685281278ebd39114c3007e76443eaaa66cf833/src/gallium/drivers/v3d/v3d_query_perfcnt.c#L263">save this fence</a> for the last submitted job, and we use it to know when this last job has been executed.</p> <p>There are quite a few details I’m not covering here. If you are interested though, you can take a look at the <a href="https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10666">merge request</a>.</p> <h2 id="gallium-hud">Gallium HUD</h2> <p>So far we have seen how the performance counters are implemented, and how to use them. In all the cases it requires writing code to create the monitor/query, start/stop it, and querying back the results, either in the driver itself or in the application through the GL_AMD_performance_monitor extension<sup id="fnref:1" role="doc-noteref"><a href="#fn:1" class="footnote" rel="footnote">1</a></sup>.</p> <p>But what if we want to get some general measurements without adding code to the application or the driver? Fortunately, there is an <a href="https://docs.mesa3d.org/envvars.html">environmental variable</a> <code class="language-plaintext highlighter-rouge">GALLIUM_HUD</code> that, when correctly, will show on top of the application some graphs with the measured counters.</p> <p>Using it is very easy; set it to <code class="language-plaintext highlighter-rouge">help</code> to know how to use it, as well as to get a list of the available counters for the current hardware.</p> <p>As example:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ env GALLIUM_HUD=L2T-CLE-reads,TLB-quads-passing-z-and-stencil-test,QPU-total-active-clk-cycles-vertex-coord-shading scorched3d </code></pre></div></div> <p>You will see:</p> <p><img src="/jasuarez/assets/post_images/2021-09-01-v3d-gallium-hud.png" alt="Performance Counters in Scorched 3D" class="center-block" /></p> <p>Bear in mind that to be able to use this you will need a kernel that supports performance counters for V3D. At the moment of writing this, no kernel has been released yet with this support. If you don’t want to wait for it, you can download the <a href="https://cgit.freedesktop.org/drm/drm/commit/?id=26a4dc29b74a137f456">patch</a>, apply it to your <a href="https://github.com/raspberrypi/linux">raspberry pi kernel</a> (which has been tested in the 5.12 branch), <a href="https://www.raspberrypi.org/documentation/computers/linux_kernel.html#building">build and install it</a>.</p> <div class="footnotes" role="doc-endnotes"> <ol> <li id="fn:1" role="doc-endnote"> <p>All this is for the case of using OpenGL; if your application uses Vulkan, there are other similar extensions, which are not yet implemented in our V3DV driver at the moment of writing this post. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p> </li> </ol> </div> Wed, 01 Sep 2021 00:00:00 +0200 https://blogs.igalia.com/jasuarez/2021/09/01/v3d-perfcounters/ https://blogs.igalia.com/jasuarez/2021/09/01/v3d-perfcounters/ graphics raspberrypi