Repository navigation
Windows: Don't make RwLock writers race readers - #165337
Conversation
|
Thanks for the pull request, and welcome! The Rust Project has assigned @ChrisDenton (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks. Please see the contribution instructions and our LLM policy for more information. |
| writer_notify: Futex, | ||
| // The number of writers going to sleep on `writer_notify`, for `wake_writer`. | ||
| #[cfg(target_os = "windows")] | ||
| blocked_writers: Futex, |
There was a problem hiding this comment.
This doesn't need to be futex-sized, but it must be large enough to hold the number of possible threads in the system if every one of them tried to write-lock at once. I suggest using AtomicU32 explicitly, that's large enough on Windows (thread IDs are 32-bit, so there cannot be more than
There was a problem hiding this comment.
The fix in general looks good but I'm a little uncomfortable sprinkling #[cfg(windows)] about the place, especially as this affects other platforms. Maybe this can be done using a separate wrapper type (e.g. BlockedWriters) that's just a ZST on most platforms? Or some way to make it easy to opt platforms in/out of this specifically.
| if self.state.compare_exchange(state, READERS_WAITING, Relaxed, Relaxed).is_err() { | ||
| // Acquire, so that `wake_writer` can't see a `blocked_writers` count from | ||
| // before the unlock of a writer that has already left. | ||
| if self.state.compare_exchange(state, READERS_WAITING, Acquire, Relaxed).is_err() { |
There was a problem hiding this comment.
I wouldn't have thought so but does this change have any measurable effect on Linux?
There was a problem hiding this comment.
No. x86_64 generates the same code for all of std. On aarch64 it's just __aarch64_cas4_relax -> __aarch64_cas4_acq on the path where readers and writers are both waiting. I compared the asm but didn't run aarch64. Can make the Acquire conditional if you prefer.
2fc9036 to
aefbd97
Compare
|
Done. Windows only for now. FreeBSD, DragonFly and Fuchsia would just need adding to the |
|
Thanks, I'll hand this over to joboet to double check the correctness r? joboet |
|
|
|
|
||
| // Wake any readers it left waiting too, so that they don't have to wait | ||
| // for a whole queue of writers. | ||
| if BlockedWriters::ENABLED { | ||
| self.wake_writer_or_readers(READERS_WAITING); | ||
| } |
There was a problem hiding this comment.
I would prefer to leave this out. The benefits here are far less clear cut, and this should probably apply to all (or no) target.
There was a problem hiding this comment.
Done. The numbers in the description are updated to match.
|
Reminder, once the PR becomes ready for a review, use |
`WakeByAddressSingle` doesn't report whether it woke anything, so `futex_wake` is always false on Windows and `wake_writer_or_readers` wakes a writer and the readers together. The writer usually loses. Count the writers going to sleep on `writer_notify` and treat a non-zero count as a wakeup that reached a writer.
aefbd97 to
f0f1f89
Compare
|
@rustbot ready |
|
Alright, this looks fine. Thank you! @bors r+ |
…uwer Rollup of 9 pull requests Successful merges: - #166487 (core: Use autotests to split up coretests) - #165145 (Various incr comp session refactors) - #165337 (Windows: Don't make RwLock writers race readers) - #165732 (feat: rename region obligations) - #166061 (Offload: generic kernels in a lib) - #166539 (Restore the section in `global-target-feature.rs`) - #165721 (avoid Terminator:None in coroutine drop shim building) - #166453 (perf: remove cache in `FoldEscapingRegions`) - #166547 (Pin i686 msvc)
Rollup merge of #165337 - R3nzTheCodeGOD:windows-rwlock-writers, r=joboet Windows: Don't make RwLock writers race readers Fixes #165314 The futex `RwLock` is meant to prefer writers. #95801 does that by waking a writer first and only waking readers if `futex_wake` says there was no writer to wake. `WakeByAddressSingle` doesn't report that, so on Windows `futex_wake` is always false and a writer and the readers are woken together to race, which is the thing #95801 set out to avoid. The writer usually loses. Just returning `true` doesn't work: `WRITERS_WAITING` can be stale, and then nothing ever wakes the readers. So this counts the writers going to sleep on `writer_notify`. If the count isn't zero, a writer still has to retry the lock. It'll either take it or find it held, and either way whoever unlocks will see `READERS_WAITING`. The compare-exchange before `wake_writer` must be `Acquire`. With `Relaxed` it can see the unlock of a writer that has already left and still read the old count, which leaves the readers asleep for good. Miri finds this if you force the interleaving, and a litmus test of just this ordering on an Apple M4 saw the old count 718829 times in 2.9 billion runs with `Relaxed` and never in 2.8 billion with `Acquire`. It can't happen on x86, where the generated code is the same. Before and after on a Ryzen 5 5600 with Windows 10. Locks are held for 20 us unless noted. | | writes/s | Writer p99 | reads/s | |---|---:|---:|---:| | Program from the issue | 1000 -> 6410 | 4.5 ms -> 36 us | | | 1 writer every 1 ms, 6 readers | x1.6 | 4 ms -> 47 us | +10% | | 3 writers, 6 readers | x2.4 | 2 ms -> 77 us | -30% | | 4 writers, 12 readers, 5 us holds | x31 | 24 ms -> 766 us | -79% | The last row is the cost. Writers go first, as on Linux, so with that many of them the readers mostly wait, and total operations drop 73%. In a `HashMap` cache shared by 6-24 threads, with 0.1-10% writes, 0-20 us between operations and 0-20 us read holds, total throughput rose in 19 of these 54 configurations and fell in 3, by about a quarter in the two with 10% writes and nothing between operations. Reader p99 got more than 1.5x worse in 8, all but one with 10% writes. Typst doesn't contend on any `RwLock` while compiling, so it doesn't change. `RwLock<T>` grows by 4 bytes unless `T` is aligned to 8. The uncontended paths are effectively unchanged. The queue-based `RwLock` is the obvious alternative and loses much less under heavy writes, but it makes uncontended reads 32% slower. Tested with `./x test library/std` and Miri on x86_64 only. FreeBSD, DragonFly and Fuchsia have the same `futex_wake` behavior and are left as they are. Adding them to the `cfg_select!` opts them in.
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#166487 (core: Use autotests to split up coretests) - rust-lang/rust#165145 (Various incr comp session refactors) - rust-lang/rust#165337 (Windows: Don't make RwLock writers race readers) - rust-lang/rust#165732 (feat: rename region obligations) - rust-lang/rust#166061 (Offload: generic kernels in a lib) - rust-lang/rust#166539 (Restore the section in `global-target-feature.rs`) - rust-lang/rust#165721 (avoid Terminator:None in coroutine drop shim building) - rust-lang/rust#166453 (perf: remove cache in `FoldEscapingRegions`) - rust-lang/rust#166547 (Pin i686 msvc)
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#166487 (core: Use autotests to split up coretests) - rust-lang/rust#165145 (Various incr comp session refactors) - rust-lang/rust#165337 (Windows: Don't make RwLock writers race readers) - rust-lang/rust#165732 (feat: rename region obligations) - rust-lang/rust#166061 (Offload: generic kernels in a lib) - rust-lang/rust#166539 (Restore the section in `global-target-feature.rs`) - rust-lang/rust#165721 (avoid Terminator:None in coroutine drop shim building) - rust-lang/rust#166453 (perf: remove cache in `FoldEscapingRegions`) - rust-lang/rust#166547 (Pin i686 msvc)
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#166487 (core: Use autotests to split up coretests) - rust-lang/rust#165145 (Various incr comp session refactors) - rust-lang/rust#165337 (Windows: Don't make RwLock writers race readers) - rust-lang/rust#165732 (feat: rename region obligations) - rust-lang/rust#166061 (Offload: generic kernels in a lib) - rust-lang/rust#166539 (Restore the section in `global-target-feature.rs`) - rust-lang/rust#165721 (avoid Terminator:None in coroutine drop shim building) - rust-lang/rust#166453 (perf: remove cache in `FoldEscapingRegions`) - rust-lang/rust#166547 (Pin i686 msvc)
Fixes #165314
The futex
RwLockis meant to prefer writers. #95801 does that by waking a writer first and only waking readers iffutex_wakesays there was no writer to wake.WakeByAddressSingledoesn't report that, so on Windowsfutex_wakeis always false and a writer and the readers are woken together to race, which is the thing #95801 set out to avoid. The writer usually loses.Just returning
truedoesn't work:WRITERS_WAITINGcan be stale, and then nothing ever wakes the readers. So this counts the writers going to sleep onwriter_notify. If the count isn't zero, a writer still has to retry the lock. It'll either take it or find it held, and either way whoever unlocks will seeREADERS_WAITING.The compare-exchange before
wake_writermust beAcquire. WithRelaxedit can see the unlock of a writer that has already left and still read the old count, which leaves the readers asleep for good. Miri finds this if you force the interleaving, and a litmus test of just this ordering on an Apple M4 saw the old count 718829 times in 2.9 billion runs withRelaxedand never in 2.8 billion withAcquire. It can't happen on x86, where the generated code is the same.Before and after on a Ryzen 5 5600 with Windows 10. Locks are held for 20 us unless noted.
The last row is the cost. Writers go first, as on Linux, so with that many of them the readers mostly wait, and total operations drop 73%. In a
HashMapcache shared by 6-24 threads, with 0.1-10% writes, 0-20 us between operations and 0-20 us read holds, total throughput rose in 19 of these 54 configurations and fell in 3, by about a quarter in the two with 10% writes and nothing between operations. Reader p99 got more than 1.5x worse in 8, all but one with 10% writes. Typst doesn't contend on anyRwLockwhile compiling, so it doesn't change.RwLock<T>grows by 4 bytes unlessTis aligned to 8. The uncontended paths are effectively unchanged. The queue-basedRwLockis the obvious alternative and loses much less under heavy writes, but it makes uncontended reads 32% slower.Tested with
./x test library/stdand Miri on x86_64 only. FreeBSD, DragonFly and Fuchsia have the samefutex_wakebehavior and are left as they are. Adding them to thecfg_select!opts them in.