2018-05-03 21:10:48 +00:00
|
|
|
#include "mbed_assert.h"
|
2016-10-26 22:06:08 +00:00
|
|
|
#include "mbed_stats.h"
|
|
|
|
#include <string.h>
|
2017-05-15 14:55:45 +00:00
|
|
|
#include <stdlib.h>
|
2016-10-26 22:06:08 +00:00
|
|
|
|
2018-05-03 21:10:48 +00:00
|
|
|
#include "device.h"
|
2018-05-02 16:35:45 +00:00
|
|
|
#ifdef MBED_CONF_RTOS_PRESENT
|
2017-05-15 14:55:45 +00:00
|
|
|
#include "cmsis_os2.h"
|
2018-05-03 16:50:01 +00:00
|
|
|
#elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED)
|
|
|
|
#warning Statistics are currently not supported without the rtos.
|
2016-10-26 22:06:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
|
|
|
|
|
|
|
|
void mbed_stats_stack_get(mbed_stats_stack_t *stats)
|
|
|
|
{
|
2018-05-03 16:23:12 +00:00
|
|
|
MBED_ASSERT(stats != NULL);
|
2016-10-26 22:06:08 +00:00
|
|
|
memset(stats, 0, sizeof(mbed_stats_stack_t));
|
|
|
|
|
2018-05-02 16:35:45 +00:00
|
|
|
#if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
|
2017-05-15 14:55:45 +00:00
|
|
|
uint32_t thread_n = osThreadGetCount();
|
|
|
|
unsigned i;
|
|
|
|
osThreadId_t *threads;
|
2016-10-26 22:06:08 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
threads = malloc(sizeof(osThreadId_t) * thread_n);
|
|
|
|
MBED_ASSERT(threads != NULL);
|
2016-10-26 22:06:08 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
osKernelLock();
|
|
|
|
thread_n = osThreadEnumerate(threads, thread_n);
|
2016-10-26 22:06:08 +00:00
|
|
|
|
2018-05-03 16:50:01 +00:00
|
|
|
for (i = 0; i < thread_n; i++) {
|
2017-05-15 14:55:45 +00:00
|
|
|
uint32_t stack_size = osThreadGetStackSize(threads[i]);
|
|
|
|
stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
|
|
|
|
stats->reserved_size += stack_size;
|
|
|
|
stats->stack_cnt++;
|
2016-10-26 22:06:08 +00:00
|
|
|
}
|
2017-05-15 14:55:45 +00:00
|
|
|
osKernelUnlock();
|
|
|
|
|
|
|
|
free(threads);
|
2016-10-26 22:06:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:30:45 +00:00
|
|
|
size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
|
|
|
|
{
|
2018-05-03 16:23:12 +00:00
|
|
|
MBED_ASSERT(stats != NULL);
|
2018-05-03 16:50:01 +00:00
|
|
|
memset(stats, 0, count * sizeof(mbed_stats_stack_t));
|
|
|
|
|
2016-10-27 15:30:45 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
2018-05-02 16:35:45 +00:00
|
|
|
#if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
|
2017-05-15 14:55:45 +00:00
|
|
|
osThreadId_t *threads;
|
2016-10-27 15:30:45 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
threads = malloc(sizeof(osThreadId_t) * count);
|
|
|
|
MBED_ASSERT(threads != NULL);
|
2016-10-27 15:30:45 +00:00
|
|
|
|
2017-05-15 14:55:45 +00:00
|
|
|
osKernelLock();
|
|
|
|
count = osThreadEnumerate(threads, count);
|
2016-10-27 15:30:45 +00:00
|
|
|
|
2018-05-03 16:50:01 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2017-05-15 14:55:45 +00:00
|
|
|
uint32_t stack_size = osThreadGetStackSize(threads[i]);
|
|
|
|
stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
|
|
|
|
stats[i].reserved_size = stack_size;
|
|
|
|
stats[i].thread_id = (uint32_t)threads[i];
|
2016-10-27 15:30:45 +00:00
|
|
|
stats[i].stack_cnt = 1;
|
|
|
|
}
|
2017-05-15 14:55:45 +00:00
|
|
|
osKernelUnlock();
|
|
|
|
|
|
|
|
free(threads);
|
2016-10-27 15:30:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:43:10 +00:00
|
|
|
size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
|
|
|
|
{
|
|
|
|
MBED_ASSERT(stats != NULL);
|
2018-05-03 15:52:46 +00:00
|
|
|
memset(stats, 0, count * sizeof(mbed_stats_thread_t));
|
2018-05-01 18:43:10 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
2018-05-03 15:52:46 +00:00
|
|
|
#if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
|
2018-05-01 18:43:10 +00:00
|
|
|
osThreadId_t *threads;
|
|
|
|
|
|
|
|
threads = malloc(sizeof(osThreadId_t) * count);
|
|
|
|
MBED_ASSERT(threads != NULL);
|
|
|
|
|
|
|
|
osKernelLock();
|
|
|
|
count = osThreadEnumerate(threads, count);
|
|
|
|
|
2018-05-03 16:50:01 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2018-05-03 15:52:46 +00:00
|
|
|
stats[i].id = (uint32_t)threads[i];
|
|
|
|
stats[i].state = (uint32_t)osThreadGetState(threads[i]);
|
|
|
|
stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
|
|
|
|
stats[i].stack_size = osThreadGetStackSize(threads[i]);
|
|
|
|
stats[i].stack_space = osThreadGetStackSpace(threads[i]);
|
|
|
|
stats[i].name = osThreadGetName(threads[i]);
|
2018-05-01 18:43:10 +00:00
|
|
|
}
|
|
|
|
osKernelUnlock();
|
|
|
|
free(threads);
|
|
|
|
#endif
|
|
|
|
return i;
|
|
|
|
}
|
2018-05-03 21:10:48 +00:00
|
|
|
|
|
|
|
void mbed_stats_sys_get(mbed_stats_sys_t *stats)
|
|
|
|
{
|
|
|
|
MBED_ASSERT(stats != NULL);
|
|
|
|
memset(stats, 0, sizeof(mbed_stats_sys_t));
|
|
|
|
|
|
|
|
#if defined(MBED_SYS_STATS_ENABLED)
|
|
|
|
|
|
|
|
stats->cpu_id = SCB->CPUID;
|
|
|
|
|
|
|
|
#if defined(__IAR_SYSTEMS_ICC__)
|
|
|
|
stats->compiler_id = IAR;
|
|
|
|
stats->compiler_version = __VER__;
|
|
|
|
#elif defined(__CC_ARM)
|
|
|
|
stats->compiler_id = ARM;
|
|
|
|
stats->compiler_version = __ARMCC_VERSION;
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
stats->compiler_id = GCC_ARM;
|
|
|
|
stats->compiler_version = (__GNUC__ * 10000 + __GNUC_MINOR__ * 100);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|