![](/media/cache/profiles/2012_sf_avc_095.jpg.50x50_q85.jpg)
2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
stats_report.h@0:7e98bbfd102a, 2018-12-01 (annotated)
- Committer:
- shimniok
- Date:
- Sat Dec 01 23:27:11 2018 +0000
- Revision:
- 0:7e98bbfd102a
- Child:
- 24:a7f92dfc5310
Initial commit w/ shell thread
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 | 0:7e98bbfd102a | 39 | printf("=============================== SYSTEM INFO ================================\r\n"); |
shimniok | 0:7e98bbfd102a | 40 | printf("Mbed OS Version: %ld \r\n", sys_stats.os_version); |
shimniok | 0:7e98bbfd102a | 41 | printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id); |
shimniok | 0:7e98bbfd102a | 42 | printf("Compiler ID: %d \r\n", sys_stats.compiler_id); |
shimniok | 0:7e98bbfd102a | 43 | printf("Compiler Version: %ld \r\n", sys_stats.compiler_version); |
shimniok | 0:7e98bbfd102a | 44 | } |
shimniok | 0:7e98bbfd102a | 45 | |
shimniok | 0:7e98bbfd102a | 46 | ~SystemReport(void) |
shimniok | 0:7e98bbfd102a | 47 | { |
shimniok | 0:7e98bbfd102a | 48 | free(thread_stats); |
shimniok | 0:7e98bbfd102a | 49 | } |
shimniok | 0:7e98bbfd102a | 50 | |
shimniok | 0:7e98bbfd102a | 51 | /** |
shimniok | 0:7e98bbfd102a | 52 | * Report on each Mbed OS Platform stats API |
shimniok | 0:7e98bbfd102a | 53 | */ |
shimniok | 0:7e98bbfd102a | 54 | void report_state(void) |
shimniok | 0:7e98bbfd102a | 55 | { |
shimniok | 0:7e98bbfd102a | 56 | report_cpu_stats(); |
shimniok | 0:7e98bbfd102a | 57 | report_heap_stats(); |
shimniok | 0:7e98bbfd102a | 58 | report_thread_stats(); |
shimniok | 0:7e98bbfd102a | 59 | |
shimniok | 0:7e98bbfd102a | 60 | // Clear next line to separate subsequent report logs |
shimniok | 0:7e98bbfd102a | 61 | printf("\r\n"); |
shimniok | 0:7e98bbfd102a | 62 | } |
shimniok | 0:7e98bbfd102a | 63 | |
shimniok | 0:7e98bbfd102a | 64 | /** |
shimniok | 0:7e98bbfd102a | 65 | * Report CPU idle and awake time in terms of percentage |
shimniok | 0:7e98bbfd102a | 66 | */ |
shimniok | 0:7e98bbfd102a | 67 | void report_cpu_stats(void) |
shimniok | 0:7e98bbfd102a | 68 | { |
shimniok | 0:7e98bbfd102a | 69 | static uint64_t prev_idle_time = 0; |
shimniok | 0:7e98bbfd102a | 70 | |
shimniok | 0:7e98bbfd102a | 71 | printf("================= CPU STATS =================\r\n"); |
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 | 0:7e98bbfd102a | 81 | printf("Idle: %d%% Usage: %d%% \r\n", idle, usage); |
shimniok | 0:7e98bbfd102a | 82 | } |
shimniok | 0:7e98bbfd102a | 83 | |
shimniok | 0:7e98bbfd102a | 84 | /** |
shimniok | 0:7e98bbfd102a | 85 | * Report current heap stats. Current heap refers to the current amount of |
shimniok | 0:7e98bbfd102a | 86 | * allocated heap. Max heap refers to the highest amount of heap allocated |
shimniok | 0:7e98bbfd102a | 87 | * since reset. |
shimniok | 0:7e98bbfd102a | 88 | */ |
shimniok | 0:7e98bbfd102a | 89 | void report_heap_stats(void) |
shimniok | 0:7e98bbfd102a | 90 | { |
shimniok | 0:7e98bbfd102a | 91 | printf("================ HEAP STATS =================\r\n"); |
shimniok | 0:7e98bbfd102a | 92 | |
shimniok | 0:7e98bbfd102a | 93 | // Collect and print heap stats |
shimniok | 0:7e98bbfd102a | 94 | mbed_stats_heap_get(&heap_stats); |
shimniok | 0:7e98bbfd102a | 95 | |
shimniok | 0:7e98bbfd102a | 96 | printf("Current heap: %lu\r\n", heap_stats.current_size); |
shimniok | 0:7e98bbfd102a | 97 | printf("Max heap size: %lu\r\n", heap_stats.max_size); |
shimniok | 0:7e98bbfd102a | 98 | } |
shimniok | 0:7e98bbfd102a | 99 | |
shimniok | 0:7e98bbfd102a | 100 | /** |
shimniok | 0:7e98bbfd102a | 101 | * Report active thread stats |
shimniok | 0:7e98bbfd102a | 102 | */ |
shimniok | 0:7e98bbfd102a | 103 | void report_thread_stats(void) |
shimniok | 0:7e98bbfd102a | 104 | { |
shimniok | 0:7e98bbfd102a | 105 | printf("================ THREAD STATS ===============\r\n"); |
shimniok | 0:7e98bbfd102a | 106 | |
shimniok | 0:7e98bbfd102a | 107 | // Collect and print running thread stats |
shimniok | 0:7e98bbfd102a | 108 | int count = mbed_stats_thread_get_each(thread_stats, max_thread_count); |
shimniok | 0:7e98bbfd102a | 109 | |
shimniok | 0:7e98bbfd102a | 110 | for (int i = 0; i < count; i++) { |
shimniok | 0:7e98bbfd102a | 111 | printf("ID: 0x%lx \r\n", thread_stats[i].id); |
shimniok | 0:7e98bbfd102a | 112 | printf("Name: %s \r\n", thread_stats[i].name); |
shimniok | 0:7e98bbfd102a | 113 | printf("State: %ld \r\n", thread_stats[i].state); |
shimniok | 0:7e98bbfd102a | 114 | printf("Priority: %ld \r\n", thread_stats[i].priority); |
shimniok | 0:7e98bbfd102a | 115 | printf("Stack Size: %ld \r\n", thread_stats[i].stack_size); |
shimniok | 0:7e98bbfd102a | 116 | printf("Stack Space: %ld \r\n", thread_stats[i].stack_space); |
shimniok | 0:7e98bbfd102a | 117 | } |
shimniok | 0:7e98bbfd102a | 118 | } |
shimniok | 0:7e98bbfd102a | 119 | }; |
shimniok | 0:7e98bbfd102a | 120 | |
shimniok | 0:7e98bbfd102a | 121 | #endif // STATS_REPORT_H |