From: Gunnar Beutner Date: Wed, 28 Mar 2012 17:50:55 +0000 (+0200) Subject: Improved FIFO performance X-Git-Tag: v0.0.1~700 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9db06ec8e715768511bba232e2e0c6f26095f78c;p=icinga2 Improved FIFO performance --- diff --git a/base/fifo.cpp b/base/fifo.cpp index dcd05dabc..fb75286fa 100644 --- a/base/fifo.cpp +++ b/base/fifo.cpp @@ -69,7 +69,7 @@ size_t FIFO::GetSize(void) const return m_BufferSize - m_Offset; } -const void *FIFO::Peek(void) const +const void *FIFO::GetReadBuffer(void) const { return m_Buffer + m_Offset; } @@ -88,7 +88,7 @@ size_t FIFO::Read(void *buffer, size_t count) return count; } -size_t FIFO::Write(const void *buffer, size_t count) +void *FIFO::GetWriteBuffer(size_t count) { char *new_buffer; @@ -98,7 +98,18 @@ size_t FIFO::Write(const void *buffer, size_t count) throw exception(/*"Out of memory."*/); m_Buffer = new_buffer; - memcpy(new_buffer + m_BufferSize + m_Offset, buffer, count); + + return new_buffer + m_Offset + m_BufferSize; +} + +size_t FIFO::Write(const void *buffer, size_t count) +{ + if (buffer != NULL) { + void *target_buffer = GetWriteBuffer(count); + memcpy(target_buffer, buffer, count); + } + m_BufferSize += count; + return count; } diff --git a/base/fifo.h b/base/fifo.h index 2d9185e9f..03e8ee30d 100644 --- a/base/fifo.h +++ b/base/fifo.h @@ -7,7 +7,6 @@ namespace icinga class FIFO : public Object { private: - static const size_t BlockSize = 16 * 1024; char *m_Buffer; size_t m_BufferSize; size_t m_Offset; @@ -16,6 +15,8 @@ private: void Optimize(void); public: + static const size_t BlockSize = 16 * 1024; + typedef shared_ptr RefType; typedef weak_ptr WeakRefType; @@ -24,7 +25,9 @@ public: size_t GetSize(void) const; - const void *Peek(void) const; + const void *GetReadBuffer(void) const; + void *GetWriteBuffer(size_t count); + size_t Read(void *buffer, size_t count); size_t Write(const void *buffer, size_t count); }; diff --git a/base/tcpclient.cpp b/base/tcpclient.cpp index b8c5e1f60..ee0f00de8 100644 --- a/base/tcpclient.cpp +++ b/base/tcpclient.cpp @@ -31,13 +31,14 @@ FIFO::RefType TCPClient::GetRecvQueue(void) int TCPClient::ReadableEventHandler(EventArgs::RefType ea) { - char buffer[4096]; int read_total, rc; read_total = 0; while (true) { - rc = recv(GetFD(), buffer, sizeof(buffer), 0); + static const size_t BufferSize = FIFO::BlockSize / 2; + char *buffer = (char *)m_RecvQueue->GetWriteBuffer(BufferSize); + rc = recv(GetFD(), buffer, BufferSize, 0); #ifdef _WIN32 if (rc < 0 && WSAGetLastError() == WSAEWOULDBLOCK) @@ -51,7 +52,7 @@ int TCPClient::ReadableEventHandler(EventArgs::RefType ea) return 0; } - m_RecvQueue->Write(buffer, rc); + m_RecvQueue->Write(NULL, rc); read_total += rc; /* make sure we don't starve other sockets */ @@ -70,7 +71,7 @@ int TCPClient::WritableEventHandler(EventArgs::RefType ea) { int rc; - rc = send(GetFD(), (const char *)m_SendQueue->Peek(), m_SendQueue->GetSize(), 0); + rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0); if (rc <= 0) { Close(); diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index 00b5636b1..827aa76e4 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -19,7 +19,7 @@ Netstring::~Netstring(void) Netstring::RefType Netstring::ReadFromFIFO(FIFO::RefType fifo) { size_t buffer_length = fifo->GetSize(); - const char *buffer = (const char *)fifo->Peek(); + const char *buffer = (const char *)fifo->GetReadBuffer(); /* minimum netstring length is 3 */ if (buffer_length < 3) diff --git a/miniapp/miniapp.cpp b/miniapp/miniapp.cpp index 40a5e75ae..0a4524ef3 100644 --- a/miniapp/miniapp.cpp +++ b/miniapp/miniapp.cpp @@ -36,7 +36,7 @@ public: JsonRpcServer::RefType ts = new_object(); ts->MakeSocket(); - ts->Bind(9999); + ts->Bind(7777); ts->Listen(); ConnectionManager::RefType cm = new_object();