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