Skip to content

Commit 991939f

Browse files
author
Cruz Monrreal
authored
Merge pull request #144 from kegilbert/quick-start-blinky
Add basic stats reporting to blinky.
2 parents d972805 + 5e06f70 commit 991939f

File tree

4 files changed

+288
-22
lines changed

4 files changed

+288
-22
lines changed

README.md

Lines changed: 129 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,114 @@ 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+
RAM0: Start 0x20000000 Size: 0x30000
128+
RAM1: Start 0x1fff0000 Size: 0x10000
129+
ROM0: Start 0x0 Size: 0x100000
130+
================= CPU STATS =================
131+
Idle: 98% Usage: 2%
132+
================ HEAP STATS =================
133+
Current heap: 1096
134+
Max heap size: 1096
135+
================ THREAD STATS ===============
136+
ID: 0x20001eac
137+
Name: main_thread
138+
State: 2
139+
Priority: 24
140+
Stack Size: 4096
141+
Stack Space: 3296
142+
143+
ID: 0x20000f5c
144+
Name: idle_thread
145+
State: 1
146+
Priority: 1
147+
Stack Size: 512
148+
Stack Space: 352
149+
150+
ID: 0x20000f18
151+
Name: timer_thread
152+
State: 3
153+
Priority: 40
154+
Stack Size: 768
155+
Stack Space: 664
156+
157+
```
54158

55159
## Troubleshooting
56160

57161
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.
162+
163+
## Related Links
164+
165+
* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html)
166+
* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html)
167+
* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html)

main.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
#include "mbed.h"
7+
#include "stats_report.h"
28

39
DigitalOut led1(LED1);
410

511
// main() runs in its own thread in the OS
6-
int main() {
12+
int main()
13+
{
14+
SystemReport sys_state(500 /* Loop delay time in ms */);
15+
716
while (true) {
17+
// Blink LED and wait 0.5 seconds
818
led1 = !led1;
9-
wait(0.5);
19+
wait(0.5f);
20+
21+
// Following the main thread wait, report on the current system status
22+
sys_state.report_state();
1023
}
1124
}
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: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef STATS_REPORT_H
7+
#define STATS_REPORT
8+
9+
#include "mbed.h"
10+
11+
/**
12+
* System Reporting library. Provides runtime information on device:
13+
* - CPU sleep, idle, and wake times
14+
* - Heap and stack usage
15+
* - Thread information
16+
* - Static system information
17+
*/
18+
class SystemReport {
19+
mbed_stats_heap_t heap_stats;
20+
mbed_stats_cpu_t cpu_stats;
21+
mbed_stats_sys_t sys_stats;
22+
23+
mbed_stats_thread_t *thread_stats;
24+
uint8_t thread_count;
25+
uint8_t max_thread_count;
26+
uint32_t sample_time_ms;
27+
28+
public:
29+
/**
30+
* SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
31+
*/
32+
SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
33+
{
34+
thread_stats = new mbed_stats_thread_t[max_thread_count];
35+
36+
// Collect the static system information
37+
mbed_stats_sys_get(&sys_stats);
38+
39+
printf("=============================== SYSTEM INFO ================================\r\n");
40+
printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
41+
printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
42+
printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
43+
printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);
44+
45+
for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
46+
if (sys_stats.ram_size[i] != 0) {
47+
printf("RAM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]);
48+
}
49+
}
50+
for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
51+
if (sys_stats.rom_size[i] != 0) {
52+
printf("ROM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]);
53+
}
54+
}
55+
}
56+
57+
~SystemReport(void)
58+
{
59+
free(thread_stats);
60+
}
61+
62+
/**
63+
* Report on each Mbed OS Platform stats API
64+
*/
65+
void report_state(void)
66+
{
67+
report_cpu_stats();
68+
report_heap_stats();
69+
report_thread_stats();
70+
71+
// Clear next line to separate subsequent report logs
72+
printf("\r\n");
73+
}
74+
75+
/**
76+
* Report CPU idle and awake time in terms of percentage
77+
*/
78+
void report_cpu_stats(void)
79+
{
80+
static uint64_t prev_idle_time = 0;
81+
82+
printf("================= CPU STATS =================\r\n");
83+
84+
// Collect and print cpu stats
85+
mbed_stats_cpu_get(&cpu_stats);
86+
87+
uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
88+
uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
89+
uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
90+
prev_idle_time = cpu_stats.idle_time;
91+
92+
printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
93+
}
94+
95+
/**
96+
* Report current heap stats. Current heap refers to the current amount of
97+
* allocated heap. Max heap refers to the highest amount of heap allocated
98+
* since reset.
99+
*/
100+
void report_heap_stats(void)
101+
{
102+
printf("================ HEAP STATS =================\r\n");
103+
104+
// Collect and print heap stats
105+
mbed_stats_heap_get(&heap_stats);
106+
107+
printf("Current heap: %lu\r\n", heap_stats.current_size);
108+
printf("Max heap size: %lu\r\n", heap_stats.max_size);
109+
}
110+
111+
/**
112+
* Report active thread stats
113+
*/
114+
void report_thread_stats(void)
115+
{
116+
printf("================ THREAD STATS ===============\r\n");
117+
118+
// Collect and print running thread stats
119+
int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
120+
121+
for (int i = 0; i < count; i++) {
122+
printf("ID: 0x%lx \r\n", thread_stats[i].id);
123+
printf("Name: %s \r\n", thread_stats[i].name);
124+
printf("State: %ld \r\n", thread_stats[i].state);
125+
printf("Priority: %ld \r\n", thread_stats[i].priority);
126+
printf("Stack Size: %ld \r\n", thread_stats[i].stack_size);
127+
printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
128+
}
129+
}
130+
};
131+
132+
#endif // STATS_REPORT_H

0 commit comments

Comments
 (0)