Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_stats.c Source File

mbed_stats.c

00001 #include "mbed_assert.h"
00002 #include "mbed_stats.h"
00003 #include "mbed_power_mgmt.h"
00004 #include <string.h>
00005 #include <stdlib.h>
00006 
00007 #include "device.h"
00008 #ifdef MBED_CONF_RTOS_PRESENT
00009 #include "cmsis_os2.h"
00010 #include "rtos_idle.h"
00011 #elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED) || defined(MBED_CPU_STATS_ENABLED)
00012 #warning Statistics are currently not supported without the rtos.
00013 #endif
00014 
00015 #if defined(MBED_CPU_STATS_ENABLED) && (!defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP))
00016 #warning CPU statistics are not supported without low power timer support.
00017 #endif
00018 
00019 void mbed_stats_cpu_get(mbed_stats_cpu_t *stats)
00020 {
00021     MBED_ASSERT(stats != NULL);
00022     memset(stats, 0, sizeof(mbed_stats_cpu_t));
00023 #if defined(MBED_CPU_STATS_ENABLED) && defined(DEVICE_LPTICKER) && defined(DEVICE_SLEEP)
00024     stats->uptime = mbed_uptime();
00025     stats->idle_time = mbed_time_idle();
00026     stats->sleep_time = mbed_time_sleep();
00027     stats->deep_sleep_time = mbed_time_deepsleep();
00028 #endif
00029 }
00030 
00031 // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
00032 void mbed_stats_stack_get(mbed_stats_stack_t *stats)
00033 {
00034     MBED_ASSERT(stats != NULL);
00035     memset(stats, 0, sizeof(mbed_stats_stack_t));
00036 
00037 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00038     uint32_t thread_n = osThreadGetCount();
00039     unsigned i;
00040     osThreadId_t *threads;
00041 
00042     threads = malloc(sizeof(osThreadId_t) * thread_n);
00043     MBED_ASSERT(threads != NULL);
00044 
00045     osKernelLock();
00046     thread_n = osThreadEnumerate(threads, thread_n);
00047 
00048     for (i = 0; i < thread_n; i++) {
00049         uint32_t stack_size = osThreadGetStackSize(threads[i]);
00050         stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
00051         stats->reserved_size += stack_size;
00052         stats->stack_cnt++;
00053     }
00054     osKernelUnlock();
00055 
00056     free(threads);
00057 #endif
00058 }
00059 
00060 size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
00061 {
00062     MBED_ASSERT(stats != NULL);
00063     memset(stats, 0, count * sizeof(mbed_stats_stack_t));
00064 
00065     size_t i = 0;
00066 
00067 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00068     osThreadId_t *threads;
00069 
00070     threads = malloc(sizeof(osThreadId_t) * count);
00071     MBED_ASSERT(threads != NULL);
00072 
00073     osKernelLock();
00074     count = osThreadEnumerate(threads, count);
00075 
00076     for (i = 0; i < count; i++) {
00077         uint32_t stack_size = osThreadGetStackSize(threads[i]);
00078         stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
00079         stats[i].reserved_size = stack_size;
00080         stats[i].thread_id = (uint32_t)threads[i];
00081         stats[i].stack_cnt = 1;
00082     }
00083     osKernelUnlock();
00084 
00085     free(threads);
00086 #endif
00087 
00088     return i;
00089 }
00090 
00091 size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
00092 {
00093     MBED_ASSERT(stats != NULL);
00094     memset(stats, 0, count * sizeof(mbed_stats_thread_t));
00095     size_t i = 0;
00096 
00097 #if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00098     osThreadId_t *threads;
00099 
00100     threads = malloc(sizeof(osThreadId_t) * count);
00101     MBED_ASSERT(threads != NULL);
00102 
00103     osKernelLock();
00104     count = osThreadEnumerate(threads, count);
00105 
00106     for (i = 0; i < count; i++) {
00107         stats[i].id = (uint32_t)threads[i];
00108         stats[i].state = (uint32_t)osThreadGetState(threads[i]);
00109         stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
00110         stats[i].stack_size = osThreadGetStackSize(threads[i]);
00111         stats[i].stack_space = osThreadGetStackSpace(threads[i]);
00112         stats[i].name = osThreadGetName(threads[i]);
00113     }
00114     osKernelUnlock();
00115     free(threads);
00116 #endif
00117     return i;
00118 }
00119 
00120 void mbed_stats_sys_get(mbed_stats_sys_t *stats)
00121 {
00122     MBED_ASSERT(stats != NULL);
00123     memset(stats, 0, sizeof(mbed_stats_sys_t));
00124 
00125 #if defined(MBED_SYS_STATS_ENABLED)
00126 #if defined(__CORTEX_M)
00127     stats->cpu_id = SCB->CPUID;
00128 #endif
00129 #if defined(__IAR_SYSTEMS_ICC__)
00130     stats->compiler_id = IAR;
00131     stats->compiler_version = __VER__;
00132 #elif defined(__CC_ARM)
00133     stats->compiler_id = ARM;
00134     stats->compiler_version = __ARMCC_VERSION;
00135 #elif defined(__GNUC__)
00136     stats->compiler_id = GCC_ARM;
00137     stats->compiler_version = (__GNUC__ * 10000 + __GNUC_MINOR__ * 100);
00138 #endif
00139 
00140 #endif
00141     return;
00142 }