Init Project for PSM

Committer:
AustinKim
Date:
Wed Sep 11 07:34:38 2019 +0000
Revision:
0:739a04e1daf4
Init Project for PSM

Who changed what in which revision?

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