Skip to content

Commit 947f555

Browse files
committed
Docs
1 parent aab1f3c commit 947f555

2 files changed

Lines changed: 819 additions & 32 deletions

File tree

Doc/library/profiling.sampling.rst

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
**Source code:** :source:`Lib/profiling/sampling/`
1515

16+
.. program:: profiling.sampling
17+
1618
--------------
1719

1820
.. image:: tachyon-logo.png
@@ -312,7 +314,7 @@ The two most fundamental parameters are the sampling interval and duration.
312314
Together, these determine how many samples will be collected during a profiling
313315
session.
314316

315-
The ``--interval`` option (``-i``) sets the time between samples in
317+
The :option:`--interval` option (:option:`-i`) sets the time between samples in
316318
microseconds. The default is 100 microseconds, which produces approximately
317319
10,000 samples per second::
318320

@@ -323,7 +325,7 @@ cost of slightly higher profiler CPU usage. Higher intervals reduce profiler
323325
overhead but may miss short-lived functions. For most applications, the
324326
default interval provides a good balance between accuracy and overhead.
325327

326-
The ``--duration`` option (``-d``) sets how long to profile in seconds. The
328+
The :option:`--duration` option (:option:`-d`) sets how long to profile in seconds. The
327329
default is 10 seconds::
328330

329331
python -m profiling.sampling run -d 60 script.py
@@ -341,8 +343,8 @@ Python programs often use multiple threads, whether explicitly through the
341343
:mod:`threading` module or implicitly through libraries that manage thread
342344
pools.
343345

344-
By default, the profiler samples only the main thread. The ``--all-threads``
345-
option (``-a``) enables sampling of all threads in the process::
346+
By default, the profiler samples only the main thread. The :option:`--all-threads`
347+
option (:option:`-a`) enables sampling of all threads in the process::
346348

347349
python -m profiling.sampling run -a script.py
348350

@@ -361,7 +363,7 @@ additional context about what the interpreter is doing at the moment each
361363
sample is taken. These synthetic frames help distinguish different types of
362364
execution that would otherwise be invisible.
363365

364-
The ``--native`` option adds ``<native>`` frames to indicate when Python has
366+
The :option:`--native` option adds ``<native>`` frames to indicate when Python has
365367
called into C code (extension modules, built-in functions, or the interpreter
366368
itself)::
367369

@@ -373,7 +375,7 @@ in the Python function that made the call. This is useful when optimizing
373375
code that makes heavy use of C extensions like NumPy or database drivers.
374376

375377
By default, the profiler includes ``<GC>`` frames when garbage collection is
376-
active. The ``--no-gc`` option suppresses these frames::
378+
active. The :option:`--no-gc` option suppresses these frames::
377379

378380
python -m profiling.sampling run --no-gc script.py
379381

@@ -415,16 +417,16 @@ This level of detail is particularly useful for:
415417
- Analyzing the effectiveness of different code patterns at the instruction level
416418
- Debugging performance issues that occur at the bytecode level
417419

418-
The ``--opcodes`` option is compatible with ``--live``, ``--flamegraph``,
419-
``--heatmap``, and ``--gecko`` formats. It requires additional memory to store
420+
The :option:`--opcodes` option is compatible with :option:`--live`, :option:`--flamegraph`,
421+
:option:`--heatmap`, and :option:`--gecko` formats. It requires additional memory to store
420422
opcode information and may slightly reduce sampling performance, but provides
421423
unprecedented visibility into Python's execution model.
422424

423425

424426
Real-time statistics
425427
--------------------
426428

427-
The ``--realtime-stats`` option displays sampling rate statistics during
429+
The :option:`--realtime-stats` option displays sampling rate statistics during
428430
profiling::
429431

430432
python -m profiling.sampling run --realtime-stats script.py
@@ -476,7 +478,7 @@ CPU execution time, or time spent holding the global interpreter lock.
476478
Wall-clock mode
477479
---------------
478480

