This a fork of https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-blinky/

Committer:
c_jin
Date:
Mon Feb 18 05:30:47 2019 +0000
Revision:
0:8b84a0b4f817
Fork of https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-blinky/

Who changed what in which revision?

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