proxygen
folly::ZeroCopyTestAsyncSocket Class Reference

#include <ZeroCopy.h>

Classes

class  Callback
 

Public Member Functions

 ZeroCopyTestAsyncSocket (size_t *counter, folly::EventBase *evb, int numLoops, size_t bufferSize, bool zeroCopy)
 
 ZeroCopyTestAsyncSocket (size_t *counter, folly::EventBase *evb, int fd, int numLoops, size_t bufferSize, bool zeroCopy)
 
 ~ZeroCopyTestAsyncSocket ()
 
void connect (const folly::SocketAddress &remote)
 
bool isZeroCopyWriteInProgress () const
 

Private Member Functions

void setZeroCopy (bool enable)
 
void setBufferSize (size_t bufferSize)
 
void clearBuffers ()
 
void getReadBuffer (void **bufReturn, size_t *lenReturn)
 
void readDataAvailable (size_t len) noexcept
 
void onConnected ()
 
void onDataReady ()
 
void onDataFinish (folly::exception_wrapper)
 
bool writeBuffer ()
 

Private Attributes

size_t * counter_ {nullptr}
 
folly::EventBaseevb_
 
int numLoops_ {0}
 
int currLoop_ {0}
 
bool zeroCopy_ {false}
 
folly::AsyncSocket::UniquePtr sock_
 
Callback callback_
 
size_t bufferSize_ {0}
 
size_t readOffset_ {0}
 
char * readBuffer_ {nullptr}
 
std::unique_ptr< folly::IOBufwriteBuffer_
 
bool client_
 

Detailed Description

Definition at line 28 of file ZeroCopy.h.

Constructor & Destructor Documentation

folly::ZeroCopyTestAsyncSocket::ZeroCopyTestAsyncSocket ( size_t *  counter,
folly::EventBase evb,
int  numLoops,
size_t  bufferSize,
bool  zeroCopy 
)
inlineexplicit

Definition at line 30 of file ZeroCopy.h.

References setBufferSize(), and setZeroCopy().

36  : counter_(counter),
37  evb_(evb),
38  numLoops_(numLoops),
39  sock_(new folly::AsyncSocket(evb)),
40  callback_(this),
41  client_(true) {
42  setBufferSize(bufferSize);
43  setZeroCopy(zeroCopy);
44  }
void setZeroCopy(bool enable)
Definition: ZeroCopy.h:82
folly::EventBase * evb_
Definition: ZeroCopy.h:197
void setBufferSize(size_t bufferSize)
Definition: ZeroCopy.h:89
folly::AsyncSocket::UniquePtr sock_
Definition: ZeroCopy.h:202
std::atomic< int > counter
folly::ZeroCopyTestAsyncSocket::ZeroCopyTestAsyncSocket ( size_t *  counter,
folly::EventBase evb,
int  fd,
int  numLoops,
size_t  bufferSize,
bool  zeroCopy 
)
inlineexplicit

Definition at line 46 of file ZeroCopy.h.

References callback_, setBufferSize(), setZeroCopy(), and sock_.

53  : counter_(counter),
54  evb_(evb),
55  numLoops_(numLoops),
56  sock_(new folly::AsyncSocket(evb, fd)),
57  callback_(this),
58  client_(false) {
59  setBufferSize(bufferSize);
60  setZeroCopy(zeroCopy);
61  // enable reads
62  if (sock_) {
63  sock_->setReadCB(&callback_);
64  }
65  }
void setZeroCopy(bool enable)
Definition: ZeroCopy.h:82
folly::EventBase * evb_
Definition: ZeroCopy.h:197
void setBufferSize(size_t bufferSize)
Definition: ZeroCopy.h:89
folly::AsyncSocket::UniquePtr sock_
Definition: ZeroCopy.h:202
std::atomic< int > counter
folly::ZeroCopyTestAsyncSocket::~ZeroCopyTestAsyncSocket ( )
inline

Definition at line 67 of file ZeroCopy.h.

References clearBuffers().

67  {
68  clearBuffers();
69  }

Member Function Documentation

void folly::ZeroCopyTestAsyncSocket::clearBuffers ( )
inlineprivate

Definition at line 131 of file ZeroCopy.h.

References readBuffer_.

Referenced by setBufferSize(), and ~ZeroCopyTestAsyncSocket().

131  {
132  if (readBuffer_) {
133  delete[] readBuffer_;
134  }
135  }
void folly::ZeroCopyTestAsyncSocket::connect ( const folly::SocketAddress remote)
inline

Definition at line 71 of file ZeroCopy.h.

References callback_, and sock_.

71  {
72  if (sock_) {
73  sock_->connect(&callback_, remote);
74  }
75  }
folly::AsyncSocket::UniquePtr sock_
Definition: ZeroCopy.h:202
void folly::ZeroCopyTestAsyncSocket::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
inlineprivate

Definition at line 137 of file ZeroCopy.h.

References bufferSize_, readBuffer_, and readOffset_.

Referenced by folly::ZeroCopyTestAsyncSocket::Callback::getReadBuffer().

137  {
138  *bufReturn = readBuffer_ + readOffset_;
139  *lenReturn = bufferSize_ - readOffset_;
140  }
bool folly::ZeroCopyTestAsyncSocket::isZeroCopyWriteInProgress ( ) const
inline

Definition at line 77 of file ZeroCopy.h.

References sock_.

