Profiling your Retro68 application using serialperformanceanalyzer
When moving Nuklear to Macintosh, it became apparent that there would be a lot of performance optimization work necessary to provide a usable GUI experience on an 8MHz Macintosh. To make the situation worse, there is no profiling tooling available for Retro68, and no way as far as I could tell to run some kind of code profiler against the pce-macplus emulator, so I came up with a simple library for profiling code over a serial port via simple function calls.
So let’s say you’re wanting to profile a Retro68 application that you’re working on. Here’s how you might go about that.
As I outlined in the README for serialperformanceanalyzer, it is a nodejs script that runs on a host machine (at the end of a serial port) and accepts 3 types of messages:
PROFILE_START x - starts tracking x
PROFILE_END x - stops tracking x. Measures and stores how long x took to run
PROFILE_COMPLETE - analyzes all profile data and prints it out in 3 charts
Most total time spent in each profile mark overall
Longest individual profile mark
Setting up your code
Next on the Retro68 C application side, I recommend setting up a distinct serial port logging function as well as some helper functions and putting everything behind a flag. Here’s how I set that up in my programs:
A PROFILING flag to turn on and off for profiling our app. We should put every piece of profiling we want to add in tags like this (the profile messages are really going to slow things down)
PROFILE_START, PROFILE_END, PROFILE_COMPLETE functions to call in our C code to correspond with the messages that serialperformanceanalyzer is watching for
So what can we do next? Start honing in on slow code!
To do that, we can do things like this throughout our code:
And as long as we have matching PROFILE_START and PROFILE_END statements, serialperformanceanalyzer will provide us with a measurement.
Last, to have serialperformanceanalyzer produce results, we must call PROFILE_COMPLETE. I recommend adding:
1 2 3
#ifdef PROFILING PROFILE_COMPLETE(); #endif
to the Terminate function, which be called by the File > Quit menu option. This will trigger the serialperformanceanalyzer application to run its analysis and print out results.
Results
Here are what some example results look like. This output should display in the terminal window that you are running serialperformanceanalyzer in assuming it is pointed to the right serial port. This example is using the PROFILING variable turned on in nuklear-quickdraw, which has provisioning to use serialperformanceanalyzer already in place.
I'm really excited about the work that I'm doing here. If you enjoyed my post and my work, please consider tipping me with a coffee. I appreciate you taking the time to read my post!