Browse Source

use memory pool for tunnel messages

pull/1703/head
orignal 3 years ago
parent
commit
ae0cf2e831
  1. 12
      libi2pd/I2NPProtocol.cpp
  2. 16
      libi2pd/Tunnel.cpp
  3. 6
      libi2pd/Tunnel.h
  4. 38
      libi2pd/util.h

12
libi2pd/I2NPProtocol.cpp

@ -38,20 +38,14 @@ namespace i2p @@ -38,20 +38,14 @@ namespace i2p
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage (bool endpoint)
{
I2NPMessage * msg = nullptr;
if (endpoint)
{
// should fit two tunnel message + tunnel gateway header, enough for one garlic encrypted streaming packet
msg = new I2NPMessageBuffer<2*i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE + 28>(); // reserved for alignment and NTCP 16 + 6 + 6
msg->Align (6);
msg->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
}
return i2p::tunnel::tunnels.NewI2NPTunnelMessage ();
else
{
msg = new I2NPMessageBuffer<i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
auto msg = new I2NPMessageBuffer<i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
msg->Align (12);
return std::shared_ptr<I2NPMessage>(msg);
}
return std::shared_ptr<I2NPMessage>(msg);
}
std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len)

16
libi2pd/Tunnel.cpp

@ -488,7 +488,7 @@ namespace tunnel @@ -488,7 +488,7 @@ namespace tunnel
i2p::util::SetThreadName("Tunnels");
std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready
uint64_t lastTs = 0, lastPoolsTs = 0;
uint64_t lastTs = 0, lastPoolsTs = 0, lastMemoryPoolTs = 0;
while (m_IsRunning)
{
try
@ -564,6 +564,11 @@ namespace tunnel @@ -564,6 +564,11 @@ namespace tunnel
ManageTunnelPools (ts);
lastPoolsTs = ts;
}
if (ts - lastMemoryPoolTs >= 120) // manage memory pool every 2 minutes
{
m_I2NPTunnelMessagesMemoryPool.CleanUpMt ();
lastMemoryPoolTs = ts;
}
}
}
catch (std::exception& ex)
@ -935,6 +940,15 @@ namespace tunnel @@ -935,6 +940,15 @@ namespace tunnel
return outboundTunnel;
}
std::shared_ptr<I2NPMessage> Tunnels::NewI2NPTunnelMessage ()
{
// should fit two tunnel message + tunnel gateway header, enough for one garlic encrypted streaming packet
auto msg = m_I2NPTunnelMessagesMemoryPool.AcquireSharedMt ();
msg->Align (6);
msg->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
return msg;
}
int Tunnels::GetTransitTunnelsExpirationTimeout ()
{
int timeout = 0;

6
libi2pd/Tunnel.h

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
#include <thread>
#include <mutex>
#include <memory>
#include "util.h"
#include "Queue.h"
#include "Crypto.h"
#include "TunnelConfig.h"
@ -40,6 +41,8 @@ namespace tunnel @@ -40,6 +41,8 @@ namespace tunnel
const int MAX_NUM_RECORDS = 8;
const int HIGH_LATENCY_PER_HOP = 250; // in milliseconds
const size_t I2NP_TUNNEL_ENPOINT_MESSAGE_SIZE = 2*TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE + 28; // reserved for alignment and NTCP 16 + 6 + 6
enum TunnelState
{
eTunnelStatePending,
@ -219,6 +222,8 @@ namespace tunnel @@ -219,6 +222,8 @@ namespace tunnel
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ();
private:
template<class TTunnel>
@ -257,6 +262,7 @@ namespace tunnel @@ -257,6 +262,7 @@ namespace tunnel
std::list<std::shared_ptr<TunnelPool>> m_Pools;
std::shared_ptr<TunnelPool> m_ExploratoryPool;
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_Queue;
i2p::util::MemoryPoolMt<I2NPMessageBuffer<I2NP_TUNNEL_ENPOINT_MESSAGE_SIZE> > m_I2NPTunnelMessagesMemoryPool;
// some stats
int m_NumSuccesiveTunnelCreations, m_NumFailedTunnelCreations;

38
libi2pd/util.h

@ -56,12 +56,8 @@ namespace util @@ -56,12 +56,8 @@ namespace util
void CleanUp ()
{
while (m_Head)
{
auto tmp = m_Head;
m_Head = static_cast<T*>(*(void * *)m_Head); // next
::operator delete ((void *)tmp);
}
CleanUp (m_Head);
m_Head = nullptr;
}
template<typename... TArgs>
@ -98,6 +94,18 @@ namespace util @@ -98,6 +94,18 @@ namespace util
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
}
protected:
void CleanUp (T * head)
{
while (head)
{
auto tmp = head;
head = static_cast<T*>(*(void * *)head); // next
::operator delete ((void *)tmp);
}
}
protected:
T * m_Head;
@ -131,6 +139,24 @@ namespace util @@ -131,6 +139,24 @@ namespace util
this->Release (it);
}
template<typename... TArgs>
std::shared_ptr<T> AcquireSharedMt (TArgs&&... args)
{
return std::shared_ptr<T>(AcquireMt (std::forward<TArgs>(args)...),
std::bind<void (MemoryPoolMt<T>::*)(T *)> (&MemoryPoolMt<T>::ReleaseMt, this, std::placeholders::_1));
}
void CleanUpMt ()
{
T * head;
{
std::lock_guard<std::mutex> l(m_Mutex);
head = this->m_Head;
this->m_Head = nullptr;
}
if (head) this->CleanUp (head);
}
private:
std::mutex m_Mutex;

Loading…
Cancel
Save