VL53L1X-MAX

Committer:
peng103617
Date:
Tue Oct 22 05:47:05 2019 +0000
Revision:
0:385e286b830a
MAX

Who changed what in which revision?

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