Skip to content

Commit

Permalink
reactor: avoid issuing systemwide memory barriers in parallel
Browse files Browse the repository at this point in the history
Before going to sleep, a reactor must force a memory barrier across
the entire system, so that other reactors can tell it is asleep and
wake it up when necessary.

This operation, however, is slow on large machines, and very unscalable,
so that if many reactors fall asleep concurrently, the result is quite
long latencies, in the tens of milliseconds.

Fix by noticing that another reactor is falling asleep, and aborting the
sleep attempt in that case.

Message-Id: <1460657435-22990-3-git-send-email-avi@scylladb.com>
  • Loading branch information
avikivity committed Apr 15, 2016
1 parent 6baac48 commit 2185f37
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/reactor.cc
Expand Up @@ -1667,14 +1667,31 @@ class reactor::lowres_timer_pollfn final : public reactor::pollfn {

class reactor::smp_pollfn final : public reactor::pollfn {
reactor& _r;
struct aligned_flag {
std::atomic<bool> flag;
char pad[63];
bool try_lock() {
return !flag.exchange(true, std::memory_order_relaxed);
}
void unlock() {
flag.store(false, std::memory_order_relaxed);
}
};
static aligned_flag _membarrier_lock;
public:
smp_pollfn(reactor& r) : _r(r) {}
virtual bool poll() final override {
return smp::poll_queues();
}
virtual bool try_enter_interrupt_mode() override {
// systemwide_memory_barrier() is very slow if run concurrently,
// so don't go to sleep if it is running now.
if (!_membarrier_lock.try_lock()) {
return false;
}
_r._sleeping.store(true, std::memory_order_relaxed);
systemwide_memory_barrier();
_membarrier_lock.unlock();
if (poll()) {
// raced
_r._sleeping.store(false, std::memory_order_relaxed);
Expand All @@ -1687,6 +1704,8 @@ class reactor::smp_pollfn final : public reactor::pollfn {
}
};

alignas(64) reactor::smp_pollfn::aligned_flag reactor::smp_pollfn::_membarrier_lock;

class reactor::epoll_pollfn final : public reactor::pollfn {
reactor& _r;
public:
Expand Down

0 comments on commit 2185f37

Please sign in to comment.