| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import contextvars |
| 4 |
import sys |
| 5 |
import timeit |
| 6 |
import types |
| 7 |
from typing import Any, Callable, List, Optional, Type |
| 8 |
|
| 9 |
from pyinstrument.low_level.pyi_timing_thread_python import ( |
| 10 |
pyi_timing_thread_get_time, |
| 11 |
pyi_timing_thread_subscribe, |
| 12 |
pyi_timing_thread_unsubscribe, |
| 13 |
) |
| 14 |
from pyinstrument.low_level.types import TimerType |
| 15 |
|
| 16 |
|
| 17 |
class PythonStatProfiler: |
| 18 |
await_stack: list[str] |
| 19 |
timing_thread_subscription: int | None = None |
| 20 |
|
| 21 |
def __init__( |
| 22 |
self, |
| 23 |
target: Callable[[types.FrameType, str, Any], Any], |
| 24 |
interval: float, |
| 25 |
context_var: contextvars.ContextVar[object | None] | None, |
| 26 |
timer_type: TimerType, |
| 27 |
timer_func: Callable[[], float] | None, |
| 28 |
): |
| 29 |
self.target = target |
| 30 |
self.interval = interval |
| 31 |
if context_var: |
| 32 |
|
| 33 |
if not isinstance(context_var, contextvars.ContextVar): |
| 34 |
raise TypeError("not a context var") |
| 35 |
self.context_var = context_var |
| 36 |
|
| 37 |
self.timer_type = timer_type |
| 38 |
|
| 39 |
if timer_type == "walltime": |
| 40 |
self.get_time = timeit.default_timer |
| 41 |
elif timer_type == "walltime_thread": |
| 42 |
self.get_time = pyi_timing_thread_get_time |
| 43 |
self.timing_thread_subscription = pyi_timing_thread_subscribe(interval) |
| 44 |
elif timer_type == "timer_func": |
| 45 |
if timer_func is None: |
| 46 |
raise TypeError("timer_func must be provided for timer_func timer_type") |
| 47 |
self.get_time = timer_func |
| 48 |
else: |
| 49 |
raise ValueError(f"invalid timer_type '{timer_type}'") |
| 50 |
|
| 51 |
self.last_invocation = self.get_time() |
| 52 |
|
| 53 |
self.last_context_var_value = context_var.get() if context_var else None |
| 54 |
self.await_stack = [] |
| 55 |
|
| 56 |
def __del__(self): |
| 57 |
if self.timing_thread_subscription is not None: |
| 58 |
pyi_timing_thread_unsubscribe(self.timing_thread_subscription) |
| 59 |
|
| 60 |
def profile(self, frame: types.FrameType, event: str, arg: Any): |
| 61 |
now = self.get_time() |
| 62 |
|
| 63 |
if self.context_var: |
| 64 |
context_var_value = self.context_var.get() |
| 65 |
last_context_var_value = self.last_context_var_value |
| 66 |
|
| 67 |
if context_var_value is not last_context_var_value: |
| 68 |
context_change_frame = frame.f_back if event == "call" else frame |
| 69 |
assert context_change_frame is not None |
| 70 |
self.target( |
| 71 |
context_change_frame, |
| 72 |
"context_changed", |
| 73 |
(context_var_value, last_context_var_value, self.await_stack), |
| 74 |
) |
| 75 |
self.last_context_var_value = context_var_value |
| 76 |
|
| 77 |
|
| 78 |
if event == "return" and frame.f_code.co_flags & 0x80: |
| 79 |
self.await_stack.append(get_frame_info(frame)) |
| 80 |
else: |
| 81 |
self.await_stack.clear() |
| 82 |
|
| 83 |
if now < self.last_invocation + self.interval: |
| 84 |
return |
| 85 |
|
| 86 |
self.last_invocation = now |
| 87 |
return self.target(frame, event, arg) |
| 88 |
|
| 89 |
|
| 90 |
""" |
| 91 |
A reimplementation of setstatprofile in Python, for prototyping/reference |
| 92 |
purposes. Not used in normal execution. |
| 93 |
""" |
| 94 |
|
| 95 |
|
| 96 |
def setstatprofile( |
| 97 |
target: Callable[[types.FrameType, str, Any], Any] | None, |
| 98 |
interval: float = 0.001, |
| 99 |
context_var: contextvars.ContextVar[object | None] | None = None, |
| 100 |
timer_type: TimerType = "walltime", |
| 101 |
timer_func: Callable[[], float] | None = None, |
| 102 |
) -> None: |
| 103 |
if target: |
| 104 |
profiler = PythonStatProfiler( |
| 105 |
target=target, |
| 106 |
interval=interval, |
| 107 |
context_var=context_var, |
| 108 |
timer_type=timer_type, |
| 109 |
timer_func=timer_func, |
| 110 |
) |
| 111 |
sys.setprofile(profiler.profile) |
| 112 |
else: |
| 113 |
sys.setprofile(None) |
| 114 |
|
| 115 |
|
| 116 |
def get_frame_info(frame: types.FrameType) -> str: |
| 117 |
frame_info = "%s\x00%s\x00%i" % ( |
| 118 |
frame.f_code.co_name, |
| 119 |
frame.f_code.co_filename, |
| 120 |
frame.f_code.co_firstlineno, |
| 121 |
) |
| 122 |
|
| 123 |
class_name = None |
| 124 |
|
| 125 |
self = frame.f_locals.get("self", None) |
| 126 |
if self and hasattr(self, "__class__") and hasattr(self.__class__, "__qualname__"): |
| 127 |
class_name = self.__class__.__qualname__ |
| 128 |
else: |
| 129 |
|
| 130 |
cls = frame.f_locals.get("cls", None) |
| 131 |
if cls and hasattr(cls, "__qualname__"): |
| 132 |
class_name = cls.__qualname__ |
| 133 |
|
| 134 |
frame_hidden = "__tracebackhide__" in frame.f_locals |
| 135 |
|
| 136 |
if class_name: |
| 137 |
frame_info += "\x01c%s" % class_name |
| 138 |
|
| 139 |
if frame.f_lineno is not None: |
| 140 |
frame_info += "\x01l%i" % frame.f_lineno |
| 141 |
|
| 142 |
if frame_hidden: |
| 143 |
frame_info += "\x01h%i" % frame_hidden |
| 144 |
|
| 145 |
return frame_info |