Making a pinball game for Playdate: Part 15, the interactive profiler

Welcome to this December adventure, where I will try to write about the process of our latest game, Devils on the Moon pinball.

If you enjoy this article consider wishlisting our game on Steam! I will talk more about the PC version of the game in later logs, but every wishlist helps.


So by now we have an instrumentation profiler that, with a couple of simple lines of code, helps us measure our game. If you don't know what I'm talking about you can catch up with the previous posts.

Well, how useful is that, really? Well in games turns out not so much, you normally want to know much of your frame budget a function is taking. A single function can run in our case at least 50 times per second and if it's inside our physics loop at least 50 * 4 physics steps = 200 times! Our current profiler records only the last snapshot of the function timing, and how many times it ran.

You could improve this by recording the max and min time it takes the function to run, and accumulate its total time, then you can divide total time / call count and get an average.

This has the problem that it hides when there is a frame drop in your game. When you are measuring something that doesn't change a lot from run to run of a program this information might be enough. But a game state is complex, changes quickly and if a single frame is slow it's important to know why.

Overlays

Coming from PICO-8, I remembered its performance overlay.

pico-8-perf-overlay.gif

As you can see, it's useful for spotting frame drops. It doesn't show much, but while you play you can see whether there have been any.

mangohud.gif

Another popular tool is MangoHud which helps you figure out how your hardware is behaving with an app. By the way that is the excellent game 868-Back

So both of these overlays give you an idea of when something slow happens in a game you are running, but they don't help you know which part of the game is slowing everything down.

Filtering and smoothing

The first thing that occurred to me was to just draw the profiler zones on the screen.

Modern CPUs have a lot of variance when running the same function over and over under seemingly identical conditions. The general recommendation to measure a function is to run it a bunch of times and get its average.

profiler-without-filtering.gif

As I said in my previous posts, I read a couple of articles from Game Developer Magazine, where I found IProf, the interactive profiler from Sean Barrett, which solved a lot of the issues I was having.

Sean's implementation was based on a series of articles from Jonathan Blow in which he describes how an interactive profiler could work and its common pitfalls. For example, in the Game Developer Magazine for December 2002 he describes this specific problem quite well:

We draw the profiling data with one zone per line, sorting by CPU usage so that the most expensive zones are at the top. But since timings in a game fluctuate a lot, if we do this naively, lines of output will move around haphazardly and the display will be hard to read.

So instead of sorting and displaying the raw timing from this frame, you can sort and display a smoothed value.

One way of doing that is by damping the value. I have talked a little about springs, but for this specific case what you want is frame-rate-independent damping. Or, as Freya Holmér would say, Lerp smoothing is broken.

So what IProf does is after every frame, it goes through all the zones and stores a smoothed value for each timing.

float sample = prof->zones[idx]; float T = 0.8; // Time to reach 90% float k = powf(0.1f, dt / T); prof->smooth[idx] = prof->smooth[idx] * k + sample * (1 - k);

There are a couple of things happening here. First we grab the sample of how long that zone took to run. Then we have a constant T, the time to reach 90%: the greater the value, the smoother (and less accurate) the result. A value of 0 means no smoothing.

k is how much of the previous value is kept for the smoothing in a value between 0 and 1.0. It can't be a constant because it needs to take into account the time it took between frames.

prof-with-smooth.gif

IProf features

While reading the documentation and code of IProf there were a lot of features I was having a hard time visualizing, and the only graphic I had was a blurry screenshot from a 20-year-old magazine. So one evening I decided to try and build IProf and see how it looked.

I used Sokol and SokolGL an OpenGL-style rendering library for Sokol GFX and made a quick and dirty port to be able to play around with IProf on my computer.

iprof-sokol.gif

One thing IProf had that mine didn't was... well, colors. IProf uses a heat-like effect on zones whose timings fluctuate, so you can quickly tell which ones are jumping around.

I could add a similar effect with dithering patterns, but I'd rather keep the profiler as cheap as possible, which means a single rectangle draw for all the row backgrounds.

Another feature is the big graph on the side with a line of a different color per zone. Not only that but you can pause the profiler mid game, and scrub the profiler to look at previous frame recordings and figure out which function was the one that caused the spike. That's really neat!

In our case we don't have that many frame drops, and for the ones we do have, we know the cause, so this feature isn't a priority. But I can see it being very useful in a future project.

I did, however, want to add a way to graph the timings of each zone. For this we have a history array in our profiler where I store a new recording at the end of every frame.

struct prof_hist { u32 us_exclusive; u32 us_inclusive; u32 hit_count; }; struct prof_hist history[HIST_SIZE][ANCHORS_SIZE];

A cool trick with C is you can completely disable the history using macros or in my case just reduce it to a single history entry.

prof-with-graph.gif

There are a lot of improvements we could do to the drawing of the graph but for now we have everything we needed to record how long parts of our game were taking and start making improvements.

This post is already a bit long, so once again, wishlist the game on Steam and see you on the next adventure.

Comments