Blinking LED example with system statistics printed to terminal

Committer:
maclobdell
Date:
Mon Mar 25 22:28:40 2019 -0500
Revision:
0:03c15e688f62
blinky app that uses mbed os 5.12 release candidate

Who changed what in which revision?

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