Closed Bug 1983309 Opened 1 year ago Closed 3 months ago

Reduce locking contention in TaskQueue

Categories

(Core :: XPCOM, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox153 --- fixed

People

(Reporter: nika, Assigned: nika)

References

(Depends on 1 open bug)

Details

Attachments

(2 files)

Currently, when running a number of tasks dispatched to a TaskQueue, we acquire the TaskQueue's QueueMonitor 3 times per-task. This is an excessive number of locking calls, and is likely partially responsible for worsening the locking contention issue from bug 1981051.

This patch aims to reduce lock contention when dispatching to TaskQueue, by reducing the frequency with which the runner thread needs to acquire the TaskQueue monitor.
Prior to this change, if there is a backlog of queued runnables, TaskQueue::Runner would acquire mQueueMonitor 3 times per event. After this change, mQueueMonitor will only be acquired when the set of tasks read the last time the monitor was held have all been processed.
TaskQueue would need to change to use an unbounded lock-free MPSC queue in order to further reduce contention when dispatching. Doing so is a much larger project, and would require more substantial changes to the TaskQueue type to make state management (especially around shutdown) avoid locking.

See Also: → 1981051
Pushed by chorotan@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/9e34621d5341 https://hg.mozilla.org/integration/autoland/rev/cfab6ef2c2e6 Revert "Bug 1983309 - Reduce lock contention in TaskQueue, r=xpcom-reviewers,jstutte" for causing multiple failures
Flags: needinfo?(nika)
Regressions: 1986817
No longer regressions: 1986817

There is an r+ patch which didn't land and no activity in this bug for 2 weeks.
:nika, could you have a look please?
If you still have some work to do, you can add an action "Plan Changes" in Phabricator.
For more information, please visit BugBot documentation.

Flags: needinfo?(nika)
Flags: needinfo?(jstutte)

I think there is just higher priority work than fixing the backout, right now.

Flags: needinfo?(nika)
Flags: needinfo?(jstutte)
Flags: needinfo?(nika)
Depends on: 2006069

MaybeAsyncCreateAudioSink used mAsyncInitTaskQueue->IsEmpty() inside
the dispatched runnable as a "let the newest call do the init"
coalesce check. With the previous commit's bulk-steal Runner,
IsEmpty() returns !mIsRunning rather than mTasks.IsEmpty(), so it
stays false for the duration of any in-flight runnable and the
coalesce check always reports "newer queued" — the AudioSink would
never actually initialize.

Replace the TaskQueue-state query with an explicit monotonic dispatch
sequence counter on the wrapper. Each MaybeAsyncCreateAudioSink call
increments mAsyncDispatchSeq and the dispatched runnable captures the
value; if the live counter has advanced past that snapshot when the
runnable runs, a newer call has superseded it and we short-circuit.
Only the newest dispatch does the init, preserving the existing
"later wins" semantic.

Attachment #9507370 - Attachment description: Bug 1983309 - Reduce lock contention in TaskQueue, r=#xpcom-reviewers! → Bug 1983309 - Reduce lock contention in TaskQueue. r=#xpcom-reviewers
Attachment #9585969 - Attachment description: WIP: Bug 1983309 - Stop relying on TaskQueue::IsEmpty() from inside an AudioSinkWrapper init runnable. r?#media-reviewers → Bug 1983309 - Stop relying on TaskQueue::IsEmpty() from inside an AudioSinkWrapper init runnable. r?#media-playback-reviewers
Pushed by jstutte@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/3d4820e909f8 https://hg.mozilla.org/integration/autoland/rev/8d217b003092 Stop relying on TaskQueue::IsEmpty() from inside an AudioSinkWrapper init runnable. r=media-playback-reviewers,nika,karlt https://github.com/mozilla-firefox/firefox/commit/325ec7d64ad2 https://hg.mozilla.org/integration/autoland/rev/aec432fc144f Reduce lock contention in TaskQueue. r=xpcom-reviewers,jstutte
Status: NEW → RESOLVED
Closed: 3 months ago
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: