CPU and wall time per fiber and per span, from sys.monitoring.
Effect has no profiler of its own (Node's --cpu-prof sees one stack of
callbacks). Python's cProfile sees the fiber loop calling steps, so the
time of every fiber lands in the same few runtime functions. This one counts
each fiber's turns instead: a fiber runs in turns (Fiber._run), each one
on one thread, until it waits or yields, and the profiler times each turn
with that thread's CPU clock and the wall clock.
It watches the start and return of Fiber._run with sys.monitoring
local events, on that one code object, so the loop has no code for it and
pays nothing while no profile runs: no event is set and no callback is
registered. While one runs, each turn costs two callbacks, whatever the
number of steps in it::
with fiber_profiler.profile() as report:
fx.run_blocking(program)
for fiber_id, t in report.fibers.items():
print(fiber_id, t.cpu_ns, t.wall_ns, t.turns)Inside an effect, report, value = yield from fiber_profiler.profiled(program).
Times are exclusive: a fiber that resumes another in its own turn (a wake made on the run's thread runs the woken fiber at once) does not count the other's turn. A turn's span is the fiber's innermost span when the turn ends. One profile runs at a time, across every thread.
Classes#
Functions#
profile#
profile() -> Generator[Report]Time every fiber turn, on every thread, until the block ends; the
report it yields is filled in then; a turn still running on another
thread when the block ends is not counted. Raises ProfilerBusyError
if a profile is already running.
Returns Generator[Report]
@contextmanager
def profile() -> Generator[Report]:
"""Time every fiber turn, on every thread, until the block ends; the
report it yields is filled in then; a turn still running on another
thread when the block ends is not counted. Raises ``ProfilerBusyError``
if a profile is already running."""
session = _start()
try:
yield session.report
finally:
session.stop()profiled#
profiled[A, E, R](eff) -> Effect[tuple[Report, A], E, R]profile() around eff, pairing its value with the complete report (as
fx.timed pairs it with a duration). Every turn of eff counts, on its
own fiber too, and so does every other fiber's turn, on every thread, while it
runs. A profile already running makes it die with ProfilerBusyError; a
failure or interruption ends the profile and drops the report.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[A, E, R] |
Returns Effect[tuple[Report, A], E, R]
def profiled[A, E, R](eff: Effect[A, E, R]) -> Effect[tuple[Report, A], E, R]:
"""``profile()`` around ``eff``, pairing its value with the complete report (as
``fx.timed`` pairs it with a duration). Every turn of ``eff`` counts, on its
own fiber too, and so does every other fiber's turn, on every thread, while it
runs. A profile already running makes it die with ``ProfilerBusyError``; a
failure or interruption ends the profile and drops the report."""
require_effect(eff, "eff")
# The yields end the turns that start and stop the profile, which it cannot count.
def use(session: _Session) -> Effect[tuple[Report, A], E, R]:
return yield_now().and_then(eff).flat_map(lambda a: yield_now().as_((session.report, a)))
return acquire_use_release(sync(_start), use, lambda session, _: sync(session.stop))