Performance profiling Mutter, GNOME Shell & apps with Tracy
Comments
Transcript
Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.
[applause] Okay. Hello everyone. Uh today I want to show you how to do performance profiling with Tracy for MK Domeshell and your apps. Uh my name is Ivan Majetsk. Uh by day uh I'm working on a PhD in computer vision. Uh but in my free time I've been a longtime contributor to open source and free software. I usually tend to lower level uh components such as uh compositors. Uh in Gnome I worked on improving the input latency in Mutter a while ago. I also implemented the screenshot UI in Gome shell that landed a few releases back. Uh and I also have two Gnome circle apps uh video trimmer and identity. Uh also for the past three
years almost uh I've been creating my own whing compositor uh called Nuri uh which has uh a unusual windowing paradigm where uh your workspaces aren't limited in size to your monitors so you can scroll them left and right uh as you can see. Uh also in 2023 uh I
participated in the grant from the sovereign tech foundation to Gnome. Uh that was a 1 million euro grant uh to improve various parts of Gnome. And my part was to uh do performance profiling and optimization for matter and Gnome shell. uh I managed to identify 12 uh performance problems uh and fixed uh seven of them which considerably improved uh the smoothness of GM shell.
So thanks to STF for that. Uh for that work I used Tracy which is a profiler by
Barto Taudul. Uh it is a frame profiler
mainly uh for video games but it has a lot of very useful visualizations and statistical functions and I found it quite useful for doing this work. So uh don't be like uh scared by this over complicated screenshot. It just has a lot of panels open all at once. I will try to explain to you how to use it uh step by step. So the main thing to understand about Tracy is its timeline view. Uh this is the main view. Uh here time is flowing from left to right and on the timeline view we see these rectangles called zones. So they indicate where uh some part of the code starts and ends. So the zone starts here and then ends here. And the zones are nested. So here we can see a recording from mut. Uh it has a function clutter flame frame clock dispatch which calls clutter frame listener frame and this is uh visualized below that parent zone. So the zones are nested. you can see what a piece of the code is doing and where it's calling into. So we already have a profiler in Gnome called cysro. Uh so what's the difference? Well, it turns out that Tracy and Cisproof largely collect the same data and even these zones in Tracy are pretty much the same as marks in cisproof. The main difference is that the Tracy zones must nest correctly. So the child zone must end before its parent zone. uh in cispro there is no such requirement uh and this affects the way the zones are visualized. So in Tracy uh it can show this nested view uh and in cisproof it has to show like the marks chart or the marks waterfall and in my opinion the tracy uh timeline view is very useful for diagnosing individual frames and other performance problems.
So let's look at an example from the STF. Uh there was one problem where shell was becoming very laggy over time. So as a user shell it became uh more and more stuttery. And uh I want to shout out and say a big thanks to Jeff 14 who you may know from our GitLab issues and project management. Uh Jeff has a slightly older hardware setup and so he was hitting a bunch of those performance problems and uh he really helped me with diagnosing them. So he was recording Tracy profiles for me to analyze. then I would propose some fix and Jeff would test it and uh see if it worked. So um I
went to uh dick matrix DMs to look up
the details of uh of this problem and well unfort fortunately it loaded on another uh on another computer. Uh so this was uh this was what I found. Uh this is from the trace that uh Jeff sent me and uh in Gomeshell as you may know
much of the UI is written in JavaScript in GGS and so when you do some uh keyboard input or mouse input in the overview it goes through JavaScript handlers. Uh in this case uh ST widget event calls into JavaScript on captured event and what I found is that on captured event was calling into a lot of tiny a lot of tiny functions. So in this case, 4 and a half thousand functions which was taking 43 milliseconds which is like several frames long. You definitely don't want your input handler taking this long. So I zoomed in and indeed uh all of these individual functions were tiny. They're barely registered and they were coming from this uh pop-up menu manager. So I suspected it was some kind of signal leak uh where uh the pop-up menu manager was connected to something and then uh the code forgot to disconnect it. So they all piled up and after a few days you had four and five four and a half thousand tiny JavaScript callbacks and indeed the fix was to disconnect the handler in the correct place and the fix worked. Jeff reported that it was one of the biggest performance improvements.
And the way I was able to quickly identify this issue is by looking at this u timeline view which let me inspect individual frames and individual functions and see that within this one function we have a weirdly large amount of nested calls. So hopefully I've picked your interested. So how do you actually use this? Now much like cysro tracy has two components. uh the profiler UI application itself where which is this application where you actually view the profile uh and also a library lip tracy linked into the target program uh which collects the data and which provides you with functions to instrument your code. Uh now Tracy is aimed primarily at game development where it's common to build from source and bundle your dependencies. So it's not really packaged in distributions but for Fedora uh I made a copper where I
have pre-built both the UI and lip tracing. Also Tracy has a very extensive manual. Uh if you have any question is probably answered in the manual. Uh if you're interested I recommend you that uh to check it out. So let's say that we have an example project in C uh GTK hello world application. Now Tracy Tracy's primary language is C++ but it also has bindings to C and some other languages and so it provides integration to both CMake and Mezan out of the box. So it's very easy to add to a C project just add a dependency in me also add the rep file pointing at its repository. Uh now at the time of this talk uh the latest stable version is 031 and since then uh we've landed a few u
uh fixes uh for example to symbol resolution. So here it's pointing to a new git version but once the next version is released uh you can switch it to the stable version. Uh so then we will use the C API so include tracy trac.h and then uh let's say that we are building our UI in the GTK application. Uh so we connect to the activate signal with this activate function and we want to measure how long this uh function is taking. So we surround it with a trace C zone. Uh these two macros trac zone starts a zone trace C zone ends the zone. They take two arguments. Uh the first one is just the variable name for the zone to store the zone in and the one is whether the zone is active in case you want to do some dynamic toggling. Also as I mentioned tracy zones can be nested. So let's say that we want to measure this GTK window present call separately from the rest of the function. So we want to see how much of this function is taken by GTK window present specifically. Uh for this I'm using the trac n macro. The n allows you to pass a explicit explicit zone name in
in this case window present. If you use the normal macro it will just use the uh name of the function. Uh so if you were writing this in Rust instead, there's the Tracy client create which wraps lip tracing and provides you with a nice API. Uh here's the same activate callback and you would write tracy client span. Here span is just a different name for zone. Uh different profilers use different names for the same thing. Here it's just called span. It's basically stands for the for the zone. Uh now rust does have destructors.
So the zone will be called uh will be closed in the drop implementation so that you don't have to call end manually. Uh and if you want to also profile window present uh you you can just start a scope with curly braces and add another span passing in the zone name. Uh so now let's record it. Uh now before uh before running the app I suggest you to run these two commands. Uh they mount the kernel tracing system. They allow Tracy to collect context switch data which comes quite useful. Uh so then you run the target program. You run the profiler UI and you see this kind of uh panel. Uh here you can enter the IP address of the client to connect.
So Tracy can actually profile over network uh which is very useful. We'll see it coming uh handy later. But also here at the bottom uh it has these discovered clients uh where it it should show your running program on your local PC. So I will just click on it and it will open a view like this which will be scrolling to the right because it's uh recording data and here at the panel at the top left it will say query backlog which is how much information is left to receive from the target program. So now you do whatever you want to profile then you close the program or click stop and you see the timeline you can zoom and pan around it. Here I've zoomed out uh it fully so this is 50 seconds of recording. This is our timeline. Uh you can see two big sections here. At the top uh the CPU scheduling uh it shows what each of the CPU cores is doing at every moment and at the bottom are our zones. Uh the zones are split by threads. Uh here you can see main thread in the middle as well as tracy sampling traces symbol worker. So for the main thread we annotated our zone. Uh the activate call happens only once and in the beginning. So it's just this tiny rectangle. But for other threads uh where we didn't annotate anything, Tracy will actually sample call stacks and emit those ghost zones where it tries to show the zones based on call stacks. So I can zoom in to our marked zone. Uh this is it zoomed in. You can see my activate and below it window present because window present is inside my activate. And if I hover over it uh it will show some statistics. So this took 50 mconds and we can see that most of my activate is actually taken up by window present. Uh Tracy also collects a lot of information for us without me annotating anything. For example, those blue circles on the timeline are call stack samples. If I mouse over uh it will show me the call stack at this location of the code. Uh also you can see that threads are annotated with green and red lines. So the green means that the thread is running computing something on the CPU and red means that the thread is currently waiting for example blocking reading from a file and if I mouse over the red region it will actually show me the back trace for this uh blocking operation. Now here you can see some symbols are unknown because I didn't have debug info installed for this library but here is VK enumerate physical devices. So probably uh at this point GTK went to open my GPU through Vulcan and it's currently waiting for the driver to give it back the handle to the GPU.
So now you can see to the left of my activate there is a big empty region and uh my guess is that this is GTK init and adinit and let's verify this. Uh let's go to our main and put the zones around GTKit and advite it. Now keep in mind in C there are no destructures and you have to end the zone uh that you started uh
otherwise things will not record correctly. Uh in Rust I would just add curly braces with spans called gtcktit.
Now record again and you can see all the space to the left of my activate is now taken up by GTK init and advite init. And atit is taking 65 milliseconds and you can see most of the thread is green. So it's actively computing something. How do you find out what it's actually doing? So there is a flame graph in Tracy for this uh in the top panel if you click on the flame button it will open the split view to the down uh to the bottom which is the flame graph. It's kind of an aggregated view of all the functions. Uh here's our advite and when I looked at this I can see that most of this time is taken up by GTK CSS provider load internal. So most of this time is spent parsing CSS and you can see that then it recurses down into this subdivide infos and uh to actually verify that this is what's taking most of the time. There's statistics window which presents the same information as a table uh sorted by the count of like samples call stack samples. You can see that subdivide infos is indeed at the top. So this is our hottest function. So while uh making the slides I noticed something weird. Uh this is adita style manager constructed. For some reason it's loading CSS twice and we did a small investigation with Alice and it turned out that uh the first time it was loading like the basic default CSS and then it was loading properties such as dark style changed or reduced motion high contrast. If any of those were non-default, it would change the CSS and load it twice. And thankfully, it was an easy fix by Alice, which went into 51.
So now, Advita style manager only loads CSS once in the end. And uh so it became like one/ird faster, which is nice.
Uh well, GTK u I mean Tracy has quite a
bit of nice functionality for inspecting repeatedly called functions. So my activate was only called once. What if we instrument some repeatedly called function? Let's try GDK's frame clock. So this is inside GTK. Now uh the frame clock is an object uh that controls the whole repainting cycle of the app. So it's called every time like every frame. Uh so I'm adding tracy zones uh around some existing GTK instrumentation. Uh and recording with Tracy uh the icon scroll demo. If you're unfamiliar, an icon scroll is a GTK demo uh which loads like a few hundred icons and then continuously scrolls them up and down. So, it's always repainting. Uh here's my 15-second recording with 2,000 zones which are too small to display. Uh I zoom in and I can see that these are my GDK frame clock paint idle zones. Uh and there are also some uh zone at the bottom because I annotated something else there. Uh and it's taking 1 millisecond. So, it seems to be fine.
But how do we actually tell like where in the monitor repaint cycle this happens like are are we losing any frames or not? Uh thankfully one of the one of the pieces of data that Tracy collects for us are DRM vlank events. So it collects when the monitor actually repaints. Uh there's this gray line at the top uh frames. I can click on this and now it highlights. It also shows this uh green red display uh at the top and uh these are the frame boundaries. So these ticks here show when my monitor actually repaints and so you can see that these GDK frame clock zones happen pretty early in the frame terminate well before uh the next frame. So we are seeing that everything is good. Uh we are not dropping any frames.
Now if I click on this zone uh I will see this window at the top left. If I click on the statistics button I can see uh a statistics panel on the right. So this will show aggregate statistics over every single call to this function essentially. And they both have some uh digit uh like time information. So mean duration, median duration, percentiles as well as this yellow histogram. So this yellow histogram aggregates once again every call to this function. So on the horizontal axis we can see the duration and on the vertical axis is how many calls uh hit that duration. So we can see that most of the calls were around 1 millisecond in length which is pretty good. Uh but also here uh it's very hard to see but there are like two calls here which are very long. Uh how do you find uh thankfully this table uh at the bottom if I expand this main thread group uh Tracy will show me every single instance of this zone and I can sort by execution time and here are my 300 millisecond call uh then the next one is like 6 millisecond and then there are a bunch of 2 and a half millisecond.
Now I can actually middle click on any of those rows and this will zoom the timeline view to that exact zone that I selected here. So this is our 300 millisecond zone. Now once again how how do we find out what the zone was what the code was actually doing? We use the frame graph. In this case I click the limit range checkbox which limits it to just the the timeline. And here you can see most of it is like GTK widget do snapshot. If I scroll it a bit down uh then it comes down to GTK texture new. So basically in this call it's just loading all of those hundreds of icons.
So uh it's expected that it takes this long. I don't know maybe if I if I were to optimize this maybe I would uh load icons in multiple threads. Uh but basically this is not unexpected. Now let's instead explore one of those 2 and a half millisecond calls. Uh and here uh here is one of those zones. I middle cllicked so the timeline zoomed to it. And here you can see u the code is waiting for something several times. So if I mouse over one of those red areas, uh you can see a call stack which says something like GDK surface update size GDK Vulcan context check swap chain. So these are the moments when I resized the window and GTK seemingly went to Vulcan to like renegotiate the surface and the swap chain. Uh so I'm not sure why it's blocking for like six six different times here. Maybe something to optimize here as well, but it's clearly visible.
Uh now uh as part of my profiling work I
instrumented matter which is the compositor. Uh I have the merge request open if you want to take a look and let's see what kind of considerations there are when uh instrumenting uh matter. So matter actually has uh existing cogle trace instrumentation which emits cisproof marks. Uh but as I said before cisproof marks and traces zones are basically the same thing. So I just took these existing macros and made them also emit traces zones with traces zone end passing in the name and uh ending and the matter macros actually use this GCC extension attribute cleanup which is kind of like a destructor so you don't have to close the zones manually. [sighs] Uh and um one important thing about matter which is Gnome's compositor uh is that you're running it constantly like you don't just open it and then close it. uh it's always running. It's your session. So by default, Tracy will collect data always from program startup uh until you connect with the profiler.
Then it will upload all of this data. So you can profile from the program startup. And here we instead want the on demand mode uh compiled with with this on demand flag which will collect profiling data only when Tracy is actually connected. So only when you are actually profiling. So if I build matter and record with Tracy, we can see a bunch of zones like this clutter flame frame clock dispatch and inside it a bunch more zones. Uh here's the statistics for clutter frame lock dispatch. Here you can see the histogram is slightly more interesting. It has several peaks and uh in this case
one of the peaks is around 10 microsconds which is very fast. Uh which is because when clutter frame clock dispatch notices that there's nothing no work to do, it just returns quickly. Uh so this is our 10 microcond peak. Uh but if it does actually have to redraw the screen, then this is our half a millisecond long uh duration. Uh you can also see here uh that I'm grouping these zones by uh display port one and display port two. So I have two monitors connected and I want to like group the zones by the monitor so I can see separately if one of my monitors takes longer to redraw compared to the other.
Uh you can do this by uh Tracy C's own text which lets you attach an arbitrary string to Tracy's zone and then in this UI you can group by user text and like filter and group them by by this user text which is quite useful. Uh Tracy can also do plots uh with the Tracy C plot macro. Quite simple just pass in the name and the current value. Uh this is how they show up. Uh the these are my bunch of plots that they added to matter like the uh GPU time that it took to complete the frame. And the nice thing about Tracy plots is that their minimum and maximum ranges always automatically adapt to what you see on screen. So let's say that I want to see uh look at this region in more detail. I zoom into it on the timeline and you can see that the plots readjusted themselves the year minimum and maximum to the values that are currently visible which makes it very uh easy and convenient to inspect a particular bet frame.
Uh now matter uh is good and all but we also have the whole Gnome shell on top with the JavaScript UI. So how do we profile that? I decided to instrument GJS which is GNO's JavaScript engine and in particular I decided to instrument uh the places which call from C to JS and uh from JS back to C. Uh GJS has this one convenient function invoke uh which calls from JS to C. Uh it first converts arguments from JS to C then prepares a native call then actually calls the function with this FFI call. So I put a zone around it. Uh but the problem with GGS is it's not only used in GM shell.
It's only it's also used in a bunch of other apps and you don't want your Tracy profiler to be running in all the other apps only in GM shell. So here I will check if Tracy is started and is connected and only then will I create an actual active tracy zone. You can see I'm passing zero here. So this is not active. And uh this would work fine. Uh but it would name all the zones like function invoke which would not be very descriptive. We want to actually know which function we are calling. Thankfully GGS has uh can provide a name here of the function and then we can use this trac which is the function which traces cone macro expands into uh we can manually call it and pass it the name and uh this will set the custom name for our zone. So now if I record a matter uh
well first of all uh another consideration u for these libraries which are used in a bunch of uh in a bunch of applications you also need uh delay init and manual lifetime compile flags which make it so tracy is not even initialized until you call this traces startup profiler. So in matter in metacrate context either the traces startup profiler call. So in m tracy will start up and run but in all other programs that use ggs it will be completely inactive will not affect performance in any way. Uh so now if I record m and shell I can see that in addition to the m zones that you saw previously there are also these colored uh javascript zones and the colors are actually something that you can add. So you can see you can set Tracy C's own color with RGB hex hex color value and
uh the way I colored this is uh every function call from C to JS is colored teal and every call from JS back to C is called purple and this makes it easy to see that for example when we are handling event we're calling JavaScript handle event then it's calling back to C emitting an update signal then that is calling JavaScript update touchpad gesture which is calling back into C emit ing update signal which is calling some more JavaScript switch workspace and um like every one of those calls it adds a little bit of overhead and uh thankfully uh this kind of coloring makes it very easy to see when it's happening and like you can easily debug when something is going wrong here. Uh finally uh one more thing that Tracy can do is profile memory allocations. Uh for this you need to wrap or instrument the allocator. Now, Rust makes it really easy because you can override the global allocator with the profiled allocator.
In C, you can override operator new and delete adding the tracy instrumentation. Uh, now of course, this will only work on allocations from Rust and C allocators respectively. For C, there is no easy option. You'd need to instrument jip with the G Malo and G3 macros or do something similar to CIS profctor, which uh hooks with LDP reload and instruments Maloan 3. Uh I will show an example from NRA. So I got the bug report that locking and unlocking the screen would uh increase the memory usage and in this test I'm locking unlocking the screen four times and you can see on this memory usage graph indeed after every unlock the memory usage is increasing.
It's not going back. Uh so in the memory table uh Tracy will actually show me all active allocations. So allocations that have not been freed by the end of the profile and it will allow me to inspect the call stacks. Uh when I was inspecting the call stacks uh hovering over the uh source code shows me the source line I noticed that a lot of these allocations were for whent objects. So my theory was that there was some kind of reference cycle where a veent object was referring back to itself. I went looking in the code. Uh indeed the log surface object uh contained this uh surface which is basically a strong pointer back to itself and it was never freed due to this reference cycle. So the fix was to make it a weak reference instead. And after this uh you can see I recorded again after each unlock the memory returns back to where it was. So the memory leak was fixed. Uh also this memory view uh has this nice tree uh where you can where Tracy shows you how many like kilobytes you allocated from uh every part of your code which also makes it easy to see when some part of your code is doing a lot of unnecessary allocations.
Uh now let's actually do try to do a demo. So I have this uh I have this OnePlus
phone here [snorts] uh which runs post market OS and mobile shell and I will uh it runs with my patches. So I will try to connect to it. Uh the good Wi-Fi doesn't uh provide like automatic discovery but I happen to know the IP address so I can connect. So you can see traces started recording. Now I will uh I can do something in the shell. So we have a little bit of data. Uh now once I'm satisfied I will click stop. Uh let me maximize dismiss this. And so this is our timeline. I can zoom it. I can pan it around. Uh hide the CPU data. So sometimes these ghost zones kind of take up all of the space. Uh but you can just
uh you can disable them in settings or you can scroll down. Here's our main thread. Here is our uh our zones. You can see my coloring. We can click at Craig uh flame cring lock dispatch. We can view the statistics. Here you can see uh the the times. This is uh where you can inspect the like uh like the longest function. Uh and
[sighs] yeah, the call stacks as well. So, so this is where the network profiling is most useful when you have some other device like a phone and you don't need to run the full profiler UI on the phone. You can run it on your laptop uh where you can inspect it on a big screen. Uh makes it quite convenient.
So, uh if Tracy is so good, uh why why not Tracy? Uh why aren't we like adding it to all of our libraries? Well, the main point is probably that it's a bit awkward to package. Uh so as you saw some important toggles in lip traci build time. Uh this is made this way uh because for game development you really want to minimize the overhead. So it like literally compiles out all of the unneeded code. But unfortunately for like distribution systemwide profiling it means that I have to make uh separate packages with different build flags. For example, network profiling is cool but by default you really don't want someone else to be able to prof to like connect to your computer. Uh so the default build here disables it and you need to install the Tracy broadcast package to uh enable it back and uh another example this no call stack. So I found that the call stack and stable resolution is quite heavy sometimes contents with GGS.
So for matter profiling uh I would recommend maybe Tracy no call stack but this has to be like a separate compilation uh separately compiled packages uh not very convenient. Uh also at the moment in Tracy there's only minimal support for multipprocess and whole system profiling which is where cysro uh kind of really shines. It would be nice to see more support there. So as I said tracy and cisproof basically collect the same data. Tracy zones and cisproof marks are basically the same thing save for one requirement. Uh so all of these useful views could in theory be implemented in cisproof. [snorts] Uh and I would say that the most useful is this timeline view with uh nested marks and with all of this information available in one place where it's very easy to zoom into specific frames and see why specific frames uh
were very long uh and like see if your
frame uh went to do a blocking read somewhere. Uh so Tracy makes this very easy by showing the information in this unified graph where you can zoom in and uh dive into like some specific uh some specific call. Uh also uh mark statistics uh histograms better sorting and filtering I found very useful in Tracy how you can click on a function and see like the time distribution. Uh so uh we could do this in cys as well. We don't have to rely on tracy necessarily. So as a recap uh for C build with lip tracy markup with tracy c zone and other functions see the tracy manual uh for us to use tracy client uh make sure you have frame pointers and debug info for good stack traces. Thankfully, modern distributions are building with frame pointers by default. Uh it is very helpful. Uh if you have broken stack traces, uh most likely you don't have frame pointers. Uh run these two commands to u before profiling. And remember the flags on demand for longunning processes such as compositors and delay the need plus manual lifetime if you want to instrument libraries such as GDK or uh GGS in this case. So uh
that's all I've got. Thank you. If you have any questions, I would be delighted to answer them.
[applause]
>> If people at home have questions, now is the time.
Hey. Uh, really cool. I wish we would all do more of the profiling stuff. So, I'm very happy that someone's looking at this. Um but one question I have is essentially um what would it require to remove like the friction and the process here like um having to recompile everything and adding the the uh I don't know instrumentation is a bit awkward I guess and makes it you have to know all the weird parameters and all the interesting libraries that are packaged and whatever. So is there a way we can get streamline all of this? So I have
two answers here. The first one is uh this is like not really tracy specific. We already have a bunch of cispro instrumentations in a bunch of our projects like m and ggs. Uh so this is kind of uh a similar deal here. Uh once again maybe if we add more displays to cispro then we can just use cysro where everything is already instrumented. And the second thing is uh so all of this data that Tracy automatically collects like call stacks and u frame data uh I
think they recently added a binary utility which can connect to a running process and like without having to instrument it. Uh I haven't tried it yet. Uh so uh really uh what you're
instrumenting by hand are like manually marked up zones for the interesting places that you want to see in your code. And also you can mark up like GPU zones. Uh otherwise uh like a lot of
many parts of Tracy can work even without manual instrumentation. But yeah it's a understandable pain point.
>> Thank you. Any other questions?
>> Thank you. This is really cool. By the way, if I wanted to look at the interaction between Gnome calendar and evolution data server, >> does it help me look at different processes together? So uh this is what I mentioned by like minimal support for multipprocess. Actually uh on git I
found that there are some first steps towards u multipprocess recording with tracy where um you can run uh two or
multiple capture sessions at the same time and then there's a binary to like merge the data together and view them as one single uh like tracy uh tracy view.
So I guess instead of threads it it will show you um processes but I from what
I've read uh it's still rather limited. So it will drop a bunch a bunch of data from that recording. So hopefully uh in the future uh people will work on like more multipprocess profiling. Uh that would be great because yeah right now this is one of the things that uh Tracy is not very good at compared to cispro. >> All right. Thank you. Any other questions, comments?
Well, I think that's it. Thank you so much.





