Skip to content

Commit

Permalink
Support to use heap for pool based on actual usage for large systems (#…
Browse files Browse the repository at this point in the history
…8299)

* Support to use heap for pool for large systems

* Update src/system/SystemObject.h

Co-authored-by: Michael Sandstedt <[email protected]>

* Use templated foreach function to safely execute operation on each object

Co-authored-by: Michael Sandstedt <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Aug 21, 2021
1 parent 211e058 commit 5673363
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 73 deletions.
3 changes: 1 addition & 2 deletions src/inet/IPEndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis
*/
typedef void (*OnReceiveErrorFunct)(IPEndPointBasis * endPoint, CHIP_ERROR err, const IPPacketInfo * pktInfo);

IPEndPointBasis() = default;
CHIP_ERROR SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback);
CHIP_ERROR JoinMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress);
CHIP_ERROR LeaveMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress);
Expand Down Expand Up @@ -178,9 +179,7 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis
#endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API

private:
IPEndPointBasis() = delete;
IPEndPointBasis(const IPEndPointBasis &) = delete;
~IPEndPointBasis() = delete;
};

#if CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down
62 changes: 24 additions & 38 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,13 @@ CHIP_ERROR InetLayer::Shutdown()
{
#if INET_CONFIG_ENABLE_DNS_RESOLVER
// Cancel all DNS resolution requests owned by this instance.
for (size_t i = 0; i < DNSResolver::sPool.Size(); i++)
{
DNSResolver * lResolver = DNSResolver::sPool.Get(*mSystemLayer, i);
DNSResolver::sPool.ForEachActiveObject([&](DNSResolver * lResolver) {
if ((lResolver != nullptr) && lResolver->IsCreatedByInetLayer(*this))
{
lResolver->Cancel();
}
}
return true;
});

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS

Expand All @@ -337,38 +336,35 @@ CHIP_ERROR InetLayer::Shutdown()

#if INET_CONFIG_ENABLE_RAW_ENDPOINT
// Close all raw endpoints owned by this Inet layer instance.
for (size_t i = 0; i < RawEndPoint::sPool.Size(); i++)
{
RawEndPoint * lEndPoint = RawEndPoint::sPool.Get(*mSystemLayer, i);
RawEndPoint::sPool.ForEachActiveObject([&](RawEndPoint * lEndPoint) {
if ((lEndPoint != nullptr) && lEndPoint->IsCreatedByInetLayer(*this))
{
lEndPoint->Close();
}
}
return true;
});
#endif // INET_CONFIG_ENABLE_RAW_ENDPOINT

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
// Abort all TCP endpoints owned by this instance.
for (size_t i = 0; i < TCPEndPoint::sPool.Size(); i++)
{
TCPEndPoint * lEndPoint = TCPEndPoint::sPool.Get(*mSystemLayer, i);
TCPEndPoint::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) {
if ((lEndPoint != nullptr) && lEndPoint->IsCreatedByInetLayer(*this))
{
lEndPoint->Abort();
}
}
return true;
});
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

#if INET_CONFIG_ENABLE_UDP_ENDPOINT
// Close all UDP endpoints owned by this instance.
for (size_t i = 0; i < UDPEndPoint::sPool.Size(); i++)
{
UDPEndPoint * lEndPoint = UDPEndPoint::sPool.Get(*mSystemLayer, i);
UDPEndPoint::sPool.ForEachActiveObject([&](UDPEndPoint * lEndPoint) {
if ((lEndPoint != nullptr) && lEndPoint->IsCreatedByInetLayer(*this))
{
lEndPoint->Close();
}
}
return true;
});
#endif // INET_CONFIG_ENABLE_UDP_ENDPOINT
}

Expand Down Expand Up @@ -410,17 +406,15 @@ bool InetLayer::IsIdleTimerRunning()
{
bool timerRunning = false;

// see if there are any TCP connections with the idle timer check in use.
for (size_t i = 0; i < TCPEndPoint::sPool.Size(); i++)
{
TCPEndPoint * lEndPoint = TCPEndPoint::sPool.Get(*mSystemLayer, i);

// See if there are any TCP connections with the idle timer check in use.
TCPEndPoint::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) {
if ((lEndPoint != nullptr) && (lEndPoint->mIdleTimeout != 0))
{
timerRunning = true;
break;
return false;
}
}
return true;
});

