Skip to content

Commit 46c1e94

Browse files
author
Cruz Monrreal
authored
Merge pull request #6795 from deepikabhavnani/thread_stats
Thread stats API
2 parents 80e1093 + 97c8818 commit 46c1e94

File tree

4 files changed

+238
-5
lines changed

4 files changed

+238
-5
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
2+
/* mbed Microcontroller Library
3+
* Copyright (c) 2018 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "greentea-client/test_env.h"
19+
#include "unity/unity.h"
20+
#include "utest/utest.h"
21+
22+
#include "mbed.h"
23+
24+
#if !defined(MBED_THREAD_STATS_ENABLED)
25+
#error [NOT_SUPPORTED] test not supported
26+
#endif
27+
28+
using namespace utest::v1;
29+
30+
static EventFlags ef;
31+
static int32_t counter = 0;
32+
33+
#define TEST_STACK_SIZE 320
34+
#define FLAG_SIGNAL_DEC 0x2
35+
#define MAX_THREAD_STATS 0x8
36+
37+
void decrement_on_event()
38+
{
39+
uint32_t ret = ef.wait_all(FLAG_SIGNAL_DEC);
40+
TEST_ASSERT_FALSE(ret & osFlagsError);
41+
TEST_ASSERT_EQUAL(FLAG_SIGNAL_DEC, ret);
42+
counter--;
43+
}
44+
45+
void increment_with_delay()
46+
{
47+
while (1) {
48+
counter++;
49+
Thread::wait(500);
50+
}
51+
}
52+
53+
void test_case_single_thread_stats()
54+
{
55+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
56+
int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
57+
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
58+
t1.start(increment_with_delay);
59+
60+
// Read stats
61+
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
62+
TEST_ASSERT_EQUAL(1, (count - old_count));
63+
64+
for (int i = 0; i < count; i++) {
65+
if (0 == strcmp(stats[i].name, "Th1")) {
66+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
67+
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].priority);
68+
break;
69+
}
70+
}
71+
72+
t1.terminate();
73+
delete[] stats;
74+
}
75+
76+
#define SINGLE_ELEMENT 1
77+
void test_case_less_count()
78+
{
79+
// Default Mbed OS has 3 threads
80+
mbed_stats_thread_t stats;
81+
int count = mbed_stats_thread_get_each(&stats, SINGLE_ELEMENT);
82+
TEST_ASSERT_EQUAL(SINGLE_ELEMENT, count);
83+
}
84+
85+
void test_case_multi_threads_blocked()
86+
{
87+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
88+
int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
89+
90+
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
91+
Thread t2(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th2");
92+
t1.start(increment_with_delay);
93+
t2.start(decrement_on_event);
94+
95+
// Read stats
96+
97+
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
98+
TEST_ASSERT_EQUAL(2, (count - old_count));
99+
for (int i = 0; i < count; i++) {
100+
if (0 == strcmp(stats[i].name, "Th2")) {
101+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
102+
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].priority);
103+
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].state);
104+
} else if (0 == strcmp (stats[i].name, "Th1")) {
105+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
106+
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].priority);
107+
}
108+
}
109+
110+
// Signal blocked thread
111+
uint32_t ret = ef.set(FLAG_SIGNAL_DEC);
112+
TEST_ASSERT_FALSE(ret & osFlagsError);
113+
114+
Thread::wait(100);
115+
116+
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
117+
TEST_ASSERT_EQUAL(1, (count - old_count));
118+
119+
t1.terminate();
120+
delete[] stats;
121+
}
122+
123+
void test_case_multi_threads_terminate()
124+
{
125+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
126+
int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
127+
128+
Thread t1(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th1");
129+
Thread t2(osPriorityNormal2, TEST_STACK_SIZE, NULL, "Th2");
130+
t2.start(increment_with_delay);
131+
t1.start(decrement_on_event);
132+
133+
// Read stats
134+
135+
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
136+
TEST_ASSERT_EQUAL(2, (count - old_count));
137+
138+
for (int i = 0; i < count; i++) {
139+
if (0 == strcmp(stats[i].name, "Th2")) {
140+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
141+
TEST_ASSERT_EQUAL(osPriorityNormal2, stats[i].priority);
142+
} else if (0 == strcmp(stats[i].name, "Th1")) {
143+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size);
144+
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].priority);
145+
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].state);
146+
}
147+
}
148+
149+
t1.terminate();
150+
t2.terminate();
151+
152+
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
153+
TEST_ASSERT_EQUAL(count, old_count);
154+
155+
delete[] stats;
156+
}
157+
158+
Case cases[] = {
159+
Case("Single Thread Stats", test_case_single_thread_stats),
160+
Case("Less count value", test_case_less_count),
161+
Case("Multiple Threads blocked", test_case_multi_threads_blocked),
162+
Case("Multiple Threads terminate", test_case_multi_threads_terminate),
163+
};
164+
165+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
166+
{
167+
GREENTEA_SETUP(20, "default_auto");
168+
return greentea_test_setup_handler(number_of_cases);
169+
}
170+
171+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
172+
173+
int main()
174+
{
175+
Harness::run(specification);
176+
}

platform/mbed_stats.c

Lines changed: 34 additions & 5 deletions
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;
@@ -67,6 +70,32 @@ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
6770
return i;
6871
}
6972

70-
#if defined(MBED_STACK_STATS_ENABLED) && !defined(MBED_CONF_RTOS_PRESENT)
71-
#warning Stack statistics are currently not supported without the rtos.
73+
size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
74+
{
75+
MBED_ASSERT(stats != NULL);
76+
memset(stats, 0, count * sizeof(mbed_stats_thread_t));
77+
size_t i = 0;
78+
79+
#if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
80+
osThreadId_t *threads;
81+
82+
threads = malloc(sizeof(osThreadId_t) * count);
83+
MBED_ASSERT(threads != NULL);
84+
85+
osKernelLock();
86+
count = osThreadEnumerate(threads, count);
87+
88+
for (i = 0; i < count; i++) {
89+
stats[i].id = (uint32_t)threads[i];
90+
stats[i].state = (uint32_t)osThreadGetState(threads[i]);
91+
stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
92+
stats[i].stack_size = osThreadGetStackSize(threads[i]);
93+
stats[i].stack_space = osThreadGetStackSpace(threads[i]);
94+
stats[i].name = osThreadGetName(threads[i]);
95+
}
96+
osKernelUnlock();
97+
free(threads);
7298
#endif
99+
100+
return i;
101+
}

platform/mbed_stats.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C" {
3232
#ifdef MBED_ALL_STATS_ENABLED
3333
#define MBED_STACK_STATS_ENABLED 1
3434
#define MBED_HEAP_STATS_ENABLED 1
35+
#define MBED_THREAD_STATS_ENABLED 1
3536
#endif
3637

3738
/**
@@ -81,6 +82,29 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats);
8182
*/
8283
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count);
8384

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+
98+
/**
99+
* Fill the passed array of stat structures with the thread stats for each available thread.
100+
*
101+
* @param stats A pointer to an array of mbed_stats_thread_t structures to fill
102+
* @param count The number of mbed_stats_thread_t structures in the provided array
103+
* @return The number of mbed_stats_thread_t structures that have been filled,
104+
* this is equal to the number of threads on the system.
105+
*/
106+
size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count);
107+
84108
#ifdef __cplusplus
85109
}
86110
#endif

rtos/TARGET_CORTEX/mbed_rtx_conf.h

Lines changed: 4 additions & 0 deletions
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)