WIP: Logging optimizations and memory management

Changed a few key logs to use printf-style instead of string
concatenation to avoid dynamic memory allocation. Still need to move
all remaining logging over to printf-style.

Added option (-DRNS_USE_ALLOCATOR=1) to override new/delete operators for
both allocation metrics and the ability to implement an alternate memory
allocator.

Added option (-DRNS_USE_TLSF=1) to use the alternate TLSF allocator
(must also define RNS_USE_ALLOCATOR). This allocator is well known for
reducing fragmentation and providing constant, O(1)-time allocation
and deallocation for real-time embedded systems.

This TLSF allocator appears to already be the default allocator for
ESP32 boards, but the allocator used by NRF52 is unknown at this time so
the TLSF allocator internal to microReticulum is only being used for
NRF52 boards at this time.

Future memory management improvements will include custom allocators for
long-lived structures like the path table that will segregate them from
the general pool of memory that is constantly cycling, thereby further
reducing fragmentation. It will also allow these structures to use
alternate memory like PSRAM present on T-Beam boards.
This commit is contained in:
Chad Attermann committed 2024-07-16 10:40:46 -06:00
1 parent 25741cf235
commit 3af5111606
30 files changed
+2428 -355

No files matched your search

+163 -3
View File
@@ -6,21 +6,181 @@
#include <assert.h>
RNS::Reticulum reticulum = {RNS::Type::NONE};
class TestInterface : public RNS::Interface {
void testDestinationTable() {
public:
TestInterface(const char* name = "TestInterface") : Interface(name) {}
virtual ~TestInterface() {}
virtual inline std::string toString() const { return "TestInterface[" + name() + "]"; }
private:
virtual void on_incoming(const RNS::Bytes& data) {
RNS::debug(toString() + ".on_incoming: data: " + data.toHex());
Interface::on_incoming(data);
}
virtual void on_outgoing(const RNS::Bytes& data) {
RNS::debug(toString() + ".on_outgoing: data: " + data.toHex());
Interface::on_outgoing(data);
}
};
class InInterface : public RNS::Interface {
public:
InInterface() : RNS::Interface("InInterface") {
OUT(false);
IN(true);
}
InInterface(const char *name) : RNS::Interface(name) {
OUT(false);
IN(true);
}
virtual ~InInterface() {
name("(deleted)");
}
virtual void on_incoming(const RNS::Bytes &data) {
HEAD("InInterface.on_incoming: data: " + data.toHex(), RNS::LOG_TRACE);
RNS::Interface::on_incoming(data);
}
virtual inline std::string toString() const { return "InInterface[" + name() + "]"; }
};
class OutInterface : public RNS::Interface {
public:
OutInterface(InInterface& in_interface) : RNS::Interface("OutInterface"), _in_interface(in_interface) {
OUT(true);
IN(false);
}
OutInterface(InInterface& in_interface, const char *name) : RNS::Interface(name), _in_interface(in_interface) {
OUT(true);
IN(false);
}
virtual ~OutInterface() {
name("(deleted)");
}
virtual void on_outgoing(const RNS::Bytes &data) {
HEAD("OutInterface.on_outgoing: data: " + data.toHex(), RNS::LOG_TRACE);
_in_interface.on_incoming(data);
RNS::Interface::on_outgoing(data);
}
virtual inline std::string toString() const { return "OutInterface[" + name() + "]"; }
private:
InInterface& _in_interface;
};
// Test AnnounceHandler
class ExampleAnnounceHandler : public RNS::AnnounceHandler {
public:
ExampleAnnounceHandler(const char* aspect_filter = nullptr) : AnnounceHandler(aspect_filter) {}
virtual ~ExampleAnnounceHandler() {}
virtual void received_announce(const RNS::Bytes& destination_hash, const RNS::Identity& announced_identity, const RNS::Bytes& app_data) {
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
INFO("ExampleAnnounceHandler: destination hash: " + destination_hash.toHex());
if (announced_identity) {
INFO("ExampleAnnounceHandler: announced identity hash: " + announced_identity.hash().toHex());
INFO("ExampleAnnounceHandler: announced identity app data: " + announced_identity.app_data().toHex());
}
if (app_data) {
INFO("ExampleAnnounceHandler: app data text: \"" + app_data.toString() + "\"");
}
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
};
//ExampleAnnounceHandler announce_handler((const char*)"example_utilities.announcesample.fruits");
//RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler("example_utilities.announcesample.fruits"));
RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler());
// Test packet receive callback
void onPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
INFO("onPacket: data: " + data.toHex());
INFO("onPacket: text: \"" + data.toString() + "\"");
//TRACE("onPacket: " + packet.debugString());
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
// Ping packet receive callback
void onPingPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
INFO("onPingPacket: data: " + data.toHex());
INFO("onPingPacket: text: \"" + data.toString() + "\"");
//TRACE("onPingPacket: " + packet.debugString());
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
RNS::Reticulum reticulum({RNS::Type::NONE});
RNS::Identity identity({RNS::Type::NONE});
RNS::Destination destination({RNS::Type::NONE});
InInterface in_interface;
OutInterface out_interface(in_interface);
void testReticulum() {
HEAD("Running testReticulum...", RNS::LOG_TRACE);
HEAD("Registering Interface instances with Transport...", RNS::LOG_TRACE);
RNS::Transport::register_interface(in_interface);
RNS::Transport::register_interface(out_interface);
RNS::Bytes transport_prv_bytes;
#ifdef ARDUINO
transport_prv_bytes.assignHex("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
#else
transport_prv_bytes.assignHex("BABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABE");
#endif
RNS::Identity transport_identity(false);
transport_identity.load_private_key(transport_prv_bytes);
RNS::Transport::identity(transport_identity);
HEAD("Creating Reticulum instance...", RNS::LOG_TRACE);
reticulum = RNS::Reticulum();
reticulum.transport_enabled(true);
reticulum.probe_destination_enabled(true);
reticulum.start();
testDestinationTable();
HEAD("Creating Identity instance...", RNS::LOG_TRACE);
// new identity
identity = RNS::Identity(false);
RNS::Bytes prv_bytes;
#ifdef ARDUINO
prv_bytes.assignHex("78E7D93E28D55871608FF13329A226CABC3903A357388A035B360162FF6321570B092E0583772AB80BC425F99791DF5CA2CA0A985FF0415DAB419BBC64DDFAE8");
#else
prv_bytes.assignHex("E0D43398EDC974EBA9F4A83463691A08F4D306D4E56BA6B275B8690A2FBD9852E9EBE7C03BC45CAEC9EF8E78C830037210BFB9986F6CA2DEE2B5C28D7B4DE6B0");
#endif
identity.load_private_key(prv_bytes);
// 22.6% (+0.7%)
HEAD("Creating Destination instance...", RNS::LOG_TRACE);
//RNS::Destination destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
destination = RNS::Destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
// 23.0% (+0.4%)
// Register DATA packet callback
HEAD("Registering packet callback with Destination...", RNS::LOG_TRACE);
destination.set_packet_callback(onPacket);
destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
{
// Register PING packet callback
HEAD("Creating PING Destination instance...", RNS::LOG_TRACE);
RNS::Destination ping_destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "example_utilities", "echo.request");
HEAD("Registering packet callback with PING Destination...", RNS::LOG_TRACE);
ping_destination.set_packet_callback(onPingPacket);
ping_destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
}
HEAD("Registering announce handler with Transport...", RNS::LOG_TRACE);
RNS::Transport::register_announce_handler(announce_handler);
HEAD("Announcing destination...", RNS::LOG_TRACE);
// test path
//destination.announce(RNS::bytesFromString("test"), true, nullptr, RNS::bytesFromString("test_tag"));
// test packet send
destination.announce(RNS::bytesFromString("test"));
// 23.9% (+0.8%)
reticulum = {RNS::Type::NONE};
}