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