return timerRunning;
}
Expand Down Expand Up @@ -879,40 +873,32 @@ void InetLayer::CancelResolveHostAddress(DNSResolveCompleteFunct onComplete, voi
if (State != kState_Initialized)
return;

for (size_t i = 0; i < DNSResolver::sPool.Size(); i++)
{
DNSResolver * lResolver = DNSResolver::sPool.Get(*mSystemLayer, i);

if (lResolver == nullptr)
{
continue;
}

DNSResolver::sPool.ForEachActiveObject([&](DNSResolver * lResolver) {
if (!lResolver->IsCreatedByInetLayer(*this))
{
continue;
return true;
}

if (lResolver->OnComplete != onComplete)
{
continue;
return true;
}

if (lResolver->AppState != appState)
{
continue;
return true;
}

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
if (lResolver->mState == DNSResolver::kState_Canceled)
{
continue;
return true;
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS

lResolver->Cancel();
break;
}
return false;
});
}

#endif // INET_CONFIG_ENABLE_DNS_RESOLVER
Expand Down
3 changes: 1 addition & 2 deletions src/inet/RawEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class DLL_EXPORT RawEndPoint : public IPEndPointBasis
*/
IPProtocol IPProto; // This data member is read-only

RawEndPoint() = default;
CHIP_ERROR Bind(IPAddressType addrType, const IPAddress & addr, InterfaceId intfId = INET_NULL_INTERFACEID);
CHIP_ERROR BindIPv6LinkLocal(InterfaceId intfId, const IPAddress & addr);
CHIP_ERROR BindInterface(IPAddressType addrType, InterfaceId intfId);
Expand All @@ -88,9 +89,7 @@ class DLL_EXPORT RawEndPoint : public IPEndPointBasis
void Free();

private:
RawEndPoint() = delete;
RawEndPoint(const RawEndPoint &) = delete;
~RawEndPoint() = delete;

static chip::System::ObjectPool<RawEndPoint, INET_CONFIG_NUM_RAW_ENDPOINTS> sPool;

Expand Down
4 changes: 2 additions & 2 deletions src/inet/TCPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
kState_Closed = 8 /**< Endpoint closed, ready for release. */
} State;

TCPEndPoint() = default;

/**
* @brief Bind the endpoint to an interface IP address.
*
Expand Down Expand Up @@ -637,9 +639,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis

#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT

TCPEndPoint(); // not defined
TCPEndPoint(const TCPEndPoint &); // not defined
~TCPEndPoint(); // not defined

void Init(InetLayer * inetLayer);
CHIP_ERROR DriveSending();
Expand Down
4 changes: 2 additions & 2 deletions src/inet/UDPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
friend class InetLayer;

public:
UDPEndPoint() = default;

CHIP_ERROR Bind(IPAddressType addrType, const IPAddress & addr, uint16_t port, InterfaceId intfId = INET_NULL_INTERFACEID);
CHIP_ERROR BindInterface(IPAddressType addrType, InterfaceId intfId);
InterfaceId GetBoundInterface();
Expand All @@ -69,9 +71,7 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
void Free();

private:
UDPEndPoint() = delete;
UDPEndPoint(const UDPEndPoint &) = delete;
~UDPEndPoint() = delete;

static chip::System::ObjectPool<UDPEndPoint, INET_CONFIG_NUM_UDP_ENDPOINTS> sPool;

Expand Down
6 changes: 5 additions & 1 deletion src/inet/tests/TestInetEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ static void TestInetEndPointInternal(nlTestSuite * inSuite, void * inContext)
testTCPEP1->Shutdown();
}

#if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
// Test the InetLayer resource limitation
static void TestInetEndPointLimit(nlTestSuite * inSuite, void * inContext)
{
Expand All @@ -496,7 +497,7 @@ static void TestInetEndPointLimit(nlTestSuite * inSuite, void * inContext)
#endif //
UDPEndPoint * testUDPEP = nullptr;
TCPEndPoint * testTCPEP = nullptr;
CHIP_ERROR err;
CHIP_ERROR err = CHIP_NO_ERROR;
char numTimersTest[CHIP_SYSTEM_CONFIG_NUM_TIMERS + 1];

#if INET_CONFIG_ENABLE_RAW_ENDPOINT
Expand Down Expand Up @@ -527,6 +528,7 @@ static void TestInetEndPointLimit(nlTestSuite * inSuite, void * inContext)
ShutdownNetwork();
ShutdownSystemLayer();
}
#endif

// Test Suite

Expand All @@ -541,7 +543,9 @@ static const nlTest sTests[] = { NL_TEST_DEF("InetEndPoint::PreTest", TestInetPr
NL_TEST_DEF("InetEndPoint::TestInetError", TestInetError),
NL_TEST_DEF("InetEndPoint::TestInetInterface", TestInetInterface),
NL_TEST_DEF("InetEndPoint::TestInetEndPoint", TestInetEndPointInternal),
#if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
NL_TEST_DEF("InetEndPoint::TestEndPointLimit", TestInetEndPointLimit),
#endif
NL_TEST_SENTINEL() };

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct ChipDeviceEvent;
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1
#define CHIP_SYSTEM_CONFIG_POOL_USE_HEAP 1

// ========== Platform-specific Configuration Overrides =========

Expand Down
9 changes: 9 additions & 0 deletions src/system/SystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@
#define CHIP_SYSTEM_CONFIG_MBED_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_MBED_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
*
* @brief
* Allocate Pool from Heap for large systems (e.g. Linux).
*/
#ifndef CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
#define CHIP_SYSTEM_CONFIG_POOL_USE_HEAP 0
#endif /* CHIP_SYSTEM_CONFIG_POOL_USE_HEAP */

/**
* @def CHIP_SYSTEM_CONFIG_NO_LOCKING
Expand Down
8 changes: 7 additions & 1 deletion src/system/SystemObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
// Include local headers
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

namespace chip {
namespace System {
Expand All @@ -52,6 +51,13 @@ DLL_EXPORT void Object::Release()
if (oldCount == 1)
{
this->mSystemLayer = nullptr;
#if CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
std::lock_guard<std::mutex> lock(*mMutexRef);
this->mPrev->mNext = this->mNext;
if (this->mNext)
this->mNext->mPrev = this->mPrev;
delete this;
#endif
__sync_synchronize();
}
else if (oldCount == 0)
Expand Down
Loading

0 comments on commit 5673363

Please sign in to comment.