Skip to content

Commit c69903c

Browse files
committed
Add basic stats reporting to blinky.
Acts as an update to the quick start experience
1 parent d972805 commit c69903c

File tree

4 files changed

+220
-21
lines changed

4 files changed

+220
-21
lines changed

README.md

Lines changed: 126 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Getting started with Blinky on mbed OS
1+
# Getting started example for Mbed OS
22

3-
This guide reviews the steps required to get Blinky working on an mbed OS platform.
3+
This guide reviews the steps required to get Blinky with the addition of dynamic OS statistics working on an Mbed OS platform.
44

55
Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
66

@@ -26,22 +26,26 @@ Your PC may take a few minutes to compile your code. At the end, you see the fol
2626
```
2727
[snip]
2828
+----------------------------+-------+-------+------+
29-
| Module | .text | .data | .bss |
30-
+----------------------------+-------+-------+------+
31-
| Misc | 13939 | 24 | 1372 |
32-
| core/hal | 16993 | 96 | 296 |
33-
| core/rtos | 7384 | 92 | 4204 |
34-
| features/FEATURE_IPV4 | 80 | 0 | 176 |
35-
| frameworks/greentea-client | 1830 | 60 | 44 |
36-
| frameworks/utest | 2392 | 512 | 292 |
37-
| Subtotals | 42618 | 784 | 6384 |
38-
+----------------------------+-------+-------+------+
39-
Allocated Heap: unknown
40-
Allocated Stack: unknown
41-
Total Static RAM memory (data + bss): 7168 bytes
42-
Total RAM memory (data + bss + heap + stack): 7168 bytes
43-
Total Flash memory (text + data + misc): 43402 bytes
44-
Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
29+
| Module | .text | .data | .bss |
30+
|--------------------|-----------|----------|----------|
31+
| [fill] | 98(+0) | 0(+0) | 2211(+0) |
32+
| [lib]/c.a | 27835(+0) | 2472(+0) | 89(+0) |
33+
| [lib]/gcc.a | 3168(+0) | 0(+0) | 0(+0) |
34+
| [lib]/misc | 248(+0) | 8(+0) | 28(+0) |
35+
| [lib]/nosys.a | 32(+0) | 0(+0) | 0(+0) |
36+
| main.o | 924(+0) | 0(+0) | 12(+0) |
37+
| mbed-os/components | 134(+0) | 0(+0) | 0(+0) |
38+
| mbed-os/drivers | 56(+0) | 0(+0) | 0(+0) |
39+
| mbed-os/features | 42(+0) | 0(+0) | 184(+0) |
40+
| mbed-os/hal | 2087(+0) | 8(+0) | 152(+0) |
41+
| mbed-os/platform | 3633(+0) | 260(+0) | 209(+0) |
42+
| mbed-os/rtos | 9370(+0) | 168(+0) | 6053(+0) |
43+
| mbed-os/targets | 9536(+0) | 12(+0) | 382(+0) |
44+
| Subtotals | 57163(+0) | 2928(+0) | 9320(+0) |
45+
Total Static RAM memory (data + bss): 12248(+0) bytes
46+
Total Flash memory (text + data): 60091(+0) bytes
47+
48+
Image: ./BUILD/K64F/GCC_ARM/mbed-os-example-blinky.bin
4549
```
4650

4751
### Program your board
@@ -50,8 +54,111 @@ Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin
5054
1. Copy the binary file to the mbed device.
5155
1. Press the reset button to start the program.
5256

