Template code Support mbed-os 5.x

Dependencies:   X_NUCLEO_IKS01A3

Committer:
noutram
Date:
Thu Aug 15 13:41:34 2019 +0000
Revision:
0:3c909a9f5c1f
Please try this!

Who changed what in which revision?

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