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

Osprey's VS Code extension has a CPU profiler built in. It samples a running Osprey program, shows where the time goes as a flame graph, and marks the hot lines in your source. This video shows the real extension profiling a real program. It then covers memory: how Osprey's backends work, and how to check a program for leaks.

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

What the video covers

  • Run & Profile CPU. In the Osprey Debug view, the Processes panel offers Run & Profile CPU (Current File). It builds the open file, starts it under the debugger and samples it. While it runs, the panel and the status bar count the samples and name the hottest function so far: the one with the most samples of its own.
  • The results. Click the counter in the status bar to stop. The results panel opens on its own, with a flame graph (left-heavy or in time order), the hot functions and the hot lines. Each hot line in the editor carries its share of the samples.
  • Attach to a running program. Every Osprey program can be profiled while it runs, with no restart. Build it with --profile for line-level results, start it, and press the flame on its row in Processes. Hovering the row shows its CPU and memory.
  • Memory. Osprey values never change once built, so the managed heap can never hold a reference cycle. You choose a memory backend when you build: gc is the default conservative tracing collector; arc releases a value when its last owner is dropped. Explicit --memory=none selects unmanaged allocation, which retains general allocations. Osprey has no live heap tracker yet, so the profiler offers no memory tracking.
  • Leaks. Build with --memory=arc and run with OSPREY_ARC_DEBUG=1. When the program exits, ARC reports how many objects are still alive. OSPREY_ARC_DEBUG=2 also lists each one by kind.

Every frame in the video is the real editor and language server. Both programs are in src/osprey/examples/profiling.

Get it working

Install the Osprey extension and the compiler as described in Install Osprey. Then, from a clone of the repository:

cd src/osprey/examples/profiling
osprey busy.osp --compile --profile -o busy    # a build whose samples carry source lines
./busy                                         # runs until stopped; profile it from Processes

Check a program for leaks:

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

Osprey is actively evolving.

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.