From 512103987a192993db98098651cf899b1c8b9b03 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:51:02 -0400 Subject: [PATCH 1/6] Further speed improvements --- MANIFEST.in | 1 + line_profiler/_diagnostics.py | 3 + line_profiler/_line_profiler.pyx | 517 ++++++++++++++++++------------ line_profiler/_map_helpers.pxd | 27 -- line_profiler/_profile_data.hpp | 204 ++++++++++++ line_profiler/_profile_data.pxd | 54 ++++ line_profiler/_profiler_base.pxd | 22 +- line_profiler/_profiler_base.pyx | 128 +++++--- line_profiler/_sys_monitoring.pxd | 19 +- line_profiler/_sys_monitoring.pyx | 197 +++++++----- line_profiler/c_trace_callbacks.c | 7 + line_profiler/c_trace_callbacks.h | 1 + line_profiler/timers.c | 127 +++++++- pyproject.toml | 6 +- tests/test_sys_monitoring.py | 14 +- 15 files changed, 950 insertions(+), 377 deletions(-) delete mode 100644 line_profiler/_map_helpers.pxd create mode 100644 line_profiler/_profile_data.hpp create mode 100644 line_profiler/_profile_data.pxd diff --git a/MANIFEST.in b/MANIFEST.in index 1c39c823..1544ef66 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -15,3 +15,4 @@ recursive-include line_profiler *.pyd recursive-include line_profiler *.c recursive-include line_profiler *.h include dev/repair_windows_wheel.py +recursive-include line_profiler *.hpp diff --git a/line_profiler/_diagnostics.py b/line_profiler/_diagnostics.py index 6ae2b1a8..40ebc1b6 100644 --- a/line_profiler/_diagnostics.py +++ b/line_profiler/_diagnostics.py @@ -91,6 +91,9 @@ def _boolean_environ( # `line_profiler._line_profiler` switches WRAP_TRACE = _boolean_environ('LINE_PROFILER_WRAP_TRACE') +# Whether to time with the CPU's timestamp counter where it's reliable +# (see line_profiler/timers.c) +USE_TSC = _boolean_environ('LINE_PROFILER_TSC', default=True) SET_FRAME_LOCAL_TRACE = _boolean_environ('LINE_PROFILER_SET_FRAME_LOCAL_TRACE') _MUST_USE_LEGACY_TRACE = not isinstance( getattr(sys, 'monitoring', None), ModuleType diff --git a/line_profiler/_line_profiler.pyx b/line_profiler/_line_profiler.pyx index 3d8b82fd..ce6b6d39 100644 --- a/line_profiler/_line_profiler.pyx +++ b/line_profiler/_line_profiler.pyx @@ -22,8 +22,7 @@ from cpython.object cimport PyObject_Hash from cpython.bytes cimport PyBytes_AS_STRING, PyBytes_GET_SIZE from cpython.exc cimport PyErr_Clear from cpython.version cimport PY_VERSION_HEX -from libc.stdint cimport int64_t, intptr_t -from libc.stdlib cimport free, malloc +from libc.stdint cimport int64_t from libcpp.unordered_map cimport unordered_map from libcpp.vector cimport vector @@ -33,10 +32,13 @@ import types from warnings import warn from line_profiler._diagnostics import ( - WRAP_TRACE, SET_FRAME_LOCAL_TRACE, USE_LEGACY_TRACE + WRAP_TRACE, SET_FRAME_LOCAL_TRACE, USE_LEGACY_TRACE, USE_TSC ) -from ._map_helpers cimport LastTime, LastTimeMap, LineTime, LineTimeMap +from ._profile_data cimport ( + Block, CodeCache, LastLine, LineData, ProfileEntry, + free_code_cache, new_code_cache, +) from ._profiler_base cimport ( _LineProfilerBase, compute_line_hash, note_events_disabled, restart_disabled_events, @@ -104,8 +106,6 @@ cdef extern from "Python_wrapper.h": void *extra) ctypedef PyCodeObject *PyCodeObjectPtr -#ctypedef unordered_map[int64, LastTime] LastTimeMap -#ctypedef unordered_map[int64, LineTime] LineTimeMap cdef extern from "c_trace_callbacks.h": # Legacy tracing ctypedef unsigned long long Py_uintptr_t @@ -122,21 +122,20 @@ cdef extern from "c_trace_callbacks.h": # Legacy tracing PyObject *disabler, TraceCallback *callback, PyFrameObject *py_frame, int what, PyObject *arg) cdef void set_local_trace(PyObject *manager, PyFrameObject *py_frame) + cdef void c_disable_line_events "disable_line_events"( + PyFrameObject *py_frame) cdef Py_uintptr_t monitoring_restart_version() cdef extern from "timers.c": PY_LONG_LONG hpTimer() double hpTimerUnit() + double hpTimerTicksToUnits() + int hpTimerInit(int allow_tsc) -#cdef struct LineTime: -# int64 code -# int lineno -# PY_LONG_LONG total_time -# long nhits - -#cdef struct LastTime: -# int f_lineno -# PY_LONG_LONG time +# Whether the timer is the CPU's timestamp counter (see timers.c), in +# which case the raw timings are in ticks, and are converted to +# hpTimerUnit()s in the views of the data (e.g. LineProfiler.get_stats()) +USING_TSC = bool(hpTimerInit(USE_TSC)) # sys.monitoring event ids, looked up once here rather than on every @@ -156,28 +155,7 @@ if _CAN_USE_SYS_MONITORING: _RERAISE_EVENT = sys.monitoring.events.RERAISE -cdef struct BlockInfo: - Py_hash_t block_hash - # Whether it's Cython code (which has no bytecode of its own) - bint is_cython - - -cdef void free_block_info(void *info) noexcept nogil: - free(info) - - -# Index of the per-code-object scratch slot (see PEP 523) in which -# get_block_info() caches a BlockInfo; negative if none is available -cdef Py_ssize_t _BLOCK_INFO_EXTRA_INDEX = ( - PyUnstable_Eval_RequestCodeExtraIndex(free_block_info)) -if _BLOCK_INFO_EXTRA_INDEX < 0: - PyErr_Clear() - warn('All the scratch slots on code objects (see PEP 523) are taken ' - 'by other extensions; line profiling still works, but with more ' - 'overhead') - - -cdef inline void compute_block_info(object code, BlockInfo *info) noexcept: +cdef inline Py_hash_t compute_block_hash(object code) noexcept: """ Compute the hash identifying the code block, which is the hash of the bytecode for Python code and of the code object for Cython code. @@ -190,49 +168,88 @@ cdef inline void compute_block_info(object code, BlockInfo *info) noexcept: # if there are any non-NULL, that indicates we're profiling Python code for i in range(size): if data[i]: - info.block_hash = hash(py_bytes_obj) - info.is_cython = False - return + return hash(py_bytes_obj) # fallback for Cython functions - info.block_hash = hash(code) - info.is_cython = True + return hash(code) + + +# Registry of the code blocks being profiled (by any instance), keyed by +# block hash: code objects with the same bytecode share the data of the +# block. The blocks are never freed, so that the pointers cached on the +# code objects stay valid. +cdef unordered_map[Py_hash_t, Block *] _blocks +# Incremented when blocks are added, invalidating the cached lookups +cdef unsigned long _blocks_generation = 1 + +# Index of the per-code-object scratch slot (see PEP 523) in which a +# CodeCache is stored; negative if none is available +cdef Py_ssize_t _CODE_CACHE_INDEX = ( + PyUnstable_Eval_RequestCodeExtraIndex(free_code_cache)) +if _CODE_CACHE_INDEX < 0: + PyErr_Clear() + warn('All the scratch slots on code objects (see PEP 523) are taken ' + 'by other extensions; line profiling still works, but with more ' + 'overhead') -cdef inline BlockInfo *get_block_info( - object code, BlockInfo *scratch) noexcept: +cdef Block *get_or_add_block(Py_hash_t block_hash) noexcept: + global _blocks_generation + cdef unordered_map[Py_hash_t, Block *].iterator it = ( + _blocks.find(block_hash)) + if it != _blocks.end(): + return deref(it).second + cdef Block *block = new Block(block_hash) + _blocks[block_hash] = block + _blocks_generation += 1 + return block + + +cdef Block *find_block(Py_hash_t block_hash) noexcept: + cdef unordered_map[Py_hash_t, Block *].iterator it = ( + _blocks.find(block_hash)) + if it == _blocks.end(): + return NULL + return deref(it).second + + +cdef Block *lookup_block(object code, CodeCache *cache) noexcept: """ - compute_block_info(), but cached on the code object so that - the trace callbacks don't have to fetch and scan its bytecode for - every event. + Slow path of :c:func:`get_block` (when the code object is seen for + the first time or blocks have been added since): find the block of + the code and cache it on the code object. + """ + if cache == NULL: + cache = new_code_cache(compute_block_hash(code)) + if cache == NULL: + return NULL + if (_CODE_CACHE_INDEX < 0 + or PyUnstable_Code_SetExtra( + code, _CODE_CACHE_INDEX, cache) < 0): + PyErr_Clear() + block = find_block(cache.block_hash) + free_code_cache(cache) + return block + cache.generation = _blocks_generation + cache.block = find_block(cache.block_hash) + return cache.block + +cdef inline Block *get_block(object code) noexcept: + """ Returns: - The cached info, or scratch (filled in) if it can't be - cached + The block of the code object, NULL if it isn't being profiled """ - cdef void *extra = NULL - cdef BlockInfo *info - if _BLOCK_INFO_EXTRA_INDEX >= 0: - if PyUnstable_Code_GetExtra( - code, _BLOCK_INFO_EXTRA_INDEX, &extra) < 0: + cdef void *cache = NULL + if _CODE_CACHE_INDEX >= 0: + if PyUnstable_Code_GetExtra(code, _CODE_CACHE_INDEX, &cache) < 0: # Only fails if code isn't a code object, which there's # nothing to cache on PyErr_Clear() - compute_block_info(code, scratch) - return scratch - if extra != NULL: - return extra - info = malloc(sizeof(BlockInfo)) - if info != NULL: - compute_block_info(code, info) - if PyUnstable_Code_SetExtra( - code, _BLOCK_INFO_EXTRA_INDEX, info) == 0: - return info - PyErr_Clear() - scratch[0] = info[0] - free(info) - return scratch - compute_block_info(code, scratch) - return scratch + return find_block(compute_block_hash(code)) + if (cache != NULL + and (cache).generation == _blocks_generation): + return (cache).block + return lookup_block(code, cache) def disable_line_events(trace_func: Callable) -> Callable: @@ -267,6 +284,17 @@ def disable_line_events(trace_func: Callable) -> Callable: return wrapper +# Looked up once here rather than on every event when wrapping around +# legacy trace callbacks +cdef object _disable_line_events = disable_line_events +cdef object _gettrace = sys.gettrace +cdef dict _TRACE_EVENT_IDS = {'call': PyTrace_CALL, + 'exception': PyTrace_EXCEPTION, + 'line': PyTrace_LINE, + 'return': PyTrace_RETURN, + 'opcode': PyTrace_OPCODE} + + # Note: this is a regular Python class to allow easy pickling. class LineStats(object): """ @@ -418,11 +446,15 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx .. _trace_trampoline: https://github.com/python/cpython/blob/\ 6cb20a219a860eaf687b2d968b41c480c7461909/Python/sysmodule.c#L1124 """ - cdef int what = {'call': PyTrace_CALL, - 'exception': PyTrace_EXCEPTION, - 'line': PyTrace_LINE, - 'return': PyTrace_RETURN, - 'opcode': PyTrace_OPCODE}[event] + return self._trace(frame, event, arg) + + @cython.profile(False) + cdef object _trace(self, object frame, object event, object arg): + """ + Implementation of :py:meth:`~.__call__` (which is also called + directly by the wrappers from :py:meth:`~.wrap_local_f_trace`). + """ + cdef int what = _TRACE_EVENT_IDS[event] if not self.recursion_guard: # Prevent recursion (e.g. when `.wrap_trace` and # `.set_frame_local_trace` are both true) @@ -431,7 +463,7 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx # Set the C-level trace callback back to # `legacy_trace_callback()` where appropriate, so that future # calls can bypass this `.__call__()` method - if sys.gettrace() is self: + if _gettrace() is self: PyEval_SetTrace(legacy_trace_callback, self) return self @@ -462,7 +494,7 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx result = None else: result = trace_func(frame, event, arg) - self(frame, event, arg) + self._trace(frame, event, arg) return result wrapper.__line_profiler_manager__ = self @@ -488,15 +520,7 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx Line-event callback for :py:attr:`!sys.monitoring.events.LINE`, passed to :py:func:`sys.monitoring.register_callback`. """ - cdef bint profiled = inner_trace_callback( - 1, &self._active, code, lineno, NULL) - if self._wrap_trace: - self.mon_state.call_callback(_LINE_EVENT, code, (lineno,), ()) - elif not profiled: - # Not being profiled and no wrapped callbacks need the - # event: stop it from firing here at all - note_events_disabled() - return _DISABLE + return on_line_event(self, code, lineno) @cython.profile(False) @cython.binding(False) @@ -506,10 +530,8 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx Return-event callback for :py:attr:`!sys.monitoring.events.PY_RETURN`, passed to :py:func:`sys.monitoring.register_callback`. """ - if self._handle_exit_event( - _PY_RETURN_EVENT, code, instruction_offset, retval): - note_events_disabled() - return _DISABLE + return on_exit_event( + self, _PY_RETURN_EVENT, code, instruction_offset, retval, True) @cython.profile(False) @cython.binding(False) @@ -519,10 +541,8 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx Yield-event callback for :py:attr:`!sys.monitoring.events.PY_YIELD`, passed to :py:func:`sys.monitoring.register_callback`. """ - if self._handle_exit_event( - _PY_YIELD_EVENT, code, instruction_offset, retval): - note_events_disabled() - return _DISABLE + return on_exit_event( + self, _PY_YIELD_EVENT, code, instruction_offset, retval, True) @cython.profile(False) @cython.binding(False) @@ -532,8 +552,8 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx Raise-event callback for :py:attr:`!sys.monitoring.events.RAISE`, passed to :py:func:`sys.monitoring.register_callback`. """ - self._handle_exit_event( - _RAISE_EVENT, code, instruction_offset, exception) + return on_exit_event( + self, _RAISE_EVENT, code, instruction_offset, exception, False) @cython.profile(False) @cython.binding(False) @@ -543,30 +563,8 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx Re-raise-event callback for :py:attr:`!sys.monitoring.events.RERAISE`, passed to :py:func:`sys.monitoring.register_callback`. """ - self._handle_exit_event( - _RERAISE_EVENT, code, instruction_offset, exception) - - cdef bint _handle_exit_event( - self, int event_id, object code, int offset, object obj) noexcept: - """ - Base for the frame-exit-event (e.g. via returning or yielding) - callbacks passed to :py:func:`sys.monitoring.register_callback`. - - Returns: - Whether the event can be disabled at the location, i.e. the - code isn't being profiled and no wrapped callbacks need it - - Note: - This is deliberately made a non-traceable C method so that - we don't fall info infinite recursion. - """ - cdef bint profiled = inner_trace_callback( - 0, &self._active, code, - PyCode_Addr2Line(code, offset), NULL) - if self._wrap_trace: - self.mon_state.call_callback(event_id, code, (offset,), (obj,)) - return False - return not profiled + return on_exit_event( + self, _RERAISE_EVENT, code, instruction_offset, exception, False) cpdef _handle_enable_event(self, prof): cdef TraceCallback* legacy_callback @@ -633,6 +631,49 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx 1 if set_frame_local_trace and USE_LEGACY_TRACE else 0) +cdef inline object on_line_event( + _LineProfilerManager manager, object code, int lineno): + """ + Handle a :py:attr:`!sys.monitoring.events.LINE` event. + + Returns: + :py:data:`sys.monitoring.DISABLE` if the event isn't needed at + the location, else :py:data:`None` + """ + cdef bint profiled = inner_trace_callback( + 1, &manager._active, code, lineno, NULL) + if manager._wrap_trace: + manager.mon_state.call_callback(_LINE_EVENT, code, lineno, None, False) + elif not profiled: + # Not being profiled and no wrapped callbacks need the event: + # stop it from firing here at all + note_events_disabled() + return _DISABLE + return None + + +cdef inline object on_exit_event( + _LineProfilerManager manager, int event_id, object code, + int offset, object obj, bint can_disable): + """ + Handle a frame-exit (e.g. via returning or yielding) event. + + Returns: + :py:data:`sys.monitoring.DISABLE` if the event isn't needed at + the location and ``can_disable`` is true, else :py:data:`None` + """ + # Note: the line number isn't needed for exit events + cdef bint profiled = inner_trace_callback( + 0, &manager._active, code, -1, NULL) + if manager._wrap_trace: + manager.mon_state.call_callback(event_id, code, offset, obj, True) + return None + if profiled or not can_disable: + return None + note_events_disabled() + return _DISABLE + + cdef class LineProfiler(_LineProfilerBase): """ Time the execution of lines of Python code. @@ -928,8 +969,53 @@ cdef class LineProfiler(_LineProfilerBase): def enable(self): self._manager._handle_enable_event(self) + def __cinit__(self, *args, **kwargs): + self._ticks_to_units = hpTimerTicksToUnits() + + def __dealloc__(self): + cdef ProfileEntry *entry + cdef Block *block + cdef size_t i, j + for i in range(self._entries.size()): + entry = self._entries[i] + block = find_block(entry.block_hash) + if block != NULL: + for j in range(block.entries.size()): + if block.entries[j] == entry: + block.entries.erase(block.entries.begin() + j) + break + del entry + self._entries.clear() + + cdef list _register_lines(self, Py_hash_t block_hash, object linenos): + cdef Block *block = get_or_add_block(block_hash) + cdef ProfileEntry *entry = NULL + cdef size_t i + for i in range(block.entries.size()): + if block.entries[i].profiler == self: + entry = block.entries[i] + break + if entry == NULL: + # Note: the entry references the instance without holding a + # reference, and is removed from the block when the instance + # is deallocated + entry = new ProfileEntry(self, block_hash) + block.entries.push_back(entry) + self._entries.push_back(entry) + new_linenos = [] + for lineno in linenos: + if entry.register_line(lineno): + new_linenos.append(lineno) + return new_linenos + cpdef disable(self): - self._c_last_time[PyThread_get_thread_ident()].clear() + cdef size_t i + cdef unsigned long ident = PyThread_get_thread_ident() + # Forget the lines last executed (there'll be no events to record + # their times) + for i in range(self._entries.size()): + self._entries[i].clear_last_lines(ident) + self._disabled_threads.add(ident) self._manager._handle_disable_event(self) def get_stats(self): @@ -937,7 +1023,7 @@ cdef class LineProfiler(_LineProfilerBase): Returns: :py:class:`LineStats` object containing the timings. """ - cdef dict cmap = self._c_code_map + cdef dict cmap = self.c_code_map all_entries = {} for code in self.code_hash_map: @@ -965,6 +1051,22 @@ cdef class LineProfiler(_LineProfilerBase): return LineStats(stats, self.timer_unit) +cdef inline bint is_profiled( + Block *block, vector[PyObject *] *instances) noexcept: + """ + Returns: + Whether any of the ``instances`` profiles the code ``block`` + """ + cdef size_t i, j + if block == NULL: + return False + for i in range(block.entries.size()): + for j in range(deref(instances).size()): + if deref(instances)[j] == block.entries[i].profiler: + return True + return False + + @cython.boundscheck(False) @cython.wraparound(False) cdef inline bint inner_trace_callback( @@ -973,102 +1075,82 @@ cdef inline bint inner_trace_callback( """ The basic building block for the trace callbacks. + Arguments: + is_line_event (bool) + Whether it is a line event, else the exit (e.g. returning or + yielding) from the code + lineno (int) + Line number of the line event (unused for exit events) + py_frame (PyFrameObject *) + The frame of the event (legacy tracing), else NULL + Returns: - Whether the location is profiled by any of the instances + Whether the location is profiled by any of the ``instances`` """ - cdef PyObject *prof_ - cdef LineProfiler prof - cdef LastTime* old + cdef Block *block = get_block(code) + cdef ProfileEntry *entry + cdef LastLine *last + cdef LineData *line cdef PY_LONG_LONG time = 0 cdef bint has_time = False - cdef bint has_last - cdef int64 code_hash - cdef int64 last_time_key = 0 cdef unsigned long ident = 0 - cdef BlockInfo scratch - # because we use Python functions like hash, we CANNOT mark this function as nogil - cdef BlockInfo *block_info = get_block_info(code, &scratch) - cdef Py_hash_t block_hash = block_info.block_hash - cdef LineTime* entry - cdef LineTimeMap* line_entries - cdef LastTimeMap* last_map - cdef unordered_map[int64, LineTimeMap].iterator line_it - cdef LastTimeMap.iterator last_it - - code_hash = compute_line_hash(block_hash, lineno) - - for prof_ in deref(instances): - prof = prof_ - # Each map is searched once, and the iterator reused, instead of - # checking with .count() and then indexing into it again - line_it = prof._c_code_map.find(code_hash) - if line_it == prof._c_code_map.end(): + cdef const void *frame_key = NULL + cdef size_t i, j, ninstances + cdef int index = -1 # (Only used for line events) + + if block == NULL: + return False + ninstances = deref(instances).size() + for i in range(block.entries.size()): + entry = block.entries[i] + # Only the active profilers record anything + for j in range(ninstances): + if deref(instances)[j] == entry.profiler: + break + else: continue + if is_line_event: + index = entry.line_index(lineno) + if index < 0: + continue if not has_time: time = hpTimer() ident = PyThread_get_thread_ident() has_time = True - # last_time_key identifies whose last-line record to use. - # for Python code, we use the executing frame so that the - # records of recursive calls to a function don't overwrite - # those of the calls further up the stack - # for Cython code under sys.monitoring, we use the code - # block, since Cython functions don't get frames of their - # own there (so the times of recursive calls of Cython - # functions are still lost) - # A frame address can theoretically equal the hash of an - # unrelated Cython code block, but that can't misattribute - # time bc (a record left by another code block is discarded - # as stale below), at worst the two drop each other's records + # frame_key identifies whose last-line record to use: the + # executing frame, so that the records of recursive calls to + # a function don't overwrite those of the calls further up + # the stack + # Note: Cython functions don't get frames of their own under + # sys.monitoring, so the key is the frame of the Python code + # calling them (and the times of their direct recursive calls + # are still lost) if py_frame != NULL: - last_time_key = py_frame - elif block_info.is_cython: - last_time_key = block_hash - else: - last_time_key = get_current_frame_key() - last_map = &(prof._c_last_time[ident]) - # deref() is Cython's version of the -> accessor in C++. if we don't use deref then - # Cython thinks that when we index last_map, - # we want pointer indexing (which is not the case) - last_it = deref(last_map).find(last_time_key) - # a record left by a frame of another function at the same - # address is stale - has_last = (last_it != deref(last_map).end() - and deref(last_it).second.block_hash == block_hash) - if has_last: - old = &deref(last_it).second - line_entries = &deref(line_it).second - # Indexing creates a zeroed entry if there isn't one yet, so - # this gets or creates it in a single lookup - # Note: explicitly `deref()`-ing here causes the new values - # to be assigned to a temp var; - # meanwhile, directly dot-accessing a pointer causes Cython - # to correctly write `ptr->attr = (ptr->attr + incr)` - entry = &deref(line_entries)[old.f_lineno] - entry.code = code_hash - entry.lineno = old.f_lineno - entry.nhits += 1 - entry.total_time += time - old.time - if is_line_event: - # Get the time again. This way, we don't record much - # time wasted in this function. - old.f_lineno = lineno - old.time = hpTimer() + frame_key = py_frame else: - # We are returning from a function, not executing a - # line. Delete the last_time record. - deref(last_map).erase(last_it) - elif is_line_event: - # (Replacing the stale record, if any) - deref(last_map)[last_time_key] = LastTime( - lineno, hpTimer(), block_hash) - elif last_it != deref(last_map).end(): - # Returning from a function with a stale record; delete it - # all the same - deref(last_map).erase(last_it) - # Otherwise there is no last_time record to delete; it may have - # already been deleted if we are profiling a generator that is - # being pumped past its end. + frame_key = get_current_frame_key() + if is_line_event: + last = entry.get_last_line(ident, frame_key) + else: + last = entry.find_last_line(ident, frame_key) + if last == NULL: + # There is no last_time record to delete; it may have + # already been deleted if we are profiling a generator + # that is being pumped past its end. + continue + if last.line_index >= 0: + line = &entry.lines[last.line_index] + line.nhits += 1 + line.total_time += time - last.time + if is_line_event: + last.line_index = index + # Get the time again. This way, we don't record much time + # wasted in this function. + last.time = hpTimer() + else: + # We are returning from a function, not executing a line. + # Delete the last_time record (freeing the slot). + last.line_index = -1 # (We only get the time if some instance is profiling the location) return has_time @@ -1107,12 +1189,21 @@ pystate.h#L16 # the restoration with `sys.settrace()` if manager_._set_frame_local_trace: set_local_trace(manager_, py_frame) + # If the code isn't being profiled, don't generate line events for + # the frame at all (unless the wrapped callbacks may need them) + if not manager_._wrap_trace: + code = PyFrame_GetCode(py_frame) + if not is_profiled(get_block(code), &manager_._active): + c_disable_line_events(py_frame) + Py_XDECREF(code) elif what == PyTrace_LINE or what == PyTrace_RETURN: code = PyFrame_GetCode(py_frame) + # Note: the line number isn't needed for return events inner_trace_callback((what == PyTrace_LINE), &manager_._active, code, - PyFrame_GetLineNumber(py_frame), + PyFrame_GetLineNumber(py_frame) + if what == PyTrace_LINE else -1, py_frame) Py_XDECREF(code) @@ -1123,13 +1214,13 @@ pystate.h#L16 # `_LineProfilerManager` or a wrapper object (see # `set_local_trace()`), wrap the callback call to make sure that # we don't recurse back here + # Note: `call_callback()` reports errors through its return value + # (it doesn't raise), so there's no need for a try-finally manager_.recursion_guard = 1 - try: - result = call_callback( - disable_line_events, manager_.legacy_callback, - py_frame, what, arg) - finally: - manager_.recursion_guard = recursion_guard + result = call_callback( + _disable_line_events, manager_.legacy_callback, + py_frame, what, arg) + manager_.recursion_guard = recursion_guard else: result = 0 diff --git a/line_profiler/_map_helpers.pxd b/line_profiler/_map_helpers.pxd deleted file mode 100644 index fc49fc57..00000000 --- a/line_profiler/_map_helpers.pxd +++ /dev/null @@ -1,27 +0,0 @@ -# cython: language_level=3 -# cython: legacy_implicit_noexcept=True -# used in _line_profiler.pyx -from libcpp.unordered_map cimport unordered_map - -# long long int is at least 64 bytes assuming c99 -ctypedef long long int int64 - -cdef extern from "Python_wrapper.h": - ctypedef long long PY_LONG_LONG - -cdef struct LastTime: - int f_lineno - PY_LONG_LONG time - long long block_hash - -cdef struct LineTime: - long long code - int lineno - PY_LONG_LONG total_time - long nhits - -# Types used for mappings to last/line times; LastTimeMap is keyed by frame -# address (Python code) or code-block hash (Cython code under -# sys.monitoring), LineTimeMap by line hash. -ctypedef unordered_map[int64, LastTime] LastTimeMap -ctypedef unordered_map[int64, LineTime] LineTimeMap diff --git a/line_profiler/_profile_data.hpp b/line_profiler/_profile_data.hpp new file mode 100644 index 00000000..3ae783eb --- /dev/null +++ b/line_profiler/_profile_data.hpp @@ -0,0 +1,204 @@ +// Profiling data structures for line_profiler's trace callbacks. +// +// Instead of hash maps keyed by (code block, line number) hashes, the +// data of each profiler for each code block is an array indexed by line +// number, and code objects find their block through a pointer cached on +// the code object itself (see CodeCache), so recording a line event is +// only a few pointer dereferences and array accesses. + +#ifndef LINE_PROFILER_PROFILE_DATA_HPP +#define LINE_PROFILER_PROFILE_DATA_HPP + +#include +#include + +#include "Python_wrapper.h" + +namespace line_profiler { + +// Timings of one line of one code block, for one profiler +struct LineData { + long nhits; + PY_LONG_LONG total_time; + // Whether the line is one of the code's lines (only those are + // profiled) + bool registered; +}; + +// A thread's record of the line last executed in a frame of a code block +// (whose time is to be recorded at the next event in the frame) +struct LastLine { + unsigned long thread_ident; + // Identifies the frame (see inner_trace_callback() in + // _line_profiler.pyx), so that the records of recursive calls don't + // overwrite each other's + const void *frame_key; + // Index into ProfileEntry.lines, -1 if there's no record (in which + // case the slot is free for the thread's other frames) + int line_index; + PY_LONG_LONG time; +}; + +// A profiler's data for a code block +struct ProfileEntry { + PyObject *profiler; // Borrowed + Py_hash_t block_hash; + // Line number of lines[0] + int first_line; + std::vector lines; + // Usually just the one frame of one thread, so a linear search is the + // fastest + std::vector last_lines; + + ProfileEntry(PyObject *profiler, Py_hash_t block_hash) + : profiler(profiler), block_hash(block_hash), first_line(0) {} + + // Returns: whether the line wasn't already registered + bool register_line(int lineno) + { + if (lines.empty()) { + first_line = lineno; + lines.resize(1, LineData{0, 0, false}); + } else if (lineno < first_line) { + lines.insert(lines.begin(), first_line - lineno, + LineData{0, 0, false}); + // The record indices shift with the lines + for (LastLine &last : last_lines) { + if (last.line_index >= 0) { + last.line_index += first_line - lineno; + } + } + first_line = lineno; + } else if (lineno - first_line >= (int)lines.size()) { + lines.resize(lineno - first_line + 1, LineData{0, 0, false}); + } + LineData &line = lines[lineno - first_line]; + if (line.registered) return false; + line.registered = true; + return true; + } + + // Returns: the index of lineno in lines if it is registered, else + // -1 + inline int line_index(int lineno) const + { + int index = lineno - first_line; + if (index < 0 || index >= (int)lines.size() + || !lines[index].registered) { + return -1; + } + return index; + } + + // Returns: the record of the thread for the frame, NULL if there + // isn't any + // Note: the records are searched from the back, where those of the + // innermost frames of recursive calls are + inline LastLine *find_last_line(unsigned long thread_ident, + const void *frame_key) + { + for (auto it = last_lines.rbegin(); it != last_lines.rend(); ++it) { + if (it->line_index >= 0 && it->thread_ident == thread_ident + && it->frame_key == frame_key) { + return &*it; + } + } + return NULL; + } + + // Returns: the record of the thread for the frame if there is one, + // else a free slot for it (with line_index -1) + inline LastLine *get_last_line(unsigned long thread_ident, + const void *frame_key) + { + LastLine *free_slot = NULL; + for (auto it = last_lines.rbegin(); it != last_lines.rend(); ++it) { + if (it->thread_ident != thread_ident) continue; + if (it->line_index < 0) { + if (free_slot == NULL) free_slot = &*it; + } else if (it->frame_key == frame_key) { + return &*it; + } + } + if (free_slot == NULL) { + last_lines.push_back(LastLine{thread_ident, frame_key, -1, 0}); + return &last_lines.back(); + } + free_slot->frame_key = frame_key; + return free_slot; + } + + // Returns: whether the thread has any slots (i.e. it has run the + // block while it was profiled) + inline bool has_thread(unsigned long thread_ident) const + { + for (const LastLine &last : last_lines) { + if (last.thread_ident == thread_ident) return true; + } + return false; + } + + // Returns: the thread's record of the most recently executed line, + // NULL if there isn't any + inline const LastLine *latest_last_line(unsigned long thread_ident) const + { + const LastLine *latest = NULL; + for (const LastLine &last : last_lines) { + if (last.line_index >= 0 && last.thread_ident == thread_ident + && (latest == NULL || last.time > latest->time)) { + latest = &last; + } + } + return latest; + } + + // Drop all the records of the thread + inline void clear_last_lines(unsigned long thread_ident) + { + for (LastLine &last : last_lines) { + if (last.thread_ident == thread_ident) last.line_index = -1; + } + } +}; + +// The data of all profilers for a code block, i.e. the code objects with +// the same block hash +struct Block { + Py_hash_t block_hash; + // The profilers' data, usually just the one + std::vector entries; + + explicit Block(Py_hash_t block_hash) : block_hash(block_hash) {} +}; + +// Cached on each code object seen by the trace callbacks (see PEP 523 +// "co_extra"), so that its block is found without hashing its bytecode +struct CodeCache { + // Value of the registry generation when block was looked up; the + // generation changes when new blocks are added, since a code object + // without a block may then have one + unsigned long generation; + Py_hash_t block_hash; + Block *block; // NULL if the code isn't being profiled +}; + +// freefunc for the CodeCache objects +inline void free_code_cache(void *cache) +{ + std::free(cache); +} + +inline CodeCache *new_code_cache(Py_hash_t block_hash) +{ + CodeCache *cache = (CodeCache *)std::malloc(sizeof(CodeCache)); + if (cache != NULL) { + cache->generation = 0; + cache->block_hash = block_hash; + cache->block = NULL; + } + return cache; +} + +} // namespace line_profiler + +#endif // LINE_PROFILER_PROFILE_DATA_HPP diff --git a/line_profiler/_profile_data.pxd b/line_profiler/_profile_data.pxd new file mode 100644 index 00000000..0618d02f --- /dev/null +++ b/line_profiler/_profile_data.pxd @@ -0,0 +1,54 @@ +# cython: language_level=3 +# Declarations for _profile_data.hpp; used in _line_profiler.pyx and +# _profiler_base.pyx +from libcpp cimport bool as cbool +from libcpp.vector cimport vector + +cdef extern from "Python_wrapper.h": + ctypedef struct PyObject + ctypedef Py_ssize_t Py_hash_t + ctypedef long long PY_LONG_LONG + +cdef extern from "_profile_data.hpp" namespace "line_profiler": + cdef cppclass LineData: + long nhits + PY_LONG_LONG total_time + cbool registered + + cdef cppclass LastLine: + unsigned long thread_ident + const void *frame_key + int line_index + PY_LONG_LONG time + + cdef cppclass ProfileEntry: + PyObject *profiler + Py_hash_t block_hash + int first_line + vector[LineData] lines + vector[LastLine] last_lines + + ProfileEntry(PyObject *profiler, Py_hash_t block_hash) + cbool register_line(int lineno) + int line_index(int lineno) + LastLine *find_last_line(unsigned long thread_ident, + const void *frame_key) + LastLine *get_last_line(unsigned long thread_ident, + const void *frame_key) + cbool has_thread(unsigned long thread_ident) + const LastLine *latest_last_line(unsigned long thread_ident) + void clear_last_lines(unsigned long thread_ident) + + cdef cppclass Block: + Py_hash_t block_hash + vector[ProfileEntry *] entries + + Block(Py_hash_t block_hash) + + cdef cppclass CodeCache: + unsigned long generation + Py_hash_t block_hash + Block *block + + void free_code_cache(void *cache) + CodeCache *new_code_cache(Py_hash_t block_hash) diff --git a/line_profiler/_profiler_base.pxd b/line_profiler/_profiler_base.pxd index 4edfa871..b1756a24 100644 --- a/line_profiler/_profiler_base.pxd +++ b/line_profiler/_profiler_base.pxd @@ -1,9 +1,9 @@ # cython: language_level=3 # cython: legacy_implicit_noexcept=True # used in _line_profiler.pyx -from libcpp.unordered_map cimport unordered_map +from libcpp.vector cimport vector -from ._map_helpers cimport LastTimeMap, LineTimeMap +from ._profile_data cimport Py_hash_t, ProfileEntry # long long int is at least 64 bytes assuming c99 ctypedef unsigned long long int uint64 @@ -12,7 +12,9 @@ ctypedef long long int int64 cdef inline int64 compute_line_hash(uint64 block_hash, uint64 linenum) noexcept: """ - Compute the hash used to store each line timing in an unordered_map. + Compute the hash identifying each line of a code block, as found in + :py:attr:`LineProfiler.code_hash_map` and + :py:attr:`LineProfiler.c_code_map`. This is fairly simple, and could use some improvement since linenum isn't technically random, however it seems to be good enough and fast enough for any practical purposes. @@ -25,16 +27,22 @@ cdef inline int64 compute_line_hash(uint64 block_hash, uint64 linenum) noexcept: # Bookkeeping for the sys.monitoring.DISABLE-ing of events at code # locations which aren't being profiled (see _line_profiler.pyx) cdef void note_events_disabled() noexcept -cdef void restart_disabled_events() +cdef void restart_disabled_events() noexcept cdef class _LineProfilerBase: - cdef unordered_map[int64, LineTimeMap] _c_code_map - # Mapping between thread-id and map of LastTime - cdef unordered_map[int64, LastTimeMap] _c_last_time + # The profiling data of each code block (owned by the instance; also + # referenced by the blocks in the registry in _line_profiler.pyx) + cdef vector[ProfileEntry *] _entries + # Idents of the threads on which the profiler has been disabled (see + # .c_last_time) + cdef set _disabled_threads + # Factor converting the raw timings to .timer_units + cdef double _ticks_to_units cdef public list functions cdef public dict code_hash_map, dupes_map cdef public double timer_unit cdef public object threaddata cpdef add_function(self, func) + cdef list _register_lines(self, Py_hash_t block_hash, object linenos) diff --git a/line_profiler/_profiler_base.pyx b/line_profiler/_profiler_base.pyx index 0a2f073b..8f8776a2 100644 --- a/line_profiler/_profiler_base.pyx +++ b/line_profiler/_profiler_base.pyx @@ -5,11 +5,12 @@ # distutils: include_dirs = python25.pxd r""" The profiling data and the (non-tracing) bookkeeping of -line_profiler._line_profiler.LineProfiler. +:py:class:`line_profiler._line_profiler.LineProfiler`. """ from sys import byteorder import sys from cpython.version cimport PY_VERSION_HEX +from ._profile_data cimport LastLine, LineData, ProfileEntry import threading import opcode import os @@ -59,14 +60,14 @@ cdef void note_events_disabled() noexcept: _events_disabled = True -cdef void restart_disabled_events(): +cdef void restart_disabled_events() noexcept: """ Re-enable the sys.monitoring events disabled at code locations, if any. This is needed when the set of profiled code changes, since code which we disabled the events for may now need to be profiled. Note: - sys.monitoring.restart_events() re-enables disabled + :py:func:`sys.monitoring.restart_events` re-enables disabled events for all tools, but tools are supposed to cope with that (they just disable the events again). """ @@ -76,6 +77,16 @@ cdef void restart_disabled_events(): sys.monitoring.restart_events() +cdef inline long long to_units(long long ticks, double ticks_to_units): + """ + Convert a raw timing to timer units (see + :py:attr:`_LineProfilerBase._ticks_to_units`). + """ + if ticks_to_units == 1.0: + return ticks + return (ticks * ticks_to_units + .5) + + cdef inline object multibyte_rstrip(bytes bytecode): """ Returns: @@ -192,12 +203,12 @@ cpdef _copy_local_sysmon_events(old_code, new_code): cdef class _LineProfilerBase: """ - Base class of line_profiler._line_profiler.LineProfiler + Base class of :py:class:`line_profiler._line_profiler.LineProfiler` holding the profiling data, and the methods for setting up and inspecting it. Note: - This is split from line_profiler._line_profiler so + This is split from :py:mod:`line_profiler._line_profiler` so that the extension modules can be compiled in parallel; the trace callbacks there access the C-level data directly. """ @@ -254,7 +265,7 @@ datamodel.html#user-defined-functions # handling on the non-Cython branch. # XXX: tests for the above assertion if necessary co_code: bytes = code.co_code - code_hashes = [] + cdef Py_hash_t block_hash if any(co_code): # Normal Python functions # Figure out how much padding we need and strip the bytecode # Notes: @@ -292,11 +303,9 @@ datamodel.html#user-defined-functions # inefficient # See if this can be sped up by not needing to iterate over # every byte - for offset, _ in enumerate(co_code): - code_hashes.append( - compute_line_hash( - hash(co_code), - PyCode_Addr2Line(code, offset))) + block_hash = hash(co_code) + linenos = [PyCode_Addr2Line(code, offset) + for offset in range(len(co_code))] else: # Cython functions have empty/zero bytecodes if CANNOT_LINE_TRACE_CYTHON: return @@ -313,32 +322,43 @@ datamodel.html#user-defined-functions return nlines = len(get_code_block(cython_source, lineno)) block_hash = hash(code) - for lineno in range(lineno, lineno + nlines): - code_hash = compute_line_hash(block_hash, lineno) - code_hashes.append(code_hash) + linenos = range(lineno, lineno + nlines) # We can't replace the code object on Cython functions, but # we can *store* a copy with the correct metadata # Note: we don't use `_copy_local_sysmon_events()` here # because Cython shim code objects don't support local # events code = code.replace(co_filename=cython_source) - # Update `._c_code_map` and `.code_hash_map` with the new line - # hashes + # Register the lines for profiling, and update .code_hash_map + # with the hashes of the new ones try: line_hashes = self.code_hash_map[code] except KeyError: line_hashes = self.code_hash_map[code] = [] - for code_hash in code_hashes: - line_hash = code_hash - if not self._c_code_map.count(line_hash): - line_hashes.append(line_hash) - self._c_code_map[line_hash] + for lineno in self._register_lines(block_hash, linenos): + # Note: PyCode_Addr2Line() gives -1 for the offsets without + # a line, which is converted like before + line_hashes.append(compute_line_hash(block_hash, lineno)) self.functions.append(func) # The events of the code may have been disabled while it wasn't # being profiled restart_disabled_events() + cdef list _register_lines(self, Py_hash_t block_hash, object linenos): + """ + Set up the profiling data for the lines of a code block. + + Returns: + The line numbers which weren't already registered (in + order, without duplicates) + + Note: + Implemented in :py:class:`line_profiler._line_profiler.LineProfiler`, + which keeps the registry of the code blocks. + """ + raise NotImplementedError + property enable_count: def __get__(self): if not hasattr(self.threaddata, 'enable_count'): @@ -370,16 +390,45 @@ datamodel.html#user-defined-functions def __exit__(self, exc_type, exc_val, exc_tb): self.disable_by_count() + def __cinit__(self, *args, **kwargs): + self._disabled_threads = set() + self._ticks_to_units = 1.0 + @property def c_code_map(self): """ - A Python view of the internal C lookup table. + A Python view of the internal profiling data: a mapping from the + hash of each profiled line (see :py:attr:`~.code_hash_map`) to + the timing entries of the line, if it has been run. """ - return self._c_code_map + cdef ProfileEntry *entry + cdef LineData *line + cdef size_t i, j + c_code_map = {} + for i in range(self._entries.size()): + entry = self._entries[i] + for j in range(entry.lines.size()): + line = &entry.lines[j] + if not line.registered: + continue + lineno = entry.first_line + j + code_hash = compute_line_hash(entry.block_hash, lineno) + entries = c_code_map.setdefault(code_hash, {}) + if line.nhits: + entries[lineno] = { + 'code': code_hash, + 'lineno': lineno, + 'total_time': to_units(line.total_time, self._ticks_to_units), + 'nhits': line.nhits} + return c_code_map @property def c_last_time(self): """ + A Python view of the internal record of the line last executed + in each code block on the current thread: a mapping from the + block hash to ``{'f_lineno': ..., 'time': ...}``. + Raises: KeyError: If no profiling data is available on the current thread. @@ -389,20 +438,27 @@ datamodel.html#user-defined-functions by block hash as before; if a function has more than one active frame, the record of the innermost one is used. """ - try: - records = (self._c_last_time)[PyThread_get_thread_ident()] - except KeyError as e: - # We haven't actually profiled anything yet - raise (KeyError('No profiling data on the current thread ' - '(`threading.get_ident()` = ' - f'{threading.get_ident()})') - .with_traceback(e.__traceback__)) from None + cdef ProfileEntry *entry + cdef const LastLine *last + cdef size_t i + cdef unsigned long ident = PyThread_get_thread_ident() + has_data = ident in self._disabled_threads c_last_time = {} - for record in records.values(): - block_hash = record.pop('block_hash') - current = c_last_time.get(block_hash) - if current is None or record['time'] > current['time']: - c_last_time[block_hash] = record + for i in range(self._entries.size()): + entry = self._entries[i] + if not entry.has_thread(ident): + continue + has_data = True + last = entry.latest_last_line(ident) + if last != NULL: + c_last_time[entry.block_hash] = { + 'f_lineno': entry.first_line + last.line_index, + 'time': to_units(last.time, self._ticks_to_units)} + if not has_data: + # We haven't actually profiled anything yet + raise KeyError('No profiling data on the current thread ' + '(`threading.get_ident()` = ' + f'{threading.get_ident()})') return c_last_time @property diff --git a/line_profiler/_sys_monitoring.pxd b/line_profiler/_sys_monitoring.pxd index 09b22532..60738301 100644 --- a/line_profiler/_sys_monitoring.pxd +++ b/line_profiler/_sys_monitoring.pxd @@ -8,12 +8,17 @@ cdef extern from "c_trace_callbacks.h": cdef class _SysMonitoringState: cdef int tool_id + cdef object tool_id_obj # (The same as a Python int) cdef object name # type: str | None - # type: dict[int, Callable | None], int = event id - cdef dict callbacks - # type: dict[int, set[tuple[code, Unpack[tuple]]]], - # int = event id, tuple = - cdef dict disabled + # The following are per line-tracing event (`LINE`, `PY_RETURN`, + # `PY_YIELD`, `RAISE`, and `RERAISE`, in that order): + # - Our callbacks; type: list[Callable | None] + cdef list own_callbacks + # - The callbacks we wrap around; type: list[Callable | None] + cdef list callbacks + # - The locations at which they returned `sys.monitoring.DISABLE`; + # type: list[set[tuple[code, int]] | None] + cdef list disabled cdef int events cdef Py_uintptr_t restart_version @@ -21,5 +26,5 @@ cdef class _SysMonitoringState: object handle_return, object handle_yield, object handle_raise, object handle_reraise) cpdef deregister(self) - cdef void call_callback(self, int event_id, object code, - object loc_args, object other_args) noexcept + cdef void call_callback(self, int event_id, object code, int loc, + object arg, bint has_arg) noexcept diff --git a/line_profiler/_sys_monitoring.pyx b/line_profiler/_sys_monitoring.pyx index 97e6b318..4d43209d 100644 --- a/line_profiler/_sys_monitoring.pyx +++ b/line_profiler/_sys_monitoring.pyx @@ -4,11 +4,11 @@ # distutils: language=c++ # distutils: include_dirs = python25.pxd r""" -Bookkeeping for wrapping around other tools' sys.monitoring -callbacks, used by line_profiler._line_profiler. +Bookkeeping for wrapping around other tools' :py:mod:`sys.monitoring` +callbacks, used by :py:mod:`line_profiler._line_profiler`. Note: - This is split from line_profiler._line_profiler so that the + This is split from :py:mod:`line_profiler._line_profiler` so that the extension modules can be compiled in parallel; none of this is on the hot path unless other tools' callbacks are being wrapped. """ @@ -29,19 +29,42 @@ cdef extern from "c_trace_callbacks.h": _CAN_USE_SYS_MONITORING = PY_VERSION_HEX >= 0x030c00b1 -cdef inline object get_current_callback(int tool_id, int event_id): - """ - Note: - Unfortunately there's no public API for directly retrieving - the current callback, no matter on the Python side or the C - side. This may become a performance bottleneck... - """ - mon = sys.monitoring - cdef object register = mon.register_callback - cdef object result = register(tool_id, event_id, None) - if result is not None: - register(tool_id, event_id, result) - return result +# The `sys.monitoring` API and constants, looked up once here rather than +# on every call of `_SysMonitoringState.call_callback()` +cdef object _register_callback = None +cdef object _get_events = None +cdef object _set_events = None +cdef object _get_local_events = None +cdef object _get_tool = None +cdef object _use_tool_id = None +cdef object _free_tool_id = None +cdef object _DISABLE = None +# The events used for line profiling, in the order of the arguments of +# `_SysMonitoringState.register()` +cdef int _NUM_LINE_TRACING_EVENTS = 5 +cdef int _LINE_TRACING_EVENT_IDS[5] +# The same as Python ints, so that they don't have to be created anew +# for every call into `sys.monitoring` +cdef tuple _LINE_TRACING_EVENT_ID_OBJS = () +cdef int _LINE_TRACING_EVENTS = 0 +if _CAN_USE_SYS_MONITORING: + _register_callback = sys.monitoring.register_callback + _get_events = sys.monitoring.get_events + _set_events = sys.monitoring.set_events + _get_local_events = sys.monitoring.get_local_events + _get_tool = sys.monitoring.get_tool + _use_tool_id = sys.monitoring.use_tool_id + _free_tool_id = sys.monitoring.free_tool_id + _DISABLE = sys.monitoring.DISABLE + for _i, _event_id in enumerate([sys.monitoring.events.LINE, + sys.monitoring.events.PY_RETURN, + sys.monitoring.events.PY_YIELD, + sys.monitoring.events.RAISE, + sys.monitoring.events.RERAISE]): + _LINE_TRACING_EVENT_IDS[_i] = _event_id + _LINE_TRACING_EVENTS |= _event_id + _LINE_TRACING_EVENT_ID_OBJS += (_event_id,) + del _i, _event_id cpdef int _patch_events(int events, int before, int after) noexcept: @@ -88,9 +111,11 @@ cdef class _SysMonitoringState: def __init__(self, tool_id: int): self.tool_id = tool_id + self.tool_id_obj = tool_id self.name = None - self.callbacks = {} - self.disabled = {} + self.own_callbacks = [None] * _NUM_LINE_TRACING_EVENTS + self.callbacks = [None] * _NUM_LINE_TRACING_EVENTS + self.disabled = [None] * _NUM_LINE_TRACING_EVENTS self.events = 0 # NO_EVENTS self.restart_version = monitoring_restart_version() @@ -105,7 +130,7 @@ cdef class _SysMonitoringState: # in use (e.g. activated via `.use_tool_id()`), and # - That doesn't result in much (< 2%) performance improvement # in tests - mon = sys.monitoring + cdef int i # Set prior state # Note: in 3.14.0a1+, calling `sys.monitoring.free_tool_id()` @@ -114,84 +139,91 @@ cdef class _SysMonitoringState: # this does have the side effect of not overriding the active # profiling tool name if one is already in use, but it's # probably better this way - self.name = mon.get_tool(self.tool_id) + self.name = _get_tool(self.tool_id) if self.name is None: - self.events = mon.events.NO_EVENTS - mon.use_tool_id(self.tool_id, 'line_profiler') + self.events = 0 # NO_EVENTS + _use_tool_id(self.tool_id, 'line_profiler') else: - self.events = mon.get_events(self.tool_id) - mon.set_events(self.tool_id, self.events | self.line_tracing_events) + self.events = _get_events(self.tool_id) + _set_events(self.tool_id, self.events | _LINE_TRACING_EVENTS) # Register tracebacks and remember the existing ones - for event_id, callback in [(mon.events.LINE, handle_line), - (mon.events.PY_RETURN, handle_return), - (mon.events.PY_YIELD, handle_yield), - (mon.events.RAISE, handle_raise), - (mon.events.RERAISE, handle_reraise)]: - self.callbacks[event_id] = mon.register_callback( - self.tool_id, event_id, callback) + self.own_callbacks = [handle_line, handle_return, handle_yield, + handle_raise, handle_reraise] + for i in range(_NUM_LINE_TRACING_EVENTS): + self.callbacks[i] = _register_callback( + self.tool_id, _LINE_TRACING_EVENT_IDS[i], + self.own_callbacks[i]) cpdef deregister(self): - mon = sys.monitoring - cdef dict wrapped_callbacks = self.callbacks + cdef int i # Restore prior state - mon.set_events(self.tool_id, self.events) + _set_events(self.tool_id, self.events) if self.name is None: - mon.free_tool_id(self.tool_id) + _free_tool_id(self.tool_id) self.name = None - self.events = mon.events.NO_EVENTS + self.events = 0 # NO_EVENTS # Reset tracebacks - while wrapped_callbacks: - mon.register_callback(self.tool_id, *wrapped_callbacks.popitem()) + for i in range(_NUM_LINE_TRACING_EVENTS): + _register_callback(self.tool_id, _LINE_TRACING_EVENT_IDS[i], + self.callbacks[i]) + self.callbacks[i] = None + self.own_callbacks[i] = None - cdef void call_callback(self, int event_id, object code, - object loc_args, object other_args) noexcept: + cdef void call_callback(self, int event_id, object code, int loc, + object arg, bint has_arg) noexcept: """ - Call the appropriate stored callback. Also take care of the - restoration of :py:mod:`sys.monitoring` callbacks, tool-ID lock, - and events should they be unset. + Call the appropriate stored callback with ``(code, loc)`` (or + ``(code, loc, arg)`` if ``has_arg`` is true). Also take care of + the restoration of :py:mod:`sys.monitoring` callbacks, tool-ID + lock, and events should they be unset. Note: - * This is deliberately made a non-traceable C method so that - we don't fall info infinite recursion. - * ``loc_args`` and ``other_args`` should be tuples. + This is deliberately made a non-traceable C method so that + we don't fall info infinite recursion. """ - mon = sys.monitoring cdef PyObject *result = NULL cdef object callback # type: Callable | None cdef object callback_after # type: Callable | None - cdef object code_location # type: tuple[code, Unpack[tuple]] + cdef object code_location # type: tuple[code, int] cdef object arg_tuple # type: tuple[code, Unpack[tuple]] - cdef object disabled # type: set[tuple[code, Unpack[tuple]]] - cdef int ev_id, events_before + cdef set disabled # type: set[tuple[code, int]] | None + cdef int i, index = -1, events_before = 0, events_after + cdef bint reacquired = False cdef Py_uintptr_t version = monitoring_restart_version() - cdef dict callbacks_before = {} + + for i in range(_NUM_LINE_TRACING_EVENTS): + if _LINE_TRACING_EVENT_IDS[i] == event_id: + index = i + break + if index < 0: + return # If we've restarted events, clear the `.disabled` registry if version != self.restart_version: self.restart_version = version - self.disabled.clear() + for i in range(_NUM_LINE_TRACING_EVENTS): + self.disabled[i] = None # Call the wrapped callback where suitable - callback = self.callbacks.get(event_id) + callback = self.callbacks[index] if callback is None: # No cached callback return - code_location = (code,) + loc_args - disabled = self.disabled.setdefault(event_id, set()) - if code_location in disabled: # Events 'disabled' for the loc - return - if not (self.events # Callback should not receive the event - | mon.get_local_events(self.tool_id, code)) & event_id: - return - - for ev_id in self.line_tracing_event_set: - callbacks_before[ev_id] = get_current_callback(self.tool_id, ev_id) + code_location = (code, loc) + disabled = self.disabled[index] + if disabled is not None and code_location in disabled: + return # Events 'disabled' for the loc + # Callback should not receive the event (only check the local + # events if the global ones don't include it) + if not (self.events & event_id): + if not (_get_local_events(self.tool_id_obj, code) & event_id): + return - arg_tuple = code_location + other_args + arg_tuple = (code, loc, arg) if has_arg else code_location try: - events_before = mon.get_events(self.tool_id) + events_before = _get_events(self.tool_id_obj) result = PyObject_Call( # Note: DECREF needed below callback, arg_tuple, NULL) else: @@ -199,24 +231,31 @@ cdef class _SysMonitoringState: # profiling will be interrupted), just mark the location so # that we stop calling the cached callback until the next # time `sys.monitoring.restart_events()` is called - if result == (mon.DISABLE): + if result == _DISABLE: + if disabled is None: + disabled = self.disabled[index] = set() disabled.add(code_location) finally: Py_XDECREF(result) # Update the events + events_after = _get_events(self.tool_id_obj) self.events = _patch_events( - self.events, events_before, mon.get_events(self.tool_id)) - # If the wrapped callback has changed: - register = mon.register_callback - for ev_id, callback in callbacks_before.items(): - # - Restore the `sys.monitoring` callback - callback_after = register(self.tool_id, ev_id, callback) - # - Remember the updated callback in `self.callbacks` - if callback is not callback_after: - self.callbacks[ev_id] = callback_after + self.events, events_before, events_after) + # If the wrapped callback has replaced any of our callbacks: + # - Restore ours, and + # - Remember the replacement as the callback to wrap + for i in range(_NUM_LINE_TRACING_EVENTS): + callback_after = _register_callback( + self.tool_id_obj, _LINE_TRACING_EVENT_ID_OBJS[i], + self.own_callbacks[i]) + if callback_after is not self.own_callbacks[i]: + self.callbacks[i] = callback_after # Reset the tool ID lock if released - if not mon.get_tool(self.tool_id): - mon.use_tool_id(self.tool_id, 'line_profiler') + if not _get_tool(self.tool_id_obj): + _use_tool_id(self.tool_id_obj, 'line_profiler') + reacquired = True # Restore the `sys.monitoring` events if unset - mon.set_events(self.tool_id, - self.events | self.line_tracing_events) + if reacquired or events_after != ( + self.events | _LINE_TRACING_EVENTS): + _set_events(self.tool_id_obj, + self.events | _LINE_TRACING_EVENTS) diff --git a/line_profiler/c_trace_callbacks.c b/line_profiler/c_trace_callbacks.c index 764b6003..ff22a8e0 100644 --- a/line_profiler/c_trace_callbacks.c +++ b/line_profiler/c_trace_callbacks.c @@ -272,6 +272,13 @@ void set_local_trace(PyObject *manager, PyFrameObject *py_frame) return; } +void disable_line_events(PyFrameObject *py_frame) +{ + /* Stop line events from being generated for the frame (e.g. because + * its code isn't being profiled) */ + py_frame->f_trace_lines = 0; +} + Py_uintptr_t monitoring_restart_version() #if PY_VERSION_HEX >= 0x030c00b1 // 3.12.0b1 { diff --git a/line_profiler/c_trace_callbacks.h b/line_profiler/c_trace_callbacks.h index e13c55c3..550f1b92 100644 --- a/line_profiler/c_trace_callbacks.h +++ b/line_profiler/c_trace_callbacks.h @@ -38,6 +38,7 @@ int call_callback( PyObject *arg ); void set_local_trace(PyObject *manager, PyFrameObject *py_frame); +void disable_line_events(PyFrameObject *py_frame); Py_uintptr_t monitoring_restart_version(); #ifdef __cplusplus diff --git a/line_profiler/timers.c b/line_profiler/timers.c index f13c7336..86f81dbc 100644 --- a/line_profiler/timers.c +++ b/line_profiler/timers.c @@ -56,14 +56,42 @@ hpTimerUnit(void) #include #include -PY_LONG_LONG -hpTimer(void) +/* + * On x86-64 Linux, time with the CPU's timestamp counter (TSC) where it + * is reliable, which is much cheaper to read than clock_gettime() + * (~7ns vs. ~17ns, and there are two reads per line event). + */ +#if defined(__linux__) && defined(__x86_64__) \ + && (defined(__GNUC__) || defined(__clang__)) +# define LINE_PROFILER_HAVE_TSC 1 +# include +# include +# include +# include +#endif + +#ifdef LINE_PROFILER_HAVE_TSC +static int lp_use_tsc = 0; +#endif +/* Nanoseconds per tick of hpTimer() */ +static double lp_ticks_to_ns = 1.0; + +static inline PY_LONG_LONG +clock_ns(void) { struct timespec ts; - PY_LONG_LONG ret; clock_gettime(CLOCK_MONOTONIC, &ts); - ret = (PY_LONG_LONG)ts.tv_sec * 1000000000 + (PY_LONG_LONG)ts.tv_nsec; - return ret; + return (PY_LONG_LONG)ts.tv_sec * 1000000000 + (PY_LONG_LONG)ts.tv_nsec; +} + +static inline PY_LONG_LONG +hpTimer(void) +{ +#ifdef LINE_PROFILER_HAVE_TSC + if (lp_use_tsc) + return (PY_LONG_LONG)__rdtsc(); +#endif + return clock_ns(); } double @@ -72,4 +100,93 @@ hpTimerUnit(void) return 0.000000001; } +#ifdef LINE_PROFILER_HAVE_TSC +/* + * Whether the TSC ticks at a constant rate (regardless of frequency + * scaling and power states), and the kernel trusts it as its clock source + * (which it doesn't when e.g. the TSCs aren't synchronized between CPUs, + * as on some VMs). + */ +static int +tsc_is_reliable(void) +{ + unsigned int eax, ebx, ecx, edx; + char clocksource[64] = {0}; + FILE *f; + if (!__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx) + || !(edx & (1u << 8))) + return 0; + f = fopen("/sys/devices/system/clocksource/clocksource0/" + "current_clocksource", "r"); + if (f == NULL) + return 0; + if (fgets(clocksource, sizeof(clocksource), f) == NULL) + clocksource[0] = '\0'; + fclose(f); + return strncmp(clocksource, "tsc", 3) == 0 + && (clocksource[3] == '\n' || clocksource[3] == '\0'); +} +#endif + +/* + * Choose the timer; must be called before hpTimer() is used. + * + * Returns: whether the TSC is used + */ +int +hpTimerInit(int allow_tsc) +{ +#ifdef LINE_PROFILER_HAVE_TSC + PY_LONG_LONG ns_start, ns_end; + unsigned long long ticks_start, ticks_end; + double ticks_to_ns; + lp_use_tsc = 0; + lp_ticks_to_ns = 1.0; + if (!allow_tsc || !tsc_is_reliable()) + return 0; + /* Measure the TSC frequency against the monotonic clock, over + * ~2ms (good to ~1e-5) */ + ns_start = clock_ns(); + ticks_start = __rdtsc(); + do { + ns_end = clock_ns(); + ticks_end = __rdtsc(); + } while (ns_end - ns_start < 2000000); + ticks_to_ns = (double)(ns_end - ns_start) + / (double)(ticks_end - ticks_start); + /* Sanity check: 100MHz to 20GHz */ + if (!(ticks_to_ns > 0.05 && ticks_to_ns < 10.0)) + return 0; + lp_ticks_to_ns = ticks_to_ns; + lp_use_tsc = 1; + return 1; +#else + (void)allow_tsc; + return 0; +#endif +} + +/* Multiply hpTimer() differences by this to convert them to + * hpTimerUnit()s */ +double +hpTimerTicksToUnits(void) +{ + return lp_ticks_to_ns; +} + +#endif + +#if defined(MS_WINDOWS) || (defined(PYOS_OS2) && defined(PYCC_GCC)) +int +hpTimerInit(int allow_tsc) +{ + (void)allow_tsc; + return 0; +} + +double +hpTimerTicksToUnits(void) +{ + return 1.0; +} #endif diff --git a/pyproject.toml b/pyproject.toml index 5be9b725..cf09a729 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,8 @@ sources = [ ] language = "c++" depends = [ - "line_profiler/_map_helpers.pxd", + "line_profiler/_profile_data.hpp", + "line_profiler/_profile_data.pxd", "line_profiler/_profiler_base.pxd", "line_profiler/_sys_monitoring.pxd", "line_profiler/Python_wrapper.h", @@ -110,7 +111,8 @@ sources = [ ] language = "c++" depends = [ - "line_profiler/_map_helpers.pxd", + "line_profiler/_profile_data.hpp", + "line_profiler/_profile_data.pxd", "line_profiler/_profiler_base.pxd", "line_profiler/Python_wrapper.h", ] diff --git a/tests/test_sys_monitoring.py b/tests/test_sys_monitoring.py index f4418372..6155eed3 100644 --- a/tests/test_sys_monitoring.py +++ b/tests/test_sys_monitoring.py @@ -934,7 +934,19 @@ def profile() -> None: nhits = int(line.split()[1]) assert nhits == n + +# -------------------------------------------------------------------- # +# Events disabled at code locations not being profiled # +# -------------------------------------------------------------------- # + + def _make_nested_funcs() -> Tuple[Callable[[int], int], Callable[[int], int]]: + """ + Returns: + inner, outer (tuple[Callable[[int], int], Callable[[int], int]]): + Fresh functions (each with their own code objects), with + ``outer()`` calling ``inner()`` + """ namespace: Dict[str, Any] = {} exec( 'def inner(n):\n' @@ -959,7 +971,7 @@ def _get_total_nhits(prof: LineProfiler, func: Callable) -> int: def test_disabled_events_restarted_on_enable() -> None: """ Check that the events which were disabled (via - sys.monitoring.DISABLE) for code which wasn't being + :py:data:`sys.monitoring.DISABLE`) for code which wasn't being profiled are re-enabled when a profiler profiling it is enabled. """ inner, outer = _make_nested_funcs() From 6282711fa03a16b603e5b15de3e481ad4d3e89d7 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:44:39 -0500 Subject: [PATCH 2/6] Address review comments --- line_profiler/_line_profiler.pyx | 85 +++++++++- line_profiler/_profile_data.hpp | 159 +++++++++++++----- line_profiler/_profile_data.pxd | 9 +- line_profiler/_profiler_base.pyx | 31 +++- line_profiler/c_trace_callbacks.c | 11 ++ line_profiler/c_trace_callbacks.h | 1 + tests/test_line_profiler.py | 263 ++++++++++++++++++++++++++++++ 7 files changed, 505 insertions(+), 54 deletions(-) diff --git a/line_profiler/_line_profiler.pyx b/line_profiler/_line_profiler.pyx index ce6b6d39..d43aae67 100644 --- a/line_profiler/_line_profiler.pyx +++ b/line_profiler/_line_profiler.pyx @@ -124,6 +124,8 @@ cdef extern from "c_trace_callbacks.h": # Legacy tracing cdef void set_local_trace(PyObject *manager, PyFrameObject *py_frame) cdef void c_disable_line_events "disable_line_events"( PyFrameObject *py_frame) + cdef void c_enable_line_events "enable_line_events"( + PyFrameObject *py_frame, PyObject *manager) cdef Py_uintptr_t monitoring_restart_version() cdef extern from "timers.c": @@ -175,12 +177,24 @@ cdef inline Py_hash_t compute_block_hash(object code) noexcept: # Registry of the code blocks being profiled (by any instance), keyed by # block hash: code objects with the same bytecode share the data of the -# block. The blocks are never freed, so that the pointers cached on the -# code objects stay valid. +# block. A block is freed when the last profiler profiling it is +# deallocated, so that the memory used is bounded by what the live +# profilers profile. cdef unordered_map[Py_hash_t, Block *] _blocks -# Incremented when blocks are added, invalidating the cached lookups +# Incremented when blocks are added or removed, invalidating the lookups +# cached on the code objects (see get_block()), so that they never use a +# freed block cdef unsigned long _blocks_generation = 1 + +def _num_registered_blocks(): + """ + Returns: + The number of code blocks in the registry (for checking the + memory use in tests). + """ + return _blocks.size() + # Index of the per-code-object scratch slot (see PEP 523) in which a # CodeCache is stored; negative if none is available cdef Py_ssize_t _CODE_CACHE_INDEX = ( @@ -252,6 +266,36 @@ cdef inline Block *get_block(object code) noexcept: return lookup_block(code, cache) +# Whether legacy_trace_callback() has turned off the line events of any +# frame (see restore_line_events()) +cdef bint _frame_lines_disabled = False + + +cdef restore_line_events(): + """ + With the legacy trace system, turn the line events back on for the + frames currently running (on all threads) whose code is now profiled, + but whose line events :c:func:`legacy_trace_callback` turned off + because it wasn't when they started (e.g. because a function was added + to a profiler, or a profiler was enabled, in the meantime). + + Note: + Suspended generators and coroutines are dealt with when they are + resumed (see :c:func:`legacy_trace_callback`). + """ + if not (USE_LEGACY_TRACE and _frame_lines_disabled): + return + for frame in sys._current_frames().values(): + while frame is not None: + if (not frame.f_trace_lines + and get_block(frame.f_code) != NULL): + f_trace = frame.f_trace + if (f_trace is None + or isinstance(f_trace, _LineProfilerManager)): + frame.f_trace_lines = True + frame = frame.f_back + + def disable_line_events(trace_func: Callable) -> Callable: """ Returns: @@ -571,9 +615,11 @@ line_profiler/blob/main/line_profiler/_line_profiler.pyx cdef bint already_active = not self._active.empty() if not self._add_active(prof): return - if not USE_LEGACY_TRACE: - # The code profiled by prof may have had its events - # disabled while it wasn't being profiled + # The code profiled by prof may have had its events disabled while + # it wasn't being profiled + if USE_LEGACY_TRACE: + restore_line_events() + else: restart_disabled_events() if already_active: return @@ -973,9 +1019,11 @@ cdef class LineProfiler(_LineProfilerBase): self._ticks_to_units = hpTimerTicksToUnits() def __dealloc__(self): + global _blocks_generation cdef ProfileEntry *entry cdef Block *block cdef size_t i, j + cdef bint removed_blocks = False for i in range(self._entries.size()): entry = self._entries[i] block = find_block(entry.block_hash) @@ -984,8 +1032,20 @@ cdef class LineProfiler(_LineProfilerBase): if block.entries[j] == entry: block.entries.erase(block.entries.begin() + j) break + if block.entries.empty(): + # No profiler profiles the block anymore + _blocks.erase(block.block_hash) + del block + removed_blocks = True del entry self._entries.clear() + if removed_blocks: + # Invalidate the pointers to the freed blocks cached on the + # code objects + # Note: the trace callbacks don't hold on to blocks across + # anything which can run Python code (and thus deallocate + # profilers), so there's no dangling pointer in use + _blocks_generation += 1 cdef list _register_lines(self, Py_hash_t block_hash, object linenos): cdef Block *block = get_or_add_block(block_hash) @@ -1006,6 +1066,10 @@ cdef class LineProfiler(_LineProfilerBase): for lineno in linenos: if entry.register_line(lineno): new_linenos.append(lineno) + # The frames running the code may have had their line events + # turned off while it wasn't being profiled (with sys.monitoring, + # add_function() restarts the events instead) + restore_line_events() return new_linenos cpdef disable(self): @@ -1164,6 +1228,7 @@ cdef extern int legacy_trace_callback( https://github.com/python/cpython/blob/de2a4036/Include/cpython/\ pystate.h#L16 """ + global _frame_lines_disabled cdef _LineProfilerManager manager_ = <_LineProfilerManager>manager cdef int result cdef int recursion_guard = manager_.recursion_guard @@ -1191,10 +1256,16 @@ pystate.h#L16 set_local_trace(manager_, py_frame) # If the code isn't being profiled, don't generate line events for # the frame at all (unless the wrapped callbacks may need them) + # Note: this is also the event for resuming a generator or + # coroutine, whose code may have become profiled since its line + # events were turned off if not manager_._wrap_trace: code = PyFrame_GetCode(py_frame) - if not is_profiled(get_block(code), &manager_._active): + if is_profiled(get_block(code), &manager_._active): + c_enable_line_events(py_frame, manager_) + else: c_disable_line_events(py_frame) + _frame_lines_disabled = True Py_XDECREF(code) elif what == PyTrace_LINE or what == PyTrace_RETURN: code = PyFrame_GetCode(py_frame) diff --git a/line_profiler/_profile_data.hpp b/line_profiler/_profile_data.hpp index 3ae783eb..b69cf322 100644 --- a/line_profiler/_profile_data.hpp +++ b/line_profiler/_profile_data.hpp @@ -1,28 +1,112 @@ // Profiling data structures for line_profiler's trace callbacks. // // Instead of hash maps keyed by (code block, line number) hashes, the -// data of each profiler for each code block is an array indexed by line -// number, and code objects find their block through a pointer cached on -// the code object itself (see CodeCache), so recording a line event is -// only a few pointer dereferences and array accesses. +// data of each profiler for each code block is an array of per-line +// timings, found by line number through a small index (see LineIndex), and +// code objects find their block through a pointer cached on the code +// object itself (see CodeCache), so recording a line event is only a few +// pointer dereferences and array accesses. #ifndef LINE_PROFILER_PROFILE_DATA_HPP #define LINE_PROFILER_PROFILE_DATA_HPP +#include #include +#include #include #include "Python_wrapper.h" namespace line_profiler { -// Timings of one line of one code block, for one profiler +// Timings of one (profiled) line of one code block, for one profiler struct LineData { long nhits; PY_LONG_LONG total_time; - // Whether the line is one of the code's lines (only those are - // profiled) - bool registered; + int lineno; +}; + +// Maps the line numbers of a code block to the slots of their LineData +// in ProfileEntry.lines. A slot never changes once assigned. +// The representation is chosen when lines are registered, so that the +// trace callbacks only do a lookup: +// - Usually, the lines of a code block are dense, and a table indexed by +// the line number (relative to the first line) is used; +// - If the line numbers are spread out disproportionately (i.e. the +// table would mostly be empty), a vector of (line number, slot) pairs +// sorted by line number is binary-searched instead, so that the memory +// used is proportional to the number of lines, not their span. +class LineIndex { +public: + // Returns: the slot of the line, -1 if it isn't registered + inline int find(int lineno) const + { + if (dense) { + unsigned offset = (unsigned)(lineno - first_line); + return (offset < (unsigned)table.size()) ? table[offset] : -1; + } + return find_sparse(lineno); + } + + // Note: the line mustn't already be registered + void insert(int lineno, int slot) + { + auto pair = std::make_pair(lineno, slot); + sorted.insert( + std::lower_bound(sorted.begin(), sorted.end(), pair), pair); + int first = sorted.front().first, last = sorted.back().first; + size_t span = (size_t)last - (size_t)first + 1; + if (span > MAX_SPAN_PER_LINE * sorted.size() + MIN_DENSE_SPAN) { + // Too sparse for a table + dense = false; + std::vector().swap(table); + } else if (dense && first == first_line) { + if (span > table.size()) table.resize(span, -1); + table[lineno - first_line] = slot; + } else { // (Re)build the table + dense = true; + first_line = first; + table.assign(span, -1); + for (const auto ®istered : sorted) { + table[registered.first - first] = registered.second; + } + } + } + + // Returns: the number of entries allocated (for checking the memory + // use in tests) + inline size_t num_entries() const { return table.size() + sorted.size(); } + + // Returns: whether it's a table (for tests) + inline bool is_dense() const { return dense; } + + // The size of a table is at most this many entries per line (plus + // MIN_DENSE_SPAN), i.e. 4 bytes per line it'd cost relative to the + // sorted vector + static const size_t MAX_SPAN_PER_LINE = 4; + static const size_t MIN_DENSE_SPAN = 64; + +private: + // (Kept out of line, so that the table lookup inlined into the trace + // callbacks stays small) +#if defined(__GNUC__) || defined(__clang__) + __attribute__((noinline)) +#elif defined(_MSC_VER) + __declspec(noinline) +#endif + int find_sparse(int lineno) const + { + auto it = std::lower_bound( + sorted.begin(), sorted.end(), std::make_pair(lineno, -1)); + return (it != sorted.end() && it->first == lineno) ? it->second : -1; + } + + bool dense = true; + // Line number of table[0] + int first_line = 0; + std::vector table; + // (line number, slot), sorted by line number + std::vector> sorted; }; // A thread's record of the line last executed in a frame of a code block @@ -33,8 +117,8 @@ struct LastLine { // _line_profiler.pyx), so that the records of recursive calls don't // overwrite each other's const void *frame_key; - // Index into ProfileEntry.lines, -1 if there's no record (in which - // case the slot is free for the thread's other frames) + // Slot of the line in ProfileEntry.lines, -1 if there's no record + // (in which case the record is free for the thread's other frames) int line_index; PY_LONG_LONG time; }; @@ -43,51 +127,45 @@ struct LastLine { struct ProfileEntry { PyObject *profiler; // Borrowed Py_hash_t block_hash; - // Line number of lines[0] - int first_line; + // The data of the (non-negative) lines, in the order they were + // registered; see index std::vector lines; + // Maps line numbers to their slots in lines + LineIndex index; + // The negative line numbers registered, i.e. the -1 that + // PyCode_Addr2Line() gives for the bytecode without a line (e.g. the + // padding added by add_function(), and exception-handling and + // generator bytecode). No events are ever recorded for them, and + // they are kept out of index, where they would make almost every + // padded function's line numbers look too sparse for a table. + std::vector lineless; // Usually just the one frame of one thread, so a linear search is the // fastest std::vector last_lines; ProfileEntry(PyObject *profiler, Py_hash_t block_hash) - : profiler(profiler), block_hash(block_hash), first_line(0) {} + : profiler(profiler), block_hash(block_hash) {} // Returns: whether the line wasn't already registered bool register_line(int lineno) { - if (lines.empty()) { - first_line = lineno; - lines.resize(1, LineData{0, 0, false}); - } else if (lineno < first_line) { - lines.insert(lines.begin(), first_line - lineno, - LineData{0, 0, false}); - // The record indices shift with the lines - for (LastLine &last : last_lines) { - if (last.line_index >= 0) { - last.line_index += first_line - lineno; - } + if (lineno < 0) { + for (int registered : lineless) { + if (registered == lineno) return false; } - first_line = lineno; - } else if (lineno - first_line >= (int)lines.size()) { - lines.resize(lineno - first_line + 1, LineData{0, 0, false}); + lineless.push_back(lineno); + return true; } - LineData &line = lines[lineno - first_line]; - if (line.registered) return false; - line.registered = true; + if (index.find(lineno) >= 0) return false; + index.insert(lineno, (int)lines.size()); + lines.push_back(LineData{0, 0, lineno}); return true; } - // Returns: the index of lineno in lines if it is registered, else - // -1 + // Returns: the slot of lineno in lines if it is registered, else -1 inline int line_index(int lineno) const { - int index = lineno - first_line; - if (index < 0 || index >= (int)lines.size() - || !lines[index].registered) { - return -1; - } - return index; + return index.find(lineno); } // Returns: the record of the thread for the frame, NULL if there @@ -175,8 +253,9 @@ struct Block { // "co_extra"), so that its block is found without hashing its bytecode struct CodeCache { // Value of the registry generation when block was looked up; the - // generation changes when new blocks are added, since a code object - // without a block may then have one + // generation changes when blocks are added (since a code object + // without a block may then have one) or removed (since the cached + // block may then be freed) unsigned long generation; Py_hash_t block_hash; Block *block; // NULL if the code isn't being profiled diff --git a/line_profiler/_profile_data.pxd b/line_profiler/_profile_data.pxd index 0618d02f..43c1d597 100644 --- a/line_profiler/_profile_data.pxd +++ b/line_profiler/_profile_data.pxd @@ -13,7 +13,7 @@ cdef extern from "_profile_data.hpp" namespace "line_profiler": cdef cppclass LineData: long nhits PY_LONG_LONG total_time - cbool registered + int lineno cdef cppclass LastLine: unsigned long thread_ident @@ -21,11 +21,16 @@ cdef extern from "_profile_data.hpp" namespace "line_profiler": int line_index PY_LONG_LONG time + cdef cppclass LineIndex: + size_t num_entries() + cbool is_dense() + cdef cppclass ProfileEntry: PyObject *profiler Py_hash_t block_hash - int first_line vector[LineData] lines + vector[int] lineless + LineIndex index vector[LastLine] last_lines ProfileEntry(PyObject *profiler, Py_hash_t block_hash) diff --git a/line_profiler/_profiler_base.pyx b/line_profiler/_profiler_base.pyx index 8f8776a2..9266ac64 100644 --- a/line_profiler/_profiler_base.pyx +++ b/line_profiler/_profiler_base.pyx @@ -337,7 +337,8 @@ datamodel.html#user-defined-functions line_hashes = self.code_hash_map[code] = [] for lineno in self._register_lines(block_hash, linenos): # Note: PyCode_Addr2Line() gives -1 for the offsets without - # a line, which is converted like before + # a line, which is registered (and hashed) like before, but + # stored apart from the other lines (see ProfileEntry) line_hashes.append(compute_line_hash(block_hash, lineno)) self.functions.append(func) @@ -407,11 +408,13 @@ datamodel.html#user-defined-functions c_code_map = {} for i in range(self._entries.size()): entry = self._entries[i] + # (The lines without a line number never have any timings) + for j in range(entry.lineless.size()): + c_code_map.setdefault( + compute_line_hash(entry.block_hash, entry.lineless[j]), {}) for j in range(entry.lines.size()): line = &entry.lines[j] - if not line.registered: - continue - lineno = entry.first_line + j + lineno = line.lineno code_hash = compute_line_hash(entry.block_hash, lineno) entries = c_code_map.setdefault(code_hash, {}) if line.nhits: @@ -422,6 +425,24 @@ datamodel.html#user-defined-functions 'nhits': line.nhits} return c_code_map + @property + def _line_slots(self): + """ + The number of per-line slots (timings and line-number index + entries) allocated for the instance's code blocks, and the number + of those blocks whose index is a table (for checking the memory + use in tests). + + Returns: + tuple[int, int] + """ + cdef size_t i, total = 0, ndense = 0 + for i in range(self._entries.size()): + total += (self._entries[i].lines.size() + + self._entries[i].index.num_entries()) + ndense += self._entries[i].index.is_dense() + return total, ndense + @property def c_last_time(self): """ @@ -452,7 +473,7 @@ datamodel.html#user-defined-functions last = entry.latest_last_line(ident) if last != NULL: c_last_time[entry.block_hash] = { - 'f_lineno': entry.first_line + last.line_index, + 'f_lineno': entry.lines[last.line_index].lineno, 'time': to_units(last.time, self._ticks_to_units)} if not has_data: # We haven't actually profiled anything yet diff --git a/line_profiler/c_trace_callbacks.c b/line_profiler/c_trace_callbacks.c index ff22a8e0..3693c784 100644 --- a/line_profiler/c_trace_callbacks.c +++ b/line_profiler/c_trace_callbacks.c @@ -279,6 +279,17 @@ void disable_line_events(PyFrameObject *py_frame) py_frame->f_trace_lines = 0; } +void enable_line_events(PyFrameObject *py_frame, PyObject *manager) +{ + /* Undo `disable_line_events()`, i.e. let the frame generate line + * events again (e.g. because its code is now being profiled), unless + * it has a frame-local trace function other than `manager` (which + * may have turned them off itself) */ + PyObject *f_trace = py_frame->f_trace; + if (f_trace == NULL || f_trace == Py_None || f_trace == manager) + py_frame->f_trace_lines = 1; +} + Py_uintptr_t monitoring_restart_version() #if PY_VERSION_HEX >= 0x030c00b1 // 3.12.0b1 { diff --git a/line_profiler/c_trace_callbacks.h b/line_profiler/c_trace_callbacks.h index 550f1b92..26834116 100644 --- a/line_profiler/c_trace_callbacks.h +++ b/line_profiler/c_trace_callbacks.h @@ -39,6 +39,7 @@ int call_callback( ); void set_local_trace(PyObject *manager, PyFrameObject *py_frame); void disable_line_events(PyFrameObject *py_frame); +void enable_line_events(PyFrameObject *py_frame, PyObject *manager); Py_uintptr_t monitoring_restart_version(); #ifdef __cplusplus diff --git a/tests/test_line_profiler.py b/tests/test_line_profiler.py index 70a69cb8..0b24f99b 100644 --- a/tests/test_line_profiler.py +++ b/tests/test_line_profiler.py @@ -1758,3 +1758,266 @@ def write(stats, filename): stats_read = LineStats.from_files(*files) assert isinstance(stats_read, LineStats) assert stats_read == stats_combined + + +# -------------------------------------------------------------------- # +# The native profiling data (line storage, registry, timer) # +# -------------------------------------------------------------------- # + + +def _total_hits(prof, func): + timings = prof.get_stats().timings + return sum(nhits for _, nhits, _ in + timings.get(_line_profiler.label(func.__code__), [])) + + +def _exec_tagged(source, first_lineno, filename): + """ + Compile ``source`` as if it started at line ``first_lineno`` of + ``filename``, and return its namespace and the line numbers of its + lines tagged with ``# ``. + """ + namespace = {} + code = compile('\n' * (first_lineno - 1) + source, filename, 'exec') + exec(code, namespace) + tags = {line.rsplit('# ', 1)[1].strip(): first_lineno + i + for i, line in enumerate(source.splitlines()) if '# ' in line} + return namespace, tags + + +def test_line_storage_far_down_in_file(tracing_core): + """ + Test that the profiling data of functions far down in a source file + takes space proportional to their number of lines (not to their line + numbers, since `PyCode_Addr2Line()` gives -1 for the bytecode without + a line, e.g. padding, exception handling and generators), and that it + takes space proportional to the number of lines even when the lines + are far apart. + """ + first = 1_000_000 + source = ( + 'def plain(n):\n' + ' return n + 1 # plain\n' + '\n' + 'def guarded(n):\n' + ' try:\n' + ' return 1 // n # divide\n' + ' except ZeroDivisionError:\n' + ' return 0 # zero\n' + '\n' + 'def gen(n):\n' + ' for i in range(n):\n' + ' yield i # yield\n' + '\n' + 'def gappy(n):\n' + ' x = n # start\n' + ' s = """\n' + + '\n' * 20_000 + + '"""\n' + ' return x + len(s) # end\n') + namespace, tags = _exec_tagged(source, first, '') + funcs = [namespace[name] for name in ('plain', 'guarded', 'gen', 'gappy')] + prof = LineProfiler(*funcs) + plain, guarded, gen, gappy = funcs + with prof: + assert plain(1) == 2 + assert guarded(0) == 0 + assert guarded(1) == 1 + assert list(gen(3)) == [0, 1, 2] + assert gappy(1) == 20_002 + slots, ndense = prof._line_slots + assert slots < 100, slots + # The functions with contiguous lines use a table, the other a + # sorted vector + assert ndense == 3 + hits = {lineno: nhits for timings in prof.get_stats().timings.values() + for lineno, nhits, _ in timings} + assert {tag: hits.get(lineno, 0) for tag, lineno in tags.items()} == { + 'plain': 1, 'divide': 2, 'zero': 1, 'yield': 3, 'start': 1, 'end': 1} + + +def test_add_function_during_execution(tracing_core): + """ + Test that the lines run by the frames of a function after it is + added to an active profiler are recorded, even if the frames were + started before (with the legacy trace system, the line events of the + frames of unprofiled code are turned off). + """ + def unrelated(): + pass + + prof = LineProfiler(unrelated) + + def recurse(n): + if n: # check + recurse(n - 1) # call + else: + prof.add_function(recurse) # add + return n # return + + # Pad the bytecode now, so that adding it later doesn't replace the + # code object of the running frames + LineProfiler().add_function(recurse) + code = recurse.__code__ + with prof: + recurse(3) + assert recurse.__code__ is code + line_stats, _ = get_tagged_line_stats(prof, recurse) + # All four frames run the last line after recurse() is added + assert line_stats['return'][0] == 4 + + +def test_add_suspended_generator(tracing_core): + """ + Test that the lines run by a suspended generator after its function + is added to an active profiler are recorded. + """ + def unrelated(): + pass + + def gen(): + yield 1 # first + yield 2 # second + + prof = LineProfiler(unrelated) + LineProfiler().add_function(gen) # (Pad the bytecode now; see above) + with prof: + g = gen() + assert next(g) == 1 + prof.add_function(gen) + assert next(g) == 2 + line_stats, _ = get_tagged_line_stats(prof, gen) + assert line_stats['second'][0] == 1 + + +def test_enable_profiler_during_execution(tracing_core): + """ + Test that the lines run by a frame after a profiler profiling its + function is enabled are recorded, when the frame was started while + another profiler was active. + """ + def unrelated(): + pass + + prof_a = LineProfiler(unrelated) + prof_b = LineProfiler() + + def outer(): + prof_b.enable_by_count() # enable + x = 1 # after + prof_b.disable_by_count() # disable + return x + + prof_b.add_function(outer) + with prof_a: + assert outer() == 1 + line_stats, _ = get_tagged_line_stats(prof_b, outer) + assert line_stats['after'][0] == 1 + + +def test_profilers_sharing_code(tracing_core): + """ + Test that profilers profiling the same function record its calls + independently, while they are enabled and disabled independently, + and after one of them is deallocated. + """ + def func(): + return 1 + + prof_a = LineProfiler(func) + prof_b = LineProfiler(func) + prof_a.enable_by_count() + func() + prof_b.enable_by_count() + func() + prof_a.disable_by_count() + func() + prof_b.disable_by_count() + func() + assert _total_hits(prof_a, func) == 2 + assert _total_hits(prof_b, func) == 2 + del prof_a + gc.collect() + with prof_b: + func() + assert _total_hits(prof_b, func) == 3 + + +def test_registry_memory_reclaimed(tracing_core): + """ + Test that the native data of the code blocks profiled is freed when + the profilers profiling them are deallocated, and that the code + objects which pointed to the freed data can be profiled again. + """ + gc.collect() + baseline = _line_profiler._num_registered_blocks() + funcs = [] + for i in range(200): + namespace, _ = _exec_tagged( + f'def f{i}(n):\n return n + {i}\n', 1, f'') + func = namespace[f'f{i}'] + prof = LineProfiler(func) + with prof: + assert func(1) == i + 1 + assert _total_hits(prof, func) == 1 + funcs.append(func) + del prof + gc.collect() + assert _line_profiler._num_registered_blocks() == baseline + # The code objects still cache (now invalidated) pointers to the + # freed blocks + func = funcs[0] + func(1) + prof = LineProfiler(func) + with prof: + func(1) + assert _total_hits(prof, func) == 1 + assert _line_profiler._num_registered_blocks() == baseline + 1 + + +@pytest.mark.parametrize('use_tsc', ['0', '1']) +def test_timer_accuracy(use_tsc): + """ + Test that the timings are accurate (compared with + `time.perf_counter()`) whether the timer is allowed to use the CPU's + timestamp counter or not, including when the thread moves between + CPU cores (where supported). + """ + script = textwrap.dedent(''' + import os + import sys + import time + from line_profiler import LineProfiler, _line_profiler + + def sleeper(cpus, delay): + for cpu in cpus: + if cpu is not None: + os.sched_setaffinity(0, {cpu}) + time.sleep(delay) + + cpus = [None] + if hasattr(os, 'sched_getaffinity'): + allowed = sorted(os.sched_getaffinity(0)) + if len(allowed) > 1: + cpus = allowed[:2] * 3 + delay = .02 + prof = LineProfiler(sleeper) + with prof: + start = time.perf_counter() + sleeper(cpus, delay) + wall = time.perf_counter() - start + stats = prof.get_stats() + (timings,) = stats.timings.values() + times = [t * stats.unit for _, _, t in timings] + assert all(t >= 0 for t in times), times + total = sum(times) + assert .9 * delay * len(cpus) <= total <= 1.1 * wall + .005, ( + total, wall) + print(_line_profiler.USING_TSC) + ''') + env = {**os.environ, 'LINE_PROFILER_TSC': use_tsc} + proc = subprocess.run([sys.executable, '-c', script], env=env, + capture_output=True, text=True) + assert proc.returncode == 0, proc.stderr + if use_tsc == '0': + assert proc.stdout.strip() == 'False' From 9e055ca16f74e9189d53bdb920a4c35b80443e36 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:52:01 -0500 Subject: [PATCH 3/6] Skip the TSC timer test when the TSC isn't used --- tests/test_line_profiler.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_line_profiler.py b/tests/test_line_profiler.py index 0b24f99b..08b8b71f 100644 --- a/tests/test_line_profiler.py +++ b/tests/test_line_profiler.py @@ -1982,6 +1982,11 @@ def test_timer_accuracy(use_tsc): `time.perf_counter()`) whether the timer is allowed to use the CPU's timestamp counter or not, including when the thread moves between CPU cores (where supported). + + Note: + Allowing the timestamp counter doesn't mean it is used (see + `hpTimerInit()` in `timers.c`), and the test is skipped if it + isn't (since it would just test the other timer again). """ script = textwrap.dedent(''' import os @@ -1989,6 +1994,8 @@ def test_timer_accuracy(use_tsc): import time from line_profiler import LineProfiler, _line_profiler + print(_line_profiler.USING_TSC, flush=True) + def sleeper(cpus, delay): for cpu in cpus: if cpu is not None: @@ -2013,11 +2020,14 @@ def sleeper(cpus, delay): total = sum(times) assert .9 * delay * len(cpus) <= total <= 1.1 * wall + .005, ( total, wall) - print(_line_profiler.USING_TSC) ''') env = {**os.environ, 'LINE_PROFILER_TSC': use_tsc} proc = subprocess.run([sys.executable, '-c', script], env=env, capture_output=True, text=True) + using_tsc = proc.stdout.split('\n', 1)[0].strip() + if use_tsc == '1' and using_tsc == 'False': + pytest.skip('the timestamp counter is not used on this machine ' + '(it needs a Linux x86-64 build, a constant-rate TSC, ' + "and 'tsc' as the kernel's clock source)") assert proc.returncode == 0, proc.stderr - if use_tsc == '0': - assert proc.stdout.strip() == 'False' + assert using_tsc == ('False' if use_tsc == '0' else 'True') From 33d11cd34b553416c1a78a68d2019c199a7cf2c8 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:02:27 -0500 Subject: [PATCH 4/6] Address more review comments --- line_profiler/_line_profiler.pyx | 30 +++++++++++------ tests/test_line_profiler.py | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/line_profiler/_line_profiler.pyx b/line_profiler/_line_profiler.pyx index d43aae67..be94e007 100644 --- a/line_profiler/_line_profiler.pyx +++ b/line_profiler/_line_profiler.pyx @@ -274,25 +274,35 @@ cdef bint _frame_lines_disabled = False cdef restore_line_events(): """ With the legacy trace system, turn the line events back on for the - frames currently running (on all threads) whose code is now profiled, - but whose line events :c:func:`legacy_trace_callback` turned off - because it wasn't when they started (e.g. because a function was added - to a profiler, or a profiler was enabled, in the meantime). + frames currently running (on all threads) whose code is now profiled + by an active profiler on their thread, but whose line events + :c:func:`legacy_trace_callback` turned off because it wasn't when they + started (e.g. because a function was added to a profiler, or a + profiler was enabled, in the meantime). Note: Suspended generators and coroutines are dealt with when they are resumed (see :c:func:`legacy_trace_callback`). """ + cdef _LineProfilerManager manager if not (USE_LEGACY_TRACE and _frame_lines_disabled): return - for frame in sys._current_frames().values(): + managers = LineProfiler._managers + for thread_id, frame in sys._current_frames().items(): + # Only the profilers active on the frames' thread record their + # line events (and the frames of a thread without any don't + # generate any) + manager = managers.get(thread_id) + if manager is None or manager._active.empty(): + continue while frame is not None: if (not frame.f_trace_lines - and get_block(frame.f_code) != NULL): - f_trace = frame.f_trace - if (f_trace is None - or isinstance(f_trace, _LineProfilerManager)): - frame.f_trace_lines = True + and is_profiled(get_block(frame.f_code), + &manager._active)): + # (Unless it has another frame-local trace function, + # which may have turned them off itself) + c_enable_line_events(frame, + manager) frame = frame.f_back diff --git a/tests/test_line_profiler.py b/tests/test_line_profiler.py index 08b8b71f..0b29d858 100644 --- a/tests/test_line_profiler.py +++ b/tests/test_line_profiler.py @@ -1915,6 +1915,63 @@ def outer(): assert line_stats['after'][0] == 1 +def test_restore_only_actively_profiled_frames(monkeypatch): + """ + Test that, with the legacy trace system, adding a function to a + profiler or enabling one only turns the line events back on in the + running frames whose code is profiled by a profiler active on their + thread, and not in those whose code is only profiled by inactive + profilers or by profilers active on other threads. + """ + import threading + + monkeypatch.setattr(_line_profiler, 'USE_LEGACY_TRACE', True) + + def unrelated(): + pass + + def other(): + pass + + prof_a = LineProfiler(unrelated) + prof_b = LineProfiler() + prof_c = LineProfiler() + trace_lines = {} + + def enable_c(): + with prof_c: + pass + + def worker(): + frame = sys._getframe() + trace_lines['started'] = frame.f_trace_lines + # Adding a function to the active profiler + prof_a.add_function(other) + trace_lines['added'] = frame.f_trace_lines + # Enabling a profiler of worker() on another thread + thread = threading.Thread(target=enable_c) + thread.start() + thread.join() + trace_lines['other thread'] = frame.f_trace_lines + # Enabling a profiler of worker() on this thread + prof_b.enable_by_count() + trace_lines['enabled'] = frame.f_trace_lines + x = 1 # after + prof_b.disable_by_count() + return x + + prof_b.add_function(worker) + prof_c.add_function(worker) + with prof_a: + assert worker() == 1 + assert trace_lines == { + 'started': False, 'added': False, 'other thread': False, + 'enabled': True} + line_stats, _ = get_tagged_line_stats(prof_b, worker) + assert line_stats['after'][0] == 1 + assert _total_hits(prof_c, worker) == 0 + + def test_profilers_sharing_code(tracing_core): """ Test that profilers profiling the same function record its calls From 81e35517b9a0ed08a3413fc1bf97dc8cb24aeb04 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:35:08 -0500 Subject: [PATCH 5/6] Document magic invariant TSC checking --- line_profiler/timers.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/line_profiler/timers.c b/line_profiler/timers.c index 86f81dbc..13c935cf 100644 --- a/line_profiler/timers.c +++ b/line_profiler/timers.c @@ -101,6 +101,28 @@ hpTimerUnit(void) } #ifdef LINE_PROFILER_HAVE_TSC +/* + * The CPUID leaf (the value of EAX passed to the CPUID instruction) for + * "Advanced Power Management Information", and the bit of EDX that it + * sets if the TSC is invariant, i.e. ticks at a constant rate regardless + * of frequency scaling and power states (P-, C- and T-states). + * + * Note: the meaning of each bit depends on the leaf, e.g. bit 8 of EDX + * means something else entirely for leaf 7 (structured extended + * features). + * + * References: + * Intel 64 and IA-32 Architectures Software Developer's Manual, + * Vol. 2A, CPUID, "Initial EAX Value 80000007H" (and Vol. 3B, + * section "Invariant TSC"); + * AMD64 Architecture Programmer's Manual, Vol. 3, appendix E, + * "CPUID Fn8000_0007_EDX", bit 8 "TscInvariant"; + * Linux's X86_FEATURE_CONSTANT_TSC and X86_FEATURE_NONSTOP_TSC, + * which are set from the same bit (arch/x86/kernel/cpu/). + */ +#define CPUID_LEAF_POWER_MANAGEMENT 0x80000007u +#define CPUID_EDX_INVARIANT_TSC (1u << 8) + /* * Whether the TSC ticks at a constant rate (regardless of frequency * scaling and power states), and the kernel trusts it as its clock source @@ -113,8 +135,10 @@ tsc_is_reliable(void) unsigned int eax, ebx, ecx, edx; char clocksource[64] = {0}; FILE *f; - if (!__get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx) - || !(edx & (1u << 8))) + /* (__get_cpuid() fails if the CPU doesn't have the leaf) */ + if (!__get_cpuid(CPUID_LEAF_POWER_MANAGEMENT, + &eax, &ebx, &ecx, &edx) + || !(edx & CPUID_EDX_INVARIANT_TSC)) return 0; f = fopen("/sys/devices/system/clocksource/clocksource0/" "current_clocksource", "r"); From 55ddb9e0ee1816ca3a313d1a54da783e36dc5364 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:47:09 -0500 Subject: [PATCH 6/6] Address more review comments v2 --- line_profiler/_line_profiler.pyx | 2 +- line_profiler/_profiler_base.pxd | 8 +++--- line_profiler/_profiler_base.pyx | 4 +-- line_profiler/_sys_monitoring.pxd | 3 +-- line_profiler/_sys_monitoring.pyx | 15 +++++------ tests/test_line_profiler.py | 45 +++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 16 deletions(-) diff --git a/line_profiler/_line_profiler.pyx b/line_profiler/_line_profiler.pyx index be94e007..3cce0d70 100644 --- a/line_profiler/_line_profiler.pyx +++ b/line_profiler/_line_profiler.pyx @@ -1089,7 +1089,7 @@ cdef class LineProfiler(_LineProfilerBase): # their times) for i in range(self._entries.size()): self._entries[i].clear_last_lines(ident) - self._disabled_threads.add(ident) + self._threads_with_last_time.add(ident) self._manager._handle_disable_event(self) def get_stats(self): diff --git a/line_profiler/_profiler_base.pxd b/line_profiler/_profiler_base.pxd index b1756a24..c1748c46 100644 --- a/line_profiler/_profiler_base.pxd +++ b/line_profiler/_profiler_base.pxd @@ -34,9 +34,11 @@ cdef class _LineProfilerBase: # The profiling data of each code block (owned by the instance; also # referenced by the blocks in the registry in _line_profiler.pyx) cdef vector[ProfileEntry *] _entries - # Idents of the threads on which the profiler has been disabled (see - # .c_last_time) - cdef set _disabled_threads + # Idents of the threads on which .c_last_time gives a (possibly + # empty) mapping, even without any last-line records: those on which + # the profiler has ever been disabled (to keep the behavior from when + # disable() created an empty record for the thread); never shrinks + cdef set _threads_with_last_time # Factor converting the raw timings to .timer_units cdef double _ticks_to_units cdef public list functions diff --git a/line_profiler/_profiler_base.pyx b/line_profiler/_profiler_base.pyx index 9266ac64..0954c1b5 100644 --- a/line_profiler/_profiler_base.pyx +++ b/line_profiler/_profiler_base.pyx @@ -392,7 +392,7 @@ datamodel.html#user-defined-functions self.disable_by_count() def __cinit__(self, *args, **kwargs): - self._disabled_threads = set() + self._threads_with_last_time = set() self._ticks_to_units = 1.0 @property @@ -463,7 +463,7 @@ datamodel.html#user-defined-functions cdef const LastLine *last cdef size_t i cdef unsigned long ident = PyThread_get_thread_ident() - has_data = ident in self._disabled_threads + has_data = ident in self._threads_with_last_time c_last_time = {} for i in range(self._entries.size()): entry = self._entries[i] diff --git a/line_profiler/_sys_monitoring.pxd b/line_profiler/_sys_monitoring.pxd index 60738301..7c3858ec 100644 --- a/line_profiler/_sys_monitoring.pxd +++ b/line_profiler/_sys_monitoring.pxd @@ -7,8 +7,7 @@ cdef extern from "c_trace_callbacks.h": cdef class _SysMonitoringState: - cdef int tool_id - cdef object tool_id_obj # (The same as a Python int) + cdef object tool_id # type: int cdef object name # type: str | None # The following are per line-tracing event (`LINE`, `PY_RETURN`, # `PY_YIELD`, `RAISE`, and `RERAISE`, in that order): diff --git a/line_profiler/_sys_monitoring.pyx b/line_profiler/_sys_monitoring.pyx index 4d43209d..1dcf164c 100644 --- a/line_profiler/_sys_monitoring.pyx +++ b/line_profiler/_sys_monitoring.pyx @@ -111,7 +111,6 @@ cdef class _SysMonitoringState: def __init__(self, tool_id: int): self.tool_id = tool_id - self.tool_id_obj = tool_id self.name = None self.own_callbacks = [None] * _NUM_LINE_TRACING_EVENTS self.callbacks = [None] * _NUM_LINE_TRACING_EVENTS @@ -218,12 +217,12 @@ cdef class _SysMonitoringState: # Callback should not receive the event (only check the local # events if the global ones don't include it) if not (self.events & event_id): - if not (_get_local_events(self.tool_id_obj, code) & event_id): + if not (_get_local_events(self.tool_id, code) & event_id): return arg_tuple = (code, loc, arg) if has_arg else code_location try: - events_before = _get_events(self.tool_id_obj) + events_before = _get_events(self.tool_id) result = PyObject_Call( # Note: DECREF needed below callback, arg_tuple, NULL) else: @@ -238,7 +237,7 @@ cdef class _SysMonitoringState: finally: Py_XDECREF(result) # Update the events - events_after = _get_events(self.tool_id_obj) + events_after = _get_events(self.tool_id) self.events = _patch_events( self.events, events_before, events_after) # If the wrapped callback has replaced any of our callbacks: @@ -246,16 +245,16 @@ cdef class _SysMonitoringState: # - Remember the replacement as the callback to wrap for i in range(_NUM_LINE_TRACING_EVENTS): callback_after = _register_callback( - self.tool_id_obj, _LINE_TRACING_EVENT_ID_OBJS[i], + self.tool_id, _LINE_TRACING_EVENT_ID_OBJS[i], self.own_callbacks[i]) if callback_after is not self.own_callbacks[i]: self.callbacks[i] = callback_after # Reset the tool ID lock if released - if not _get_tool(self.tool_id_obj): - _use_tool_id(self.tool_id_obj, 'line_profiler') + if not _get_tool(self.tool_id): + _use_tool_id(self.tool_id, 'line_profiler') reacquired = True # Restore the `sys.monitoring` events if unset if reacquired or events_after != ( self.events | _LINE_TRACING_EVENTS): - _set_events(self.tool_id_obj, + _set_events(self.tool_id, self.events | _LINE_TRACING_EVENTS) diff --git a/tests/test_line_profiler.py b/tests/test_line_profiler.py index 0b29d858..48a71789 100644 --- a/tests/test_line_profiler.py +++ b/tests/test_line_profiler.py @@ -1836,6 +1836,51 @@ def test_line_storage_far_down_in_file(tracing_core): 'plain': 1, 'divide': 2, 'zero': 1, 'yield': 3, 'start': 1, 'end': 1} +def test_line_index_rebuilt(tracing_core): + """ + Test that the line index of a profiler's data for a code block is + rebuilt when lines are registered below its first line, and switched + to a sorted vector when they are too far apart, by adding to the + profiler copies of a profiled function moved up and down the file + (which have the same bytecode, and thus share the data). + """ + namespace, tags = _exec_tagged( + 'def func(n):\n' + ' x = n + 1 # add\n' + ' return x # return\n', 1000, '') + func = namespace['func'] + prof = LineProfiler(func) # Pads the bytecode + code = func.__code__ + funcs = [func] + + def add_twin(shift): + twin_code = code.replace(co_firstlineno=code.co_firstlineno + shift) + twin = types.FunctionType(twin_code, {}) + prof.add_function(twin) + assert twin.__code__.co_code == code.co_code + funcs.append(twin) + return prof._line_slots + + # There's only one block, so one table or none + _, ndense = prof._line_slots + assert ndense == 1 + # Below the first line: the table is rebuilt + _, ndense = add_twin(-10) + assert ndense == 1 + # Far below the last line: switched to a sorted vector + slots, ndense = add_twin(5000) + assert ndense == 0 + assert slots < 100, slots + with prof: + for f in funcs: + assert f(1) == 2 + timings = prof.get_stats().timings + for f, shift in zip(funcs, [0, -10, 5000]): + hits = {lineno: nhits for lineno, nhits, _ in + timings[_line_profiler.label(f.__code__)]} + assert hits == {tags['add'] + shift: 1, tags['return'] + shift: 1} + + def test_add_function_during_execution(tracing_core): """ Test that the lines run by the frames of a function after it is