Add critical path information to the gecko profiler
Categories
(Core :: Gecko Profiler, 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.
Comment 1•16 days ago
•
|
||
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.
| Assignee | ||
Updated•16 days ago
|
Updated•16 days ago
|
| Assignee | ||
Comment 2•15 days ago
|
||
(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.
| Assignee | ||
Comment 3•15 days ago
|
||
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
| Assignee | ||
Comment 4•15 days ago
|
||
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.
| Assignee | ||
Comment 5•15 days ago
|
||
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.
| Assignee | ||
Comment 6•15 days ago
|
||
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.
| Assignee | ||
Comment 8•10 days ago
|
||
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.
| Assignee | ||
Comment 9•8 days ago
|
||
Discussed with mstange and jrmuizel, the the proposal to upgrade some flow markers to causal markers, and then add more causal markers, seems acceptable.
| Assignee | ||
Updated•7 days ago
|
| Assignee | ||
Comment 10•1 day ago
|
||
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.
| Assignee | ||
Comment 11•1 day ago
|
||
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.
| Assignee | ||
Comment 12•1 day ago
|
||
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.
| Assignee | ||
Comment 13•1 day ago
|
||
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.
| Assignee | ||
Comment 14•1 day ago
|
||
| Assignee | ||
Comment 15•1 day ago
|
||
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.
| Assignee | ||
Comment 16•1 day ago
|
||
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.
| Assignee | ||
Comment 17•1 day ago
|
||
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.
| Assignee | ||
Comment 18•1 day ago
|
||
| Assignee | ||
Comment 19•1 day ago
|
||
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.
| Assignee | ||
Comment 20•1 day ago
|
||
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.
| Assignee | ||
Comment 21•1 day ago
|
||
| Assignee | ||
Comment 22•1 day ago
|
||
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.
| Assignee | ||
Comment 23•2 hours ago
|
||
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.
| Assignee | ||
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Updated•2 hours ago
|
Description
•