Mangue Baja team's code to frontal ECU

Committer:
einsteingustavo
Date:
Wed Jul 24 20:03:52 2019 +0000
Revision:
0:12fb9cbcabcc
Mangue Baja team's code to frontal ECU

Who changed what in which revision?

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