A) fix deadlocks in thread pool caused by EventCount

This fixed 2 deadlocks caused by sloppiness in the EventCount logic.
Both most likely were introduced by cl/236729920 which includes the new EventCount algorithm:
01da8caf00

bug #1 (Prewait):
Prewait must not consume existing signals.
Consider the following scenario.
There are 2 thread pool threads (1 and 2) and 1 external thread (3). RunQueue is empty.
Thread 1 checks the queue, calls Prewait, checks RunQueue again and now is going to call CommitWait.
Thread 2 checks the queue and now is going to call Prewait.
Thread 3 submits 2 tasks, EventCount signals is set to 1 because only 1 waiter is registered the second signal is discarded).
Now thread 2 resumes and calls Prewait and takes away the signal.
Thread 1 resumes and calls CommitWait, there are no pending signals anymore, so it blocks.
As the result we have 2 tasks, but only 1 thread is running.

bug #2 (CancelWait):
CancelWait must not take away a signal if it's not sure that the signal was meant for this thread.
When one thread blocks and another submits a new task concurrently, the EventCount protocol guarantees only the following properties (similar to the Dekker's algorithm):
(a) the registered waiter notices presence of the new task and does not block
(b) the signaler notices presence of the waiters and wakes it
(c) both the waiter notices presence of the new task and signaler notices presence of the waiter
[it's only that both of them do not notice each other must not be possible, because it would lead to a deadlock]
CancelWait is called for cases (a) and (c). For case (c) it is OK to take the notification signal away, but it's not OK for (a) because nobody queued a signals for us and we take away a signal meant for somebody else.
Consider:
Thread 1 calls Prewait, checks RunQueue, it's empty, now it's going to call CommitWait.
Thread 3 submits 2 tasks, EventCount signals is set to 1 because only 1 waiter is registered the second signal is discarded).
Thread 2 calls Prewait, checks RunQueue, discovers the tasks, calls CancelWait and consumes the pending signal (meant for thread 1).
Now Thread 1 resumes and calls CommitWait, since there are no signals it blocks.
As the result we have 2 tasks, but only 1 thread is running.

Both deadlocks are only a problem if the tasks require parallelism. Most computational tasks do not require parallelism, i.e. a single thread will run task 1, finish it and then dequeue and run task 2.

This fix undoes some of the sloppiness in the EventCount that was meant to reduce CPU consumption by idle threads, because we now have more threads running in these corner cases. But we still don't have pthread_yield's and maybe the strictness introduced by this change will actually help to reduce tail latency because we will have threads running when we actually need them running.



B) fix deadlock in thread pool caused by RunQueue

This fixed a deadlock caused by sloppiness in the RunQueue logic.
Most likely this was introduced with the non-blocking thread pool.
The deadlock only affects workloads that require parallelism.
Most computational tasks don't require parallelism.

