Test

Committer:
APS_Lab
Date:
Tue May 14 03:20:59 2019 +0000
Revision:
1:b7f576b50a32
Parent:
0:00acc05bf822
First Version

Who changed what in which revision?

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