Skip to content

Commit b13b6f0

Browse files
committed
Docs
1 parent a4685fd commit b13b6f0

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Doc/library/profiling.sampling.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ Generate a line-by-line heatmap::
146146

147147
python -m profiling.sampling run --heatmap script.py
148148

149+
Enable opcode-level profiling to see which bytecode instructions are executing::
150+
151+
python -m profiling.sampling run --opcodes --flamegraph script.py
152+
149153

150154
Commands
151155
========
@@ -379,6 +383,44 @@ see substantial time in ``<GC>`` frames, consider investigating object
379383
allocation rates or using object pooling.
380384

381385

386+
Opcode-aware profiling
387+
----------------------
388+
389+
The ``--opcodes`` option enables instruction-level profiling that captures
390+
which Python bytecode instructions are executing at each sample::
391+
392+
python -m profiling.sampling run --opcodes --flamegraph script.py
393+
394+
This feature provides visibility into Python's bytecode execution, including
395+
adaptive specialization optimizations. When a generic instruction like
396+
``LOAD_ATTR`` is specialized at runtime into a more efficient variant like
397+
``LOAD_ATTR_INSTANCE_VALUE``, the profiler shows both the specialized name
398+
and the base instruction.
399+
400+
Opcode information appears in several output formats:
401+
402+
- **Live mode**: An opcode panel shows instruction-level statistics for the
403+
selected function, accessible via keyboard navigation
404+
- **Flame graphs**: Nodes display opcode information when available, helping
405+
identify which instructions consume the most time
406+
- **Heatmap**: Expandable bytecode panels per source line show instruction
407+
breakdown with specialization percentages
408+
- **Gecko format**: Opcode transitions are emitted as interval markers in the
409+
Firefox Profiler timeline
410+
411+
This level of detail is particularly useful for:
412+
413+
- Understanding the performance impact of Python's adaptive specialization
414+
- Identifying hot bytecode instructions that might benefit from optimization
415+
- Analyzing the effectiveness of different code patterns at the instruction level
416+
- Debugging performance issues that occur at the bytecode level
417+
418+
The ``--opcodes`` option is compatible with ``--live``, ``--flamegraph``,
419+
``--heatmap``, and ``--gecko`` formats. It requires additional memory to store
420+
opcode information and may slightly reduce sampling performance, but provides
421+
unprecedented visibility into Python's execution model.
422+
423+
382424
Real-time statistics
383425
--------------------
384426

@@ -760,6 +802,11 @@ and thread status statistics (GIL held percentage, CPU usage, GC time). The
760802
main table shows function statistics with the currently sorted column indicated
761803
by an arrow (▼).
762804

805+
When ``--opcodes`` is enabled, an additional opcode panel appears below the
806+
main table, showing instruction-level statistics for the currently selected
807+
function. This panel displays which bytecode instructions are executing most
808+
frequently, including specialized variants and their base opcodes.
809+
763810

764811
Keyboard commands
765812
-----------------
@@ -813,6 +860,11 @@ Within live mode, keyboard commands control the display:
813860
:kbd:`h` or :kbd:`?`
814861
Show the help screen with all available commands.
815862

863+
:kbd:`j` / :kbd:`k`
864+
Navigate through opcode entries in the opcode panel (when ``--opcodes`` is
865+
enabled). These keys scroll through the instruction-level statistics for the
866+
currently selected function.
867+
816868
When profiling finishes (duration expires or target process exits), the display
817869
shows a "PROFILING COMPLETE" banner and freezes the final results. You can
818870
still navigate, sort, and filter the results before pressing :kbd:`q` to exit.
@@ -939,6 +991,13 @@ Sampling options
939991

940992
Enable async-aware profiling for asyncio programs.
941993

994+
.. option:: --opcodes
995+
996+
Gather bytecode opcode information for instruction-level profiling. Shows
997+
which bytecode instructions are executing, including specializations.
998+
Compatible with ``--live``, ``--flamegraph``, ``--heatmap``, and ``--gecko``
999+
formats only.
1000+
9421001

9431002
Mode options
9441003
------------

0 commit comments

Comments
 (0)