SiliconLabs / arduino

Arduino Core for Silicon Labs devices
85 stars 15 forks source link

Excessive use of RAM by certain static objects #36

Open lyusupov opened 7 months ago

lyusupov commented 7 months ago

Core version: 1.0.0 Variant:          BGM220 Explorer Kit

Let's take this simple sketch and built it for BGM220 Explorer Kit target:

extern "C" void * _sbrk   (int);

static uint32_t BGM220_getFreeHeap()
{
  char top;
  return &top - reinterpret_cast<char*>(_sbrk(0));
}

void setup()
{
  Serial.begin(38400);

  Serial.print("Free Heap = "); Serial.println(BGM220_getFreeHeap());
  Serial.flush();
}
void loop() { }

We can see that this simple 'hello world' kind of sketch consumes 19392 bytes (59%) out of 32 KBytes RAM available in the BGM220.

Sketch uses 222668 bytes (42%) of program storage space. Maximum is 524288 bytes.
Global variables use 19392 bytes (59%) of dynamic memory, leaving 13376 bytes for local variables. Maximum is 32768 bytes.

These are the static objects of the sketch that consume most of RAM space:

$ arm-none-eabi-nm -n --size-sort sketch.elf | grep ' B ' | tail -12

00000050 B nvm3_defaultHandleData
00000050 B xQueueRegistry
00000058 B ADC
00000064 B mbedtls_psa_slots_mutex
00000084 B sl_spidrv_usart_mikroe_handle_data
00000090 B PWM
000000c0 B ll_scan
000000c0 B SPI
00000108 B Serial
00000110 B bg_pool_pools
00000138 B __sf
0000013c B Wire

$ arm-none-eabi-nm -n --size-sort sketch.elf | grep ' b ' | tail -12

00000140 b Timer_Stack.0
00000150 b context_vcom
00000188 b RAILINT_f5d6bd4b87b830444924423a67685a3c
00000200 b gpioCallbacks
00000200 b ll_radioTxBuffer
00000200 b RAIL_DefaultRxFifo
00000280 b Idle_Stack.2
00000320 b defaultCache
00000400 b _ZL18arduino_task_stack
00000460 b pxReadyTasksLists
00000800 b sl_stack
00001400 b ucHeap

To reproduce

$ arduino-cli config add board_manager.additional_urls https://siliconlabs.github.io/arduino/package_arduinosilabs_index.json 
$ arduino-cli core install SiliconLabs:silabs@1.0.0
$ export BOARD=SiliconLabs:silabs:bgm220explorerkit
$ arduino-cli compile -v --build-path=/tmp/arduino_silabs -b "$BOARD" <path-to-sketch-folder>
silabs-bozont commented 6 months ago

Hello @lyusupov,

Thank you for your detailed analysis! The BGM220 already has it's RAM usage cut back in order to fit into the 32K of RAM. Some of these RAM hogs will be removed in the next release - for example the SPI and Wire libraries will be moved out and will only take up RAM when you actually use them. Most of these are part of the radio stack or FreeRTOS - but we can look into further optimizing the OS buffer/stack sizes. I also see some entries like the ADC which can be further optimized.

Thanks again for the analysis, we'll continuously improve the RAM usage!