Skip to content

Commit b36944f

Browse files
authored
Merge pull request #3141 from geky/stats-stack
stats - Add cleaner stats API for stack usage
2 parents 98029e3 + e90fff3 commit b36944f

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

platform/mbed_alloc_wrappers.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static mbed_stats_heap_t heap_stats = {0, 0, 0, 0, 0};
5757
void mbed_stats_heap_get(mbed_stats_heap_t *stats)
5858
{
5959
#ifdef MBED_HEAP_STATS_ENABLED
60+
extern uint32_t mbed_heap_size;
61+
heap_stats.reserved_size = mbed_heap_size;
62+
6063
malloc_stats_mutex->lock();
6164
memcpy(stats, &heap_stats, sizeof(mbed_stats_heap_t));
6265
malloc_stats_mutex->unlock();

platform/mbed_stats.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "mbed_stats.h"
2+
#include <string.h>
3+
4+
#if MBED_CONF_RTOS_PRESENT
5+
#include "cmsis_os.h"
6+
#endif
7+
8+
// note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
9+
10+
void mbed_stats_stack_get(mbed_stats_stack_t *stats)
11+
{
12+
memset(stats, 0, sizeof(mbed_stats_stack_t));
13+
14+
#if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
15+
osThreadEnumId enumid = _osThreadsEnumStart();
16+
osThreadId threadid;
17+
18+
while ((threadid = _osThreadEnumNext(enumid))) {
19+
osEvent e;
20+
21+
e = _osThreadGetInfo(threadid, osThreadInfoStackMax);
22+
if (e.status == osOK) {
23+
stats->max_size += (uint32_t)e.value.p;
24+
}
25+
26+
e = _osThreadGetInfo(threadid, osThreadInfoStackSize);
27+
if (e.status == osOK) {
28+
stats->reserved_size += (uint32_t)e.value.p;
29+
}
30+
31+
stats->stack_cnt += 1;
32+
}
33+
#endif
34+
}
35+
36+
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
37+
{
38+
memset(stats, 0, count*sizeof(mbed_stats_stack_t));
39+
size_t i = 0;
40+
41+
#if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
42+
osThreadEnumId enumid = _osThreadsEnumStart();
43+
osThreadId threadid;
44+
45+
while ((threadid = _osThreadEnumNext(enumid)) && i < count) {
46+
osEvent e;
47+
48+
e = _osThreadGetInfo(threadid, osThreadInfoStackMax);
49+
if (e.status == osOK) {
50+
stats[i].max_size = (uint32_t)e.value.p;
51+
}
52+
53+
e = _osThreadGetInfo(threadid, osThreadInfoStackSize);
54+
if (e.status == osOK) {
55+
stats[i].reserved_size = (uint32_t)e.value.p;
56+
}
57+
58+
stats[i].thread_id = (uint32_t)threadid;
59+
stats[i].stack_cnt = 1;
60+
i += 1;
61+
}
62+
#endif
63+
64+
return i;
65+
}
66+
67+
#if MBED_STACK_STATS_ENABLED && !MBED_CONF_RTOS_PRESENT
68+
#warning Stack statistics are currently not supported without the rtos.
69+
#endif

platform/mbed_stats.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
#ifndef MBED_STATS_H
2020
#define MBED_STATS_H
21+
#include <stdint.h>
22+
#include <stddef.h>
2123

2224
#ifdef __cplusplus
2325
extern "C" {
@@ -27,15 +29,43 @@ typedef struct {
2729
uint32_t current_size; /**< Bytes allocated currently. */
2830
uint32_t max_size; /**< Max bytes allocated at a given time. */
2931
uint32_t total_size; /**< Cumulative sum of bytes ever allocated. */
32+
uint32_t reserved_size; /**< Current number of bytes allocated for the heap. */
3033
uint32_t alloc_cnt; /**< Current number of allocations. */
3134
uint32_t alloc_fail_cnt; /**< Number of failed allocations. */
3235
} mbed_stats_heap_t;
3336

3437
/**
35-
* Fill the passed in structure with heap stats.
38+
* Fill the passed in heap stat structure with heap stats.
39+
*
40+
* @param stats A pointer to the mbed_stats_heap_t structure to fill
3641
*/
3742
void mbed_stats_heap_get(mbed_stats_heap_t *stats);
3843

44+
typedef struct {
45+
uint32_t thread_id; /**< Identifier for thread that owns the stack. */
46+
uint32_t max_size; /**< Sum of the maximum number of bytes used in each stack. */
47+
uint32_t reserved_size; /**< Current number of bytes allocated for all stacks. */
48+
uint32_t stack_cnt; /**< Number of stacks currently allocated. */
49+
} mbed_stats_stack_t;
50+
51+
/**
52+
* Fill the passed in structure with stack stats.
53+
*
54+
* @param stats A pointer to the mbed_stats_stack_t structure to fill
55+
*/
56+
void mbed_stats_stack_get(mbed_stats_stack_t *stats);
57+
58+
/**
59+
* Fill the passed array of stat structures with the stack stats
60+
* for each available stack.
61+
*
62+
* @param stats A pointer to an array of mbed_stats_stack_t structures to fill
63+
* @param count The number of mbed_stats_stack_t structures in the provided array
64+
* @return The number of mbed_stats_stack_t structures that have been filled,
65+
* this is equal to the number of stacks on the system.
66+
*/
67+
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count);
68+
3969
#ifdef __cplusplus
4070
}
4171
#endif

0 commit comments

Comments
 (0)