Skip to content

[WIP] Causal flows and critical path analysis - #6507

Draft
staktrace wants to merge 15 commits into
firefox-devtools:mainfrom
staktrace:causal-flow
Draft

staktrace wants to merge 15 commits into
firefox-devtools:mainfrom
staktrace:causal-flow

Conversation

@staktrace

@staktrace staktrace commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Main | Deploy preview

Part of https://bugzilla.mozilla.org/show_bug.cgi?id=2094156. This is still under development and should not be reviewed yet.

…Entry

Pairs causal triggers and effects across threads following the pairing rule
(earliest open trigger is primary, shared effects do not consume, no pairing
with consumed triggers) and finds the same-thread causal entry point from
interval nesting. Includes unit tests.
markerCause reports one causal hop around a marker: its causal fields, the
resolved primary and alternate causes of an effect, the effects of a
trigger, and the enclosing effect-carrying interval.

markerCriticalPath walks causal links backward across threads, with a pure
walk in profile-query/critical-path.ts. It stops where no causal interval
covers the position, on an unknown cause, at the maximum depth, at the
profile start, on a non-decreasing time or on a revisited marker. Waits are
split into time queued behind other work and idle time.

Where no causal interval covers the position, the walk ends in a
zero-length root segment that adds nothing to the totals: we do not know
that the path waited since the profile start. The root lists up to 3
innermost interval markers of any type that contain it, as a hint at what
to instrument next.
`marker cause m-N` shows the causal links around a marker, and `marker
critical-path m-N [--max-depth N]` prints the backward critical path as
chronological running, wait, root and unknown-cause segments with marker
handles, zoom-ready timestamp names, totals by thread and a stop reason.
Both support --json, and guide.txt and schemas.txt document them.
An effect with no known trigger that is nested in another causal interval
on the same thread ran synchronously inside it (for example a script
runner run immediately). The walk now continues in the enclosing interval
and flags the segment, instead of stopping with an unknown cause.
An effect with no trigger that starts within 100ms of the first sample of
its process most likely lost its trigger when the profiler buffer filled
up. The walk now stops with 'recording-start' instead of 'unknown-cause',
and unknown-cause segments say how far they are from that point.
"Show critical path to this marker" in the marker context menu adds a row
under the timeline ruler with the backward walk over causal links: running
segments colored by thread, waits as thinner bars (queued behind other
work or idle), and the ends of the path. Hovering a segment shows its
thread, markers and timings; clicking it selects that thread and time
range. The target is kept in the URL as criticalPath=<thread>-<marker>.
The walk followed the trigger of each effect and reported the time between
the trigger and the effect as waiting, split into "queued behind other
work" and idle. On a busy thread most of the path was that unexplained
queued time, and most of it was the task that emitted the trigger still
running after it did.

Work on a thread starts once its trigger has arrived and the thread is
free, so whatever ran on the effect's thread between the trigger and the
effect delayed it. That work is now on the path, as running segments
flagged `blocking`, and only the time the thread was idle counts as
waiting. --causal keeps the chain of causes alone.

The result also lists what the running time was spent in, and lists only
the last 200 segments by default, since a path now has one segment per
task.
The Focus button on the critical path row selects the threads that ran on
the path and keeps only the samples taken while each of them did, so the
call tree, flame graph and stack chart show the path as a single merged
thread. Samples outside of the path lose their stack before the threads
are merged, since each thread has its own time ranges on the path. Markers
are not affected. The focus is kept in the URL as criticalPathFocus.

The per-thread selectors cannot import the critical path selectors, which
use them to read every thread's markers, so the sample ranges reach them
through a small module that the critical path selectors register with.
Causal flow fields are flow fields that also carry a cause, so a marker with
a plain terminating-flow-id field ends a causal flow of the same id too. It
is not an effect and creates no link, but triggers before it no longer pair
with a later effect. Gecko emits such a marker for a runnable that is
dropped without running; without this, the next runnable allocated at the
same address was linked to the dropped runnable's dispatch.

Also describe the three causal formats as flows in the format docs.
The critical path put everything that ran on an effect's thread between its
trigger and its start on the path, as work that delayed it. That assumes the
effect was ready to run from the moment of its trigger, which is not true of
a timer: what the thread does before the timer comes due does not delay the
callback, and removing it would not make the callback run sooner.

