From b6e8f44c0da273b09289c8bad4781095b9d14df0 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 26 Oct 2016 17:06:08 -0500 Subject: [PATCH 1/4] stats - Added stack stats api Matched heap stats api - void mbed_stats_heap_get(mbed_stats_heap_t *) - void mbed_stats_stack_get(mbed_stats_stack_t *) --- platform/mbed_stats.c | 37 +++++++++++++++++++++++++++++++++++++ platform/mbed_stats.h | 12 ++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 platform/mbed_stats.c diff --git a/platform/mbed_stats.c b/platform/mbed_stats.c new file mode 100644 index 0000000000..533ae08805 --- /dev/null +++ b/platform/mbed_stats.c @@ -0,0 +1,37 @@ +#include "mbed_stats.h" +#include + +#if MBED_CONF_RTOS_PRESENT +#include "cmsis_os.h" +#endif + +// note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp + +void mbed_stats_stack_get(mbed_stats_stack_t *stats) +{ + memset(stats, 0, sizeof(mbed_stats_stack_t)); + +#if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT + osThreadEnumId enumid = _osThreadsEnumStart(); + osThreadId threadid; + + while ((threadid = _osThreadEnumNext(enumid))) { + osEvent e; + + e = _osThreadGetInfo(threadid, osThreadInfoStackMax); + if (e.status == osOK) { + stats->max_size += (uint32_t)e.value.p; + } + + e = _osThreadGetInfo(threadid, osThreadInfoStackSize); + if (e.status == osOK) { + stats->reserved_size += (uint32_t)e.value.p; + } + + stats->stack_cnt += 1; + } +#elif MBED_STACK_STATS_ENABLED +#warning Stack statistics are not supported without the rtos. +#endif +} + diff --git a/platform/mbed_stats.h b/platform/mbed_stats.h index c9f4765550..b5eac88c23 100644 --- a/platform/mbed_stats.h +++ b/platform/mbed_stats.h @@ -18,6 +18,7 @@ */ #ifndef MBED_STATS_H #define MBED_STATS_H +#include #ifdef __cplusplus extern "C" { @@ -36,6 +37,17 @@ typedef struct { */ void mbed_stats_heap_get(mbed_stats_heap_t *stats); +typedef struct { + uint32_t max_size; /**< Sum of the maximum number of bytes used in each stack. */ + uint32_t reserved_size; /**< Current number of bytes allocated for all stacks. */ + uint32_t stack_cnt; /**< Number of stacks currently allocated. */ +} mbed_stats_stack_t; + +/** + * Fill the passed in structure with stack stats. + */ +void mbed_stats_stack_get(mbed_stats_stack_t *stats); + #ifdef __cplusplus } #endif From 14aa57f81c0948b6558852bf8d7c1895fd9e1582 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 26 Oct 2016 17:49:29 -0500 Subject: [PATCH 2/4] stats - Added stats for reserved heap space for consistency --- platform/mbed_alloc_wrappers.cpp | 3 +++ platform/mbed_stats.h | 1 + 2 files changed, 4 insertions(+) diff --git a/platform/mbed_alloc_wrappers.cpp b/platform/mbed_alloc_wrappers.cpp index 4cf9a3a2cf..aaa9d95539 100644 --- a/platform/mbed_alloc_wrappers.cpp +++ b/platform/mbed_alloc_wrappers.cpp @@ -57,6 +57,9 @@ static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0}; void mbed_stats_heap_get(mbed_stats_heap_t *stats) { #ifdef MBED_HEAP_STATS_ENABLED + extern uint32_t mbed_heap_size; + heap_stats.reserved_size = mbed_heap_size; + malloc_stats_mutex->lock(); memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t)); malloc_stats_mutex->unlock(); diff --git a/platform/mbed_stats.h b/platform/mbed_stats.h index b5eac88c23..241aa01049 100644 --- a/platform/mbed_stats.h +++ b/platform/mbed_stats.h @@ -28,6 +28,7 @@ typedef struct { uint32_t current_size; /**< Bytes allocated currently. */ uint32_t max_size; /**< Max bytes allocated at a given time. */ uint32_t total_size; /**< Cumulative sum of bytes ever allocated. */ + uint32_t reserved_size; /**< Current number of bytes allocated for the heap. */ uint32_t alloc_cnt; /**< Current number of allocations. */ uint32_t alloc_fail_cnt; /**< Number of failed allocations. */ } mbed_stats_heap_t; From 9b630b3e0d7429b11eed278295d9692a7bd52f16 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 27 Oct 2016 10:30:45 -0500 Subject: [PATCH 3/4] stats - Added stack stats api for individual threads Added the following - size_t mbed_stats_stack_get_each(mbed_stats_stack_t *, size_t) --- platform/mbed_stats.c | 36 ++++++++++++++++++++++++++++++++++-- platform/mbed_stats.h | 8 ++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/platform/mbed_stats.c b/platform/mbed_stats.c index 533ae08805..9c05b5a05d 100644 --- a/platform/mbed_stats.c +++ b/platform/mbed_stats.c @@ -30,8 +30,40 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats) stats->stack_cnt += 1; } -#elif MBED_STACK_STATS_ENABLED -#warning Stack statistics are not supported without the rtos. #endif } +size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count) +{ + memset(stats, 0, count*sizeof(mbed_stats_stack_t)); + size_t i = 0; + +#if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT + osThreadEnumId enumid = _osThreadsEnumStart(); + osThreadId threadid; + + while ((threadid = _osThreadEnumNext(enumid)) && i < count) { + osEvent e; + + e = _osThreadGetInfo(threadid, osThreadInfoStackMax); + if (e.status == osOK) { + stats[i].max_size = (uint32_t)e.value.p; + } + + e = _osThreadGetInfo(threadid, osThreadInfoStackSize); + if (e.status == osOK) { + stats[i].reserved_size = (uint32_t)e.value.p; + } + + stats[i].thread_id = (uint32_t)threadid; + stats[i].stack_cnt = 1; + i += 1; + } +#endif + + return i; +} + +#if MBED_STACK_STATS_ENABLED && !MBED_CONF_RTOS_PRESENT +#warning Stack statistics are currently not supported without the rtos. +#endif diff --git a/platform/mbed_stats.h b/platform/mbed_stats.h index 241aa01049..295f63376c 100644 --- a/platform/mbed_stats.h +++ b/platform/mbed_stats.h @@ -19,6 +19,7 @@ #ifndef MBED_STATS_H #define MBED_STATS_H #include +#include #ifdef __cplusplus extern "C" { @@ -39,6 +40,7 @@ typedef struct { void mbed_stats_heap_get(mbed_stats_heap_t *stats); typedef struct { + uint32_t thread_id; /**< Identifier for thread that owns the stack. */ uint32_t max_size; /**< Sum of the maximum number of bytes used in each stack. */ uint32_t reserved_size; /**< Current number of bytes allocated for all stacks. */ uint32_t stack_cnt; /**< Number of stacks currently allocated. */ @@ -49,6 +51,12 @@ typedef struct { */ void mbed_stats_stack_get(mbed_stats_stack_t *stats); +/** + * Fill the passed array of stat structures with the stack stats + * for each available stack. + */ +size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count); + #ifdef __cplusplus } #endif From e90fff3a6095e469d977dcf8601325c58471233b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 28 Oct 2016 11:20:12 -0500 Subject: [PATCH 4/4] stats - Added doxygen documentation to stats functions --- platform/mbed_stats.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/platform/mbed_stats.h b/platform/mbed_stats.h index 295f63376c..4e9dd62999 100644 --- a/platform/mbed_stats.h +++ b/platform/mbed_stats.h @@ -35,7 +35,9 @@ typedef struct { } mbed_stats_heap_t; /** - * Fill the passed in structure with heap stats. + * Fill the passed in heap stat structure with heap stats. + * + * @param stats A pointer to the mbed_stats_heap_t structure to fill */ void mbed_stats_heap_get(mbed_stats_heap_t *stats); @@ -47,13 +49,20 @@ typedef struct { } mbed_stats_stack_t; /** - * Fill the passed in structure with stack stats. + * Fill the passed in structure with stack stats. + * + * @param stats A pointer to the mbed_stats_stack_t structure to fill */ void mbed_stats_stack_get(mbed_stats_stack_t *stats); /** - * Fill the passed array of stat structures with the stack stats - * for each available stack. + * Fill the passed array of stat structures with the stack stats + * for each available stack. + * + * @param stats A pointer to an array of mbed_stats_stack_t structures to fill + * @param count The number of mbed_stats_stack_t structures in the provided array + * @return The number of mbed_stats_stack_t structures that have been filled, + * this is equal to the number of stacks on the system. */ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count);