How benfred/py-spy works
Main Components
a) Core Components:
• Sampler Engine (src/sampler.rs)
• Python Process Interface (src/python_process_info.rs)
• Memory Reading Layer
• Python Interpreter State Handler (src/python_interpreters.rs)
• Output Formatters (flamegraph.rs, speedscope.rs, console_viewer.rs)
b) Supporting Components:
• Python Version Bindings (src/python_bindings/*)
• Configuration Handler (src/config.rs)
• Platform-specific Memory Access Implementations
┌─────────────────────────────────────────┐
• Command Interface │
• (record, top, dump commands) │
• ┤
• Sampling Engine │
• (sampling coordination, timing) │
• ┤
• Memory Access Layer │
• (process_vm_readv, vm_read, etc.) │
• ┤
• Python Interface Layer │
• (interpreter state, thread tracking) │
• ┤
• Output Formatters │
• (flamegraph, speedscope, console) │
• ┘
Key Relationships
• How the Python Interface Layer interacts with different Python version bindings
• Data flow from sampling to output generation
• External system interactions (OS-specific memory reading calls)
Implementation Details to Highlight
• Non-blocking sampling capability
• GIL state detection
• Native extension profiling support
• Subprocess monitoring
• Thread state tracking
Notes
• Clear separation between the profiler and target Python process
• Where system calls and memory access occur
• Non-intrusive nature of the profiling mechanism
• Zero overhead sampling
• Process separation
• Cross-platform support
• Multiple Python version compatibility
• Various output format support