When a nonterminating effect of the same id ran since the trigger, it is the
previous step of the flow (for a timer, the timer thread posting it to its
target thread). Start the wait of the effect at the end of that step, and
continue the walk through the step, whose own wait reaches back to the
trigger. An incomplete task followed by its completion is walked the same
way.
An effect starts once it is ready and its thread is free. The critical path
followed the trigger in every case, and listed everything that ran on the
thread between the trigger and the effect as work that delayed it. Both
halves were wrong for the question the path should answer, which is where a
marginal speedup would make the end come sooner:

- Work that was over before the thread went idle did not delay the effect:
  had the effect been ready, it would have run in the gap. Only the
  back-to-back run of tasks that ended where the effect started is on the
  path.
- When there is such a run, the thread was what the effect waited for last.
  Speeding up the work behind the trigger would change nothing, so the
  trigger is off the path, and the walk continues from what caused the
  oldest task of the run.

An effect nested in an interval (a microtask in its task) is still delayed
by the rest of that interval, and the walk still follows its trigger.

The delay is followed however short it is. MIN_DELAY_TO_FOLLOW_MS is the
cutoff below which the walk would stay on the trigger instead; it is 0 for
now, so that unrelated work that got in the way is always surfaced.
`marker critical-path <handle> --from <handle>` gives the path from the
start of one marker to the end of another.

Without a start, the walk follows whatever kept a thread busy when a step
became ready. That is right when the work belongs to what is being
measured, but any unrelated task that happened to be running takes the
path into its own chain of causes, away from the scenario.

With a start marker, a forward sweep (computeCausalDescendants) finds
everything that is causally downstream of it. The walk then only leaves
the chain of causes for work that is downstream of the start. Unrelated
work that delayed a step is still on the path, flagged `unrelated`, but
the walk goes on with the step's trigger. The walk stops at the start
marker, or reports that no chain of causal markers leads to it, and the
path is cut at the start time either way.

The timeline UI does not offer a start marker yet.
Once a critical path is shown, the marker context menu offers "Start the
critical path at this marker" for markers that start before the target
ends, and "Remove as critical path start" on the current start marker.

With a start marker the row shows the path from that marker only. Work
that delayed the path without being caused by the start marker is
hatched, and the left end of the path is red when the walk could not
reach the start marker. A "Start" button in the row label removes the
start again.

The start marker is in the URL as criticalPathStart, next to
criticalPath. It is dropped when the path is hidden, or when a new target
ends before it.
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.90613% with 143 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.34%. Comparing base (e5facc8) to head (69aa1ec).
⚠️ Report is 22 commits behind head on main.

Files with missing lines Patch % Lines
src/components/timeline/CriticalPath.tsx 82.28% 42 Missing and 3 partials ⚠️
src/profile-query/formatters/marker-causal.ts 84.26% 29 Missing and 2 partials ⚠️
src/profile-logic/causal-links.ts 92.06% 28 Missing ⚠️
src/components/shared/MarkerContextMenu.tsx 28.12% 22 Missing and 1 partial ⚠️
src/profile-query/critical-path.ts 96.27% 8 Missing ⚠️
src/test/fixtures/profiles/causal-markers.ts 80.00% 7 Missing ⚠️
src/selectors/critical-path.ts 98.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6507      +/-   ##
==========================================
+ Coverage   85.23%   85.34%   +0.10%     
==========================================
  Files         372      379       +7     
  Lines       40730    42074    +1344     
  Branches    11565    12124     +559     
==========================================
+ Hits        34717    35906    +1189     
- Misses       5573     5722     +149     
- Partials      440      446       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

When an effect was ready before its thread was free, the critical path
follows the work that kept the thread busy and not the effect's own
trigger. That is the right choice for a marginal improvement, but the
output did not say how close the call was, or what the path left out.

The running segment of such an effect now records the skipped trigger
and its slack: how long the effect was ready before it started. When the
walk did not go through the task that emitted the trigger, the trigger's
own chain of causes is followed back until it reaches a task the walk
went through, which gives how much of the path would be replaced if the
trigger were followed.

profiler-cli marker critical-path prints this on the segment, and lists
the steps that would replace the most before the segments. A step with a
small slack that replaces a large part of the path means the path nearly
went a very different way.
The critical path follows the work that kept a thread busy whenever an
effect was ready before its thread was free, however short the wait. A
wait of a few microseconds behind a task of another chain can then send
the path down that chain for seconds.

walkCriticalPath takes minSlackMs: an effect that was ready for no
longer than that before its thread was free is treated as having started
as soon as it was ready, and the walk follows its trigger. The work that
delayed it is still listed. The default of 0 keeps the path as it was.

profiler-cli marker critical-path takes it as --min-slack <ms>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant