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