Skip to content

Commit 1dad739

Browse files
author
deepikabhavnani
committed
Thread statistics addition.
API to get all system threads information and states.
1 parent 30e39ee commit 1dad739

File tree

3 files changed

+227
-2
lines changed

3 files changed

+227
-2
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* 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, WITHOUT
13+
* 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+
#include "mbed.h"
18+
#include "greentea-client/test_env.h"
19+
#include "unity/unity.h"
20+
#include "utest/utest.h"
21+
#include "mbed_stats.h"
22+
#include <stdlib.h>
23+
#include <stdio.h>
24+
25+
#if !defined(MBED_THREAD_STATS_ENABLED)
26+
#warning [NOT_SUPPORTED] test not supported
27+
#endif
28+
29+
using namespace utest::v1;
30+
31+
static EventFlags ef;
32+
static int32_t counter = 0;
33+
34+
#define TEST_STACK_SIZE 320
35+
#define FLAG_SIGNAL_DEC 0x2
36+
#define MAX_THREAD_STATS 0x8
37+
38+
void decrement_on_event()
39+
{
40+
uint32_t ret = ef.wait_all(FLAG_SIGNAL_DEC);
41+
TEST_ASSERT_FALSE(ret & osFlagsError);
42+
TEST_ASSERT_EQUAL(FLAG_SIGNAL_DEC, ret);
43+
counter--;
44+
}
45+
46+
void increment_with_delay()
47+
{
48+
while (1) {
49+
counter++;
50+
Thread::wait(500);
51+
}
52+
}
53+
54+
void test_case_single_thread_stats()
55+
{
56+
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
57+
t1.start(increment_with_delay);
58+
59+
// Read stats
60+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
61+
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
62+
TEST_ASSERT_EQUAL(4, count);
63+
64+
for (int i = 0; i < count; i++) {
65+
if (0 == strcmp (stats[i].thread_name, "Th1")) {
66+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
67+
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].thread_priority);
68+
break;
69+
}
70+
}
71+
delete[] stats;
72+
t1.terminate();
73+
}
74+
75+
void test_case_less_count()
76+
{
77+
// Default Mbed OS has 3 threads
78+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[2];
79+
int count = mbed_stats_thread_get_each(stats, 2);
80+
TEST_ASSERT_EQUAL(2, count);
81+
delete[] stats;
82+
}
83+
84+
void test_case_multi_threads_blocked()
85+
{
86+
Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1");
87+
Thread t2(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th2");
88+
t1.start(increment_with_delay);
89+
t2.start(decrement_on_event);
90+
91+
// Read stats
92+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
93+
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
94+
TEST_ASSERT_EQUAL(5, count);
95+
96+
for (int i = 0; i < count; i++) {
97+
if (0 == strcmp (stats[i].thread_name, "Th2")) {
98+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
99+
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].thread_priority);
100+
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].thread_state);
101+
} else if (0 == strcmp (stats[i].thread_name, "Th1")) {
102+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
103+
TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].thread_priority);
104+
}
105+
}
106+
107+
// Signal blocked thread
108+
uint32_t ret = ef.set(FLAG_SIGNAL_DEC);
109+
TEST_ASSERT_FALSE(ret & osFlagsError);
110+
111+
Thread::wait(100);
112+
113+
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
114+
TEST_ASSERT_EQUAL(4, count);
115+
116+
delete[] stats;
117+
t1.terminate();
118+
}
119+
120+
void test_case_multi_threads_terminate()
121+
{
122+
Thread t1(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th1");
123+
Thread t2(osPriorityNormal2, TEST_STACK_SIZE, NULL, "Th2");
124+
t2.start(increment_with_delay);
125+
t1.start(decrement_on_event);
126+
127+
// Read stats
128+
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
129+
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
130+
TEST_ASSERT_EQUAL(5, count);
131+
132+
for (int i = 0; i < count; i++) {
133+
if (0 == strcmp (stats[i].thread_name, "Th2")) {
134+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
135+
TEST_ASSERT_EQUAL(osPriorityNormal2, stats[i].thread_priority);
136+
} else if (0 == strcmp (stats[i].thread_name, "Th1")) {
137+
TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size);
138+
TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].thread_priority);
139+
TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].thread_state);
140+
}
141+
}
142+
143+
t1.terminate();
144+
t2.terminate();
145+
146+
count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
147+
TEST_ASSERT_EQUAL(3, count);
148+
149+
delete[] stats;
150+
t1.terminate();
151+
}
152+
153+
Case cases[] = {
154+
Case("Single Thread Stats", test_case_single_thread_stats),
155+
Case("Less count value", test_case_less_count),
156+
Case("Multiple Threads blocked", test_case_multi_threads_blocked),
157+
Case("Multiple Threads terminate", test_case_multi_threads_terminate),
158+
};
159+
160+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
161+
{
162+
GREENTEA_SETUP(20, "default_auto");
163+
return greentea_test_setup_handler(number_of_cases);
164+
}
165+
166+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
167+
168+
int main()
169+
{
170+
Harness::run(specification);
171+
}

platform/mbed_stats.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
6767
return i;
6868
}
6969

70-
#if defined(MBED_STACK_STATS_ENABLED) && !defined(MBED_CONF_RTOS_PRESENT)
71-
#warning Stack statistics are currently not supported without the rtos.
70+
size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
71+
{
72+
MBED_ASSERT(stats != NULL);
73+
memset(stats, 0, count*sizeof(mbed_stats_thread_t));
74+
size_t i = 0;
75+
76+
#if defined(MBED_THREAD_STATS_ENABLED) && MBED_CONF_RTOS_PRESENT
77+
osThreadId_t *threads;
78+
79+
threads = malloc(sizeof(osThreadId_t) * count);
80+
MBED_ASSERT(threads != NULL);
81+
82+
osKernelLock();
83+
count = osThreadEnumerate(threads, count);
84+
85+
for(i = 0; i < count; i++) {
86+
stats[i].thread_id = (uint32_t)threads[i];
87+
stats[i].thread_state = (uint32_t)osThreadGetState(threads[i]);
88+
stats[i].thread_priority = (uint32_t)osThreadGetPriority(threads[i]);
89+
stats[i].thread_stack_size = osThreadGetStackSize(threads[i]);
90+
stats[i].thread_stack_space = osThreadGetStackSpace(threads[i]);
91+
stats[i].thread_name = osThreadGetName(threads[i]);
92+
}
93+
osKernelUnlock();
94+
free(threads);
95+
#endif
96+
97+
return i;
98+
}
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.
72102
#endif

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
/**
@@ -46,6 +47,19 @@ typedef struct {
4647
uint32_t alloc_fail_cnt; /**< Number of failed allocations. */
4748
} mbed_stats_heap_t;
4849

50+
/**
51+
* struct mbed_stats_thread_t definition
52+
*/
53+
54+
typedef struct {
55+
uint32_t thread_id; /**< Thread Object Identifier */
56+
uint32_t thread_state; /**< Thread Object State */
57+
uint32_t thread_priority; /**< Thread Priority */
58+
uint32_t thread_stack_size; /**< Thread Stack Size */
59+
uint32_t thread_stack_space; /**< Thread remaining stack size */
60+
const char *thread_name; /**< Thread Object name */
61+
} mbed_stats_thread_t;
62+
4963
/**
5064
* Fill the passed in heap stat structure with heap stats.
5165
*
@@ -81,6 +95,16 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats);
8195
*/
8296
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count);
8397

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 stacks 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

0 commit comments

Comments
 (0)