Switch from MallocAllocator to Zephyr k_malloc allocator#327
Switch from MallocAllocator to Zephyr k_malloc allocator#327
Conversation
Replace Fw::MallocAllocator with a ZephyrKmallocAllocator backed by Zephyr's k_malloc/k_free so that heap usage is tracked by the kernel memory pool, giving an accurate picture of runtime RAM consumption. - Implement ZephyrKmallocAllocator with alignment support via k_aligned_alloc - Unify ComCcsdsSband and ComCcsdsLora allocation namespaces into a single ComCcsds::Allocation namespace - Update ReferenceDeploymentTopology to use the shared allocator - Configure HEAP_MEM_POOL_SIZE=65536 in prj.conf for the kernel heap Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
LeStarch
left a comment
There was a problem hiding this comment.
Another thing, it this should be configured as the default memory allocator using the allocator.
FprimeZephyrReference/project/config/ComCcsdsConfig/ComCcsdsSubtopologyConfig.cpp
Outdated
Show resolved
Hide resolved
FprimeZephyrReference/project/config/ComCcsdsConfig/ComCcsdsSubtopologyConfig.cpp
Outdated
Show resolved
Hide resolved
PROVESFlightControllerReference/ReferenceDeployment/Top/ReferenceDeploymentTopology.cpp
Show resolved
Hide resolved
…tion Update fprime-zephyr submodule to pick up the upstreamed Fw::ZephyrKmallocAllocator and remove the local copy from ComCcsdsSubtopologyConfig, keeping the same ComCcsds::Allocation interface.
The header only needs the base Fw::MemAllocator type for the extern declaration. The concrete ZephyrKmallocAllocator type from MemoryAllocation.hpp is only needed in the .cpp for instantiation.
fprime config headers are resolved via the config/ include path prefix, matching how the framework itself includes them (e.g. in MemAllocator.cpp).
Picks up the fix that rounds allocation size up to a multiple of alignment, satisfying the C11 aligned_alloc requirement and enabling ZephyrKmallocAllocator as the project-wide default allocator.
Restore ComCcsdsSubtopologyConfig to use MallocAllocator with separate ComCcsdsSband/ComCcsdsLora namespaces (matching main), and update fprime-zephyr submodule to reverted main.
Update fprime-zephyr submodule to include ZephyrKmallocAllocator as a targeted allocator (not project-wide override). Update ComCcsds config to use unified namespace and ZephyrKmallocAllocator for S-Band and LoRa subtopologies only.
Points to aligned-alloc-size-rounding branch (f228013) which rounds allocation sizes to a multiple of alignment before calling k_aligned_alloc, preventing undefined behavior on Zephyr targets. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Points submodule to a5e01c3, the merge commit on fprime-zephyr main that includes the aligned_alloc size-rounding fix. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Points to 171c8cc which adds alignof(max_align_t) threshold to avoid k_aligned_alloc on ARM when not needed, on top of the size-rounding fix.
|
Replace the previous Zephyr kmalloc allocator behavior with an alignment-safe implementation for RP2350/M33. This change fixes Once approved, do not merged yet, since it needs to update the fprime-zephyr repo first |
|
Great PR! Learned a lot by looking though it, and we can see a 15% increase in proper RAM allocation. Super awesome! And the consolidated Comcsds bloc is much cleaner as well so thank you for cleaning up the subtopologies ensuring when we add coms subtoplotogies they are all good I have two quick pieces of feedback, more of suggestions than anything. Even if we deem them important, we can always do them later in another PR
Some things we could add to cover more ground:
Zephyr’s platform config selects Os_Generic_PriorityQueue (lib/fprime-zephyr/cmake/platform/zephyr/Platform/CMakeLists.txt), In lib/fprime/Os/Generic/PriorityQueue.cpp allocates internal queue storage via In lib/fprime/Fw/Types/MemAllocator.cpp, getAnAllocator falls back to the SYSTEM allocator unless registerAllocator is called, which is it not in our code I believe SYSTEM is constructed from Fw::MemoryAllocation::DefaultMemoryAllocatorType in lib/fprime/default/config/MemoryAllocation.hpp which currently defaults to Fw::MallocAllocator, which is why I think overrride the prj.conf could be worthwhile!
|
Description
Replace
Fw::MallocAllocator(libcmalloc) with a customZephyrKmallocAllocatorbacked by Zephyr'sk_malloc/k_free. This ensures heap usage is tracked by the kernel memory pool, giving an accurate picture of runtime RAM consumption. Previously, the build reported ~75% RAM usage but libc malloc allocations were invisible to Zephyr's memory accounting.Also unifies the separate
ComCcsdsSbandandComCcsdsLoraallocation namespaces into a singleComCcsds::Allocationnamespace, and configuresHEAP_MEM_POOL_SIZE=65536inprj.conffor the Zephyr kernel heap.Related Issues/Tickets
Closes #313
How Has This Been Tested?
Screenshots / Recordings (if applicable)
N/A
Checklist
Further Notes / Considerations