53-
The LED on your platform turns on and off.
57+
The LED on your platform turns on and off. The main thread will additionally take a snapshot of the device's runtime statistics and display it over serial to your PC. The snapshot includes:
58+
59+
* System Information:
60+
* Mbed OS Version: Will currently default to 999999
61+
* Compiler ID
62+
* ARM = 1
63+
* GCC_ARM = 2
64+
* IAR = 3
65+
* [CPUID Register Information](#cpuid-register-information)
66+
* [Compiler Version](#compiler-version)
67+
* CPU Statistics
68+
* Percentage of runtime that the device has spent awake versus in sleep
69+
* Heap Statistics
70+
* Current heap size
71+
* Max heap size which refers to the largest the heap has grown to
72+
* Thread Statistics
73+
* Provides information on all running threads in the OS including
74+
* Thread ID
75+
* Thread Name
76+
* Thread State
77+
* Thread Priority
78+
* Thread Stack Size
79+
* Thread Stack Space
80+
81+
#### Compiler Version
82+
83+
| Compiler | Version Layout |
84+
| -------- | -------------- |
85+
| ARM | PVVbbbb (P = Major; VV = Minor; bbbb = build number) |
86+
| GCC | VVRRPP (VV = Version; RR = Revision; PP = Patch) |
87+
| IAR | VRRRPPP (V = Version; RRR = Revision; PPP = Patch) |
88+
89+
#### CPUID Register Information
90+
91+
| Bit Field | Field Description | Values |
92+
| --------- | ----------------- | ------ |
93+
|[31:24] | Implementer | 0x41 = ARM |
94+
|[23:20] | Variant | Major revision 0x0 = Revision 0 |
95+
|[19:16] | Architecture | 0xC = Baseline Architecture |
96+
| | | 0xF = Constant (Mainline Architecture) |
97+
|[15:4] | Part Number | 0xC20 = Cortex-M0 |
98+
| | | 0xC60 = Cortex-M0+ |
99+
| | | 0xC23 = Cortex-M3 |
100+
| | | 0xC24 = Cortex-M4 |
101+
| | | 0xC27 = Cortex-M7 |
102+
| | | 0xD20 = Cortex-M23 |
103+
| | | 0xD21 = Cortex-M33 |
104+
|[3:0] | Revision | Minor revision: 0x1 = Patch 1 |
105+
106+
107+
108+
You can view individual examples and additional API information of the statistics collection tools at the bottom of the page in the [related links section](#related-links).
109+
110+
111+
### Output
112+
113+
To view the serial output you can use any terminal client of your choosing such as [PuTTY](http://www.putty.org/) or [CoolTerm](http://freeware.the-meiers.org/).
114+
115+
The default baud rate for this application is set to `115200` and may be modified in the `mbed_app.json` file.
116+
117+
You can find more information on the Mbed OS configuration tools and serail communication in Mbed OS in the related [related links section](#related-links).
118+
119+
The output should contain the following block transmitted at the blinking LED frequency (actual values may vary depending on your target, build profile, and toolchain):
120+
121+
```
122+
=============================== SYSTEM INFO ================================
123+
Mbed OS Version: 999999
124+
CPU ID: 0x410fc241
125+
Compiler ID: 2
126+
Compiler Version: 60300
127+
================= CPU STATS =================
128+
Idle: 98% Usage: 2%
129+
================ HEAP STATS =================
130+
Current heap: 1096
131+
Max heap size: 1096
132+
================ THREAD STATS ===============
133+
ID: 0x20001eac
134+
Name: main_thread
135+
State: 2
136+
Priority: 24
137+
Stack Size: 4096
138+
Stack Space: 3296
139+
140+
ID: 0x20000f5c
141+
Name: idle_thread
142+
State: 1
143+
Priority: 1
144+
Stack Size: 512
145+
Stack Space: 352
146+
147+
ID: 0x20000f18
148+
Name: timer_thread
149+
State: 3
150+
Priority: 40
151+
Stack Size: 768
152+
Stack Space: 664
153+
154+
```
54155

55156
## Troubleshooting
56157

57158
If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
159+
160+
## Related Links
161+
162+
* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html)
163+
* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html)
164+
* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html)

main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#include "mbed.h"
2+
#include "stats_report.h"
23

34
DigitalOut led1(LED1);
45

56
// main() runs in its own thread in the OS
67
int main() {
8+
SystemReport sys_state(500);
9+
710
while (true) {
11+
// Blink LED and wait 0.5 seconds
812
led1 = !led1;
9-
wait(0.5);
13+
wait(0.5f);
14+
15+
// Following the main thread wait, report on the current system status
16+
sys_state.report_state();
1017
}
1118
}
12-

mbed_app.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"target_overrides": {
3+
"*": {
4+
"platform.stdio-baud-rate": 115200,
5+
"platform.stack-stats-enabled": true,
6+
"platform.heap-stats-enabled": true,
7+
"platform.cpu-stats-enabled": true,
8+
"platform.thread-stats-enabled": true,
9+
"platform.sys-stats-enabled": true
10+
}
11+
}
12+
}

stats_report.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class SystemReport {
2+
mbed_stats_heap_t heap_stats;
3+
mbed_stats_cpu_t cpu_stats;
4+
mbed_stats_sys_t sys_stats;
5+
6+
mbed_stats_thread_t *thread_stats;
7+
uint8_t thread_count;
8+
uint8_t max_thread_count;
9+
uint32_t sample_time_ms;
10+
11+
public:
12+
SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate) {
13+
thread_count = osThreadGetCount();
14+
thread_stats = new mbed_stats_thread_t[thread_count];
15+
16+
mbed_stats_sys_get(&sys_stats);
17+
18+
printf("=============================== SYSTEM INFO ================================\r\n");
19+
printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
20+
printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
21+
printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
22+
printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);
23+
}
24+
25+
~SystemReport(void) {
26+
free(thread_stats);
27+
}
28+
29+
void report_state(void) {
30+
report_cpu_stats();
31+
report_heap_stats();
32+
report_thread_stats();
33+
}
34+
35+
void report_cpu_stats(void) {
36+
static uint64_t prev_idle_time = 0;
37+
38+
printf("================= CPU STATS =================\r\n");
39+
40+
mbed_stats_cpu_get(&cpu_stats);
41+
42+
uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
43+
uint8_t idle = (diff * 100) / (sample_time_ms*1000); // usec;
44+
uint8_t usage = 100 - ((diff * 100) / (sample_time_ms*1000)); // usec;;
45+
prev_idle_time = cpu_stats.idle_time;
46+
47+
printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
48+
}
49+
50+
void report_heap_stats(void) {
51+
printf("================ HEAP STATS =================\r\n");
52+
53+
mbed_stats_heap_get(&heap_stats);
54+
55+
printf("Current heap: %lu\r\n", heap_stats.current_size);
56+
printf("Max heap size: %lu\r\n", heap_stats.max_size);
57+
}
58+
59+
void report_thread_stats(void) {
60+
printf("================ THREAD STATS ===============\r\n");
61+
62+
int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
63+
64+
for(int i = 0; i < count; i++) {
65+
printf("ID: 0x%lx \r\n", thread_stats[i].id);
66+
printf("Name: %s \r\n", thread_stats[i].name);
67+
printf("State: %ld \r\n", thread_stats[i].state);
68+
printf("Priority: %ld \r\n", thread_stats[i].priority);
69+
printf("Stack Size: %ld \r\n", thread_stats[i].stack_size);
70+
printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
71+
printf("\r\n");
72+
}
73+
}
74+
};

0 commit comments

Comments
 (0)