test

Committer:
kstokely
Date:
Mon Mar 25 19:37:05 2019 +0000
Revision:
0:50a0fdd7f221
test os program

Who changed what in which revision?

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