hbjhjb

Dependencies:   mbed X-NUCLEO-IHM05A1

Committer:
gidiana
Date:
Thu Dec 06 16:33:09 2018 +0000
Revision:
12:a34ecb37c03f
update mbed-os;

Who changed what in which revision?

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