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