Skip to content

Commit 97c8818

Browse files
author
deepikabhavnani
committed
Cleanup
1. Enable watermark to get stack space information 2. Restructured code 3. Throw error if MBED_THREAD_STATS_ENABLED is not set 4. Astyle changes
1 parent f43b16f commit 97c8818

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

TESTS/mbed_platform/stats_thread/main.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "mbed.h"
2323

2424
#if !defined(MBED_THREAD_STATS_ENABLED)
25-
#warning [NOT_SUPPORTED] test not supported
25+
#error [NOT_SUPPORTED] test not supported
2626
#endif
2727

2828
using namespace utest::v1;
@@ -54,16 +54,15 @@ void test_case_single_thread_stats()
5454
{
5555
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
5656
int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
57-
5857
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
5958
t1.start(increment_with_delay);
6059

6160
// Read stats
6261
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
63-
TEST_ASSERT_EQUAL(1, (count-old_count));
62+
TEST_ASSERT_EQUAL(1, (count - old_count));
6463

65-
for(int i = 0; i < count; i++) {
66-
if(0 == strcmp(stats[i].name, "Th1")) {
64+
for (int i = 0; i < count; i++) {
65+
if (0 == strcmp(stats[i].name, "Th1")) {
6766
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
6867
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].priority);
6968
break;
@@ -96,13 +95,13 @@ void test_case_multi_threads_blocked()
9695
// Read stats
9796

9897
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
99-
TEST_ASSERT_EQUAL(2, (count-old_count));
100-
for(int i = 0; i < count; i++) {
101-
if(0 == strcmp(stats[i].name, "Th2")) {
98+
TEST_ASSERT_EQUAL(2, (count - old_count));
99+
for (int i = 0; i < count; i++) {
100+
if (0 == strcmp(stats[i].name, "Th2")) {
102101
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
103102
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].priority);
104103
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].state);
105-
} else if(0 == strcmp (stats[i].name, "Th1")) {
104+
} else if (0 == strcmp (stats[i].name, "Th1")) {
106105
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
107106
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].priority);
108107
}
@@ -115,7 +114,7 @@ void test_case_multi_threads_blocked()
115114
Thread::wait(100);
116115

117116
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
118-
TEST_ASSERT_EQUAL(1, (count-old_count));
117+
TEST_ASSERT_EQUAL(1, (count - old_count));
119118

120119
t1.terminate();
121120
delete[] stats;
@@ -134,13 +133,13 @@ void test_case_multi_threads_terminate()
134133
// Read stats
135134

136135
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
137-
TEST_ASSERT_EQUAL(2, (count-old_count));
136+
TEST_ASSERT_EQUAL(2, (count - old_count));
138137

139-
for(int i = 0; i < count; i++) {
140-
if(0 == strcmp(stats[i].name, "Th2")) {
138+
for (int i = 0; i < count; i++) {
139+
if (0 == strcmp(stats[i].name, "Th2")) {
141140
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
142141
TEST_ASSERT_EQUAL(osPriorityNormal2, stats[i].priority);
143-
} else if(0 == strcmp(stats[i].name, "Th1")) {
142+
} else if (0 == strcmp(stats[i].name, "Th1")) {
144143
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
145144
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].priority);
146145
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].state);

platform/mbed_stats.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#ifdef MBED_CONF_RTOS_PRESENT
77
#include "cmsis_os2.h"
8+
#elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED)
9+
#warning Statistics are currently not supported without the rtos.
810
#endif
911

1012
// note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
@@ -25,7 +27,7 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats)
2527
osKernelLock();
2628
thread_n = osThreadEnumerate(threads, thread_n);
2729

28-
for(i = 0; i < thread_n; i++) {
30+
for (i = 0; i < thread_n; i++) {
2931
uint32_t stack_size = osThreadGetStackSize(threads[i]);
3032
stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
3133
stats->reserved_size += stack_size;
@@ -40,7 +42,8 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats)
4042
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
4143
{
4244
MBED_ASSERT(stats != NULL);
43-
memset(stats, 0, count*sizeof(mbed_stats_stack_t));
45+
memset(stats, 0, count * sizeof(mbed_stats_stack_t));
46+
4447
size_t i = 0;
4548

4649
#if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
@@ -52,7 +55,7 @@ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
5255
osKernelLock();
5356
count = osThreadEnumerate(threads, count);
5457

55-
for(i = 0; i < count; i++) {
58+
for (i = 0; i < count; i++) {
5659
uint32_t stack_size = osThreadGetStackSize(threads[i]);
5760
stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
5861
stats[i].reserved_size = stack_size;
@@ -82,7 +85,7 @@ size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
8285
osKernelLock();
8386
count = osThreadEnumerate(threads, count);
8487

85-
for(i = 0; i < count; i++) {
88+
for (i = 0; i < count; i++) {
8689
stats[i].id = (uint32_t)threads[i];
8790
stats[i].state = (uint32_t)osThreadGetState(threads[i]);
8891
stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
@@ -96,7 +99,3 @@ size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
9699

97100
return i;
98101
}
99-
100-
#if (defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED)) && !defined(MBED_CONF_RTOS_PRESENT)
101-
#warning statistics are currently not supported without the rtos.
102-
#endif

platform/mbed_stats.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ typedef struct {
4747
uint32_t alloc_fail_cnt; /**< Number of failed allocations. */
4848
} mbed_stats_heap_t;
4949

50-
/**
51-
* struct mbed_stats_thread_t definition
52-
*/
53-
54-
typedef struct {
55-
uint32_t id; /**< Thread Object Identifier */
56-
uint32_t state; /**< Thread Object State */
57-
uint32_t priority; /**< Thread Priority */
58-
uint32_t stack_size; /**< Thread Stack Size */
59-
uint32_t stack_space; /**< Thread remaining stack size */
60-
const char *name; /**< Thread Object name */
61-
} mbed_stats_thread_t;
62-
6350
/**
6451
* Fill the passed in heap stat structure with heap stats.
6552
*
@@ -95,6 +82,19 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats);
9582
*/
9683
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count);
9784

85+
/**
86+
* struct mbed_stats_thread_t definition
87+
*/
88+
89+
typedef struct {
90+
uint32_t id; /**< Thread Object Identifier */
91+
uint32_t state; /**< Thread Object State */
92+
uint32_t priority; /**< Thread Priority */
93+
uint32_t stack_size; /**< Thread Stack Size */
94+
uint32_t stack_space; /**< Thread remaining stack size */
95+
const char *name; /**< Thread Object name */
96+
} mbed_stats_thread_t;
97+
9898
/**
9999
* Fill the passed array of stat structures with the thread stats for each available thread.
100100
*

rtos/TARGET_CORTEX/mbed_rtx_conf.h

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
#define OS_STACK_WATERMARK 1
5959
#endif
6060

61+
#if !defined(OS_STACK_WATERMARK) && defined(MBED_THREAD_STATS_ENABLED)
62+
#define OS_STACK_WATERMARK 1
63+
#endif
64+
6165
/* Run threads unprivileged when uVisor is enabled. */
6266
#if defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)
6367
# define OS_PRIVILEGE_MODE 0

0 commit comments

Comments
 (0)