From 8934aaa9124f9384be918f0d6b9fc66e7b688666 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Mon, 15 Aug 2016 14:53:48 +0200 Subject: [PATCH] Load mon: populate memory usage i field for NuttX --- src/modules/load_mon/load_mon.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/modules/load_mon/load_mon.cpp b/src/modules/load_mon/load_mon.cpp index 690e955a38..e4437eab64 100644 --- a/src/modules/load_mon/load_mon.cpp +++ b/src/modules/load_mon/load_mon.cpp @@ -93,6 +93,9 @@ private: /* Do a calculation of the CPU load and publish it. */ void _compute(); + /* Calculate the memory usage */ + float _ram_used(); + bool _taskShouldExit; bool _taskIsRunning; struct work_s _work; @@ -163,6 +166,7 @@ void LoadMon::_compute() _cpuload.timestamp = hrt_absolute_time(); _cpuload.load = 1.0f - (float)interval_idletime / (float)LOAD_MON_INTERVAL_US; + _cpuload.ram_usage = _ram_used(); if (_cpuload_pub == nullptr) { _cpuload_pub = orb_advertise(ORB_ID(cpuload), &_cpuload); @@ -172,7 +176,32 @@ void LoadMon::_compute() } } +float LoadMon::_ram_used() +{ +#ifdef _PX4_NUTTX + struct mallinfo mem; +#ifdef CONFIG_CAN_PASS_STRUCTS + mem = mallinfo(); +#else + (void)mallinfo(&mem); +#endif + + // mem.arena: total ram (bytes) + // mem.uordblks: used (bytes) + // mem.fordblks: free (bytes) + // mem.mxordblk: largest remaining block (bytes) + + float load = (float)mem.uordblks / mem.arena; + + // Check for corruption of the allocation counters + if ((mem.arena > CONFIG_DRAM_SIZE) || (mem.fordblks > CONFIG_DRAM_SIZE)) { + return 1.0f; + } +#else + return 0.0f; +#endif +} /** * Print the correct usage.