-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Heavy contention on blocking #2528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It looks to me like the lock is held for the minimum and correct span (e.g. not including actually running the blocking operation closure). In my own testing of an alternative thread pool (in blocking-permit crate), use of (parking_lot) mutex lock and condvar with minimized notifications appears to give the best possible performance, at least on linux. What if anything is suggested as an alternative? |
The solution here would be to use a lockless queue, e.g. crossbeam_deque. @Darksonn On that note, why was crossbeam stuff purged from tokio? Every tokio runtime's injector uses locks which seems like a clear downgrade from a lockless queue. |
I've seen this causing perf issues before on code which does a ton of file io. |
I've spent a bit of time thinking about what the optimal solution could look like:
Tasks are pushed to the SegQueue. Active threads poll the queue, twice if |
We generally want to avoid adding new dependencies if we can at all avoid it. |
One nice thing about the injector queue is that --- unlike run queues --- tasks cannot be dropped from outside of the queue while in the injector queue. This means that we may be able to use an approach like Vyukov's intrusive singly-linked MPSC queue for injector queues --- this is something I've wanted to look into for a while, actually. |
Don't we need MPMC though? |
I've experienced this in benchmarks with high RPS doing very small reads from files on a ramdisk. Replacing |
|
Code that invokes
spawn_blocking
, orblock_in_place
(which internally callsspawn_blocking
), all end up taking a shared lock here:tokio/tokio/src/runtime/blocking/pool.rs
Line 153 in 221f421
This causes a lot of unnecessary contention between unrelated tasks if you frequently need to run blocking code. The problem is exacerbated as load increases, because more calls to
spawn_blocking
causes each individual call to become more expensive, and each call holds up an executor thread.The text was updated successfully, but these errors were encountered: