Daniel Vizcaya / Mbed OS 04_RTOS_Embebidos
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_stats.c Source File

mbed_stats.c

00001 #include "mbed_stats.h"
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include "mbed_assert.h"
00005 
00006 #ifdef MBED_CONF_RTOS_PRESENT
00007 #include "cmsis_os2.h"
00008 #endif
00009 
00010 // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
00011 
00012 void mbed_stats_stack_get(mbed_stats_stack_t *stats)
00013 {
00014     MBED_ASSERT(stats != NULL);
00015     memset(stats, 0, sizeof(mbed_stats_stack_t));
00016 
00017 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00018     uint32_t thread_n = osThreadGetCount();
00019     unsigned i;
00020     osThreadId_t *threads;
00021 
00022     threads = malloc(sizeof(osThreadId_t) * thread_n);
00023     MBED_ASSERT(threads != NULL);
00024 
00025     osKernelLock();
00026     thread_n = osThreadEnumerate(threads, thread_n);
00027 
00028     for(i = 0; i < thread_n; i++) {
00029         uint32_t stack_size = osThreadGetStackSize(threads[i]);
00030         stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
00031         stats->reserved_size += stack_size;
00032         stats->stack_cnt++;
00033     }
00034     osKernelUnlock();
00035 
00036     free(threads);
00037 #endif
00038 }
00039 
00040 size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
00041 {
00042     MBED_ASSERT(stats != NULL);
00043     memset(stats, 0, count*sizeof(mbed_stats_stack_t));
00044     size_t i = 0;
00045 
00046 #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
00047     osThreadId_t *threads;
00048 
00049     threads = malloc(sizeof(osThreadId_t) * count);
00050     MBED_ASSERT(threads != NULL);
00051 
00052     osKernelLock();
00053     count = osThreadEnumerate(threads, count);
00054 
00055     for(i = 0; i < count; i++) {
00056         uint32_t stack_size = osThreadGetStackSize(threads[i]);
00057         stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
00058         stats[i].reserved_size = stack_size;
00059         stats[i].thread_id = (uint32_t)threads[i];
00060         stats[i].stack_cnt = 1;
00061     }
00062     osKernelUnlock();
00063 
00064     free(threads);
00065 #endif
00066 
00067     return i;
00068 }
00069 
00070 #if defined(MBED_STACK_STATS_ENABLED) && !defined(MBED_CONF_RTOS_PRESENT)
00071 #warning Stack statistics are currently not supported without the rtos.
00072 #endif