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