Skip to content

Commit

Permalink
Add malloc_count Component to track heap usage (#1743)
Browse files Browse the repository at this point in the history
* 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
mikee47 authored and slaff committed Jul 8, 2019
1 parent efc4d8b commit 6b0c71b
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 61 deletions.
9 changes: 9 additions & 0 deletions Sming/Arch/Host/Components/heap/component.mk
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
7 changes: 0 additions & 7 deletions Sming/Arch/Host/Components/heap/heap.c

This file was deleted.

18 changes: 18 additions & 0 deletions Sming/Arch/Host/Components/heap/heap.cpp
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
}
4 changes: 3 additions & 1 deletion Sming/Arch/Host/Components/hostlib/hostmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ void host_printf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
host_puts(buffer);
}

void host_printfp(const char* fmt, const char* pretty_function, ...)
Expand Down
16 changes: 16 additions & 0 deletions Sming/Components/malloc_count/component.mk
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
63 changes: 63 additions & 0 deletions Sming/Components/malloc_count/include/malloc_count.h
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
Loading

0 comments on commit 6b0c71b

Please sign in to comment.