@@ -353,74 +353,9 @@ The statistical profiler produces output similar to deterministic profilers but
353353.. module :: profiling.sampling
354354 :synopsis: Python statistical profiler.
355355
356- This section documents the programmatic interface for the :mod: `!profiling.sampling ` module.
357356For command-line usage, see :ref: `sampling-profiler-cli `. For conceptual information
358357about statistical profiling, see :ref: `statistical-profiling `
359358
360- .. 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, native=False, gc=True)
361-
362- Sample a Python process and generate profiling data.
363-
364- This is the main entry point for statistical profiling. It creates a
365- :class: `SampleProfiler `, collects stack traces from the target process, and
366- outputs the results in the specified format.
367-
368- :param int pid: Process ID of the target Python process
369- :param int sort: Sort order for pstats output (default: 2 for cumulative time)
370- :param int sample_interval_usec: Sampling interval in microseconds (default: 100)
371- :param int duration_sec: Duration to sample in seconds (default: 10)
372- :param str filename: Output filename (None for stdout/default naming)
373- :param bool all_threads: Whether to sample all threads (default: False)
374- :param int limit: Maximum number of functions to display (default: None)
375- :param bool show_summary: Whether to show summary statistics (default: True)
376- :param str output_format: Output format - 'pstats' or 'collapsed' (default: 'pstats')
377- :param bool realtime_stats: Whether to display real-time statistics (default: False)
378- :param bool native: Whether to include ``<native> `` frames (default: False)
379- :param bool gc: Whether to include ``<GC> `` frames (default: True)
380-
381- :raises ValueError: If output_format is not 'pstats' or 'collapsed'
382-
383- Examples::
384-
385- # Basic usage - profile process 1234 for 10 seconds
386- import profiling.sampling
387- profiling.sampling.sample(1234)
388-
389- # Profile with custom settings
390- profiling.sampling.sample(1234, duration_sec=30, sample_interval_usec=50, all_threads=True)
391-
392- # Generate collapsed stack traces for flamegraph.pl
393- profiling.sampling.sample(1234, output_format='collapsed', filename='profile.collapsed')
394-
395- .. class :: SampleProfiler(pid, sample_interval_usec, all_threads)
396-
397- Low-level API for the statistical profiler.
398-
399- This profiler uses periodic stack sampling to collect performance data
400- from running Python processes with minimal overhead. It can attach to
401- any Python process by PID and collect stack traces at regular intervals.
402-
403- :param int pid: Process ID of the target Python process
404- :param int sample_interval_usec: Sampling interval in microseconds
405- :param bool all_threads: Whether to sample all threads or just the main thread
406-
407- .. method :: sample(collector, duration_sec=10)
408-
409- Sample the target process for the specified duration.
410-
411- Collects stack traces from the target process at regular intervals
412- and passes them to the provided collector for processing.
413-
414- :param collector: Object that implements ``collect() `` method to process stack traces
415- :param int duration_sec: Duration to sample in seconds (default: 10)
416-
417- The method tracks sampling statistics and can display real-time
418- information if realtime_stats is enabled.
419-
420- .. seealso ::
421-
422- :ref: `sampling-profiler-cli `
423- Command-line interface documentation for the statistical profiler.
424359
425360Deterministic Profiler Command Line Interface
426361=============================================
0 commit comments