Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize allocation of managed memory #2940

Merged
merged 3 commits into from
May 31, 2024

Conversation

AdrianSoundy
Copy link
Member

@AdrianSoundy AdrianSoundy commented May 18, 2024

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)
  • 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

  • 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).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • New Features

    • Enhanced memory management for ESP32 targets, ensuring better performance and stability.
    • Improved support for Bluetooth and Thread configurations.
  • Improvements

    • Optimized memory allocation for various APIs including OneWire, Bluetooth, and I2C Slave.
    • Adjusted managed heap allocation to better utilize available memory.
  • Bug Fixes

    • Removed outdated ESP32_RESERVED_RAM_FOR_IDF_ALLOCATION parameter for cleaner configuration.

Copy link

coderabbitai bot commented May 18, 2024

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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between cbb40c9 and dc10fe8.
Files selected for processing (2)
  • targets/ESP32/CMakePresets.json (3 hunks)
  • targets/ESP32/_nanoCLR/Memory.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • targets/ESP32/CMakePresets.json

targets/ESP32/_nanoCLR/Memory.cpp Outdated Show resolved Hide resolved
targets/ESP32/_nanoCLR/Memory.cpp Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between dc10fe8 and c59a96b.
Files selected for processing (1)
  • targets/ESP32/_nanoCLR/Memory.cpp (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • targets/ESP32/_nanoCLR/Memory.cpp

@AdrianSoundy AdrianSoundy added the Platform: ESP32 Everything related specifically with ESP32 platform label May 18, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c59a96b and ef3c353.

Files selected for processing (2)
  • targets/ESP32/CMakePresets.json (16 hunks)
  • targets/ESP32/_nanoCLR/Memory.cpp (4 hunks)
Files skipped from review due to trivial changes (1)
  • targets/ESP32/CMakePresets.json
Additional context used
Learnings (1)
targets/ESP32/_nanoCLR/Memory.cpp (1)
User: AdrianSoundy
PR: nanoframework/nf-interpreter#2940
File: targets/ESP32/_nanoCLR/Memory.cpp:140-140
Timestamp: 2024-05-18T23:04:15.581Z
Learning: In the `targets/ESP32/_nanoCLR/Memory.cpp` file, the `largestFreeBlock` value is already logged in the message: `ESP_LOGI(TAG, "Managed heap allocated: %d (from max:%d)", managedHeapSize, largestFreeBlock);`.
Additional comments not posted (3)
targets/ESP32/_nanoCLR/Memory.cpp (3)

24-28: Adjust the MINIMUM_FREE_MEMORY_FOR_IDF based on the target.

This change allows for different memory reservations based on the ESP32 model, which is a sensible approach given the varying capabilities of different hardware versions.


31-33: Define extra memory reserves for Bluetooth and Thread configurations.

These definitions are clear and align with the need to reserve additional memory when specific features are enabled, ensuring that the system remains stable and functional under these configurations.


169-169: Enhance logging for better traceability.

The log message provides valuable information about the allocation process, enhancing traceability and debugging capabilities.

targets/ESP32/_nanoCLR/Memory.cpp Show resolved Hide resolved
targets/ESP32/_nanoCLR/Memory.cpp Show resolved Hide resolved
@AdrianSoundy
Copy link
Member Author

AdrianSoundy commented May 30, 2024

@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

Copy link
Member

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a nice improvement! Well done Adrian. 👍🏻

@josesimoes
Copy link
Member

Available memory looks much better today for most targets!

@AdrianSoundy AdrianSoundy changed the title Resolve C3 managed memory size Optimize allocation of managed memory May 31, 2024
@AdrianSoundy AdrianSoundy merged commit 13f2e11 into nanoframework:main May 31, 2024
17 checks passed
@AdrianSoundy AdrianSoundy deleted the C3-managed-memory branch May 31, 2024 00:46
@DarkNoir
Copy link

DarkNoir commented May 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Config-and-Build Platform: ESP32 Everything related specifically with ESP32 platform Type: enhancement
Projects
None yet
4 participants