Commit b412547b authored by Dan Melnic's avatar Dan Melnic Committed by Facebook GitHub Bot

Remove PollIoBackend inline submit

Summary: Remove PollIoBackend inline submit

Reviewed By: danobi

Differential Revision: D21625033

fbshipit-source-id: 02ee55e64a2538c6a2392ea70912f4ae9b642963
parent 509ab86b
...@@ -92,18 +92,14 @@ bool IoUringBackend::FdRegistry::free( ...@@ -92,18 +92,14 @@ bool IoUringBackend::FdRegistry::free(
return false; return false;
} }
IoUringBackend::IoUringBackend( IoUringBackend::IoUringBackend(Options options)
size_t capacity, : PollIoBackend(options),
size_t maxSubmit, fdRegistry_(ioRing_, options.useRegisteredFds ? options.capacity : 0) {
size_t maxGet,
bool useRegisteredFds)
: PollIoBackend(capacity, maxSubmit, maxGet),
fdRegistry_(ioRing_, useRegisteredFds ? capacity : 0) {
::memset(&ioRing_, 0, sizeof(ioRing_)); ::memset(&ioRing_, 0, sizeof(ioRing_));
::memset(&params_, 0, sizeof(params_)); ::memset(&params_, 0, sizeof(params_));
params_.flags |= IORING_SETUP_CQSIZE; params_.flags |= IORING_SETUP_CQSIZE;
params_.cq_entries = capacity; params_.cq_entries = options.capacity;
// allocate entries both for poll add and cancel // allocate entries both for poll add and cancel
if (::io_uring_queue_init_params(2 * maxSubmit_, &ioRing_, &params_)) { if (::io_uring_queue_init_params(2 * maxSubmit_, &ioRing_, &params_)) {
...@@ -142,7 +138,7 @@ IoUringBackend::IoUringBackend( ...@@ -142,7 +138,7 @@ IoUringBackend::IoUringBackend(
// we need to call the init before adding the timer fd // we need to call the init before adding the timer fd
// so we avoid a deadlock - waiting for the queue to be drained // so we avoid a deadlock - waiting for the queue to be drained
if (useRegisteredFds) { if (options.useRegisteredFds) {
// now init the file registry // now init the file registry
// if this fails, we still continue since we // if this fails, we still continue since we
// can run without registered fds // can run without registered fds
...@@ -202,7 +198,9 @@ bool IoUringBackend::isAvailable() { ...@@ -202,7 +198,9 @@ bool IoUringBackend::isAvailable() {
static folly::once_flag initFlag; static folly::once_flag initFlag;
folly::call_once(initFlag, [&]() { folly::call_once(initFlag, [&]() {
try { try {
IoUringBackend backend(1024, 128); Options options;
options.setCapacity(1024);
IoUringBackend backend(options);
} catch (const NotAvailable&) { } catch (const NotAvailable&) {
sAvailable = false; sAvailable = false;
} }
......
...@@ -32,11 +32,7 @@ class IoUringBackend : public PollIoBackend { ...@@ -32,11 +32,7 @@ class IoUringBackend : public PollIoBackend {
using std::runtime_error::runtime_error; using std::runtime_error::runtime_error;
}; };
explicit IoUringBackend( explicit IoUringBackend(Options options);
size_t capacity,
size_t maxSubmit = 128,
size_t maxGet = static_cast<size_t>(-1),
bool useRegisteredFds = false);
~IoUringBackend() override; ~IoUringBackend() override;
// returns true if the current Linux kernel version // returns true if the current Linux kernel version
...@@ -160,6 +156,12 @@ class IoUringBackend : public PollIoBackend { ...@@ -160,6 +156,12 @@ class IoUringBackend : public PollIoBackend {
size_t submit_internal(); size_t submit_internal();
// submit
size_t maxSubmit_;
// process
size_t maxGet_;
std::unique_ptr<IoSqe[]> entries_; std::unique_ptr<IoSqe[]> entries_;
// io_uring related // io_uring related
......
...@@ -140,11 +140,8 @@ PollIoBackend::SocketPair::~SocketPair() { ...@@ -140,11 +140,8 @@ PollIoBackend::SocketPair::~SocketPair() {
} }
} }
PollIoBackend::PollIoBackend(size_t capacity, size_t maxSubmit, size_t maxGet) PollIoBackend::PollIoBackend(Options options)
: capacity_(capacity), : options_(options), numEntries_(options.capacity) {
numEntries_(capacity),
maxSubmit_(maxSubmit),
maxGet_(maxGet) {
// create the timer fd // create the timer fd
timerFd_ = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); timerFd_ = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (timerFd_ < 0) { if (timerFd_ < 0) {
...@@ -544,38 +541,13 @@ int PollIoBackend::eb_event_add(Event& event, const struct timeval* timeout) { ...@@ -544,38 +541,13 @@ int PollIoBackend::eb_event_add(Event& event, const struct timeval* timeout) {
CHECK(iocb); CHECK(iocb);
iocb->event_ = &event; iocb->event_ = &event;
if (maxSubmit_) { // just append it
// just append it submitList_.push_back(*iocb);
submitList_.push_back(*iocb); if (~event_ref_flags(ev) & EVLIST_INTERNAL) {
if (~event_ref_flags(ev) & EVLIST_INTERNAL) { numInsertedEvents_++;
numInsertedEvents_++;
}
event_ref_flags(ev) |= EVLIST_INSERTED;
event.setUserData(iocb);
return 0;
} else {
auto* entry = allocSubmissionEntry(); // this can be nullptr
iocb->prepPollAdd(
entry,
ev->ev_fd,
getPollFlags(ev->ev_events),
(ev->ev_events & EV_PERSIST) != 0);
int ret = submitOne(iocb);
if (ret == 1) {
if (~event_ref_flags(ev) & EVLIST_INTERNAL) {
numInsertedEvents_++;
}
event_ref_flags(ev) |= EVLIST_INSERTED;
event.setUserData(iocb);
} else {
releaseIoCb(iocb);
}
if (ret != 1) {
throw std::runtime_error("io_submit error");
}
return (ret == 1) ? 0 : -1;
} }
event_ref_flags(ev) |= EVLIST_INSERTED;
event.setUserData(iocb);
} }
return 0; return 0;
......
...@@ -35,7 +35,40 @@ namespace folly { ...@@ -35,7 +35,40 @@ namespace folly {
class PollIoBackend : public EventBaseBackendBase { class PollIoBackend : public EventBaseBackendBase {
public: public:
explicit PollIoBackend(size_t capacity, size_t maxSubmit, size_t maxGet); struct Options {
Options() = default;
Options& setCapacity(size_t v) {
capacity = v;
return *this;
}
Options& setMaxSubmit(size_t v) {
maxSubmit = v;
return *this;
}
Options& setMaxGet(size_t v) {
maxGet = v;
return *this;
}
Options& setUseRegisteredFds(bool v) {
useRegisteredFds = v;
return *this;
}
size_t capacity{0};
size_t maxSubmit{128};
size_t maxGet{static_cast<size_t>(-1)};
bool useRegisteredFds{false};
};
explicit PollIoBackend(Options options);
~PollIoBackend() override; ~PollIoBackend() override;
// from EventBaseBackendBase // from EventBaseBackendBase
...@@ -327,7 +360,7 @@ class PollIoBackend : public EventBaseBackendBase { ...@@ -327,7 +360,7 @@ class PollIoBackend : public EventBaseBackendBase {
return numIoCbInUse_; return numIoCbInUse_;
} }
size_t capacity_; Options options_;
size_t numEntries_; size_t numEntries_;
IoCb* timerEntry_{nullptr}; IoCb* timerEntry_{nullptr};
IoCb* signalReadEntry_{nullptr}; IoCb* signalReadEntry_{nullptr};
...@@ -348,12 +381,8 @@ class PollIoBackend : public EventBaseBackendBase { ...@@ -348,12 +381,8 @@ class PollIoBackend : public EventBaseBackendBase {
std::map<int, std::set<Event*>> signals_; std::map<int, std::set<Event*>> signals_;
// submit // submit
size_t maxSubmit_;
IoCbList submitList_; IoCbList submitList_;
// process
size_t maxGet_;
// loop related // loop related
bool loopBreak_{false}; bool loopBreak_{false};
bool shuttingDown_{false}; bool shuttingDown_{false};
......
...@@ -26,8 +26,13 @@ static constexpr size_t kMaxGet = static_cast<size_t>(-1); ...@@ -26,8 +26,13 @@ static constexpr size_t kMaxGet = static_cast<size_t>(-1);
struct IoUringBackendProvider { struct IoUringBackendProvider {
static std::unique_ptr<folly::EventBaseBackendBase> getBackend() { static std::unique_ptr<folly::EventBaseBackendBase> getBackend() {
try { try {
return std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kCapacity, kMaxSubmit, kMaxGet, false /* useRegisteredFds */); options.setCapacity(kCapacity)
.setMaxSubmit(kMaxSubmit)
.setMaxGet(kMaxGet)
.setUseRegisteredFds(false);
return std::make_unique<folly::IoUringBackend>(options);
} catch (const IoUringBackend::NotAvailable&) { } catch (const IoUringBackend::NotAvailable&) {
return nullptr; return nullptr;
} }
......
...@@ -177,8 +177,9 @@ void testEventFD(bool overflow, bool persist, bool asyncRead) { ...@@ -177,8 +177,9 @@ void testEventFD(bool overflow, bool persist, bool asyncRead) {
std::unique_ptr<folly::EventBaseBackendBase> backend; std::unique_ptr<folly::EventBaseBackendBase> backend;
try { try {
backend = std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kBackendCapacity, kBackendMaxSubmit); options.setCapacity(kBackendCapacity).setMaxSubmit(kBackendMaxSubmit);
backend = std::make_unique<folly::IoUringBackend>(options);
} catch (const folly::IoUringBackend::NotAvailable&) { } catch (const folly::IoUringBackend::NotAvailable&) {
} }
...@@ -215,8 +216,10 @@ void testInvalidFd(size_t numTotal, size_t numValid, size_t numInvalid) { ...@@ -215,8 +216,10 @@ void testInvalidFd(size_t numTotal, size_t numValid, size_t numInvalid) {
std::unique_ptr<folly::EventBaseBackendBase> backend; std::unique_ptr<folly::EventBaseBackendBase> backend;
try { try {
backend = std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kBackendCapacity, kBackendMaxSubmit); options.setCapacity(kBackendCapacity).setMaxSubmit(kBackendMaxSubmit);
backend = std::make_unique<folly::IoUringBackend>(options);
} catch (const folly::IoUringBackend::NotAvailable&) { } catch (const folly::IoUringBackend::NotAvailable&) {
} }
...@@ -363,8 +366,13 @@ void testAsyncUDPRecvmsg(bool useRegisteredFds) { ...@@ -363,8 +366,13 @@ void testAsyncUDPRecvmsg(bool useRegisteredFds) {
std::unique_ptr<folly::EventBaseBackendBase> backend; std::unique_ptr<folly::EventBaseBackendBase> backend;
try { try {
backend = std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kBackendCapacity, kBackendMaxSubmit, kBackendMaxGet, useRegisteredFds); options.setCapacity(kBackendCapacity)
.setMaxSubmit(kBackendMaxSubmit)
.setMaxGet(kBackendMaxGet)
.setUseRegisteredFds(useRegisteredFds);
backend = std::make_unique<folly::IoUringBackend>(options);
} catch (const folly::IoUringBackend::NotAvailable&) { } catch (const folly::IoUringBackend::NotAvailable&) {
} }
...@@ -475,17 +483,15 @@ TEST(IoUringBackend, RegisteredFds) { ...@@ -475,17 +483,15 @@ TEST(IoUringBackend, RegisteredFds) {
std::unique_ptr<folly::IoUringBackend> backendNoReg; std::unique_ptr<folly::IoUringBackend> backendNoReg;
try { try {
backendReg = std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kBackendCapacity, options.setCapacity(kBackendCapacity)
kBackendMaxSubmit, .setMaxSubmit(kBackendMaxSubmit)
kBackendMaxGet, .setMaxGet(kBackendMaxGet)
true /*useRegisteredFds*/); .setUseRegisteredFds(true);
backendNoReg = std::make_unique<folly::IoUringBackend>( backendReg = std::make_unique<folly::IoUringBackend>(options);
kBackendCapacity, options.setUseRegisteredFds(false);
kBackendMaxSubmit, backendNoReg = std::make_unique<folly::IoUringBackend>(options);
kBackendMaxGet,
false /*useRegisteredFds*/);
} catch (const folly::IoUringBackend::NotAvailable&) { } catch (const folly::IoUringBackend::NotAvailable&) {
} }
...@@ -535,8 +541,13 @@ static constexpr size_t kMaxGet = static_cast<size_t>(-1); ...@@ -535,8 +541,13 @@ static constexpr size_t kMaxGet = static_cast<size_t>(-1);
struct IoUringBackendProvider { struct IoUringBackendProvider {
static std::unique_ptr<folly::EventBaseBackendBase> getBackend() { static std::unique_ptr<folly::EventBaseBackendBase> getBackend() {
try { try {
return std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kCapacity, kMaxSubmit, kMaxGet, false /* useRegisteredFds */); options.setCapacity(kCapacity)
.setMaxSubmit(kMaxSubmit)
.setMaxGet(kMaxGet)
.setUseRegisteredFds(false);
return std::make_unique<folly::IoUringBackend>(options);
} catch (const IoUringBackend::NotAvailable&) { } catch (const IoUringBackend::NotAvailable&) {
return nullptr; return nullptr;
} }
...@@ -546,8 +557,12 @@ struct IoUringBackendProvider { ...@@ -546,8 +557,12 @@ struct IoUringBackendProvider {
struct IoUringRegFdBackendProvider { struct IoUringRegFdBackendProvider {
static std::unique_ptr<folly::EventBaseBackendBase> getBackend() { static std::unique_ptr<folly::EventBaseBackendBase> getBackend() {
try { try {
return std::make_unique<folly::IoUringBackend>( folly::PollIoBackend::Options options;
kCapacity, kMaxSubmit, kMaxGet, true /* useRegisteredFds */); options.setCapacity(kCapacity)
.setMaxSubmit(kMaxSubmit)
.setMaxGet(kMaxGet)
.setUseRegisteredFds(true);
return std::make_unique<folly::IoUringBackend>(options);
} catch (const IoUringBackend::NotAvailable&) { } catch (const IoUringBackend::NotAvailable&) {
return nullptr; return nullptr;
} }
......
...@@ -91,8 +91,12 @@ class BackendEventBase : public EventBase { ...@@ -91,8 +91,12 @@ class BackendEventBase : public EventBase {
static std::unique_ptr<folly::EventBaseBackendBase> getBackend( static std::unique_ptr<folly::EventBaseBackendBase> getBackend(
bool useRegisteredFds, bool useRegisteredFds,
size_t capacity) { size_t capacity) {
return std::make_unique<IoUringBackend>( folly::PollIoBackend::Options options;
capacity, 256, 128, useRegisteredFds); options.setCapacity(capacity)
.setMaxSubmit(256)
.setMaxGet(128)
.setUseRegisteredFds(useRegisteredFds);
return std::make_unique<IoUringBackend>(options);
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment