nf-interpreter icon indicating copy to clipboard operation
nf-interpreter copied to clipboard

Resolve C3 managed memory size

Open AdrianSoundy opened this issue 1 year ago • 1 comments

Description

For some reason we were getting a much smaller size of managed heap when running on IF 5.x firmware then previously even through parameters where unchanged. To try to correct this another PR was merged with a changed ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION parameter (changed from 100 -> 50). This improved memory size but not enough.

I have investigated why this happened and adjusted code to stop it happening on future releases.

The reason is due to the IDF 5.x version having a more fragmented heap although still a similar amount of free memory.   We were working out how much memory to allocate for managed heap based on largest free block minus amount of memory to reserve for IDF.

This has now been changed to use the free space instead of largest free then sized to fit largest block.
This is a more accurate value to reserve for IDF. The CmakePreset.json values have been restored to original values.

Largest free block with IDF 4.4.6 was 221184. With IDF 5.1.3 it is 114688 Free heap for 4.4.6 is 240600 and 232884 for IDF 5.1.3

Motivation and Context

Issue reported on discord

How Has This Been Tested?

Tested with local builds and c# test program.

Types of changes

  • [ ] Improvement (non-breaking change that improves a feature, code or algorithm)
  • [x] Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • [ ] New feature (non-breaking change which adds functionality to code)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Config and build (change in the configuration and build system, has no impact on code or features)
  • [ ] Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • [ ] Dependencies (update dependencies and changes associated, has no impact on code or features)
  • [ ] Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • [x] My code follows the code style of this project (only if there are changes in source code).
  • [ ] My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • [ ] I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • [x] I have read the CONTRIBUTING document.
  • [x] I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • Improvements
    • Increased reserved RAM for ESP32 targets from 50 to 100, enhancing memory allocation stability.
    • Optimized memory management for ESP32 by adjusting managed heap size based on the largest available memory block, improving overall performance and reliability.

AdrianSoundy avatar May 18 '24 05:05 AdrianSoundy

Walkthrough

The changes primarily involve memory management adjustments and API updates for the ESP32 target. Key modifications include removing the ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION parameter, defining new constants for memory allocation, and updating the logic for managed heap allocation. These changes aim to optimize memory usage and improve support for various APIs like Bluetooth, OneWire, and I2C Slave.

Changes

Files/Paths Change Summary
targets/ESP32/CMakePresets.json Removed ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION and updated API configurations for Bluetooth, OneWire, I2C Slave, USB CDC, and SPIRAM.
targets/ESP32/_nanoCLR/Memory.cpp Added constants for minimum memory requirements and adjusted heap allocation logic for Bluetooth and Thread configurations.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant ESP32
    participant MemoryManager

    User ->> ESP32: Start Application
    ESP32 ->> MemoryManager: Initialize Memory
    MemoryManager -->> ESP32: Allocate Managed Heap
    ESP32 ->> MemoryManager: Configure Bluetooth and Thread
    MemoryManager -->> ESP32: Reserve Extra Memory
    ESP32 ->> User: Application Running

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar May 18 '24 05:05 coderabbitai[bot]

@josesimoes These latest changes change the way we allocated the native memory when no spiram. Originally we would take ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION and subtract off largest free block to get managed heap size. This was sort of ok when native heap is 1 large free block but we had cases with IDF 5.x where it was 2 smaller blocks. With this algorithm we ended up with very small managed heap size.

The new algorithm uses the free native memory instead to get a more accurate idea of available memory.
If no ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION is specified we set the minimum memory for IDF to 100K and add more memory if BLE or THREAD is specified in config. If ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION is specified it uses that instead. The minimum for ESP32_H2 is set to 30K for now as less available memory. (May be adjusted on next PR)

Note these changes only affect native memory allocation to IRAM and not the original SPIRAM allocation

To help with this all the ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION were removed from CmakePresets.json. A lot of the original entries didn't do anything due to heap fragmentation.

Testing

From testing comparing IDF 4 -> IDF 5 for various targets Available memory on startup

S3 target are not listed as normally using PSRAM

Target Managed 4 Native free 4 >>> Managed 5 Native free 5 Note
ESP32_REV0 101412 69816 >>> 101412 66136
ESP32_REV3 101412 68012 >>> 101496 69588
M5CORE 50172 142024 >>> 101376 52156 May need to be adjusted
ESP32_PICO 86016 80804 >>> 101412 64744
XIAO_C3 109608 66108 >>> 105504 59480
ESP32_C6_THREAD ? ? >>> 112764 103700
ESP32_H2_THREAD ? ? >>> 45612 65188

AdrianSoundy avatar May 30 '24 10:05 AdrianSoundy

Available memory looks much better today for most targets!

josesimoes avatar May 30 '24 14:05 josesimoes

Yay, thankyou master Soundy! This completely fixed the memory issues I was having with XIAO_ESP32C3

DarkNoir avatar May 31 '24 05:05 DarkNoir