@@ -339,6 +339,79 @@ The statistical profiler produces output similar to deterministic profilers but
339339
340340.. _profile-cli :
341341
342+ :mod: `profile.sample ` Module Reference
343+ =======================================================
344+
345+ .. module :: profile.sample
346+ :synopsis: Python statistical profiler.
347+
348+ This section documents the programmatic interface for the :mod: `profile.sample ` module.
349+ For command-line usage, see :ref: `sampling-profiler-cli `. For conceptual information
350+ about statistical profiling, see :ref: `statistical-profiling `
351+
352+ .. function :: sample(pid, *, sort=2, sample_interval_usec=100, duration_sec=10, filename=None, all_threads=False, limit=None, show_summary=True, output_format="pstats", realtime_stats=False)
353+
354+ Sample a Python process and generate profiling data.
355+
356+ This is the main entry point for statistical profiling. It creates a
357+ :class: `SampleProfiler `, collects stack traces from the target process, and
358+ outputs the results in the specified format.
359+
360+ :param int pid: Process ID of the target Python process
361+ :param int sort: Sort order for pstats output (default: 2 for cumulative time)
362+ :param int sample_interval_usec: Sampling interval in microseconds (default: 100)
363+ :param int duration_sec: Duration to sample in seconds (default: 10)
364+ :param str filename: Output filename (None for stdout/default naming)
365+ :param bool all_threads: Whether to sample all threads (default: False)
366+ :param int limit: Maximum number of functions to display (default: None)
367+ :param bool show_summary: Whether to show summary statistics (default: True)
368+ :param str output_format: Output format - 'pstats' or 'collapsed' (default: 'pstats')
369+ :param bool realtime_stats: Whether to display real-time statistics (default: False)
370+
371+ :raises ValueError: If output_format is not 'pstats' or 'collapsed'
372+
373+ Examples::
374+
375+ # Basic usage - profile process 1234 for 10 seconds
376+ import profile.sample
377+ profile.sample.sample(1234)
378+
379+ # Profile with custom settings
380+ profile.sample.sample(1234, duration_sec=30, sample_interval_usec=50, all_threads=True)
381+
382+ # Generate collapsed stack traces for flamegraph.pl
383+ profile.sample.sample(1234, output_format='collapsed', filename='profile.collapsed')
384+
385+ .. class :: SampleProfiler(pid, sample_interval_usec, all_threads)
386+
387+ Low-level API for the statistical profiler.
388+
389+ This profiler uses periodic stack sampling to collect performance data
390+ from running Python processes with minimal overhead. It can attach to
391+ any Python process by PID and collect stack traces at regular intervals.
392+
393+ :param int pid: Process ID of the target Python process
394+ :param int sample_interval_usec: Sampling interval in microseconds
395+ :param bool all_threads: Whether to sample all threads or just the main thread
396+
397+ .. method :: sample(collector, duration_sec=10)
398+
399+ Sample the target process for the specified duration.
400+
401+ Collects stack traces from the target process at regular intervals
402+ and passes them to the provided collector for processing.
403+
404+ :param collector: Object that implements ``collect() `` method to process stack traces
405+ :param int duration_sec: Duration to sample in seconds (default: 10)
406+
407+ The method tracks sampling statistics and can display real-time
408+ information if realtime_stats is enabled.
409+
410+ .. seealso ::
411+
412+ :ref: `sampling-profiler-cli `
413+ Command-line interface documentation for the statistical profiler.
414+
342415Deterministic Profiler Command Line Interface
343416=============================================
344417
0 commit comments