Waypoints 2022Q3: An OCaml profiling library in one file

I wrote a "one file" implementation of my favourite profiling technique, "waypoints".

The idea of waypoints is that you can mark points (waypoints!) in your code, and the profiling library will record, for each pair of waypoints, the count (how many times each pair of waypoints was traversed), and the total time (from which it can compute the average time). Here is some example code:



Here there are 5 waypoints, w1... The instrumented code consists of a for-loop, wrapping some trivial branching code, with random sleeps at various points. By default, when the program terminates the profiling data is printed to stdout:



This tells you (for example) that the w2 branch was taken 7 times, and the w3 branch was taken 3 times. The average time for the w2-w2' section was 3.7 * 10^9, compared with 117 * 10^6 for w3-w3'. Clearly it is worth trying to optimise the w2-w2' section first...

The "time" here is measured using the time stamp counter, i.e., measured in CPU cycles (although some care must be taken e.g. if frequency scaling is in use, or other complications). Measuring the time at a point in the code is extremely quick, and the timer is (usually) extremely accurate. 

Of course, there are lots of other profiling libraries. Quite similar is "landmarks" from LexiFi https://github.com/LexiFi/landmarks. Of course, this is more sophisticated. However, landmarks requires that each entered section is exited, so regions have to be "well-bracketed". This is a different approach from the waypoints shown above, where a waypoint is just a point in the code (no need for well-bracketing etc.).

Why implement in one file? The hope is that this enables easy use of the library without the overhead of having a fully-fledged opam package: you can simply add a single file to your project while profiling and debugging, then throw it away when finished.

Comments

Popular posts from this blog

The Economics of Writing (Good) Code Documentation

What is so special about cross-directory rename of a directory?

A model of file update