Example program for debugging failing event queue.

Committer:
sjalloq
Date:
Sun Apr 07 14:13:13 2019 +0000
Revision:
0:c9670ffe21d4
First release of TCPSocket example.  Client sigio() never gets called.

Who changed what in which revision?

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