| # Copyright 2023 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Trace model data structures.""" | |
| # Memory layout of the model classes | |
| # | |
| # A trace model can hold millions of Event and SchedulingRecord objects, so | |
| # their per-instance overhead dominates host memory during trace import. To | |
| # keep it down: | |
| # | |
| # * Event, SchedulingRecord, and every subclass of either declare | |
| # __slots__, as do trace_time.TimePoint and trace_time.TimeDelta. A | |
| # subclass must declare its own __slots__ (listing only the attributes it | |
| # adds, or `()` if it adds none); a class without one silently gets a | |
| # per-instance __dict__ back, and nothing fails except memory usage. | |
| # * Empty args are stored as a single shared, empty, read-only mapping, and | |
| # non-empty args are wrapped in a read-only view rather than copied, so | |
| # neither reading args nor constructing an event allocates a dict. | |
| # * Event interns its category and name with sys.intern(). A trace has few | |
| # distinct values for these, but the importers create a new string object | |
| # for every event, so without interning each event carries its own copy. | |
| # Interned strings are not immortal, so they are freed with the model. | |
| # * DurationEvent stores child_durations and child_flows as tuples (defaulting | |
| # to the singleton `()`), avoiding two empty list allocations per leaf | |
| # slice. | |
| # | |
| # Nothing enforces the __slots__ convention. If that ever becomes necessary, | |
| # add a test that walks Event.__subclasses__() and | |
| # SchedulingRecord.__subclasses__() recursively and asserts that no instance | |
| # has a __dict__. | |
| # Dual-form Event subclass constructors (@typing.overload) | |
| # | |
| # Each Event subclass (InstantEvent, CounterEvent, DurationEvent, AsyncEvent, | |
| # and FlowEvent) temporarily accepts two mutually exclusive constructor forms | |
| # while callers across repositories migrate away from allocating a throwaway | |
| # `base = Event(...)` wrapper: | |
| # | |
| # 1. Legacy form: pass `base: Event` positionally and omit keyword Event | |
| # fields (e.g. `CounterEvent(id, base)`). | |
| # 2. Direct form: omit `base` and pass `category`, `name`, `start`, `pid`, | |
| # and `tid` (plus optional `args`) by keyword (e.g. | |
| # `CounterEvent(id, category=..., name=..., start=..., pid=..., tid=...)`), | |
| # initializing Event slots directly without allocating a second Event. | |
| # | |
| # Python has no runtime method overloading: only the final undecorated | |
| # `__init__` exists at runtime, where `_init_event_from_base_or_fields()` | |
| # enforces that callers use one form or the other (raising TypeError | |
| # otherwise). The `@typing.overload` stubs (`...`) above each `__init__` never | |
| # execute. They tell static type checkers (`mypy`) to ignore the permissive | |
| # runtime signature and check every call site strictly against one of the two | |
| # overloaded forms. | |
| # | |
| # TODO(https://fxbug.dev/364925797): Delete the legacy `base` overload and | |
| # `_init_event_from_base_or_fields()` once external callers are migrated. | |
| import copy | |
| import enum | |
| import sys | |
| import typing | |
| from collections.abc import Iterator, Mapping, Sequence | |
| from functools import total_ordering | |
| from types import MappingProxyType | |
| from typing import Any, Self | |
| from trace_processing import trace_time | |
| INT32_MIN = -0x80000000 | |
| _EMPTY_ARGS: MappingProxyType[str, Any] = MappingProxyType({}) | |
| # TODO(b/577959138): Switch to frozendict (PEP 814) once the in-tree Python is | |
| # 3.15 or newer. It would make args truly immutable and isolated from the | |
| # caller without the extra MappingProxyType object per set of args. | |
| def _read_only_args( | |
| args: Mapping[str, Any] | None, | |
| ) -> MappingProxyType[str, Any]: | |
| """Returns a read-only view of `args` without copying it. | |
| The view reflects later changes the caller makes to `args`; callers that | |
| need isolation must pass a copy. Empty or missing args map to a shared | |
| empty view. | |
| """ | |
| if not args: | |
| return _EMPTY_ARGS | |
| if isinstance(args, MappingProxyType): | |
| return args | |
| return MappingProxyType(args) | |
| class InstantEventScope(enum.Enum): | |
| """Scope within a trace that an `InstantEvent` applies to.""" | |
| THREAD = enum.auto() | |
| PROCESS = enum.auto() | |
| GLOBAL = enum.auto() | |
| class FlowEventPhase(enum.Enum): | |
| """Phase within the control flow lifecycle that a `FlowEvent` applies to.""" | |
| START = enum.auto() | |
| STEP = enum.auto() | |
| END = enum.auto() | |
| class Event: | |
| """Base class for all trace events in a trace model. Contains fields that | |
| are common to all trace event types. | |
| """ | |
| # Subclasses must declare their own __slots__; see "Memory layout of the | |
| # model classes" at the top of this file. | |
| __slots__ = ("category", "name", "start", "pid", "tid", "_args") | |
| def __init__( | |
| self, | |
| category: str, | |
| name: str, | |
| start: trace_time.TimePoint, | |
| pid: int, | |
| tid: int, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| # Intern category and name: a trace has few distinct values for each | |
| # but the importers produce a separate string object per event. See | |
| # "Memory layout of the model classes" at the top of this file. | |
| self.category = sys.intern(category) | |
| self.name = sys.intern(name) | |
| self.start = start | |
| self.pid = pid | |
| self.tid = tid | |
| # Any extra arguments that the event contains. These are not copied: | |
| # the FXT importer deliberately passes one dict to every event that | |
| # shares an arg set, and copying would duplicate it per event. | |
| self._args: MappingProxyType[str, Any] = _read_only_args(args) | |
| @property | |
| def args(self) -> MappingProxyType[str, Any]: | |
| return self._args | |
| @args.setter | |
| def args(self, rhs: Mapping[str, Any] | None) -> None: | |
| self._args = _read_only_args(rhs) | |
| def end_time(self) -> trace_time.TimePoint | None: | |
| """Get the ending time for this Event | |
| Returns: | |
| * The end time, if the Event has a `duration` | |
| * None otherwise | |
| """ | |
| return None | |
| def _init_event_from_base_or_fields( | |
| event: Event, | |
| base: Event | None, | |
| category: str | None, | |
| name: str | None, | |
| start: trace_time.TimePoint | None, | |
| pid: int | None, | |
| tid: int | None, | |
| args: Mapping[str, Any] | None, | |
| ) -> None: | |
| """Initializes `Event` base slots from either `base` or keyword fields.""" | |
| if base is not None: | |
| if ( | |
| category is not None | |
| or name is not None | |
| or start is not None | |
| or pid is not None | |
| or tid is not None | |
| or args is not None | |
| ): | |
| raise TypeError( | |
| "Cannot specify both 'base' and individual Event fields" | |
| ) | |
| Event.__init__( | |
| event, | |
| base.category, | |
| base.name, | |
| base.start, | |
| base.pid, | |
| base.tid, | |
| base.args, | |
| ) | |
| else: | |
| if ( | |
| category is None | |
| or name is None | |
| or start is None | |
| or pid is None | |
| or tid is None | |
| ): | |
| raise TypeError( | |
| "Must specify either 'base' or all of " | |
| "'category', 'name', 'start', 'pid', and 'tid'" | |
| ) | |
| Event.__init__(event, category, name, start, pid, tid, args) | |
| def _extract_dict_base_fields( | |
| event_dict: Mapping[str, Any], | |
| ) -> tuple[str, str, trace_time.TimePoint, int, int, Mapping[str, Any] | None]: | |
| """Extracts common `Event` fields from a Chrome JSON trace event dict.""" | |
| return ( | |
| event_dict["cat"], | |
| event_dict["name"], | |
| trace_time.TimePoint.from_microseconds(event_dict["ts"]), | |
| event_dict["pid"], | |
| event_dict["tid"], | |
| event_dict.get("args"), | |
| ) | |
| class InstantEvent(Event): | |
| """An event that corresponds to a single moment in time.""" | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = ("scope",) | |
| INSTANT_EVENT_SCOPE_MAP: dict[str, InstantEventScope] = { | |
| "g": InstantEventScope.GLOBAL, | |
| "p": InstantEventScope.PROCESS, | |
| "t": InstantEventScope.THREAD, | |
| } | |
| # See "Dual-form Event subclass constructors" at the top of this file. | |
| @typing.overload | |
| def __init__(self, scope: InstantEventScope, base: Event) -> None: | |
| ... | |
| @typing.overload | |
| def __init__( | |
| self, | |
| scope: InstantEventScope, | |
| *, | |
| category: str, | |
| name: str, | |
| start: trace_time.TimePoint, | |
| pid: int, | |
| tid: int, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| ... | |
| def __init__( | |
| self, | |
| scope: InstantEventScope, | |
| base: Event | None = None, | |
| *, | |
| category: str | None = None, | |
| name: str | None = None, | |
| start: trace_time.TimePoint | None = None, | |
| pid: int | None = None, | |
| tid: int | None = None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| _init_event_from_base_or_fields( | |
| self, base, category, name, start, pid, tid, args | |
| ) | |
| self.scope: InstantEventScope = scope | |
| @classmethod | |
| def consume_dict(cls, event_dict: dict[str, Any]) -> Self: | |
| """Factory-style constructor.""" | |
| scope_key: str = "s" | |
| if scope_key not in event_dict: | |
| raise TypeError( | |
| f"Expected dictionary to have a field '{scope_key}' of type " | |
| f"str: {event_dict}" | |
| ) | |
| if not isinstance(event_dict[scope_key], str): | |
| raise TypeError( | |
| f"Expected dictionary to have a field '{scope_key}' of type " | |
| f"str: {event_dict}" | |
| ) | |
| scope_str: str = event_dict[scope_key] | |
| if scope_str not in InstantEvent.INSTANT_EVENT_SCOPE_MAP: | |
| raise TypeError( | |
| f"Expected '{scope_key}' (scope field) of dict to be one of " | |
| f"{list(InstantEvent.INSTANT_EVENT_SCOPE_MAP)}: {event_dict}" | |
| ) | |
| scope: InstantEventScope = InstantEvent.INSTANT_EVENT_SCOPE_MAP[ | |
| scope_str | |
| ] | |
| category, name, start, pid, tid, args = _extract_dict_base_fields( | |
| event_dict | |
| ) | |
| return cls( | |
| scope, | |
| category=category, | |
| name=name, | |
| start=start, | |
| pid=pid, | |
| tid=tid, | |
| args=args, | |
| ) | |
| class CounterEvent(Event): | |
| """An event that tracks the count of some quantity.""" | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = ("id",) | |
| # See "Dual-form Event subclass constructors" at the top of this file. | |
| @typing.overload | |
| def __init__(self, id: int | None, base: Event) -> None: | |
| ... | |
| @typing.overload | |
| def __init__( | |
| self, | |
| id: int | None, | |
| *, | |
| category: str, | |
| name: str, | |
| start: trace_time.TimePoint, | |
| pid: int, | |
| tid: int, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| ... | |
| def __init__( | |
| self, | |
| id: int | None, | |
| base: Event | None = None, | |
| *, | |
| category: str | None = None, | |
| name: str | None = None, | |
| start: trace_time.TimePoint | None = None, | |
| pid: int | None = None, | |
| tid: int | None = None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| _init_event_from_base_or_fields( | |
| self, base, category, name, start, pid, tid, args | |
| ) | |
| self.id: int | None = id | |
| @classmethod | |
| def consume_dict(cls, event_dict: dict[str, Any]) -> Self: | |
| """Factory-style constructor.""" | |
| id_key: str = "id" | |
| id: int | None = None | |
| if id_key in event_dict: | |
| try: | |
| id = int(event_dict[id_key], 0) | |
| except (TypeError, ValueError) as t: | |
| raise TypeError( | |
| f"Expected '{id_key}' field to be an int or a string that " | |
| f"parses as int: {event_dict}" | |
| ) from t | |
| category, name, start, pid, tid, args = _extract_dict_base_fields( | |
| event_dict | |
| ) | |
| return cls( | |
| id, | |
| category=category, | |
| name=name, | |
| start=start, | |
| pid=pid, | |
| tid=tid, | |
| args=args, | |
| ) | |
| class DurationEvent(Event): | |
| """An event which describes work that is happening synchronously on one | |
| thread. | |
| In the Fuchsia trace model, matching begin/end duration in the raw Chrome | |
| trace format are merged into a single `DurationEvent`. Chrome complete | |
| events become `DurationEvent`s as well. Dangling Chrome begin/end events | |
| (i.e. they don't have a matching end/begin event) are dropped. | |
| """ | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = ("duration", "parent", "_child_durations", "_child_flows") | |
| duration: trace_time.TimeDelta | None | |
| parent: Self | None | |
| _child_durations: tuple[Self, ...] | |
| _child_flows: tuple[FlowEvent, ...] | |
| # See "Dual-form Event subclass constructors" at the top of this file. | |
| @typing.overload | |
| def __init__( | |
| self, | |
| duration: trace_time.TimeDelta | None, | |
| parent: Self | None, | |
| child_durations: Sequence[Self], | |
| child_flows: Sequence[FlowEvent], | |
| base: Event, | |
| ) -> None: | |
| ... | |
| @typing.overload | |
| def __init__( | |
| self, | |
| duration: trace_time.TimeDelta | None, | |
| parent: Self | None = None, | |
| child_durations: Sequence[Self] = (), | |
| child_flows: Sequence[FlowEvent] = (), | |
| *, | |
| category: str, | |
| name: str, | |
| start: trace_time.TimePoint, | |
| pid: int, | |
| tid: int, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| ... | |
| def __init__( | |
| self, | |
| duration: trace_time.TimeDelta | None, | |
| parent: Self | None = None, | |
| child_durations: Sequence[Self] = (), | |
| child_flows: Sequence[FlowEvent] = (), | |
| base: Event | None = None, | |
| *, | |
| category: str | None = None, | |
| name: str | None = None, | |
| start: trace_time.TimePoint | None = None, | |
| pid: int | None = None, | |
| tid: int | None = None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| _init_event_from_base_or_fields( | |
| self, base, category, name, start, pid, tid, args | |
| ) | |
| self.duration = duration | |
| self.parent = parent | |
| self._child_durations = tuple(child_durations) | |
| self._child_flows = tuple(child_flows) | |
| @property | |
| def child_durations(self) -> tuple[Self, ...]: | |
| return self._child_durations | |
| @child_durations.setter | |
| def child_durations(self, rhs: Sequence[Self]) -> None: | |
| self._child_durations = tuple(rhs) | |
| @property | |
| def child_flows(self) -> tuple[FlowEvent, ...]: | |
| return self._child_flows | |
| @child_flows.setter | |
| def child_flows(self, rhs: Sequence[FlowEvent]) -> None: | |
| self._child_flows = tuple(rhs) | |
| @classmethod | |
| def consume_dict(cls, event_dict: dict[str, Any]) -> Self: | |
| """Factory-style constructor.""" | |
| duration_key: str = "dur" | |
| duration: trace_time.TimeDelta | None = None | |
| microseconds: float | int | None = event_dict.get(duration_key, None) | |
| if microseconds is not None: | |
| if not isinstance(microseconds, (int, float)): | |
| raise TypeError( | |
| f"Expected dictionary to have a field '{duration_key}' of " | |
| f"type float or int: {event_dict}" | |
| ) | |
| duration = trace_time.TimeDelta.from_microseconds( | |
| float(microseconds) | |
| ) | |
| category, name, start, pid, tid, args = _extract_dict_base_fields( | |
| event_dict | |
| ) | |
| return cls( | |
| duration, | |
| category=category, | |
| name=name, | |
| start=start, | |
| pid=pid, | |
| tid=tid, | |
| args=args, | |
| ) | |
| @typing.override | |
| def end_time(self) -> trace_time.TimePoint | None: | |
| if self.duration: | |
| return self.start + self.duration | |
| else: | |
| return None | |
| class AsyncEvent(Event): | |
| """An event which describes work which is happening asynchronously and which | |
| may span multiple threads. | |
| Dangling Chrome async begin/end events are dropped. | |
| """ | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = ("id", "duration") | |
| # See "Dual-form Event subclass constructors" at the top of this file. | |
| @typing.overload | |
| def __init__( | |
| self, id: int, duration: trace_time.TimeDelta | None, base: Event | |
| ) -> None: | |
| ... | |
| @typing.overload | |
| def __init__( | |
| self, | |
| id: int, | |
| duration: trace_time.TimeDelta | None, | |
| *, | |
| category: str, | |
| name: str, | |
| start: trace_time.TimePoint, | |
| pid: int, | |
| tid: int, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| ... | |
| def __init__( | |
| self, | |
| id: int, | |
| duration: trace_time.TimeDelta | None, | |
| base: Event | None = None, | |
| *, | |
| category: str | None = None, | |
| name: str | None = None, | |
| start: trace_time.TimePoint | None = None, | |
| pid: int | None = None, | |
| tid: int | None = None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| _init_event_from_base_or_fields( | |
| self, base, category, name, start, pid, tid, args | |
| ) | |
| self.id: int = id | |
| self.duration: trace_time.TimeDelta | None = duration | |
| @classmethod | |
| def consume_dict(cls, id: int, event_dict: dict[str, Any]) -> Self: | |
| """Factory-style constructor.""" | |
| category, name, start, pid, tid, args = _extract_dict_base_fields( | |
| event_dict | |
| ) | |
| return cls( | |
| id, | |
| None, | |
| category=category, | |
| name=name, | |
| start=start, | |
| pid=pid, | |
| tid=tid, | |
| args=args, | |
| ) | |
| @typing.override | |
| def end_time(self) -> trace_time.TimePoint | None: | |
| if self.duration: | |
| return self.start + self.duration | |
| else: | |
| return None | |
| class FlowEvent(Event): | |
| """An event which describes control flow handoffs between threads or across | |
| processes. | |
| Malformed flow events are dropped. Malformed flow events could be any of: | |
| * A begin flow event with a (category, name, id) tuple already in progress | |
| (this is uncommon in practice). | |
| * A step flow event with no preceding (category, name, id) tuple. | |
| * An end flow event with no preceding (category, name, id) tuple. | |
| """ | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = ( | |
| "id", | |
| "phase", | |
| "enclosing_duration", | |
| "previous_flow", | |
| "next_flow", | |
| ) | |
| FLOW_EVENT_PHASE_MAP: dict[str, FlowEventPhase] = { | |
| "s": FlowEventPhase.START, | |
| "t": FlowEventPhase.STEP, | |
| "f": FlowEventPhase.END, | |
| } | |
| # See "Dual-form Event subclass constructors" at the top of this file. | |
| @typing.overload | |
| def __init__( | |
| self, | |
| id: str, | |
| phase: FlowEventPhase, | |
| enclosing_duration: DurationEvent | None, | |
| previous_flow: Self | None, | |
| next_flow: Self | None, | |
| base: Event, | |
| ) -> None: | |
| ... | |
| @typing.overload | |
| def __init__( | |
| self, | |
| id: str, | |
| phase: FlowEventPhase, | |
| enclosing_duration: DurationEvent | None = None, | |
| previous_flow: Self | None = None, | |
| next_flow: Self | None = None, | |
| *, | |
| category: str, | |
| name: str, | |
| start: trace_time.TimePoint, | |
| pid: int, | |
| tid: int, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| ... | |
| def __init__( | |
| self, | |
| id: str, | |
| phase: FlowEventPhase, | |
| enclosing_duration: DurationEvent | None = None, | |
| previous_flow: Self | None = None, | |
| next_flow: Self | None = None, | |
| base: Event | None = None, | |
| *, | |
| category: str | None = None, | |
| name: str | None = None, | |
| start: trace_time.TimePoint | None = None, | |
| pid: int | None = None, | |
| tid: int | None = None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| _init_event_from_base_or_fields( | |
| self, base, category, name, start, pid, tid, args | |
| ) | |
| self.id: str = id | |
| self.phase: FlowEventPhase = phase | |
| self.enclosing_duration: DurationEvent | None = enclosing_duration | |
| self.previous_flow: Self | None = previous_flow | |
| self.next_flow: Self | None = next_flow | |
| @classmethod | |
| def consume_dict( | |
| cls, | |
| id: str, | |
| enclosing_duration: DurationEvent | None, | |
| event_dict: dict[str, Any], | |
| ) -> Self: | |
| """Factory-style constructor.""" | |
| phase_key: str = "ph" | |
| if phase_key not in event_dict: | |
| raise TypeError( | |
| f"Expected dictionary to have a field '{phase_key}' of type " | |
| f"str: {event_dict}" | |
| ) | |
| if not isinstance(event_dict[phase_key], str): | |
| raise TypeError( | |
| f"Expected dictionary to have a field '{phase_key}' of type " | |
| f"str: {event_dict}" | |
| ) | |
| phase_str: str = event_dict[phase_key] | |
| if phase_str not in FlowEvent.FLOW_EVENT_PHASE_MAP: | |
| raise TypeError( | |
| f"Expected '{phase_key}' (phase field) of dict to be one of " | |
| f"{list(FlowEvent.FLOW_EVENT_PHASE_MAP)}: {event_dict}" | |
| ) | |
| phase: FlowEventPhase = FlowEvent.FLOW_EVENT_PHASE_MAP[phase_str] | |
| category, name, start, pid, tid, args = _extract_dict_base_fields( | |
| event_dict | |
| ) | |
| return cls( | |
| id, | |
| phase, | |
| enclosing_duration=enclosing_duration, | |
| category=category, | |
| name=name, | |
| start=start, | |
| pid=pid, | |
| tid=tid, | |
| args=args, | |
| ) | |
| @total_ordering | |
| class ThreadState(enum.Enum): | |
| """Phase within the control flow lifecycle that a `FlowEvent` applies to.""" | |
| # Basic thread states, in zx_info_thread_t.state. | |
| ZX_THREAD_STATE_NEW = 0x0000 | |
| ZX_THREAD_STATE_RUNNING = 0x0001 | |
| ZX_THREAD_STATE_SUSPENDED = 0x0002 | |
| # BLOCKED is never returned by itself. | |
| # It is always returned with a more precise reason. | |
| # See below. | |
| ZX_THREAD_STATE_BLOCKED = 0x0003 | |
| ZX_THREAD_STATE_DYING = 0x0004 | |
| ZX_THREAD_STATE_DEAD = 0x0005 | |
| # More precise thread states. | |
| ZX_THREAD_STATE_BLOCKED_EXCEPTION = 0x0103 | |
| ZX_THREAD_STATE_BLOCKED_SLEEPING = 0x0203 | |
| ZX_THREAD_STATE_BLOCKED_FUTEX = 0x0303 | |
| ZX_THREAD_STATE_BLOCKED_PORT = 0x0403 | |
| ZX_THREAD_STATE_BLOCKED_CHANNEL = 0x0503 | |
| ZX_THREAD_STATE_BLOCKED_WAIT_ONE = 0x0603 | |
| ZX_THREAD_STATE_BLOCKED_WAIT_MANY = 0x0703 | |
| ZX_THREAD_STATE_BLOCKED_INTERRUPT = 0x0803 | |
| ZX_THREAD_STATE_BLOCKED_PAGER = 0x0903 | |
| def __lt__(self, other: Any) -> bool: | |
| if isinstance(other, ThreadState): | |
| return self.value < other.value | |
| return NotImplemented | |
| class SchedulingRecord: | |
| """A record giving us information about cpu scheduling decisions""" | |
| # Subclasses must declare their own __slots__; see "Memory layout of the | |
| # model classes" at the top of this file. | |
| __slots__ = ("start", "tid", "prio", "_args") | |
| def __init__( | |
| self, | |
| start: trace_time.TimePoint, | |
| tid: int, | |
| prio: int | None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| self.start: trace_time.TimePoint = start | |
| self.tid: int = tid | |
| self.prio: int | None = prio | |
| # Copy non-empty args so the record is isolated from later changes to | |
| # the caller's mapping, even if that mapping is itself a read-only | |
| # view of something mutable. | |
| self._args: MappingProxyType[str, Any] = _read_only_args( | |
| dict(args) if args else None | |
| ) | |
| @property | |
| def args(self) -> MappingProxyType[str, Any]: | |
| return self._args | |
| @args.setter | |
| def args(self, rhs: Mapping[str, Any] | None) -> None: | |
| self._args = _read_only_args(rhs) | |
| def is_idle(self) -> bool: | |
| """ | |
| True if the incoming thread is the idle thread | |
| """ | |
| return self.prio == INT32_MIN | |
| class ContextSwitch(SchedulingRecord): | |
| """A record indicating that a thread has been scheduled on a given cpu""" | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = ("outgoing_tid", "outgoing_prio", "outgoing_state") | |
| def __init__( | |
| self, | |
| start: trace_time.TimePoint, | |
| incoming_tid: int, | |
| outgoing_tid: int, | |
| incoming_prio: int | None, | |
| outgoing_prio: int | None, | |
| outgoing_state: ThreadState, | |
| args: Mapping[str, Any] | None = None, | |
| ): | |
| super().__init__(start, incoming_tid, incoming_prio, args) | |
| self.outgoing_tid = outgoing_tid | |
| self.outgoing_prio = outgoing_prio | |
| self.outgoing_state = outgoing_state | |
| class Waking(SchedulingRecord): | |
| """A record indicating that a thread has been unblocked and is waiting to run on a given cpu""" | |
| # Empty, but required: without it Waking gets a per-instance __dict__. | |
| # See "Memory layout of the model classes" at the top of this file. | |
| __slots__ = () | |
| def __init__( | |
| self, | |
| start: trace_time.TimePoint, | |
| tid: int, | |
| prio: int | None, | |
| args: Mapping[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__(start, tid, prio, args) | |
| class Thread: | |
| """A thread within a trace model.""" | |
| def __init__( | |
| self, | |
| tid: int, | |
| name: str | None = None, | |
| events: list[Event] | None = None, | |
| ) -> None: | |
| self.tid: int = tid | |
| self.name: str = "" if name is None else name | |
| self.events: list[Event] = [] if events is None else events | |
| class Process: | |
| """A process within a trace model.""" | |
| def __init__( | |
| self, | |
| pid: int, | |
| name: str | None = None, | |
| threads: list[Thread] | None = None, | |
| ) -> None: | |
| self.pid: int = pid | |
| self.name: str = "" if name is None else name | |
| self.threads: list[Thread] = [] if threads is None else threads | |
| class Model: | |
| """The root of the trace model.""" | |
| def __init__( | |
| self, | |
| processes: Sequence[Process], | |
| scheduling_records: Mapping[int, Sequence[SchedulingRecord]], | |
| ): | |
| self.processes: Sequence[Process] = processes | |
| self.scheduling_records: Mapping[ | |
| int, Sequence[SchedulingRecord] | |
| ] = scheduling_records | |
| def all_events(self) -> Iterator[Event]: | |
| for process in self.processes: | |
| for thread in process.threads: | |
| for event in thread.events: | |
| yield event | |
| def slice( | |
| self, | |
| start: trace_time.TimePoint | None = None, | |
| end: trace_time.TimePoint | None = None, | |
| ) -> Self: | |
| """Extract a sub-Model defined by a time interval. | |
| Args: | |
| start: Start of the time interval. If None, the start time of the | |
| source Model is used. Only trace events that begin at or after | |
| `start` will be included in the sub-model. | |
| end: End of the time interval. If None, the end time of the source | |
| Model is used. Only trace events that end at or before `end` | |
| will be included in the sub-model. | |
| """ | |
| # The various event types have references to other events, which we will | |
| # need to update so that all the relations in the new model stay within | |
| # the new model. This dict tracks for each event of the old model, which | |
| # event in the new model it corresponds to. | |
| new_event_map: dict[Any, Event] = {} | |
| def get_new_event[T](old_event: T) -> T | None: | |
| new_event: Event | None = new_event_map.get(old_event) | |
| if not new_event: | |
| return None | |
| assert isinstance(new_event, type(old_event)) | |
| return new_event | |
| # Step 1: Populate the model with new event objects. These events will | |
| # have references into the old model. | |
| new_processes = [] | |
| for process in self.processes: | |
| new_process = Process(process.pid, process.name) | |
| new_processes.append(new_process) | |
| for thread in process.threads: | |
| new_thread = Thread(thread.tid, thread.name) | |
| new_process.threads.append(new_thread) | |
| for event in thread.events: | |
| # Exclude any event that starts or ends outside of the | |
| # specified range. | |
| if (start is not None and event.start < start) or ( | |
| end is not None and event.start > end | |
| ): | |
| continue | |
| # Exclude any event whose duration ends outside of the | |
| # specified range. | |
| if isinstance(event, (AsyncEvent, DurationEvent)): | |
| if ( | |
| end is not None | |
| and event.duration > end - event.start | |
| ): | |
| continue | |
| new_event = copy.copy(event) | |
| new_thread.events.append(new_event) | |
| new_event.args = event.args | |
| new_event_map[event] = new_event | |
| # Step 2: Replace all referenced events by their corresponding ones in | |
| # the new model. | |
| for process in new_processes: | |
| for thread in process.threads: | |
| for event in thread.events: | |
| if isinstance(event, DurationEvent): | |
| if event.parent: | |
| event.parent = get_new_event(event.parent) | |
| updated_child_durations = [] | |
| for de in event.child_durations: | |
| new_duration = get_new_event(de) | |
| if new_duration is not None: | |
| updated_child_durations.append(new_duration) | |
| event.child_durations = updated_child_durations | |
| updated_child_flows = [] | |
| for fe in event.child_flows: | |
| new_flow = get_new_event(fe) | |
| if new_flow is not None: | |
| updated_child_flows.append(new_flow) | |
| event.child_flows = updated_child_flows | |
| elif isinstance(event, FlowEvent): | |
| event.enclosing_duration = get_new_event( | |
| event.enclosing_duration | |
| ) | |
| event.previous_flow = get_new_event(event.previous_flow) | |
| event.next_flow = get_new_event(event.next_flow) | |
| def slice_scheduling_records(record: SchedulingRecord) -> bool: | |
| return (start is None or record.start >= start) and ( | |
| end is None or record.start <= end | |
| ) | |
| new_scheduling_records = { | |
| cpu: list(filter(slice_scheduling_records, records)) | |
| for cpu, records in self.scheduling_records.items() | |
| } | |
| return self.__class__(new_processes, new_scheduling_records) |