Uart with ADXL362

Dependencies:   ADXL362

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stats_report.h Source File

stats_report.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #ifndef STATS_REPORT_H
00007 #define STATS_REPORT
00008 
00009 #include "mbed.h"
00010 
00011 /**
00012  *  System Reporting library. Provides runtime information on device:
00013  *      - CPU sleep, idle, and wake times
00014  *      - Heap and stack usage
00015  *      - Thread information
00016  *      - Static system information
00017  */
00018 class SystemReport {
00019     mbed_stats_heap_t   heap_stats;
00020     mbed_stats_cpu_t    cpu_stats;
00021     mbed_stats_sys_t    sys_stats;
00022 
00023     mbed_stats_thread_t *thread_stats;
00024     uint8_t   thread_count;
00025     uint8_t   max_thread_count;
00026     uint32_t  sample_time_ms;
00027 
00028 public:
00029     /**
00030      *  SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
00031      */
00032     SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
00033     {
00034         thread_stats = new mbed_stats_thread_t[max_thread_count];
00035 
00036         // Collect the static system information
00037         mbed_stats_sys_get(&sys_stats);
00038 
00039         printf("=============================== SYSTEM INFO  ================================\r\n");
00040         printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
00041         printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
00042         printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
00043         printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);
00044     }
00045 
00046     ~SystemReport(void)
00047     {
00048         free(thread_stats);
00049     }
00050 
00051     /**
00052      *  Report on each Mbed OS Platform stats API
00053      */
00054     void report_state(void)
00055     {
00056         report_cpu_stats();
00057         report_heap_stats();
00058         report_thread_stats();
00059 
00060         // Clear next line to separate subsequent report logs
00061         printf("\r\n");
00062     }
00063 
00064     /**
00065      *  Report CPU idle and awake time in terms of percentage
00066      */
00067     void report_cpu_stats(void)
00068     {
00069         static uint64_t prev_idle_time = 0;
00070 
00071         printf("================= CPU STATS =================\r\n");
00072 
00073         // Collect and print cpu stats
00074         mbed_stats_cpu_get(&cpu_stats);
00075 
00076         uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
00077         uint8_t idle = (diff * 100) / (sample_time_ms * 1000);  // usec;
00078         uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000));  // usec;;
00079         prev_idle_time = cpu_stats.idle_time;
00080 
00081         printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
00082     }
00083 
00084     /**
00085      *  Report current heap stats. Current heap refers to the current amount of
00086      *  allocated heap. Max heap refers to the highest amount of heap allocated
00087      *  since reset.
00088      */
00089     void report_heap_stats(void)
00090     {
00091         printf("================ HEAP STATS =================\r\n");
00092 
00093         // Collect and print heap stats
00094         mbed_stats_heap_get(&heap_stats);
00095 
00096         printf("Current heap: %lu\r\n", heap_stats.current_size);
00097         printf("Max heap size: %lu\r\n", heap_stats.max_size);
00098     }
00099 
00100     /**
00101      *  Report active thread stats
00102      */
00103     void report_thread_stats(void)
00104     {
00105         printf("================ THREAD STATS ===============\r\n");
00106 
00107         // Collect and print running thread stats
00108         int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
00109 
00110         for (int i = 0; i < count; i++) {
00111             printf("ID: 0x%lx \r\n",        thread_stats[i].id);
00112             printf("Name: %s \r\n",         thread_stats[i].name);
00113             printf("State: %ld \r\n",       thread_stats[i].state);
00114             printf("Priority: %ld \r\n",    thread_stats[i].priority);
00115             printf("Stack Size: %ld \r\n",  thread_stats[i].stack_size);
00116             printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
00117         }
00118     }
00119 };
00120 
00121 #endif // STATS_REPORT_H