-
Notifications
You must be signed in to change notification settings - Fork 158
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
Custom allocator / heap profiler #350
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add custom allocation hook functions and a built-in heap profiler.
The new Memory module provides methods for overriding the default C++ allocation and a central malloc / free selector. If enabled via build switches, the allocations will be handled by the Memory module, instead of going to the host system allocator directly. There exist two possible uses:
If the Memory module is not enabled via build switches, then this has no effect on the allocations.
Usage
The following features can be enabled:
MO_OVERRIDE_ALLOCATION=1
: enable routing allocations through the Memory module. The firmware can now set its custom allocator usingmo_mem_set_malloc_free(...)
MO_ENABLE_HEAP_PROFILER=1
: track allocations and enable the functionMO_MEM_PRINT_STATS()
to print heap stats. This feature depends on settingMO_OVERRIDE_ALLOCATION=1
.Changes
The API in MicroOcpp.h / MicroOcpp_c.h didn't change. But if the firmware integration gets beyond the API, then the following changes are relevant:
std::string
has been replaced byMicroOcpp::String
which is a typedef of either the samestd::string
if the Memory module is disabled, orstd::basic_string<char, std::char_traits<char>, MicroOcpp::Allocator<char>>
if enabled. To construct a String, usemakeString(const char *memoryTag)
and give it a memory tag which will show up in the heap statisticsstd::vector
has been replaced byMicroOcpp::Vector
, a typedef ofstd::vector
orstd::vector<T, MicroOcpp::Allocator<T>>
. UsemakeVector(const char *memoryTag)
DynamicJsonDocument
isMicroOcpp::JsonDoc
now which becomesDynamicJsonDocument
again if the Memory module is off andBasicJsonDocument<ArduinoJsonAllocator>
if onLimitations
This method of redirecting all allocations depends on explicitly changing the allocation everywhere in the code base. It is likely that some allocations have been overlooked and in future changes, it could be forgotten about in some places.
For
std::function
, it's not really common to replace the internal allocator and therefore it's not well supported by STL implementations. For now, the MO-internal uses of std::function remain unchanged, making them a blind spot of the heap profiler. In future, they should be replaced by classic C-style callback functions for example.If MO requires system resources (like
snprintf
) which allocate memory internally, that isn't taken into account. The Memory module can only track explicit allocations inside the MO library.Example
The example output of running all unit tests looks like this:
The heap usage at exiting the unit tests is 0 bytes (MO has been deinitalized) and total maximum over all tests was 31544 bytes.