479-
Wall-clock mode (``--mode=wall``) captures all samples regardless of what the
481+
Wall-clock mode (:option:`--mode`\ ``=wall``) captures all samples regardless of what the
480482
thread is doing. This is the default mode and provides a complete picture of
481483
where time passes during program execution::
482484

@@ -496,7 +498,7 @@ latency.
496498
CPU mode
497499
--------
498500

499-
CPU mode (``--mode=cpu``) records samples only when the thread is actually
501+
CPU mode (:option:`--mode`\ ``=cpu``) records samples only when the thread is actually
500502
executing on a CPU core::
501503

502504
python -m profiling.sampling run --mode=cpu script.py
@@ -530,7 +532,7 @@ connection pooling, or reducing wait time instead.
530532
GIL mode
531533
--------
532534

533-
GIL mode (``--mode=gil``) records samples only when the thread holds Python's
535+
GIL mode (:option:`--mode`\ ``=gil``) records samples only when the thread holds Python's
534536
global interpreter lock::
535537

536538
python -m profiling.sampling run --mode=gil script.py
@@ -562,7 +564,7 @@ output goes to stdout, a file, or a directory depending on the format.
562564
pstats format
563565
-------------
564566

565-
The pstats format (``--pstats``) produces a text table similar to what
567+
The pstats format (:option:`--pstats`) produces a text table similar to what
566568
deterministic profilers generate. This is the default output format::
567569

568570
python -m profiling.sampling run script.py
@@ -609,31 +611,31 @@ interesting functions that highlights:
609611
samples (high cumulative/direct multiplier). These are frequently-nested
610612
functions that appear deep in many call chains.
611613

612-
Use ``--no-summary`` to suppress both the legend and summary sections.
614+
Use :option:`--no-summary` to suppress both the legend and summary sections.
613615

614616
To save pstats output to a file instead of stdout::
615617

616618
python -m profiling.sampling run -o profile.txt script.py
617619

618620
The pstats format supports several options for controlling the display.
619-
The ``--sort`` option determines the column used for ordering results::
621+
The :option:`--sort` option determines the column used for ordering results::
620622

621623
python -m profiling.sampling run --sort=tottime script.py
622624
python -m profiling.sampling run --sort=cumtime script.py
623625
python -m profiling.sampling run --sort=nsamples script.py
624626

625-
The ``--limit`` option restricts output to the top N entries::
627+
The :option:`--limit` option restricts output to the top N entries::
626628

627629
python -m profiling.sampling run --limit=30 script.py
628630

629-
The ``--no-summary`` option suppresses the header summary that precedes the
631+
The :option:`--no-summary` option suppresses the header summary that precedes the
630632
statistics table.
631633

632634

633635
Collapsed stacks format
634636
-----------------------
635637

636-
Collapsed stacks format (``--collapsed``) produces one line per unique call
638+
Collapsed stacks format (:option:`--collapsed`) produces one line per unique call
637639
stack, with a count of how many times that stack was sampled::
638640

639641
python -m profiling.sampling run --collapsed script.py
@@ -663,7 +665,7 @@ visualization where you can click to zoom into specific call paths.
663665
Flame graph format
664666
------------------
665667

666-
Flame graph format (``--flamegraph``) produces a self-contained HTML file with
668+
Flame graph format (:option:`--flamegraph`) produces a self-contained HTML file with
667669
an interactive flame graph visualization::
668670

669671
python -m profiling.sampling run --flamegraph script.py
@@ -709,7 +711,7 @@ or through their callees.
709711
Gecko format
710712
------------
711713

712-
Gecko format (``--gecko``) produces JSON output compatible with the Firefox
714+
Gecko format (:option:`--gecko`) produces JSON output compatible with the Firefox
713715
Profiler::
714716

715717
python -m profiling.sampling run --gecko script.py
@@ -736,14 +738,14 @@ Firefox Profiler timeline:
736738
- **Code type markers**: distinguish Python code from native (C extension) code
737739
- **GC markers**: indicate garbage collection activity
738740

739-
For this reason, the ``--mode`` option is not available with Gecko format;
741+
For this reason, the :option:`--mode` option is not available with Gecko format;
740742
all relevant data is captured automatically.
741743

