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