mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
* Add deterministic profiler and canonical Python 3.15 tools
Use the merged operation critical sections with interpreter-bound profiler
entry checks and short payload borrows. Trace owned code references for GC.
Import the canonical tracing and offline profiling output closure via the
repository updater, retaining existing test markers.
Assisted-by: dot:GPT-6.1
* Clarify standalone profiling license cross-reference
Preserve license terms and copyright notices; point to the reproduced
Apache text below instead of CPython's original OpenSSL section above.
Assisted-by: dot:GPT-6.1
* Defer unsupported sampling package from profiler extraction
Keep the deterministic tracing profiler and its canonical tools together.
Defer the entirely new sampling production, tests, assets and their license
as one closure until native record and binary-collector support is ready.
Retain canonical pstats for deterministic empty-profile handling; its
upstream sampled-file sorting issue is not patched here.
Assisted-by: dot:GPT-6.1
* Stage profiling root as a namespace until sampling is available
Omit the whole newly added canonical package initializer rather than
advertise an absent sampling module or hand-edit canonical __all__.
Explicit profiling.tracing and cProfile imports remain available.
The namespace root intentionally lacks canonical metadata/automatic exports.
This addresses test___all__ failures in the prior Linux, macOS and Windows
CI runs. No test assertions, skips or canonical file contents are changed.
Assisted-by: dot:GPT-6.1
* Match canonical static builtin profiler labels and metadata
Normalize the raw builtin receiver instead of its call-argument accessor.
Snapshot static method module metadata at construction, matching CPython,
so explicit module mutation and deletion remain observable. Avoid a
profiler-time fallback that would obscure deleted metadata.
Correct float.__getformat__ to CPython's classmethod calling convention,
including subclass binding, rather than special-casing its profiler label.
Assisted-by: dot:GPT-6.1
* Support collision-safe frozen package containers
Generate empty frozen packages only for source namespace directories with
real frozen descendants. Preserve genuine modules and packages across
frozen-library registrations while retaining real-real replacement order.
Record the container marker in FrozenModule and its library envelope;
legacy module/package encodings remain unchanged. This is closed-tree
frozen packaging, not full PEP 420 namespace merging.
Add structural codec, mock generator, and registration-priority checks.
* Revert frozen package containers before restoring canonical profiling root
The container implementation changed filesystem namespace precedence.
Restore the existing importer and frozen envelope unchanged; the following
offline sampling closure restores the canonical regular package root.
This reverses d5a8049234.
Assisted-by: dot:GPT-6.1
* Restore canonical profiling package with offline sampling codec
Provide native records, status constants and genuine uncompressed binary
reader/writer APIs for canonical offline tools. Keep the unsupported live
process capability absent and preserve existing canonical test gates.
Restore the complete selected sampling production/test/assets closure via
update_lib, including its asyncio frame-schema dependency. The prior frozen
container extension is reverted separately; canonical packages work with
both filesystem and frozen imports without changing namespace semantics.
Assisted-by: dot:GPT-6.1
* Keep platform-dependent C long normalization explicit
Allow only the c_long-to-i32 conversion lint: Windows uses the same type,
while LP64 requires the existing truncation to match CPython normalization.
No runtime behavior or test assertions change.
Assisted-by: dot:GPT-6.1
* Include profiling notices in library and desktop packages
Bundle a byte-identical copy of the root profiling notice in rustpython-pylib
and include the root notice in configured desktop resources. Avoid a symlink
so Windows checkouts preserve the actual notice text. Terms are unchanged.
Assisted-by: dot:GPT-6.1
* Keep profiling asset license notice in pylib crate
* Serve frozen profiling assets through native resource readers
* Preserve canonical profiling asset line endings on checkout
---------
Co-authored-by: youknowdot <dot@youknowone.org>
95 lines
3.2 KiB
Python
Vendored
95 lines
3.2 KiB
Python
Vendored
"""Opcode utilities for bytecode-level profiler visualization.
|
|
|
|
This module provides utilities to get opcode names and detect specialization
|
|
status using the opcode module's metadata. Used by heatmap and flamegraph
|
|
collectors to display which bytecode instructions are executing at each
|
|
source line, including Python's adaptive specialization optimizations.
|
|
"""
|
|
|
|
import opcode
|
|
|
|
# Build opcode name mapping: opcode number -> opcode name
|
|
# This includes both standard opcodes and specialized variants (Python 3.11+)
|
|
_OPCODE_NAMES = dict(enumerate(opcode.opname))
|
|
if hasattr(opcode, "_specialized_opmap"):
|
|
for name, op in opcode._specialized_opmap.items():
|
|
_OPCODE_NAMES[op] = name
|
|
|
|
# Build deopt mapping: specialized opcode number -> base opcode number
|
|
# Python 3.11+ uses adaptive specialization where generic opcodes like
|
|
# LOAD_ATTR can be replaced at runtime with specialized variants like
|
|
# LOAD_ATTR_INSTANCE_VALUE. This mapping lets us show both forms.
|
|
_DEOPT_MAP = {}
|
|
if hasattr(opcode, "_specializations") and hasattr(
|
|
opcode, "_specialized_opmap"
|
|
):
|
|
for base_name, variant_names in opcode._specializations.items():
|
|
base_opcode = opcode.opmap.get(base_name)
|
|
if base_opcode is not None:
|
|
for variant_name in variant_names:
|
|
variant_opcode = opcode._specialized_opmap.get(variant_name)
|
|
if variant_opcode is not None:
|
|
_DEOPT_MAP[variant_opcode] = base_opcode
|
|
|
|
|
|
def get_opcode_info(opcode_num):
|
|
"""Get opcode name and specialization info from an opcode number.
|
|
|
|
Args:
|
|
opcode_num: The opcode number (0-255 or higher for specialized)
|
|
|
|
Returns:
|
|
A dict with keys:
|
|
- 'opname': The opcode name (e.g., 'LOAD_ATTR_INSTANCE_VALUE')
|
|
- 'base_opname': The base opcode name (e.g., 'LOAD_ATTR')
|
|
- 'is_specialized': True if this is a specialized instruction
|
|
"""
|
|
opname = _OPCODE_NAMES.get(opcode_num)
|
|
if opname is None:
|
|
return {
|
|
"opname": f"<{opcode_num}>",
|
|
"base_opname": f"<{opcode_num}>",
|
|
"is_specialized": False,
|
|
}
|
|
|
|
base_opcode = _DEOPT_MAP.get(opcode_num)
|
|
if base_opcode is not None:
|
|
base_opname = _OPCODE_NAMES.get(base_opcode, f"<{base_opcode}>")
|
|
return {
|
|
"opname": opname,
|
|
"base_opname": base_opname,
|
|
"is_specialized": True,
|
|
}
|
|
|
|
return {
|
|
"opname": opname,
|
|
"base_opname": opname,
|
|
"is_specialized": False,
|
|
}
|
|
|
|
|
|
def format_opcode(opcode_num):
|
|
"""Format an opcode for display, showing base opcode for specialized ones.
|
|
|
|
Args:
|
|
opcode_num: The opcode number (0-255 or higher for specialized)
|
|
|
|
Returns:
|
|
A formatted string like 'LOAD_ATTR' or 'LOAD_ATTR_INSTANCE_VALUE (LOAD_ATTR)'
|
|
"""
|
|
info = get_opcode_info(opcode_num)
|
|
if info["is_specialized"]:
|
|
return f"{info['opname']} ({info['base_opname']})"
|
|
return info["opname"]
|
|
|
|
|
|
def get_opcode_mapping():
|
|
"""Get opcode name and deopt mappings for JavaScript consumption.
|
|
|
|
Returns:
|
|
A dict with keys:
|
|
- 'names': Dict mapping opcode numbers to opcode names
|
|
- 'deopt': Dict mapping specialized opcode numbers to base opcode numbers
|
|
"""
|
|
return {"names": _OPCODE_NAMES, "deopt": _DEOPT_MAP}
|