Skip to content

Commit

Permalink
Add support for readv functions
Browse files Browse the repository at this point in the history
Summary: Add support for readv functions

Reviewed By: mingtaoy

Differential Revision: D40089998

fbshipit-source-id: 577a8f6c2bd1b833544ff7ce6400b4d3a9dc222f
  • Loading branch information
Dan Melnic authored and facebook-github-bot committed Oct 20, 2022
1 parent e86aacd commit 9c6f03c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
10 changes: 5 additions & 5 deletions fizz/protocol/AsyncFizzBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ AsyncFizzBase::AsyncFizzBase(
: folly::WriteChainAsyncTransportWrapper<folly::AsyncTransportWrapper>(
std::move(transport)),
handshakeTimeout_(*this, transport_->getEventBase()),
transportOptions_(std::move(options)),
ioVecQueue_(folly::IOBufIovecBuilder::Options().setBlockSize(
transportOptions_.readVecBlockSize)) {
transportOptions_(std::move(options)) {
setReadMode(transportOptions_.readMode);
}

Expand Down Expand Up @@ -413,14 +411,16 @@ void AsyncFizzBase::getReadBuffer(void** bufReturn, size_t* lenReturn) {
}

void AsyncFizzBase::getReadBuffers(folly::IOBufIovecBuilder::IoVecVec& iovs) {
ioVecQueue_.allocateBuffers(iovs, transportOptions_.readVecReadSize);
DCHECK(!!transportOptions_.ioVecQueue);
transportOptions_.ioVecQueue->allocateBuffers(iovs);
}

void AsyncFizzBase::readDataAvailable(size_t len) noexcept {
DelayedDestruction::DestructorGuard dg(this);

if (getReadMode() == folly::AsyncTransport::ReadCallback::ReadMode::ReadVec) {
auto tmp = ioVecQueue_.extractIOBufChain(len);
DCHECK(!!transportOptions_.ioVecQueue);
auto tmp = transportOptions_.ioVecQueue->extractIOBufChain(len);
transportReadBuf_.append(std::move(tmp));
} else {
transportReadBuf_.postallocate(len);
Expand Down
31 changes: 19 additions & 12 deletions fizz/protocol/AsyncFizzBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ class AsyncFizzBase : public folly::WriteChainAsyncTransportWrapper<
std::unique_ptr<folly::IOBuf> endOfData) = 0;
};

/* Interface used to get a reference to an folly::IOBufIovecBuilder
*/
struct IOVecQueueOps {
virtual ~IOVecQueueOps() = default;
virtual void allocateBuffers(folly::IOBufIovecBuilder::IoVecVec& iovs) = 0;
virtual std::unique_ptr<folly::IOBuf> extractIOBufChain(size_t len) = 0;
};

struct TransportOptions {
/**
* Controls whether or not the async recv callback should be registered
Expand All @@ -106,22 +114,23 @@ class AsyncFizzBase : public folly::WriteChainAsyncTransportWrapper<
* ReadMode::ReadVec
* Under this mode, Fizz will use vectored IO (`readv`) to read
* incoming data. This can help avoid additional copies at the expense
* of allocating extra ref counting objects. The overall mem usage is
* also dependent on the readVecBlockSize value.
* of allocating extra ref counting objects.
*
*/
folly::AsyncReader::ReadCallback::ReadMode readMode{ReadMode::ReadBuffer};

/*
* AsyncTransport read vec block size
*/
size_t readVecBlockSize{
folly::IOBufIovecBuilder::Options::kDefaultBlockSize};

/*
* AsyncTransport read vec read size
* Under `ReadMode::ReadVec`, Fizz will allocate buffers for vectored I/O
* through the `iovecQueue`.
*
* This field must be set if `readMode` is `ReadMode::ReadVec`.
*
* Multiple AsyncFizzBase instances may share the underlying
* `IOVecQueueOps`, provided that the implementation allows it. This can be
* useful in limiting the amount of memory allocated across a process with
* many Fizz connections.
*/
size_t readVecReadSize{4000};
std::shared_ptr<IOVecQueueOps> ioVecQueue{};

/**
* Under ReadMode::ReadBuffer, whenever Fizz's available read buffer space
Expand Down Expand Up @@ -519,7 +528,5 @@ class AsyncFizzBase : public folly::WriteChainAsyncTransportWrapper<

TransportOptions transportOptions_;
std::unique_ptr<FizzMsgHdr> msgHdr_;

folly::IOBufIovecBuilder ioVecQueue_;
};
} // namespace fizz
16 changes: 10 additions & 6 deletions fizz/test/AsyncFizzBaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,19 @@ MATCHER_P(BufMatches, expected, "") {
}

struct ReadCB {
constexpr static AsyncFizzBase::TransportOptions Options = {
false, // registerEventCallback
};
static const AsyncFizzBase::TransportOptions Options;
};

const AsyncFizzBase::TransportOptions ReadCB::Options = {
false, // registerEventCallback
};

struct RecvCB {
constexpr static AsyncFizzBase::TransportOptions Options = {
true, // registerEventCallback
};
static const AsyncFizzBase::TransportOptions Options;
};

const AsyncFizzBase::TransportOptions RecvCB::Options = {
true, // registerEventCallback
};

using TestTypes = ::testing::Types<ReadCB, RecvCB>;
Expand Down

0 comments on commit 9c6f03c

Please sign in to comment.