Commit a0394d84 authored by Scott Haskin's avatar Scott Haskin Committed by Facebook Github Bot

Adding method getSendBufInUse and getRecvBufInUse to

Summary:
Adding ability to get the current number of bytes used in the tx
(transmit/send) socket buffer and the rx (receive) socket buffer.

Reviewed By: yfeldblum

Differential Revision: D15966995

fbshipit-source-id: 29a51cd4caad265590baa45385712659fb3f3dac
parent 74bf4289
......@@ -32,8 +32,14 @@
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sstream>
#include <thread>
#if __linux__
#include <linux/sockios.h>
#include <sys/ioctl.h>
#endif
#if FOLLY_HAVE_VLA
#define FOLLY_HAVE_VLA_01 1
#else
......@@ -1710,6 +1716,54 @@ int AsyncSocket::setRecvBufSize(size_t bufsize) {
return 0;
}
#if __linux__
size_t AsyncSocket::getSendBufInUse() const {
if (fd_ == NetworkSocket()) {
std::stringstream issueString;
issueString << "AsyncSocket::getSendBufInUse() called on non-open socket "
<< this << "(state=" << state_ << ")";
VLOG(4) << issueString.str();
throw std::logic_error(issueString.str());
}
size_t returnValue = 0;
if (-1 == ::ioctl(fd_.toFd(), SIOCOUTQ, &returnValue)) {
int errnoCopy = errno;
std::stringstream issueString;
issueString << "Failed to get the tx used bytes on Socket: " << this
<< "(fd=" << fd_ << ", state=" << state_
<< "): " << errnoStr(errnoCopy);
VLOG(2) << issueString.str();
throw std::logic_error(issueString.str());
}
return returnValue;
}
size_t AsyncSocket::getRecvBufInUse() const {
if (fd_ == NetworkSocket()) {
std::stringstream issueString;
issueString << "AsyncSocket::getRecvBufInUse() called on non-open socket "
<< this << "(state=" << state_ << ")";
VLOG(4) << issueString.str();
throw std::logic_error(issueString.str());
}
size_t returnValue = 0;
if (-1 == ::ioctl(fd_.toFd(), SIOCINQ, &returnValue)) {
std::stringstream issueString;
int errnoCopy = errno;
issueString << "Failed to get the rx used bytes on Socket: " << this
<< "(fd=" << fd_ << ", state=" << state_
<< "): " << errnoStr(errnoCopy);
VLOG(2) << issueString.str();
throw std::logic_error(issueString.str());
}
return returnValue;
}
#endif
int AsyncSocket::setTCPProfile(int profd) {
if (fd_ == NetworkSocket()) {
VLOG(4) << "AsyncSocket::setTCPProfile() called on non-open socket " << this
......
......@@ -716,6 +716,26 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
*/
int setRecvBufSize(size_t bufsize);
#if __linux__
/**
* @brief This method is used to get the number of bytes that are currently
* stored in the TCP send/tx buffer
*
* @return the number of bytes in the send/tx buffer or folly::none if there
* was a problem
*/
size_t getSendBufInUse() const;
/**
* @brief This method is used to get the number of bytes that are currently
* stored in the TCP receive/rx buffer
*
* @return the number of bytes in the receive/rx buffer or folly::none if
* there was a problem
*/
size_t getRecvBufInUse() const;
#endif
/**
* Sets a specific tcp personality
* Available only on kernels 3.2 and greater
......
......@@ -3754,3 +3754,40 @@ TEST(AsyncSocketTest, V4TosReflectTest) {
ASSERT_EQ(value, 0x2c);
}
#endif
#if __linux__
TEST(AsyncSocketTest, getBufInUse) {
EventBase eventBase;
std::shared_ptr<AsyncServerSocket> server(
AsyncServerSocket::newSocket(&eventBase));
server->bind(0);
server->listen(5);
std::shared_ptr<AsyncSocket> client = AsyncSocket::newSocket(&eventBase);
client->connect(nullptr, server->getAddress());
NetworkSocket servfd = server->getNetworkSocket();
auto clientAccepted =
AsyncSocket::newSocket(nullptr, netops::accept(servfd, nullptr, nullptr));
clientAccepted->setRecvBufSize(3000);
std::string testData;
for (int i = 0; i < 10000; ++i) {
testData += "0123456789";
}
client->write(nullptr, (const void*)testData.c_str(), testData.size());
eventBase.loop();
size_t recvBufSize = clientAccepted->getRecvBufInUse();
size_t sendBufSize = client->getSendBufInUse();
EXPECT_EQ((recvBufSize + sendBufSize), testData.size());
EXPECT_GE(recvBufSize, 0);
EXPECT_GE(sendBufSize, 0);
}
#endif
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