2018-05-03 21:10:48 +00:00
|
|
|
#include "mbed_assert.h"
|
2016-10-26 22:06:08 +00:00
|
|
|
#include "mbed_stats.h"
|
2018-05-07 21:10:06 +00:00
|
|
|
#include "mbed_power_mgmt.h"
|
2018-07-06 16:29:56 +00:00
|
|
|
#include "mbed_version.h"
|
2016-10-26 22:06:08 +00:00
|
|
|
#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-07 21:10:06 +00:00
|
|
|
#include "rtos_idle.h"
|
|
|
|
#elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED) || defined(MBED_CPU_STATS_ENABLED)
|
2018-05-03 16:50:01 +00:00
|
|
|
#warning Statistics are currently not supported without the rtos.
|
2016-10-26 22:06:08 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-19 23:16:42 +00:00
|
|
|
#if defined(MBED_CPU_STATS_ENABLED) && (!DEVICE_LPTICKER || !DEVICE_SLEEP)
|
2018-05-07 21:10:06 +00:00
|
|
|
#warning CPU statistics are not supported without low power timer support.
|
|
|
|
#endif
|
2016-10-26 22:06:08 +00:00
|
|
|
|
2018-05-07 21:10:06 +00:00
|
|
|
void mbed_stats_cpu_get(mbed_stats_cpu_t *stats)
|
|
|
|
{
|
|
|
|
MBED_ASSERT(stats != NULL);
|
|
|
|
memset(stats, 0, sizeof(mbed_stats_cpu_t));
|
2018-12-19 23:16:42 +00:00
|
|
|
#if defined(MBED_CPU_STATS_ENABLED) && DEVICE_LPTICKER && DEVICE_SLEEP
|
2018-05-07 21:10:06 +00:00
|
|
|
stats->uptime = mbed_uptime();
|
|
|
|
stats->idle_time = mbed_time_idle();
|
|
|
|
stats->sleep_time = mbed_time_sleep();
|
|
|
|
stats->deep_sleep_time = mbed_time_deepsleep();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
|
2016-10-26 22:06:08 +00:00
|
|
|
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);
|
2018-08-08 13:18:29 +00:00
|
|
|
// Don't fail on lack of memory
|
|
|
|
if (!threads) {
|
|
|
|
return;
|
|
|
|
}
|
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);
|
2018-08-08 13:18:29 +00:00
|
|
|
// Don't fail on lack of memory
|
|
|
|
if (!threads) {
|
|
|
|
return 0;
|
|
|
|
}
|
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)
|
2018-07-06 16:29:56 +00:00
|
|
|
stats->os_version = MBED_VERSION;
|
2018-11-14 18:50:26 +00:00
|
|
|
#if defined(MBED_RAM_START) && defined(MBED_RAM_SIZE)
|
2018-11-05 20:39:37 +00:00
|
|
|
stats->ram_start[0] = MBED_RAM_START;
|
|
|
|
stats->ram_size[0] = MBED_RAM_SIZE;
|
2018-11-14 18:50:26 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBED_ROM_START) && defined(MBED_ROM_SIZE)
|
2018-11-05 20:39:37 +00:00
|
|
|
stats->rom_start[0] = MBED_ROM_START;
|
|
|
|
stats->rom_size[0] = MBED_ROM_SIZE;
|
2018-11-14 18:50:26 +00:00
|
|
|
#endif
|
2018-11-05 20:39:37 +00:00
|
|
|
#if defined(MBED_RAM1_START) && defined(MBED_RAM1_SIZE)
|
|
|
|
stats->ram_start[1] = MBED_RAM1_START;
|
|
|
|
stats->ram_size[1] = MBED_RAM1_SIZE;
|
|
|
|
#endif
|
|
|
|
#if defined(MBED_RAM2_START) && defined(MBED_RAM2_SIZE)
|
|
|
|
stats->ram_start[2] = MBED_RAM2_START;
|
|
|
|
stats->ram_size[2] = MBED_RAM2_SIZE;
|
|
|
|
#endif
|
|
|
|
#if defined(MBED_RAM3_START) && defined(MBED_RAM3_SIZE)
|
|
|
|
stats->ram_start[3] = MBED_RAM3_START;
|
|
|
|
stats->ram_size[3] = MBED_RAM3_SIZE;
|
|
|
|
#endif
|
|
|
|
#if defined(MBED_ROM1_START) && defined(MBED_ROM1_SIZE)
|
|
|
|
stats->rom_start[1] = MBED_ROM1_START;
|
|
|
|
stats->rom_size[1] = MBED_ROM1_SIZE;
|
|
|
|
#endif
|
|
|
|
#if defined(MBED_ROM2_START) && defined(MBED_ROM2_SIZE)
|
|
|
|
stats->rom_start[2] = MBED_ROM2_START;
|
|
|
|
stats->rom_size[2] = MBED_ROM2_SIZE;
|
|
|
|
#endif
|
|
|
|
#if defined(MBED_ROM3_START) && defined(MBED_ROM3_SIZE)
|
|
|
|
stats->rom_start[3] = MBED_ROM3_START;
|
|
|
|
stats->rom_size[3] = MBED_ROM3_SIZE;
|
|
|
|
#endif
|
|
|
|
|
2018-05-15 15:04:48 +00:00
|
|
|
#if defined(__CORTEX_M)
|
2018-05-03 21:10:48 +00:00
|
|
|
stats->cpu_id = SCB->CPUID;
|
2018-05-15 15:04:48 +00:00
|
|
|
#endif
|
2018-05-03 21:10:48 +00:00
|
|
|
#if defined(__IAR_SYSTEMS_ICC__)
|
|
|
|
stats->compiler_id = IAR;
|
|
|
|
stats->compiler_version = __VER__;
|
2019-02-21 23:12:09 +00:00
|
|
|
#elif defined(__ARMCC_VERSION)
|
2018-05-03 21:10:48 +00:00
|
|
|
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;
|
|
|
|
}
|