Skip to content

Commit

Permalink
Memory diagnostics "sys.info.mem" register (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 authored Nov 7, 2024
1 parent 3012edd commit c9e7714
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
24 changes: 19 additions & 5 deletions libcyphal_demo/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Application::Application()
: o1_heap_mr_{s_heap_arena}
, storage_{"/tmp/" NODE_NAME}
, registry_{o1_heap_mr_}
, regs_{registry_}
, regs_{o1_heap_mr_, registry_}
{
cetl::pmr::set_default_resource(&o1_heap_mr_);

Expand Down Expand Up @@ -77,14 +77,12 @@ Application::~Application()
///
void Application::getUniqueId(uavcan::node::GetInfo::Response_1_0::_traits_::TypeOf::unique_id& out)
{
using unique_id = uavcan::node::GetInfo::Response_1_0::_traits_::TypeOf::unique_id;

const auto result = storage_.get(".unique_id", out);
if (cetl::get_if<libcyphal::platform::storage::Error>(&result) != nullptr)
{
std::random_device rd; // Seed for the random number engine
std::mt19937 gen(rd()); // Mersenne Twister engine
std::uniform_int_distribution<std::uint8_t> dis(0, 255); // Distribution range for bytes
std::mt19937 gen{rd()}; // Mersenne Twister engine
std::uniform_int_distribution<std::uint8_t> dis{0, 255}; // Distribution range for bytes

// Populate the default; it is only used at the first run.
for (auto& b : out)
Expand All @@ -95,3 +93,19 @@ void Application::getUniqueId(uavcan::node::GetInfo::Response_1_0::_traits_::Typ
(void) storage_.put(".unique_id", out);
}
}

Application::Regs::Value Application::Regs::getSysInfoMem() const
{
Value value{{&o1_heap_mr_}};
auto& uint64s = value.set_natural64();

const auto diagnostics = o1_heap_mr_.queryDiagnostics();
uint64s.value.reserve(5); // five fields gonna push
uint64s.value.push_back(diagnostics.capacity);
uint64s.value.push_back(diagnostics.allocated);
uint64s.value.push_back(diagnostics.peak_allocated);
uint64s.value.push_back(diagnostics.peak_request_size);
uint64s.value.push_back(diagnostics.oom_count);

return value;
}
44 changes: 25 additions & 19 deletions libcyphal_demo/src/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class Application final

struct Regs
{
using Value = libcyphal::application::registry::IRegister::Value;

template <std::size_t Footprint>
using Register = libcyphal::application::registry::Register<Footprint>;

/// Defines the footprint size of the type-erased register.
/// The Footprint size is passed to internal unbounded variant
/// which in turn should be big enough to store any register implementation.
Expand Down Expand Up @@ -82,21 +87,18 @@ class Application final
}

private:
CETL_NODISCARD libcyphal::application::registry::IRegister::Value makeStringValue(
const cetl::string_view sv) const
CETL_NODISCARD Value makeStringValue(const cetl::string_view sv) const
{
using Value = libcyphal::application::registry::IRegister::Value;

const Value::allocator_type allocator{&memory_};
Value value{allocator};
auto& str = value.set_string();
std::copy(sv.begin(), sv.end(), std::back_inserter(str.value));
return value;
}

platform::String<N> value_;
cetl::pmr::memory_resource& memory_;
libcyphal::application::registry::Register<RegisterFootprint> register_;
platform::String<N> value_;
cetl::pmr::memory_resource& memory_;
Register<RegisterFootprint> register_;

}; // StringParam

Expand Down Expand Up @@ -138,38 +140,42 @@ class Application final
}

private:
CETL_NODISCARD libcyphal::application::registry::IRegister::Value makeNatural16Value() const
CETL_NODISCARD Value makeNatural16Value() const
{
using Value = libcyphal::application::registry::IRegister::Value;

const Value::allocator_type allocator{&memory_};
Value value{allocator};
auto& uint16s = value.set_natural16();
uint16s.value.push_back(value_.front());
return value;
}

std::array<std::uint16_t, N> value_;
cetl::pmr::memory_resource& memory_;
libcyphal::application::registry::Register<RegisterFootprint> register_;
std::array<std::uint16_t, N> value_;
cetl::pmr::memory_resource& memory_;
Register<RegisterFootprint> register_;

}; // Natural16Param

explicit Regs(libcyphal::application::registry::Registry& registry)
: registry_{registry}
Regs(platform::O1HeapMemoryResource& o1_heap_mr, libcyphal::application::registry::Registry& registry)
: o1_heap_mr_{o1_heap_mr}
, registry_{registry}
, sys_info_mem_{registry.route("sys.info.mem", [this] { return getSysInfoMem(); })}
{
}

private:
friend class Application;

Value getSysInfoMem() const;

platform::O1HeapMemoryResource& o1_heap_mr_;
libcyphal::application::registry::Registry& registry_;

// clang-format off
StringParam<MaxIfaceLen> can_iface_ { "uavcan.can.iface", registry_, {"vcan0"}, {true}};
StringParam<MaxNodeDesc> node_desc_ { "uavcan.node.description", registry_, {NODE_NAME}, {true}};
Natural16Param<1> node_id_ { "uavcan.node.id", registry_, {65535U}, {true}};
StringParam<MaxIfaceLen> udp_iface_ { "uavcan.udp.iface", registry_, {"127.0.0.1"}, {true}};
StringParam<MaxIfaceLen> can_iface_ { "uavcan.can.iface", registry_, {"vcan0"}, {true}};
StringParam<MaxNodeDesc> node_desc_ { "uavcan.node.description", registry_, {NODE_NAME}, {true}};
Natural16Param<1> node_id_ { "uavcan.node.id", registry_, {65535U}, {true}};
StringParam<MaxIfaceLen> udp_iface_ { "uavcan.udp.iface", registry_, {"127.0.0.1"}, {true}};
Register<RegisterFootprint> sys_info_mem_;
// clang-format on

}; // Regs
Expand Down

0 comments on commit c9e7714

Please sign in to comment.