Add memory diagnostics and optimize PSRAM usage

This commit is contained in:
2026-08-25 11:08:02 +02:00
parent f6a275c543
commit d779fb046e
7 changed files with 106 additions and 14 deletions
+31 -3
View File
@@ -2,6 +2,7 @@
#include <string.h>
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
@@ -16,6 +17,9 @@
#define SESSION_BROKER_SLOT_BITS 3U
#define SESSION_BROKER_SLOT_MASK ((1U << SESSION_BROKER_SLOT_BITS) - 1U)
#define SESSION_BROKER_MAX_GENERATION (UINT32_MAX >> SESSION_BROKER_SLOT_BITS)
#define SESSION_BROKER_OUTPUT_STORAGE_SIZE (SESSION_BROKER_OUTPUT_SIZE + 1U)
#define SESSION_BROKER_EVENT_STORAGE_SIZE \
(SESSION_BROKER_EVENT_QUEUE_LENGTH * sizeof(session_broker_event_t))
_Static_assert(
SESSION_BROKER_MAX_CLIENTS == (1U << SESSION_BROKER_SLOT_BITS),
@@ -24,6 +28,10 @@ _Static_assert(
typedef struct {
StreamBufferHandle_t output;
QueueHandle_t events;
StaticStreamBuffer_t output_control;
StaticQueue_t events_control;
uint8_t *output_storage;
uint8_t *event_storage;
session_broker_client_id_t id;
uint32_t generation;
session_broker_client_type_t type;
@@ -183,6 +191,10 @@ static void cleanup_allocations(void)
vStreamBufferDelete(s_slots[i].output);
s_slots[i].output = NULL;
}
heap_caps_free(s_slots[i].event_storage);
s_slots[i].event_storage = NULL;
heap_caps_free(s_slots[i].output_storage);
s_slots[i].output_storage = NULL;
}
if (s_mutex != NULL) {
vSemaphoreDelete(s_mutex);
@@ -209,9 +221,25 @@ esp_err_t session_broker_init(void)
}
for (size_t i = 0; i < SESSION_BROKER_MAX_CLIENTS; ++i) {
s_slots[i].output = xStreamBufferCreate(SESSION_BROKER_OUTPUT_SIZE, 1U);
s_slots[i].events = xQueueCreate(SESSION_BROKER_EVENT_QUEUE_LENGTH,
sizeof(session_broker_event_t));
s_slots[i].output_storage = heap_caps_calloc_prefer(
1U, SESSION_BROKER_OUTPUT_STORAGE_SIZE, 2,
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT,
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
s_slots[i].event_storage = heap_caps_calloc_prefer(
1U, SESSION_BROKER_EVENT_STORAGE_SIZE, 2,
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT,
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (s_slots[i].output_storage != NULL) {
s_slots[i].output = xStreamBufferCreateStatic(
SESSION_BROKER_OUTPUT_STORAGE_SIZE, 1U,
s_slots[i].output_storage, &s_slots[i].output_control);
}
if (s_slots[i].event_storage != NULL) {
s_slots[i].events = xQueueCreateStatic(
SESSION_BROKER_EVENT_QUEUE_LENGTH,
sizeof(session_broker_event_t),
s_slots[i].event_storage, &s_slots[i].events_control);
}
if (s_slots[i].output == NULL || s_slots[i].events == NULL) {
cleanup_allocations();
return ESP_ERR_NO_MEM;