MQTT Outbox Malloc from PSRAM

NotMyRealName
Posts: 41
Joined: Thu Feb 13, 2020 1:35 am

MQTT Outbox Malloc from PSRAM

Postby NotMyRealName » Fri Dec 16, 2022 12:55 am

I have an application I am developing for a commercial project that uses an ESP32 with SPI PSRAM.

I've just realised that messages I queue in the MQTT outbox are duplicated in a buffer allocated from a standard malloc call. With the way I have it setup this will come from the internal RAM. Would it be possible/practical to modify the library to have a config option to prefer allocation of all outbox buffers from the external RAM?

I realise there is a custom outbox implementation config option, I haven't found examples for that yet, but might have a go at that too.
That's not as simple of course... :)

xien551
Posts: 69
Joined: Wed Feb 02, 2022 4:04 am

Re: MQTT Outbox Malloc from PSRAM

Postby xien551 » Fri Dec 16, 2022 4:18 am

Directly wirite or read the values through the address of external ram. Maybe there are examples in IDF.

Or do "idf menuconfig", maybe you could configure it as your wish.

NotMyRealName
Posts: 41
Joined: Thu Feb 13, 2020 1:35 am

Re: MQTT Outbox Malloc from PSRAM

Postby NotMyRealName » Sun Dec 18, 2022 7:28 pm

Thanks for the suggestion. I understand what you are suggesting, but I can already write or read external PSRAM just fine.

I've already been through the MQTT code. The issue is in the implementation of the MQTT library. Anything you pass to the MQTT publish function will be copied to an internal outbox. This is a linked list of dynamically allocated buffers. These buffers are allocated using malloc.

In other component libraries there is an option in the config to prefer allocation from external memory. There is no option like this for MQTT.

It basically means that I could in theory buffer and prepare a large message (e.g. 10-50KB) in external ram just fine but then be unable to send it because there might not be enough internal memory to duplicate it in the outbox.

Another solution (harder to implement) would be a zero copy publish.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: MQTT Outbox Malloc from PSRAM

Postby ESP_igrr » Mon Dec 19, 2022 10:09 am

Hi NotMyRealName,
Your suggestion does make sense, could you please open a feature request at https://github.com/espressif/esp-mqtt/issues? This way it is going to be tracked by the team working on esp-mqtt.

NotMyRealName
Posts: 41
Joined: Thu Feb 13, 2020 1:35 am

Re: MQTT Outbox Malloc from PSRAM

Postby NotMyRealName » Mon Dec 19, 2022 9:03 pm

Thanks ESP_igrr,

Link to the issue in case anyone wants to find it:
https://github.com/espressif/esp-mqtt/issues/242

Who is online

Users browsing this forum: Majestic-12 [Bot], Nespressif and 123 guests