RTOS Template for ELEC35X 2019

Committer:
noutram
Date:
Wed Sep 25 15:02:03 2019 +0000
Revision:
0:2f993270f60e
RTOS Template for 2019

Who changed what in which revision?

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