Open Bug 2094156 Opened 16 days ago Updated 2 hours ago

Add critical path information to the gecko profiler

Categories

(Core :: Gecko Profiler, enhancement)

enhancement

Tracking

()

People

(Reporter: kats, Assigned: kats)

References

Details

(Whiteboard: [fxpe])

Attachments

(1 file, 13 external files)

749.12 KB, image/png
Details

It would be super valuable for the profiler to expose the "critical path" leading up to a given end marker. It would do this by tracing backwards from the marker, crossing threads as needed for enqueue/dispatch of runnables and other such cross-process interaction, until it got to the start of the profile. In terms of choosing where to focus optimization efforts, stuff that is consistently on the critical path should generally yield the most benefit.

Note that I'm mostly interested in ensuring this information is saved in the profile and can be computed by the profiler CLI. Displaying it in the profiler UI is a separate problem that is explicitly not part of this bug.

In order to actually implement this, we will need to augment the data currently saved in a profile. The notion of "flows" as it currently exists, I believe would provide some useful information but doesn't always give direct causal links between two threads. Based on a discussion with Claude it seems that network channels use flows, to link multiple different actions related to a channel into a single flow. This kind of usage overlaps but not quite the direct causal links we would want for critical path detection. I'm not sure yet if it makes sense to have a different kind of marker for causal links.

The notion of "flows" as it currently exists, I believe would provide some useful information but doesn't always give direct causal links between two threads.

Yes not always, but it gets you pretty far. Whenever the communication between two threads is runnable-based, i.e. if thread A puts a runnable in thread B's queue, and if thread A does the enqueuing while it's currently running a runnable itself, then we can connect the two runnables. Let's call the flows for the two runnables "X" and "Y". The UI prototype in https://github.com/firefox-devtools/profiler/pull/5190 makes X an "incoming flow" for Y; it creates this relationship by noticing that Y has an instant marker (for the enqueuing) which occurred while a stack-based marker (the duration marker of runnable X's Run method) that belongs to flow X was already on the same thread. So the association also depends on marking certain duration markers as stack-based, and it depends on the same-thread-ness.

We should enumerate the cases where we don't have flow connections between threads. I believe there might be some around timers; jrmuizel has local patches to address some of the gaps.

We have https://github.com/firefox-devtools/profiler/issues/6225 for adding flow support to profiler-cli.

Whiteboard: [fxpe]

