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.
312314Together, these determine how many samples will be collected during a profiling
313315session.
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
316318microseconds. The default is 100 microseconds, which produces approximately
31731910,000 samples per second::
318320
@@ -323,7 +325,7 @@ cost of slightly higher profiler CPU usage. Higher intervals reduce profiler
323325overhead but may miss short-lived functions. For most applications, the
324326default 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
327329default 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
342344pools.
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
361363sample is taken. These synthetic frames help distinguish different types of
362364execution 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
365367called into C code (extension modules, built-in functions, or the interpreter
366368itself)::
367369
@@ -373,7 +375,7 @@ in the Python function that made the call. This is useful when optimizing
373375code that makes heavy use of C extensions like NumPy or database drivers.
374376
375377By 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
420422opcode information and may slightly reduce sampling performance, but provides
421423unprecedented visibility into Python's execution model.
422424
423425
424426Real-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
428430profiling::
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.
476478Wall-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
480482thread is doing. This is the default mode and provides a complete picture of
481483where time passes during program execution::
482484
@@ -496,7 +498,7 @@ latency.
496498CPU 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
500502executing 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.
530532GIL 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
534536global 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.
562564pstats 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
566568deterministic 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
614616To save pstats output to a file instead of stdout::
615617
616618 python -m profiling.sampling run -o profile.txt script.py
617619
618620The 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
630632statistics table.
631633
632634
633635Collapsed 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
637639stack, 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.
663665Flame 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
667669an interactive flame graph visualization::
668670
669671 python -m profiling.sampling run --flamegraph script.py
@@ -709,7 +711,7 @@ or through their callees.
709711Gecko 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
713715Profiler::
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;
740742all relevant data is captured automatically.
741743
742744
743745Heatmap format
744746--------------
745747
746- Heatmap format (`` --heatmap ` `) generates an interactive HTML visualization
748+ Heatmap format (:option: ` --heatmap `) generates an interactive HTML visualization
747749showing 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.
786788Live 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
790792data, 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
802804main table shows function statistics with the currently sorted column indicated
803805by 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
806808main table, showing instruction-level statistics for the currently selected
807809function. This panel displays which bytecode instructions are executing most
808810frequently, including specialized variants and their base opcodes.
@@ -869,16 +871,16 @@ When profiling finishes (duration expires or target process exits), the display
869871shows a "PROFILING COMPLETE" banner and freezes the final results. You can
870872still 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
874876interface rather than producing file output.
875877
876878
877879Async-aware profiling
878880=====================
879881
880882For 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
882884rather 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.
898900Async 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
907909on the CPU is profiled. This shows where your program is actively spending time
908910and 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
911913other tasks) are also included. This mode is useful for understanding what your
912914program is waiting on, but produces larger profiles since every suspended task
913915appears in each sample.
@@ -936,8 +938,8 @@ Option restrictions
936938-------------------
937939
938940Async-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
943945Command-line interface
0 commit comments