test

Committer:
Perijah
Date:
Wed Feb 27 17:02:00 2019 +0000
Revision:
0:1a1d87c75d25
public test

Who changed what in which revision?

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