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