PopBack must not fail spuriously. If it does, it can effectively lead to single thread consuming several wake up signals.
Consider 2 worker threads are blocked.
External thread submits a task. One of the threads is woken.
It tries to steal the task, but fails due to a spurious failure in PopBack (external thread submits another task and holds the lock).
The thread executes blocking protocol again (it won't block because NonEmptyQueueIndex is precise and the thread will discover pending work, but it has called PrepareWait).
Now external thread submits another task and signals EventCount again.
The signal is consumed by the first thread again. But now we have 2 tasks pending but only 1 worker thread running.

It may be possible to fix this in a different way: make EventCount::CancelWait forward wakeup signal to a blocked thread rather then consuming it. But this looks more complex and I am not 100% that it will fix the bug.
It's also possible to have 2 versions of PopBack: one will do try_to_lock and another won't. Then worker threads could first opportunistically check all queues with try_to_lock, and only use the blocking version before blocking. But let's first fix the bug with the simpler change.
This commit is contained in:
Rasmus Munk Larsen 2019-05-08 10:16:46 -07:00
parent 45b40d91ca
commit e5ac8cbd7a
4 changed files with 18 additions and 23 deletions

View File

@ -20,8 +20,7 @@ namespace Eigen {
// if (predicate) // if (predicate)
// return act(); // return act();
// EventCount::Waiter& w = waiters[my_index]; // EventCount::Waiter& w = waiters[my_index];
// if (!ec.Prewait(&w)) // ec.Prewait(&w);
// return act();
// if (predicate) { // if (predicate) {
// ec.CancelWait(&w); // ec.CancelWait(&w);
// return act(); // return act();
@ -62,23 +61,17 @@ class EventCount {
} }
// Prewait prepares for waiting. // Prewait prepares for waiting.
// If Prewait returns true, the thread must re-check the wait predicate // After calling Prewait, the thread must re-check the wait predicate
// and then call either CancelWait or CommitWait. // and then call either CancelWait or CommitWait.
// Otherwise, the thread should assume the predicate may be true void Prewait() {
// and don't call CancelWait/CommitWait (there was a concurrent Notify call).
bool Prewait() {
uint64_t state = state_.load(std::memory_order_relaxed); uint64_t state = state_.load(std::memory_order_relaxed);
for (;;) { for (;;) {
CheckState(state); CheckState(state);
uint64_t newstate = state + kWaiterInc; uint64_t newstate = state + kWaiterInc;
if ((state & kSignalMask) != 0) {
// Consume the signal and cancel waiting.
newstate -= kSignalInc + kWaiterInc;
}
CheckState(newstate); CheckState(newstate);
if (state_.compare_exchange_weak(state, newstate, if (state_.compare_exchange_weak(state, newstate,
std::memory_order_seq_cst)) std::memory_order_seq_cst))
return (state & kSignalMask) == 0; return;
} }
} }
@ -118,8 +111,13 @@ class EventCount {
for (;;) { for (;;) {
CheckState(state, true); CheckState(state, true);
uint64_t newstate = state - kWaiterInc; uint64_t newstate = state - kWaiterInc;
// Also take away a signal if any. // We don't know if the thread was also notified or not,
if ((state & kSignalMask) != 0) newstate -= kSignalInc; // so we should not consume a signal unconditionaly.
// Only if number of waiters is equal to number of signals,
// we know that the thread was notified and we must take away the signal.
if (((state & kWaiterMask) >> kWaiterShift) ==
((state & kSignalMask) >> kSignalShift))
newstate -= kSignalInc;
CheckState(newstate); CheckState(newstate);
if (state_.compare_exchange_weak(state, newstate, if (state_.compare_exchange_weak(state, newstate,
std::memory_order_acq_rel)) std::memory_order_acq_rel))

View File

@ -379,7 +379,7 @@ class ThreadPoolTempl : public Eigen::ThreadPoolInterface {
eigen_plain_assert(!t->f); eigen_plain_assert(!t->f);
// We already did best-effort emptiness check in Steal, so prepare for // We already did best-effort emptiness check in Steal, so prepare for
// blocking. // blocking.
if (!ec_.Prewait()) return true; ec_.Prewait();
// Now do a reliable emptiness check. // Now do a reliable emptiness check.
int victim = NonEmptyQueueIndex(); int victim = NonEmptyQueueIndex();
if (victim != -1) { if (victim != -1) {

View File

@ -97,11 +97,9 @@ class RunQueue {
} }
// PopBack removes and returns the last elements in the queue. // PopBack removes and returns the last elements in the queue.
// Can fail spuriously.
Work PopBack() { Work PopBack() {
if (Empty()) return Work(); if (Empty()) return Work();
std::unique_lock<std::mutex> lock(mutex_, std::try_to_lock); std::unique_lock<std::mutex> lock(mutex_);
if (!lock) return Work();
unsigned back = back_.load(std::memory_order_relaxed); unsigned back = back_.load(std::memory_order_relaxed);
Elem* e = &array_[back & kMask]; Elem* e = &array_[back & kMask];
uint8_t s = e->state.load(std::memory_order_relaxed); uint8_t s = e->state.load(std::memory_order_relaxed);
@ -115,11 +113,10 @@ class RunQueue {
} }
// PopBackHalf removes and returns half last elements in the queue. // PopBackHalf removes and returns half last elements in the queue.
// Returns number of elements removed. But can also fail spuriously. // Returns number of elements removed.
unsigned PopBackHalf(std::vector<Work>* result) { unsigned PopBackHalf(std::vector<Work>* result) {
if (Empty()) return 0; if (Empty()) return 0;
std::unique_lock<std::mutex> lock(mutex_, std::try_to_lock); std::unique_lock<std::mutex> lock(mutex_);
if (!lock) return 0;
unsigned back = back_.load(std::memory_order_relaxed); unsigned back = back_.load(std::memory_order_relaxed);
unsigned size = Size(); unsigned size = Size();
unsigned mid = back; unsigned mid = back;

View File

@ -30,10 +30,10 @@ static void test_basic_eventcount()
EventCount ec(waiters); EventCount ec(waiters);
EventCount::Waiter& w = waiters[0]; EventCount::Waiter& w = waiters[0];
ec.Notify(false); ec.Notify(false);
VERIFY(ec.Prewait()); ec.Prewait();
ec.Notify(true); ec.Notify(true);
ec.CommitWait(&w); ec.CommitWait(&w);
VERIFY(ec.Prewait()); ec.Prewait();
ec.CancelWait(); ec.CancelWait();
} }
@ -112,7 +112,7 @@ static void test_stress_eventcount()
unsigned idx = rand_reentrant(&rnd) % kQueues; unsigned idx = rand_reentrant(&rnd) % kQueues;
if (queues[idx].Pop()) continue; if (queues[idx].Pop()) continue;
j--; j--;
if (!ec.Prewait()) continue; ec.Prewait();
bool empty = true; bool empty = true;
for (int q = 0; q < kQueues; q++) { for (int q = 0; q < kQueues; q++) {
if (!queues[q].Empty()) { if (!queues[q].Empty()) {