00

Committer:
peng103617
Date:
Thu Oct 17 03:27:19 2019 +0000
Revision:
0:69db88456c7f
916

Who changed what in which revision?

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