From 157546b8cdb8e6523b52650670b5773b14ff7064 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 16 Jul 2024 09:11:56 +0200 Subject: [PATCH 1/3] Print detailed and per-thread memory information if ArduinoCore-mbed/libmbed.a was compiled with the correct flags: Edit variants/OPTA/conf/mbed_app.json target.macros_add: [ @@ -16,7 +16,10 @@ VIRTIO_DRIVER_ONLY, NO_ATOMIC_64_SUPPORT, METAL_MAX_DEVICE_REGIONS=2, - RPMSG_BUFFER_SIZE=512 + RPMSG_BUFFER_SIZE=512, + MBED_HEAP_STATS_ENABLED=1, + MBED_STACK_STATS_ENABLED=1, + MBED_MEM_TRACING_ENABLED=1 ] --- examples/opcua_server/opcua_server.ino | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/examples/opcua_server/opcua_server.ino b/examples/opcua_server/opcua_server.ino index b5a84da..50a2550 100644 --- a/examples/opcua_server/opcua_server.ino +++ b/examples/opcua_server/opcua_server.ino @@ -9,6 +9,10 @@ # define ARDUINO_OPEN62541_O1HEAP_DEBUG (0) /* Change to (1) if you want to see debug messages on Serial concerning o1heap memory calls. */ #endif +#if MBED_HEAP_STATS_ENABLED && MBED_MEM_TRACING_ENABLED && MBED_STACK_STATS_ENABLED +#include "mbed_mem_trace.h" +#endif + /************************************************************************************** * GLUE CODE **************************************************************************************/ @@ -277,6 +281,23 @@ void setup() o1heapGetDiagnostics(o1heap_ins).allocated, o1heapGetDiagnostics(o1heap_ins).peak_allocated); +#if MBED_HEAP_STATS_ENABLED && MBED_MEM_TRACING_ENABLED && MBED_STACK_STATS_ENABLED + /* Print stack/heap memory information. For information how to enable it + * see https://os.mbed.com/blog/entry/Tracking-memory-usage-with-Mbed-OS/ + */ + int num_thds = osThreadGetCount(); + mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(num_thds * sizeof(mbed_stats_stack_t)); + + num_thds = mbed_stats_stack_get_each(stats, num_thds); + for (int i = 0; i < num_thds; i++) + UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Thread: 0x%lX, Stack size: %lu / %lu", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size); + free(stats); + + mbed_stats_heap_t heap_stats; + mbed_stats_heap_get(&heap_stats); + UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Heap size: %lu / %lu bytes", heap_stats.current_size, heap_stats.reserved_size); +#endif + /* Run the server (until ctrl-c interrupt) */ UA_StatusCode const status = UA_Server_runUntilInterrupt(opc_ua_server); }); From 586970ba389df7ea3eca33d23409339ba1eb3cbe Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 16 Jul 2024 09:16:12 +0200 Subject: [PATCH 2/3] Minimal documentation on how to recompile mbed core for detailed memory tracing. --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 8687aa3..c950f5e 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,18 @@ git clone https://github.com/FreeOpcUa/opcua-client-gui && cd opcua-client-gui python3 -m pip install --upgrade pyopenssl python3 -m pip install --upgrade . ``` + +### How-to-enable detailed heap/stack memory debugging information +* Edit [`variants/OPTA/conf/mbed_app.json`](https://github.com/arduino/ArduinoCore-mbed/blob/main/variants/OPTA/conf/mbed_app.json) +```diff +"target.macros_add": [ + ... ++ "MBED_HEAP_STATS_ENABLED=1", ++ "MBED_STACK_STATS_ENABLED=1", ++ "MBED_MEM_TRACING_ENABLED=1" +``` +* Recompile `libmbed.a` +```bash +cd ArduinoCore-mbed +./mbed-os-to-arduino -a -g OPTA:OPTA +``` From 64de8140493ecb20693dc5dcf2acb9fe5fe81f3a Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 16 Jul 2024 09:21:45 +0200 Subject: [PATCH 3/3] Additionally identify thread not only by ID but also by name. --- examples/opcua_server/opcua_server.ino | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/opcua_server/opcua_server.ino b/examples/opcua_server/opcua_server.ino index 50a2550..c2ad08d 100644 --- a/examples/opcua_server/opcua_server.ino +++ b/examples/opcua_server/opcua_server.ino @@ -285,13 +285,18 @@ void setup() /* Print stack/heap memory information. For information how to enable it * see https://os.mbed.com/blog/entry/Tracking-memory-usage-with-Mbed-OS/ */ - int num_thds = osThreadGetCount(); - mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(num_thds * sizeof(mbed_stats_stack_t)); + size_t const num_thds = osThreadGetCount(); + mbed_stats_stack_t *stack_stats = (mbed_stats_stack_t *) malloc(num_thds * sizeof(mbed_stats_stack_t)); + mbed_stats_stack_get_each(stack_stats, num_thds); + + mbed_stats_thread_t * thd_stats = (mbed_stats_thread_t *) malloc(num_thds * sizeof(mbed_stats_thread_t)); + mbed_stats_thread_get_each(thd_stats, num_thds); - num_thds = mbed_stats_stack_get_each(stats, num_thds); for (int i = 0; i < num_thds; i++) - UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Thread: 0x%lX, Stack size: %lu / %lu", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size); - free(stats); + UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Thread: 0x%lX (\"%s\"), Stack size: %lu / %lu", + stack_stats[i].thread_id, thd_stats[i].name, stack_stats[i].max_size, stack_stats[i].reserved_size); + free(stack_stats); + free(thd_stats); mbed_stats_heap_t heap_stats; mbed_stats_heap_get(&heap_stats);