zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.7k stars 6.54k forks source link

Bluetooth: Host RX buffer allocation not optimal #72260

Open cvinayak opened 5 months ago

cvinayak commented 5 months ago

Describe the bug Bluetooth Host Buffers are allocated for maximum count and maximum buffer size of the ACL RX, or EVT RX or ISO RX counts and sizes.

https://github.com/zephyrproject-rtos/zephyr/blob/15fa7f79da1942d89a460112921a2b5c2e24c7b5/include/zephyr/bluetooth/buf.h#L88-L95

Typically, each of the buffer sizes and buffer counts are very much varying in the ranges, but a firmware is being built for maximum RAM allocation, and low RAM utilization.

To Reproduce Steps to reproduce the behavior:

  1. mkdir -p build/iso_broadcast; cd build/iso_broadcast
  2. cmake -GNinja -DBOARD=nrf52833dk_nrf52820 -DEXTRA_CONF_FILE=overlay-bt_ll_sw_split.conf -DCONFIG_BT_ISO_RX_BUF_COUNT=1 -DCONFIG_BT_ISO_RX_MTU=251 ../../samples/bluetooth/iso_broadcast
  3. ninja
  4. See size
  5. Memory region Used Size Region Size %age Used FLASH: 117096 B 256 KB 44.67% RAM: 24934 B 32 KB 76.09% IDT_LIST: 0 GB 32 KB 0.00%

  6. Build with 10 bytes less RX MTU
  7. cmake -GNinja -DBOARD=nrf52833dk_nrf52820 -DEXTRA_CONF_FILE=overlay-bt_ll_sw_split.conf -DCONFIG_BT_ISO_RX_BUF_COUNT=1 -DCONFIG_BT_ISO_RX_MTU=241 ../../samples/bluetooth/iso_broadcast
  8. ninja
  9. See size
  10. Memory region Used Size Region Size %age Used FLASH: 117096 B 256 KB 44.67% RAM: 24834 B 32 KB 75.79% IDT_LIST: 0 GB 32 KB 0.00%

  11. The difference is 10 times because in the sample, CONFIG_BT_BUF_EVT_RX_COUNT=10

Expected behavior RAM usage should optimally reflect the Kconfig counts and size configured.

Impact Showstopper for resource constraint devices.

Logs and console output See description above for memory usage.

Environment (please complete the following information):

Additional context https://discord.com/channels/720317445772017664/1235629095526858762

jori-nordic commented 3 months ago

I don't think this is a bug. It's the way the stack allocates memory due to the current HCI architecture. I still agree we should optimize it though.