my first "os5" bare-metal on online-compiler

Committer:
okano
Date:
Sat Jun 15 06:49:03 2019 +0000
Revision:
0:4cdcddbd6c3d
my first "os5" bare-metal

Who changed what in which revision?

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