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