hfyfyu

Committer:
bogthe
Date:
Sat Jan 12 13:55:39 2019 +0000
Revision:
0:d9dd72246908
uyjg

Who changed what in which revision?

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