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