-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add malloc_count Component to track heap usage (#1743)
* Add malloc_count Component * Revised original code to work for Host and Esp8266 * Revise `host_printf` so it doesn't use the heap * Fix missing `memcpy_aligned` or `memcmp_aligned` functions. * Fix unknown `system_get_time()` * Use malloc_count to provide return value for `system_get_free_heap_size()`
- Loading branch information
Showing
9 changed files
with
466 additions
and
61 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# We require malloc_count to keep track of heap usage for system_get_free_heap_size() | ||
# If using valgrind, for example, disabling hooking provides a cleaner trace | ||
COMPONENT_VARS := ENABLE_MALLOC_COUNT | ||
ENABLE_MALLOC_COUNT ?= 1 | ||
|
||
ifeq ($(ENABLE_MALLOC_COUNT),1) | ||
GLOBAL_CFLAGS += -DENABLE_MALLOC_COUNT | ||
COMPONENT_DEPENDS := malloc_count | ||
endif |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
#include "include/heap.h" | ||
#ifdef ENABLE_MALLOC_COUNT | ||
#include <malloc_count.h> | ||
#endif | ||
|
||
// Notional RAM available | ||
const uint32_t memorySize = 128 * 1024; | ||
|
||
uint32_t system_get_free_heap_size(void) | ||
{ | ||
#ifdef ENABLE_MALLOC_COUNT | ||
uint32_t current = MallocCount::getCurrent(); | ||
return (current < memorySize) ? (memorySize - current) : 0; | ||
#else | ||
return memorySize; | ||
#endif | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Hook all the memory allocation functions we need to monitor heap activity | ||
ifeq ($(SMING_ARCH),Host) | ||
EXTRA_LDFLAGS := \ | ||
-Wl,-wrap,malloc \ | ||
-Wl,-wrap,calloc \ | ||
-Wl,-wrap,realloc \ | ||
-Wl,-wrap,free | ||
else | ||
EXTRA_LDFLAGS := \ | ||
-Wl,-wrap,pvPortMalloc \ | ||
-Wl,-wrap,pvPortCalloc \ | ||
-Wl,-wrap,pvPortRealloc \ | ||
-Wl,-wrap,pvPortZalloc \ | ||
-Wl,-wrap,pvPortZallocIram \ | ||
-Wl,-wrap,vPortFree | ||
endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/****************************************************************************** | ||
* malloc_count.h | ||
* | ||
* Header containing prototypes of user-callable functions to retrieve run-time | ||
* information about malloc()/free() allocation. | ||
* | ||
****************************************************************************** | ||
* Copyright (C) 2013 Timo Bingmann <[email protected]> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*****************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <functional> | ||
|
||
namespace MallocCount | ||
{ | ||
/* returns the currently allocated amount of memory */ | ||
size_t getCurrent(void); | ||
|
||
/* returns the current peak memory allocation */ | ||
size_t getPeak(void); | ||
|
||
/* resets the peak memory allocation to current */ | ||
void resetPeak(void); | ||
|
||
/* returns the total number of allocations */ | ||
size_t getAllocCount(void); | ||
|
||
/* typedef of callback function */ | ||
typedef std::function<void(size_t current)> MallocCountCallback; | ||
|
||
/* supply malloc_count with a callback function that is invoked on each change | ||
* of the current allocation. The callback function must not use malloc()/realloc()/free() | ||
* or it will go into an endless recursive loop! */ | ||
void setCallback(MallocCountCallback callback); | ||
|
||
/* dynamically enable/disable logging */ | ||
void enableLogging(bool enable); | ||
|
||
/* allocations less than this threshold are never logged */ | ||
void setLogThreshold(size_t threshold); | ||
|
||
}; // namespace MallocCount |
Oops, something went wrong.