(In reply to Markus Stange [:mstange] from comment #1)

We should enumerate the cases where we don't have flow connections between threads. I believe there might be some around timers; jrmuizel has local patches to address some of the gaps.

I steered claude to do a full implementation which seems to have turned out pretty well. It got to the point where it a full speedometer run can be traced from the end of iteration-9 all the way through to the start of iteration-1. I had it implement new markers alongside the flow ones (and in a lot of cases it just updated the existing macro to do both markers) because I still think conceptually they are a bit different and for a PoC I didn't want to conflate the two. Happy to revisit that if needed.

Patches can be found at:
https://github.com/mozilla-firefox/firefox/compare/main...staktrace:firefox:causal-markers
https://github.com/firefox-devtools/profiler/compare/main...staktrace:profiler:causal-markers

I'm still iterating on this so patches may change.

Also for future reference: to do this successfully you generally want to profile all the threads, and increase the buffer size as it generates a lot of markers. For my speedometer profiles with the above patches I had to run with: ./mach raptor --browsertime -t speedometer3 --page-cycles 1 --gecko-profile --gecko-profile-features "stackwalk,js,cpu,flows" --gecko-profile-threads "*" --gecko-profile-entries 200000000

I know I said the UI changes were out of scope for this bug but it turned out that was the easiest way to evaluate the implementation that Claude cooked up.

In this screenshot we can see the new track at the top for the critical path. I mouse-over'd a random point which gives a tooltip with some information as to which thread is on the critical path and why. The Focus button (toggled on in the screenshot) in the track label uses the existing focus code to focus in on only things that are on the critical path, which gives us a better understanding of where to invest optimization effort.

It seems possible to combine the new markers with the old markers to remove duplication. However, the causal flow markers have more strict semantics than the existing flow markers, so they do need to be distinct marker types. Effectively, some of the existing flow markers would be "upgraded" to causal markers, while some would not. And then new causal markers would be added to fill in the missing gaps. The profiler side would be able to use the causal markers to do the critical path analysis, and causal+flow markers for visualizing flows.

What do you think of the approach in the previous comment? Worth pursuing or is there a different direction I should go in?

Overall the gecko changes in the branch look reasonable to me. The profiler side changes are much more involved and I haven't dug into that side yet.

Flags: needinfo?(mstange.moz)

Jeff, can you take a look too?

Flags: needinfo?(jmuizelaar)

Just to clarify: currently the gecko patches are structured to have the causal markers alongside the existing flow markers, so there's a lot of duplication. I want to restructure the gecko patches so that they are simpler to understand, by reusing the existing flow markers where possible. In order to do that, the existing flow markers would be split into two categories - one would be "causal" markers used for the critical path, and the other would be regular flow markers to associate arbitrary "flow" things. That's the change I want feedback on, to see if it makes sense or if I have any misunderstandings and there's a better architecture to pursue.

Depends on: 2096642

Discussed with mstange and jrmuizel, the the proposal to upgrade some flow markers to causal markers, and then add more causal markers, seems acceptable.

Flags: needinfo?(jmuizelaar) needinfo?(mstange.moz)
Depends on: 2098223
Depends on: 2099982

This adds the infrastructure and utility code for causal flow
markers, and documents them in the markers-guide. It adds the new
marker types next to the existing flow marker types, along with C++
macros to use them easily. It also adds equivalent APIs in Rust,
so that a flow a can have one end in Rust and the other in C++.
In all cases the markers are only recorded when the flows feature is
active.

CausalPending and related code allow for coalescing when many requests
are served by one piece of work, such as multiple requests for a refresh
driver tick served by a single tick. This coalescing prevents flooding
the buffer.

This upgrades the flow markers to be casual markers which have stricter
semantics.

  • The marker in TaskController::AddTask becomes CausalFlowMarker, the trigger.
  • The "task" field of the Task marker becomes CausalFlowTerminating, the effect.
  • The "task" field of the IncompleteTask marker becomes CausalFlowNonterminating,
    because it runs again. Note that the schema here was also named "Task", and
    schemas are keyed by name in the profile, so only one of the two formats was
    ever streamed. This fixes the pre-existing issue by renaming it to "IncompleteTask".

To help keep things in sync between the terminating and nonterminating variants,
this patch also extracts a macro to avoid duplication.

This patch upgrades a few other pre-existing flow markers for runnables and
IPC flows to causal markers. The IPC one necessitated a new marker to
distinguish between the send and receive sides (cause and effect). As
with the previous commit a macro is used to avoid code duplication.

Some runnables have a Run() that is the main loop of their thread: it only
returns when the thread shuts down. Their Runnable marker therefore spans
the life of the thread and contains the markers of everything else the
thread does.

Now that the Runnable marker is the effect of a causal flow, that marker
says the thread was running one piece of work the whole time. This can
confuse the critical path analysis.

This commit fixes the problem by adding mozilla::EventLoopRunnable for such
runnables to implement. In such cases the marker on the Runnable is skipped,
the same way it's done for nsIThreadPool.

MessageLoop::PostTask queues the task without going through an event queue
that records a flow, so the Runnable marker of a task run by a MessageLoop
had no trigger. Record one when the task is posted.

The cache I/O thread has its own prioritized event queue and runs its events
directly. Those events had neither a dispatch flow marker nor a Runnable marker,
so were missing from the causal flow chain. This adds the markers necessary
to include them.

A timer callback runs because the timer was armed. The callback in
nsTimerImpl::Fire is the terminating effect and the arming is the trigger.

Most timers are cancelled or re-armed before they fire, so we avoid spurious
markers by only recording the (backdated) trigger marker if the timer fires.

The id is derived from the timer's sequence number, which changes each time
the timer is initialized, and is tagged to keep it apart from the other
small process-scoped ids.

A socket becoming ready is the consequence of an earlier connect or write.
Record a trigger when nsSocketTransport::InitiateSocket starts a
non-blocking connect and when nsSocketOutputStream::Write writes bytes, and
the effect around OnSocketReady in the socket transport service's poll loop.
Many writes can precede one notification, so the markers are deduplicated
with a CausalPending.

A transaction sent to the scene builder thread is what makes the next scene
build necessary, so we record a trigger in WebRenderAPI and the causal effect
around the schene builder hooks in Rust. Note that several transactions can
be coalesced into one scene build.

Check that the three causal marker types are recorded with their schema
formats, that CausalPending records only the first of several triggers and
one effect, and that a runnable that implements EventLoopRunnable gets no
Runnable marker while other runnables do.

I think these patches are ready for review now. I'm still doing more testing and may find additional places we need markers but we can do that in follow-ups as needed.

Attachment #9697317 - Attachment description: WIP: Bug 2094156 - Add causal flow marker types and helpers. r?#profiler-reviewers → Bug 2094156 - Add causal flow marker types and helpers. r?#profiler-reviewers
Attachment #9697318 - Attachment description: WIP: Bug 2094156 - Upgrade TaskController flows to causal. r?#profiler-reviewers → Bug 2094156 - Upgrade TaskController flows to causal. r?#profiler-reviewers
Attachment #9697319 - Attachment description: WIP: Bug 2094156 - Upgrade runnable and IPC flows to causal. r?#profiler-reviewers → Bug 2094156 - Upgrade runnable and IPC flows to causal. r?#profiler-reviewers
Attachment #9697320 - Attachment description: WIP: Bug 2094156 - Do not record a Runnable marker for a thread's event loop. r?#profiler-reviewers → Bug 2094156 - Do not record a Runnable marker for a thread's event loop. r?#profiler-reviewers
Attachment #9697321 - Attachment description: WIP: Bug 2094156 - Upgrade the microtask flows to causal. r?#profiler-reviewers → Bug 2094156 - Upgrade the microtask flows to causal. r?#profiler-reviewers
Attachment #9697322 - Attachment description: WIP: Bug 2094156 - Record a causal flow for tasks posted to a MessageLoop. r?#profiler-reviewers → Bug 2094156 - Record a causal flow for tasks posted to a MessageLoop. r?#profiler-reviewers
Attachment #9697323 - Attachment description: WIP: Bug 2094156 - Record causal flows for the cache I/O thread's event queue. r?#profiler-reviewers → Bug 2094156 - Record causal flows for the cache I/O thread's event queue. r?#profiler-reviewers
Attachment #9697324 - Attachment description: WIP: Bug 2094156 - Record causal flows for timers. r?#profiler-reviewers → Bug 2094156 - Record causal flows for timers. r?#profiler-reviewers
Attachment #9697325 - Attachment description: WIP: Bug 2094156 - Record causal flows for the refresh driver. r?#profiler-reviewers → Bug 2094156 - Record causal flows for the refresh driver. r?#profiler-reviewers
Attachment #9697326 - Attachment description: WIP: Bug 2094156 - Record causal flows for sockets. r?#profiler-reviewers → Bug 2094156 - Record causal flows for sockets. r?#profiler-reviewers
Attachment #9697327 - Attachment description: WIP: Bug 2094156 - Record causal flows for WebRender scene builds. r?#profiler-reviewers → Bug 2094156 - Record causal flows for WebRender scene builds. r?#profiler-reviewers
Attachment #9697328 - Attachment description: WIP: Bug 2094156 - Record causal flows for async stylesheet parsing. r?#profiler-reviewers → Bug 2094156 - Record causal flows for async stylesheet parsing. r?#profiler-reviewers
Attachment #9697329 - Attachment description: WIP: Bug 2094156 - Test the causal flow markers. r?#profiler-reviewers → Bug 2094156 - Test the causal flow markers. r?#profiler-reviewers
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: