Mbed statistics
Mbed OS provides a set of functions that you can use to capture the memory and thread statistics at runtime. mbed_stats.h
declares these functions. To enable all Mbed OS platform statistics, you must enable the following Mbed OS configuration option:
{
"target_overrides": {
"*": {
"platform.all-stats-enabled": true
}
}
}
Tip: See the documentation of the Arm Mbed configuration system for more details about mbed_app.json
.
Memory statistics
You can use memory statistics functions to capture heap use, cumulative stack use or stack use for each thread at runtime. To enable memory use monitoring, you must enable the following Mbed OS configuration options:
{
"target_overrides": {
"*": {
"platform.heap-stats-enabled": true,
"platform.stack-stats-enabled": true
}
}
}
Note: Each malloc
or calloc
memory allocation call adds an overhead of 8 bytes when heap memory statistics are enabled.
Thread statistics
You can use the thread statistics function mbed_stats_thread_get_each
to capture the thread ID, state, priority, name and stack information for all active threads at runtime. To enable thread monitoring, you must enable the following Mbed OS configuration options:
{
"target_overrides": {
"*": {
"platform.thread-stats-enabled": true
}
}
}
System information
You can use the mbed_stats_sys_get
function to get the CPU ID, compiler information and RAM and ROM memories on the target device. To enable system information fetching, you must enable the following Mbed OS configuration option:
{
"target_overrides": {
"*": {
"platform.sys-stats-enabled": true
}
}
}
CPU statistics
You can use the mbed_stats_cpu_get
function to get the uptime, idle time and sleep time information. Timing information available is cumulative because the system is on. Please note CPU statistics depend on the availability of the low power timer in the hardware. To enable fetching of CPU information, you must enable the following Mbed OS configuration option:
{
"target_overrides": {
"*": {
"platform.cpu-stats-enabled": true
}
}
}
Mbed statistics function reference
Memory statistics example
/*
* Copyright (c) 2017 - 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#define MAX_THREAD_INFO 10
mbed_stats_heap_t heap_info;
mbed_stats_stack_t stack_info[ MAX_THREAD_INFO ];
int main()
{
debug("\nThis message is from debug function");
debug_if(1, "\nThis message is from debug_if function");
debug_if(0, "\nSOMETHING WRONG!!! This message from debug_if function shouldn't show on bash");
printf("\nMemoryStats:");
mbed_stats_heap_get(&heap_info);
printf("\n\tBytes allocated currently: %ld", heap_info.current_size);
printf("\n\tMax bytes allocated at a given time: %ld", heap_info.max_size);
printf("\n\tCumulative sum of bytes ever allocated: %ld", heap_info.total_size);
printf("\n\tCurrent number of bytes allocated for the heap: %ld", heap_info.reserved_size);
printf("\n\tCurrent number of allocations: %ld", heap_info.alloc_cnt);
printf("\n\tNumber of failed allocations: %ld", heap_info.alloc_fail_cnt);
mbed_stats_stack_get(&stack_info[0]);
printf("\nCumulative Stack Info:");
printf("\n\tMaximum number of bytes used on the stack: %ld", stack_info[0].max_size);
printf("\n\tCurrent number of bytes allocated for the stack: %ld", stack_info[0].reserved_size);
printf("\n\tNumber of stacks stats accumulated in the structure: %ld", stack_info[0].stack_cnt);
mbed_stats_stack_get_each(stack_info, MAX_THREAD_INFO);
printf("\nThread Stack Info:");
for (int i = 0; i < MAX_THREAD_INFO; i++) {
if (stack_info[i].thread_id != 0) {
printf("\n\tThread: %d", i);
printf("\n\t\tThread Id: 0x%08lX", stack_info[i].thread_id);
printf("\n\t\tMaximum number of bytes used on the stack: %ld", stack_info[i].max_size);
printf("\n\t\tCurrent number of bytes allocated for the stack: %ld", stack_info[i].reserved_size);
printf("\n\t\tNumber of stacks stats accumulated in the structure: %ld", stack_info[i].stack_cnt);
}
}
printf("\nDone...\n\n");
}
Thread statistics example
/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include <inttypes.h>
#if !defined(MBED_THREAD_STATS_ENABLED)
#error "Thread statistics not enabled"
#endif
#define MAX_THREAD_STATS 0x8
#define BLINKY_THREAD_STACK 224
#define IDLE_THREAD_STACK 384
#define STOP_FLAG 0xEF
#define WAIT_TIME_MS 500
// Initialise the digital pin LED1 as an output
DigitalOut led1(LED1);
static EventFlags idle_ef;
void blinky()
{
while (1) {
led1 = !led1;
thread_sleep_for(WAIT_TIME_MS);
}
}
void idle()
{
idle_ef.wait_all(STOP_FLAG);
}
int main()
{
Thread *blinky_thread = new Thread(osPriorityNormal, BLINKY_THREAD_STACK, nullptr, "blinky_thread");
blinky_thread->start(blinky);
Thread *idle_thread = new Thread(osPriorityNormal, IDLE_THREAD_STACK, nullptr, "idle_thread");
idle_thread->start(idle);
// Sleep helps other created threads to run
thread_sleep_for(10);
mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS];
int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS);
for (int i = 0; i < count; i++) {
printf("ID: 0x%" PRIx32 "\n", stats[i].id);
printf("Name: %s \n", stats[i].name);
printf("State: %" PRId32 "\n", stats[i].state);
printf("Priority: %" PRId32 "\n", stats[i].priority);
printf("Stack Size: %" PRId32 "\n", stats[i].stack_size);
printf("Stack Space: %" PRId32 "\n", stats[i].stack_space);
printf("\n");
}
blinky_thread->terminate();
idle_thread->terminate();
return 0;
}
System information example
/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include <inttypes.h>
#if !defined(MBED_SYS_STATS_ENABLED)
#error [NOT_SUPPORTED] System statistics not supported
#endif
int main()
{
mbed_stats_sys_t stats;
mbed_stats_sys_get(&stats);
printf("Mbed OS Version: %" PRId32 "\n", stats.os_version);
/* CPUID Register information
[31:24]Implementer 0x41 = ARM
[23:20]Variant Major revision 0x0 = Revision 0
[19:16]Architecture 0xC = Baseline Architecture
0xF = Constant (Mainline Architecture)
[15:4]PartNO 0xC20 = Cortex-M0
0xC60 = Cortex-M0+
0xC23 = Cortex-M3
0xC24 = Cortex-M4
0xC27 = Cortex-M7
0xD20 = Cortex-M23
0xD21 = Cortex-M33
[3:0]Revision Minor revision: 0x1 = Patch 1
*/
printf("CPU ID: 0x%" PRIx32 "\n", stats.cpu_id);
/* Compiler IDs
ARM = 1
GCC_ARM = 2
IAR = 3
*/
printf("Compiler ID: %d \n", stats.compiler_id);
/* Compiler versions:
ARM: PVVbbbb (P = Major; VV = Minor; bbbb = build number)
GCC: VVRRPP (VV = Version; RR = Revision; PP = Patch)
IAR: VRRRPPP (V = Version; RRR = Revision; PPP = Patch)
*/
printf("Compiler Version: %" PRId32 "\n", stats.compiler_version);
/* RAM / ROM memory start and size information */
for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
if (stats.ram_size[i] != 0) {
printf("RAM%d: Start 0x%" PRIx32 " Size: 0x%" PRIx32 "\n", i, stats.ram_start[i], stats.ram_size[i]);
}
}
for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
if (stats.rom_size[i] != 0) {
printf("ROM%d: Start 0x%" PRIx32 " Size: 0x%" PRIx32 "\n", i, stats.rom_start[i], stats.rom_size[i]);
}
}
return 0;
}
CPU statistics example
/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "platform/mbed_thread.h"
#if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP)
#error [NOT_SUPPORTED] test not supported
#endif
// Initialise the digital pin LED1 as an output
DigitalOut led1(LED1);
#define MAX_THREAD_STACK 384
#define SAMPLE_TIME_MS 2000
#define LOOP_TIME_MS 3000
uint64_t prev_idle_time = 0;
int32_t wait_time_ms = 5000;
void busy_thread()
{
volatile uint64_t i = ~0;
while(i--) {
led1 = !led1;
thread_sleep_for(wait_time_ms);
}
}
void print_cpu_stats()
{
mbed_stats_cpu_t stats;
mbed_stats_cpu_get(&stats);
// Calculate the percentage of CPU usage
uint64_t diff_usec = (stats.idle_time - prev_idle_time);
uint8_t idle = (diff_usec * 100) / (SAMPLE_TIME_MS*1000);
uint8_t usage = 100 - ((diff_usec * 100) / (SAMPLE_TIME_MS*1000));
prev_idle_time = stats.idle_time;
printf("Time(us): Up: %lld", stats.uptime);
printf(" Idle: %lld", stats.idle_time);
printf(" Sleep: %lld", stats.sleep_time);
printf(" DeepSleep: %lld\n", stats.deep_sleep_time);
printf("Idle: %d%% Usage: %d%%\n\n", idle, usage);
}
int main()
{
// Request the shared queue
EventQueue *stats_queue = mbed_event_queue();
Thread *thread;
int id;
id = stats_queue->call_every(SAMPLE_TIME_MS, print_cpu_stats);
thread = new Thread(osPriorityNormal, MAX_THREAD_STACK);
thread->start(busy_thread);
// Steadily increase the system load
for (int count = 1; ; count++) {
thread_sleep_for(LOOP_TIME_MS);
if (wait_time_ms <= 0) {
break;
}
wait_time_ms -= 1000;
}
thread->terminate();
stats_queue->cancel(id);
return 0;
}