Init Project for Ping

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