Profiling Osprey programs in VS Code: CPU, memory and leaks

Profiling shows where a program spends its time. The Osprey VS Code extension samples a running program, draws a flame graph, and marks the hottest lines in your source. Any Osprey program can be profiled while it runs, with no restart.

Profiling Osprey programs in VS Code: CPU, memory and leaks (4:16). Captions are on; choose a chapter to jump to it.

Profile the open file

  1. Install the compiler and the VS Code extension (Install Osprey).
  2. Open an .osp file. In the Osprey Debug view, open Processes and choose Run & Profile CPU (Current File).
  3. While it runs, the status bar counts samples and names the hottest function so far.
  4. Click the counter to stop. The results open: a flame graph, the hot functions and the hot lines. Each hot line in the editor shows its share of the samples.

Profile a running program

Build with --profile so samples carry source lines, start the program, then press the flame on its row in Processes. Hover the row to see its CPU and memory.

cd src/osprey/examples/profiling
osprey busy.osp --compile --profile -o busy
./busy

Check for memory leaks

Choose a memory backend when you build: gc (the default, a tracing collector), arc (reference counting, frees a value when its last owner drops it) or ownership (moves and borrows checked at compile time). Osprey values never change once built, so they cannot form reference cycles.

Build with ARC and set OSPREY_ARC_DEBUG=1. When the program exits, it reports the objects still alive:

osprey words.osp --compile --memory=arc -o words
OSPREY_ARC_DEBUG=1 ./words
[osp-arc] exit: 0 live objects, 0 KiB (+0 immortal)
[osp-arc] peak_live_bytes=1588894

OSPREY_ARC_DEBUG=2 also lists each live object by kind. The profiler does not track the heap yet.

Both programs are in src/osprey/examples/profiling.

Transcript

  1. Osprey has a CPU profiler built into VS Code. In this tutorial, we'll read two small programs, predict where their time and memory go, then prove it with the profiler and the ARC leak check.
  2. Here's what you'll learn. Profile a program straight from VS Code.
  3. Read the flame graph, and the heat the profiler puts on your source lines.
  4. Attach to a program that's already running, without restarting it.
  5. And check a program for leaks with the ARC memory backend.
  6. Start with the code, so you know what to expect. Busy.osp has two kinds of work. Fib is the classic doubly recursive Fibonacci: every call makes two more.
  7. Collatz counts the steps of the Collatz sequence. Halve an even number, triple an odd one and add one, until you reach one. Both branches are tail calls.
  8. Longest runs collatz for every number from n down to one, and keeps the longest chain. With n at twenty thousand, that's twenty thousand sequences per round.
  9. Round to prints one round, then calls itself, forever. So the program runs until you stop it, which is exactly what you want for a profiler.
  10. Main installs a handler for the Arith effect. Integer overflow in Osprey is an effect, not a crash, and this handler says to wrap. Then main starts round one.
  11. Now profile it. Open busy.osp, and press Run and Profile CPU in the Processes view. Osprey builds it, starts it, and starts sampling.
  12. The sample count climbs while it runs, and the panel names the hottest function so far.
  13. To stop, click the counter in the status bar.
  14. The results open on their own. The flame graph shows where the time went: the wider the frame, the hotter the code.
  15. Back in the source, each hot line carries its share of the samples, and collatz stands out, as the code predicted: it runs twenty thousand sequences every round.
  16. You can also profile a program that's already running, with no restart. Build it with --profile, so its samples carry source lines, start it from the terminal, and it appears in Processes.
  17. Hover its row to see its CPU and memory, then press the flame to start sampling.
  18. Stop it the same way, from the status bar.
  19. Memory works differently in Osprey. Values never change once they're built, so the heap can never hold a cycle.
  20. You choose a memory backend when you build. The default is a tracing garbage collector.
  21. With --memory=arc, reference counting frees each value the moment its last owner lets go.
  22. Osprey has no live heap tracker yet, so the profiler doesn't pretend to offer one. Each row in Processes shows the program's memory.
  23. Words.osp is the leak-check sample. Label builds a new string for a number.
  24. Total builds fifty thousand of those strings, adds up their lengths, and drops each one straight away. Under ARC, every one of them should be freed.
  25. In the editor, build it with ARC, and run it with OSPREY_ARC_DEBUG set to one. When the program exits, ARC counts every object still alive.
  26. Here's the same check from the command line. Compile with --memory=arc, then run with the leak report on.
  27. Zero live objects. Fifty thousand strings were built and every one was freed. Set the variable to two, and ARC lists each survivor by kind, when there are any.
  28. Now change the hot code. In a one-round copy of busy.osp, make longest search two thousand numbers instead of twenty thousand. That's a tenth of the collatz work.
  29. Run both. Fib is untouched, and still prints the same number. The longest chain drops from two hundred and seventy eight steps to one hundred and eighty one, because the search covers fewer numbers. Profile it again to see where the time goes now.
  30. To try it, build with --profile to attach with source lines, and check for leaks with ARC and the leak report. Both samples are in the repository. Osprey is actively evolving. Try it on your projects.