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