742744

743745
Heatmap format
744746
--------------
745747

746-
Heatmap format (``--heatmap``) generates an interactive HTML visualization
748+
Heatmap format (:option:`--heatmap`) generates an interactive HTML visualization
747749
showing sample counts at the source line level::
748750

749751
python -m profiling.sampling run --heatmap script.py
@@ -786,7 +788,7 @@ interpretation of hierarchical visualizations.
786788
Live mode
787789
=========
788790

789-
Live mode (``--live``) provides a terminal-based real-time view of profiling
791+
Live mode (:option:`--live`) provides a terminal-based real-time view of profiling
790792
data, similar to the ``top`` command for system processes::
791793

792794
python -m profiling.sampling run --live script.py
@@ -802,7 +804,7 @@ and thread status statistics (GIL held percentage, CPU usage, GC time). The
802804
main table shows function statistics with the currently sorted column indicated
803805
by an arrow (▼).
804806

805-
When ``--opcodes`` is enabled, an additional opcode panel appears below the
807+
When :option:`--opcodes` is enabled, an additional opcode panel appears below the
806808
main table, showing instruction-level statistics for the currently selected
807809
function. This panel displays which bytecode instructions are executing most
808810
frequently, including specialized variants and their base opcodes.
@@ -869,16 +871,16 @@ When profiling finishes (duration expires or target process exits), the display
869871
shows a "PROFILING COMPLETE" banner and freezes the final results. You can
870872
still navigate, sort, and filter the results before pressing :kbd:`q` to exit.
871873

872-
Live mode is incompatible with output format options (``--collapsed``,
873-
``--flamegraph``, and so on) because it uses an interactive terminal
874+
Live mode is incompatible with output format options (:option:`--collapsed`,
875+
:option:`--flamegraph`, and so on) because it uses an interactive terminal
874876
interface rather than producing file output.
875877

876878

877879
Async-aware profiling
878880
=====================
879881

880882
For programs using :mod:`asyncio`, the profiler offers async-aware mode
881-
(``--async-aware``) that reconstructs call stacks based on the task structure
883+
(:option:`--async-aware`) that reconstructs call stacks based on the task structure
882884
rather than the raw Python frames::
883885

884886
python -m profiling.sampling run --async-aware async_script.py
@@ -898,16 +900,16 @@ and presenting stacks that reflect the ``await`` chain.
898900
Async modes
899901
-----------
900902

901-
The ``--async-mode`` option controls which tasks appear in the profile::
903+
The :option:`--async-mode` option controls which tasks appear in the profile::
902904

903905
python -m profiling.sampling run --async-aware --async-mode=running async_script.py
904906
python -m profiling.sampling run --async-aware --async-mode=all async_script.py
905907

906-
With ``--async-mode=running`` (the default), only the task currently executing
908+
With :option:`--async-mode`\ ``=running`` (the default), only the task currently executing
907909
on the CPU is profiled. This shows where your program is actively spending time
908910
and is the typical choice for performance analysis.
909911

910-
With ``--async-mode=all``, tasks that are suspended (awaiting I/O, locks, or
912+
With :option:`--async-mode`\ ``=all``, tasks that are suspended (awaiting I/O, locks, or
911913
other tasks) are also included. This mode is useful for understanding what your
912914
program is waiting on, but produces larger profiles since every suspended task
913915
appears in each sample.
@@ -936,8 +938,8 @@ Option restrictions
936938
-------------------
937939

938940
Async-aware mode uses a different stack reconstruction mechanism and is
939-
incompatible with: ``--native``, ``--no-gc``, ``--all-threads``, and
940-
``--mode=cpu`` or ``--mode=gil``.
941+
incompatible with: :option:`--native`, :option:`--no-gc`, :option:`--all-threads`, and
942+
:option:`--mode`\ ``=cpu`` or :option:`--mode`\ ``=gil``.
941943

942944

943945
Command-line interface

0 commit comments

Comments
 (0)