Profiling Osprey 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 in VS Code: CPU, memory and leaks (2:00). 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: default allocates and never collects, gc is a conservative tracing collector, and arc frees each value the moment its last owner lets go. 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 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 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 alpha software.

Transcript

  1. Osprey has a CPU profiler built into VS Code. Here's how to find your hot code, read your memory, and check a program for leaks.
  2. Open a program, and press Run and Profile CPU in the Processes view. Osprey builds it, starts it under the debugger, and starts sampling.
  3. The count climbs while it runs, and the panel names the hottest function so far.
  4. To stop, click the counter in the status bar.
  5. The results open on their own. The flame graph shows where the time went: the wider the frame, the hotter the code.
  6. Back in the source, each hot line carries its share of the samples, so collatz stands out at a glance.
  7. You can also profile a program that's already running, with no restart. Build it with dash dash profile so its samples carry source lines, start it from the terminal, and it shows up in Processes.
  8. Hover its row to see its CPU and memory, then press the flame to start sampling.
  9. Stop it the same way, from the status bar.
  10. Memory works differently in Osprey. Values never change once they're built, so the heap can never hold a cycle.
  11. You choose a memory backend when you build: the default, a tracing GC, or ARC, which frees each value the moment its last owner lets go.
  12. 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.
  13. To check for leaks, build with ARC, and run with OSPREY_ARC_DEBUG set to one.
  14. When the program exits, ARC counts every object still alive. Zero means nothing leaked. Set it to two, and it lists each survivor by kind.
  15. Both programs are in the Osprey repo, under examples slash profiling. Find more at ospreylang.dev.