Revert "Try factoring out a mutex to see if its faster."

This reverts commit 982c32322bd78bb8485f8a8819b65922d259bb5c.
This commit is contained in:
Adam Ierymenko 2020-11-17 12:52:48 -08:00
parent 982c32322b
commit 4bd58d47cf
2 changed files with 15 additions and 32 deletions

View File

@ -92,17 +92,12 @@ LinuxEthernetTap::LinuxEthernetTap(
_homePath(homePath), _homePath(homePath),
_mtu(mtu), _mtu(mtu),
_fd(0), _fd(0),
_enabled(true), _enabled(true)
_bufferReadPtr(0),
_bufferWritePtr(0)
{ {
static std::mutex s_tapCreateLock; static std::mutex s_tapCreateLock;
char procpath[128],nwids[32]; char procpath[128],nwids[32];
struct stat sbuf; struct stat sbuf;
for(unsigned long i=0;i<ZT_BUFFER_POOL_SIZE;++i)
_buffers[i] = 0;
// Create only one tap at a time globally. // Create only one tap at a time globally.
std::lock_guard<std::mutex> tapCreateLock(s_tapCreateLock); std::lock_guard<std::mutex> tapCreateLock(s_tapCreateLock);
@ -257,14 +252,15 @@ LinuxEthernetTap::LinuxEthernetTap(
if (FD_ISSET(_fd,&readfds)) { if (FD_ISSET(_fd,&readfds)) {
for(;;) { // read until there are no more packets, then return to outer select() loop for(;;) { // read until there are no more packets, then return to outer select() loop
if (!buf) { if (!buf) {
for(unsigned int k=0;k<ZT_BUFFER_POOL_SIZE;++k) { std::lock_guard<std::mutex> l(_buffers_l);
buf = reinterpret_cast<void *>(_buffers[_bufferReadPtr++ & ZT_BUFFER_POOL_MASK].exchange(0)); if (_buffers.empty()) {
if (buf) buf = malloc(ZT_TAP_BUF_SIZE);
if (!buf)
break; break;
} else {
buf = _buffers.back();
_buffers.pop_back();
} }
buf = malloc(ZT_TAP_BUF_SIZE);
if (!buf)
continue;
} }
n = (int)::read(_fd,reinterpret_cast<uint8_t *>(buf) + r,ZT_TAP_BUF_SIZE - r); n = (int)::read(_fd,reinterpret_cast<uint8_t *>(buf) + r,ZT_TAP_BUF_SIZE - r);
@ -298,22 +294,16 @@ LinuxEthernetTap::LinuxEthernetTap(
MAC to,from; MAC to,from;
std::pair<void *,int> qi; std::pair<void *,int> qi;
while (_tapq.get(qi)) { while (_tapq.get(qi)) {
uint8_t *b = reinterpret_cast<uint8_t *>(qi.first); uint8_t *const b = reinterpret_cast<uint8_t *>(qi.first);
if (b) { if (b) {
to.setTo(b, 6); to.setTo(b, 6);
from.setTo(b + 6, 6); from.setTo(b + 6, 6);
unsigned int etherType = Utils::ntoh(((const uint16_t *)b)[6]); unsigned int etherType = Utils::ntoh(((const uint16_t *)b)[6]);
_handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void *)(b + 14),(unsigned int)(qi.second - 14)); _handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void *)(b + 14),(unsigned int)(qi.second - 14));
{
for(unsigned int k=0;k<ZT_BUFFER_POOL_SIZE;++k) { std::lock_guard<std::mutex> l(_buffers_l);
uintptr_t zero = 0; _buffers.push_back(qi.first);
if (_buffers[_bufferWritePtr++ & ZT_BUFFER_POOL_MASK].compare_exchange_strong(zero,reinterpret_cast<uintptr_t>(b))) {
b = nullptr;
break;
}
} }
if (b)
free(b);
} else break; } else break;
} }
}); });
@ -331,11 +321,8 @@ LinuxEthernetTap::~LinuxEthernetTap()
_tapReaderThread.join(); _tapReaderThread.join();
_tapProcessorThread.join(); _tapProcessorThread.join();
for(unsigned int k=0;k<ZT_BUFFER_POOL_SIZE;++k) { for(std::vector<void *>::iterator i(_buffers.begin());i!=_buffers.end();++i)
void *p = reinterpret_cast<void *>(_buffers[k].load()); free(*i);
if (p)
free(p);
}
std::vector< std::pair<void *,int> > dv(_tapq.drain()); std::vector< std::pair<void *,int> > dv(_tapq.drain());
for(std::vector< std::pair<void *,int> >::iterator i(dv.begin());i!=dv.end();++i) { for(std::vector< std::pair<void *,int> >::iterator i(dv.begin());i!=dv.end();++i) {
if (i->first) if (i->first)

View File

@ -28,9 +28,6 @@
#include "EthernetTap.hpp" #include "EthernetTap.hpp"
#include "BlockingQueue.hpp" #include "BlockingQueue.hpp"
#define ZT_BUFFER_POOL_SIZE 64
#define ZT_BUFFER_POOL_MASK 63U
namespace ZeroTier { namespace ZeroTier {
class LinuxEthernetTap : public EthernetTap class LinuxEthernetTap : public EthernetTap
@ -76,8 +73,7 @@ private:
std::thread _tapReaderThread; std::thread _tapReaderThread;
std::thread _tapProcessorThread; std::thread _tapProcessorThread;
std::mutex _buffers_l; std::mutex _buffers_l;
std::atomic<uintptr_t> _buffers[ZT_BUFFER_POOL_SIZE]; std::vector<void *> _buffers;
std::atomic<uintptr_t> _bufferReadPtr,_bufferWritePtr;
BlockingQueue< std::pair<void *,int> > _tapq; BlockingQueue< std::pair<void *,int> > _tapq;
}; };