WIZnet-IoTShield-AMM592-SMS example

Committer:
hkjung
Date:
Tue Mar 19 00:25:29 2019 +0000
Revision:
0:5cd6077e0a8e
Initial Release

Who changed what in which revision?

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