![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MbedOS 5.15.7 example, using Interrupt mode with expansion board onboard sensor. Serial port set to default (9600) baud.
Dependencies: X_NUCLEO_53L1A1_mbed
stats_report.h@4:b3346923eb1e, 2021-05-11 (annotated)
- Committer:
- johnAlexander
- Date:
- Tue May 11 09:37:42 2021 +0000
- Revision:
- 4:b3346923eb1e
- Parent:
- 0:6b7696e7df5e
Add support for MbedOS v6.x.; Override serial port default of 9600 baud, for printfs. Now set at 115200 baud, to match header comment.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnAlexander | 0:6b7696e7df5e | 1 | /* mbed Microcontroller Library |
johnAlexander | 0:6b7696e7df5e | 2 | * Copyright (c) 2018 ARM Limited |
johnAlexander | 0:6b7696e7df5e | 3 | * SPDX-License-Identifier: Apache-2.0 |
johnAlexander | 0:6b7696e7df5e | 4 | */ |
johnAlexander | 0:6b7696e7df5e | 5 | |
johnAlexander | 0:6b7696e7df5e | 6 | #ifndef STATS_REPORT_H |
johnAlexander | 0:6b7696e7df5e | 7 | #define STATS_REPORT |
johnAlexander | 0:6b7696e7df5e | 8 | |
johnAlexander | 0:6b7696e7df5e | 9 | #include "mbed.h" |
johnAlexander | 0:6b7696e7df5e | 10 | |
johnAlexander | 0:6b7696e7df5e | 11 | /** |
johnAlexander | 0:6b7696e7df5e | 12 | * System Reporting library. Provides runtime information on device: |
johnAlexander | 0:6b7696e7df5e | 13 | * - CPU sleep, idle, and wake times |
johnAlexander | 0:6b7696e7df5e | 14 | * - Heap and stack usage |
johnAlexander | 0:6b7696e7df5e | 15 | * - Thread information |
johnAlexander | 0:6b7696e7df5e | 16 | * - Static system information |
johnAlexander | 0:6b7696e7df5e | 17 | */ |
johnAlexander | 0:6b7696e7df5e | 18 | class SystemReport { |
johnAlexander | 0:6b7696e7df5e | 19 | mbed_stats_heap_t heap_stats; |
johnAlexander | 0:6b7696e7df5e | 20 | mbed_stats_cpu_t cpu_stats; |
johnAlexander | 0:6b7696e7df5e | 21 | mbed_stats_sys_t sys_stats; |
johnAlexander | 0:6b7696e7df5e | 22 | |
johnAlexander | 0:6b7696e7df5e | 23 | mbed_stats_thread_t *thread_stats; |
johnAlexander | 0:6b7696e7df5e | 24 | uint8_t thread_count; |
johnAlexander | 0:6b7696e7df5e | 25 | uint8_t max_thread_count; |
johnAlexander | 0:6b7696e7df5e | 26 | uint32_t sample_time_ms; |
johnAlexander | 0:6b7696e7df5e | 27 | |
johnAlexander | 0:6b7696e7df5e | 28 | public: |
johnAlexander | 0:6b7696e7df5e | 29 | /** |
johnAlexander | 0:6b7696e7df5e | 30 | * SystemReport - Sample rate in ms is required to handle the CPU percent awake logic |
johnAlexander | 0:6b7696e7df5e | 31 | */ |
johnAlexander | 0:6b7696e7df5e | 32 | SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate) |
johnAlexander | 0:6b7696e7df5e | 33 | { |
johnAlexander | 0:6b7696e7df5e | 34 | thread_stats = new mbed_stats_thread_t[max_thread_count]; |
johnAlexander | 0:6b7696e7df5e | 35 | |
johnAlexander | 0:6b7696e7df5e | 36 | // Collect the static system information |
johnAlexander | 0:6b7696e7df5e | 37 | mbed_stats_sys_get(&sys_stats); |
johnAlexander | 0:6b7696e7df5e | 38 | |
johnAlexander | 0:6b7696e7df5e | 39 | printf("=============================== SYSTEM INFO ================================\r\n"); |
johnAlexander | 0:6b7696e7df5e | 40 | printf("Mbed OS Version: %ld \r\n", sys_stats.os_version); |
johnAlexander | 0:6b7696e7df5e | 41 | printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id); |
johnAlexander | 0:6b7696e7df5e | 42 | printf("Compiler ID: %d \r\n", sys_stats.compiler_id); |
johnAlexander | 0:6b7696e7df5e | 43 | printf("Compiler Version: %ld \r\n", sys_stats.compiler_version); |
johnAlexander | 0:6b7696e7df5e | 44 | |
johnAlexander | 0:6b7696e7df5e | 45 | for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) { |
johnAlexander | 0:6b7696e7df5e | 46 | if (sys_stats.ram_size[i] != 0) { |
johnAlexander | 0:6b7696e7df5e | 47 | printf("RAM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]); |
johnAlexander | 0:6b7696e7df5e | 48 | } |
johnAlexander | 0:6b7696e7df5e | 49 | } |
johnAlexander | 0:6b7696e7df5e | 50 | for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) { |
johnAlexander | 0:6b7696e7df5e | 51 | if (sys_stats.rom_size[i] != 0) { |
johnAlexander | 0:6b7696e7df5e | 52 | printf("ROM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]); |
johnAlexander | 0:6b7696e7df5e | 53 | } |
johnAlexander | 0:6b7696e7df5e | 54 | } |
johnAlexander | 0:6b7696e7df5e | 55 | } |
johnAlexander | 0:6b7696e7df5e | 56 | |
johnAlexander | 0:6b7696e7df5e | 57 | ~SystemReport(void) |
johnAlexander | 0:6b7696e7df5e | 58 | { |
johnAlexander | 0:6b7696e7df5e | 59 | free(thread_stats); |
johnAlexander | 0:6b7696e7df5e | 60 | } |
johnAlexander | 0:6b7696e7df5e | 61 | |
johnAlexander | 0:6b7696e7df5e | 62 | /** |
johnAlexander | 0:6b7696e7df5e | 63 | * Report on each Mbed OS Platform stats API |
johnAlexander | 0:6b7696e7df5e | 64 | */ |
johnAlexander | 0:6b7696e7df5e | 65 | void report_state(void) |
johnAlexander | 0:6b7696e7df5e | 66 | { |
johnAlexander | 0:6b7696e7df5e | 67 | report_cpu_stats(); |
johnAlexander | 0:6b7696e7df5e | 68 | report_heap_stats(); |
johnAlexander | 0:6b7696e7df5e | 69 | report_thread_stats(); |
johnAlexander | 0:6b7696e7df5e | 70 | |
johnAlexander | 0:6b7696e7df5e | 71 | // Clear next line to separate subsequent report logs |
johnAlexander | 0:6b7696e7df5e | 72 | printf("\r\n"); |
johnAlexander | 0:6b7696e7df5e | 73 | } |
johnAlexander | 0:6b7696e7df5e | 74 | |
johnAlexander | 0:6b7696e7df5e | 75 | /** |
johnAlexander | 0:6b7696e7df5e | 76 | * Report CPU idle and awake time in terms of percentage |
johnAlexander | 0:6b7696e7df5e | 77 | */ |
johnAlexander | 0:6b7696e7df5e | 78 | void report_cpu_stats(void) |
johnAlexander | 0:6b7696e7df5e | 79 | { |
johnAlexander | 0:6b7696e7df5e | 80 | static uint64_t prev_idle_time = 0; |
johnAlexander | 0:6b7696e7df5e | 81 | |
johnAlexander | 0:6b7696e7df5e | 82 | printf("================= CPU STATS =================\r\n"); |
johnAlexander | 0:6b7696e7df5e | 83 | |
johnAlexander | 0:6b7696e7df5e | 84 | // Collect and print cpu stats |
johnAlexander | 0:6b7696e7df5e | 85 | mbed_stats_cpu_get(&cpu_stats); |
johnAlexander | 0:6b7696e7df5e | 86 | |
johnAlexander | 0:6b7696e7df5e | 87 | uint64_t diff = (cpu_stats.idle_time - prev_idle_time); |
johnAlexander | 0:6b7696e7df5e | 88 | uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec; |
johnAlexander | 0:6b7696e7df5e | 89 | uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;; |
johnAlexander | 0:6b7696e7df5e | 90 | prev_idle_time = cpu_stats.idle_time; |
johnAlexander | 0:6b7696e7df5e | 91 | |
johnAlexander | 0:6b7696e7df5e | 92 | printf("Idle: %d%% Usage: %d%% \r\n", idle, usage); |
johnAlexander | 0:6b7696e7df5e | 93 | } |
johnAlexander | 0:6b7696e7df5e | 94 | |
johnAlexander | 0:6b7696e7df5e | 95 | /** |
johnAlexander | 0:6b7696e7df5e | 96 | * Report current heap stats. Current heap refers to the current amount of |
johnAlexander | 0:6b7696e7df5e | 97 | * allocated heap. Max heap refers to the highest amount of heap allocated |
johnAlexander | 0:6b7696e7df5e | 98 | * since reset. |
johnAlexander | 0:6b7696e7df5e | 99 | */ |
johnAlexander | 0:6b7696e7df5e | 100 | void report_heap_stats(void) |
johnAlexander | 0:6b7696e7df5e | 101 | { |
johnAlexander | 0:6b7696e7df5e | 102 | printf("================ HEAP STATS =================\r\n"); |
johnAlexander | 0:6b7696e7df5e | 103 | |
johnAlexander | 0:6b7696e7df5e | 104 | // Collect and print heap stats |
johnAlexander | 0:6b7696e7df5e | 105 | mbed_stats_heap_get(&heap_stats); |
johnAlexander | 0:6b7696e7df5e | 106 | |
johnAlexander | 0:6b7696e7df5e | 107 | printf("Current heap: %lu\r\n", heap_stats.current_size); |
johnAlexander | 0:6b7696e7df5e | 108 | printf("Max heap size: %lu\r\n", heap_stats.max_size); |
johnAlexander | 0:6b7696e7df5e | 109 | } |
johnAlexander | 0:6b7696e7df5e | 110 | |
johnAlexander | 0:6b7696e7df5e | 111 | /** |
johnAlexander | 0:6b7696e7df5e | 112 | * Report active thread stats |
johnAlexander | 0:6b7696e7df5e | 113 | */ |
johnAlexander | 0:6b7696e7df5e | 114 | void report_thread_stats(void) |
johnAlexander | 0:6b7696e7df5e | 115 | { |
johnAlexander | 0:6b7696e7df5e | 116 | printf("================ THREAD STATS ===============\r\n"); |
johnAlexander | 0:6b7696e7df5e | 117 | |
johnAlexander | 0:6b7696e7df5e | 118 | // Collect and print running thread stats |
johnAlexander | 0:6b7696e7df5e | 119 | int count = mbed_stats_thread_get_each(thread_stats, max_thread_count); |
johnAlexander | 0:6b7696e7df5e | 120 | |
johnAlexander | 0:6b7696e7df5e | 121 | for (int i = 0; i < count; i++) { |
johnAlexander | 0:6b7696e7df5e | 122 | printf("ID: 0x%lx \r\n", thread_stats[i].id); |
johnAlexander | 0:6b7696e7df5e | 123 | printf("Name: %s \r\n", thread_stats[i].name); |
johnAlexander | 0:6b7696e7df5e | 124 | printf("State: %ld \r\n", thread_stats[i].state); |
johnAlexander | 0:6b7696e7df5e | 125 | printf("Priority: %ld \r\n", thread_stats[i].priority); |
johnAlexander | 0:6b7696e7df5e | 126 | printf("Stack Size: %ld \r\n", thread_stats[i].stack_size); |
johnAlexander | 0:6b7696e7df5e | 127 | printf("Stack Space: %ld \r\n", thread_stats[i].stack_space); |
johnAlexander | 0:6b7696e7df5e | 128 | } |
johnAlexander | 0:6b7696e7df5e | 129 | } |
johnAlexander | 0:6b7696e7df5e | 130 | }; |
johnAlexander | 0:6b7696e7df5e | 131 | |
johnAlexander | 0:6b7696e7df5e | 132 | #endif // STATS_REPORT_H |