77  {
78  return sock_->isZeroCopyWriteInProgress();
79  }
folly::AsyncSocket::UniquePtr sock_
Definition: ZeroCopy.h:202
void folly::ZeroCopyTestAsyncSocket::onConnected ( )
inlineprivate
void folly::ZeroCopyTestAsyncSocket::onDataFinish ( folly::exception_wrapper  )
inlineprivate
void folly::ZeroCopyTestAsyncSocket::onDataReady ( )
inlineprivate

Definition at line 155 of file ZeroCopy.h.

References client_, counter_, currLoop_, evb_, numLoops_, folly::EventBase::runInLoop(), folly::EventBase::terminateLoopSoon(), and writeBuffer().

Referenced by readDataAvailable().

155  {
156  currLoop_++;
157  if (client_ && currLoop_ >= numLoops_) {
158  evb_->runInLoop(
159  [this] {
160  if (counter_ && (0 == --(*counter_))) {
162  }
163  },
164  false /*thisIteration*/);
165  return;
166  }
167  writeBuffer();
168  }
folly::EventBase * evb_
Definition: ZeroCopy.h:197
void runInLoop(LoopCallback *callback, bool thisIteration=false)
Definition: EventBase.cpp:520
void terminateLoopSoon()
Definition: EventBase.cpp:493
void folly::ZeroCopyTestAsyncSocket::readDataAvailable ( size_t  len)
inlineprivatenoexcept

Definition at line 142 of file ZeroCopy.h.

References bufferSize_, onDataReady(), and readOffset_.

Referenced by folly::ZeroCopyTestAsyncSocket::Callback::readDataAvailable().

142  {
143  readOffset_ += len;
144  if (readOffset_ == bufferSize_) {
145  readOffset_ = 0;
146  onDataReady();
147  }
148  }
void folly::ZeroCopyTestAsyncSocket::setBufferSize ( size_t  bufferSize)
inlineprivate

Definition at line 89 of file ZeroCopy.h.

References bufferSize_, clearBuffers(), and readBuffer_.

Referenced by ZeroCopyTestAsyncSocket().

89  {
90  clearBuffers();
91  bufferSize_ = bufferSize;
92 
93  readBuffer_ = new char[bufferSize_];
94  }
void folly::ZeroCopyTestAsyncSocket::setZeroCopy ( bool  enable)
inlineprivate

Definition at line 82 of file ZeroCopy.h.

References sock_, and zeroCopy_.

Referenced by onConnected(), and ZeroCopyTestAsyncSocket().

82  {
83  zeroCopy_ = enable;
84  if (sock_) {
85  sock_->setZeroCopy(zeroCopy_);
86  }
87  }
folly::AsyncSocket::UniquePtr sock_
Definition: ZeroCopy.h:202
bool folly::ZeroCopyTestAsyncSocket::writeBuffer ( )
inlineprivate

Definition at line 178 of file ZeroCopy.h.

References bufferSize_, folly::gen::move, folly::NONE, sock_, folly::IOBuf::takeOwnership(), folly::WRITE_MSG_ZEROCOPY, writeBuffer_, and zeroCopy_.

Referenced by onConnected(), and onDataReady().

178  {
179  // use calloc to make sure the memory is touched
180  // if the memory is just malloc'd, running the zeroCopyOn
181  // and the zeroCopyOff back to back on a system that does not support
182  // zerocopy leads to the second test being much slower
183  writeBuffer_ =
185 
186  if (sock_ && writeBuffer_) {
187  sock_->writeChain(
188  nullptr,
191  }
192 
193  return true;
194  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::AsyncSocket::UniquePtr sock_
Definition: ZeroCopy.h:202
std::unique_ptr< folly::IOBuf > writeBuffer_
Definition: ZeroCopy.h:208
static std::unique_ptr< IOBuf > takeOwnership(void *buf, std::size_t capacity, FreeFunction freeFn=nullptr, void *userData=nullptr, bool freeOnError=true)
Definition: IOBuf.h:304

Member Data Documentation

size_t folly::ZeroCopyTestAsyncSocket::bufferSize_ {0}
private
Callback folly::ZeroCopyTestAsyncSocket::callback_
private

Definition at line 203 of file ZeroCopy.h.

Referenced by connect(), and ZeroCopyTestAsyncSocket().

bool folly::ZeroCopyTestAsyncSocket::client_
private

Definition at line 210 of file ZeroCopy.h.

Referenced by onDataFinish(), and onDataReady().

size_t* folly::ZeroCopyTestAsyncSocket::counter_ {nullptr}
private

Definition at line 196 of file ZeroCopy.h.

Referenced by onDataFinish(), and onDataReady().

int folly::ZeroCopyTestAsyncSocket::currLoop_ {0}
private

Definition at line 199 of file ZeroCopy.h.

Referenced by onDataReady().

int folly::ZeroCopyTestAsyncSocket::numLoops_ {0}
private

Definition at line 198 of file ZeroCopy.h.

Referenced by folly::ZeroCopyTestServer::connectionAccepted(), and onDataReady().

char* folly::ZeroCopyTestAsyncSocket::readBuffer_ {nullptr}
private

Definition at line 207 of file ZeroCopy.h.

Referenced by clearBuffers(), getReadBuffer(), and setBufferSize().

size_t folly::ZeroCopyTestAsyncSocket::readOffset_ {0}
private

Definition at line 206 of file ZeroCopy.h.

Referenced by getReadBuffer(), and readDataAvailable().

std::unique_ptr<folly::IOBuf> folly::ZeroCopyTestAsyncSocket::writeBuffer_
private

Definition at line 208 of file ZeroCopy.h.

Referenced by writeBuffer().

bool folly::ZeroCopyTestAsyncSocket::zeroCopy_ {false}
private

The documentation for this class was generated from the following file: