Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 #include "mbed_stats.h"
marcozecchini 0:9fca2b23d0ba 2 #include <string.h>
marcozecchini 0:9fca2b23d0ba 3 #include <stdlib.h>
marcozecchini 0:9fca2b23d0ba 4 #include "mbed_assert.h"
marcozecchini 0:9fca2b23d0ba 5
marcozecchini 0:9fca2b23d0ba 6 #if MBED_CONF_RTOS_PRESENT
marcozecchini 0:9fca2b23d0ba 7 #include "cmsis_os2.h"
marcozecchini 0:9fca2b23d0ba 8 #endif
marcozecchini 0:9fca2b23d0ba 9
marcozecchini 0:9fca2b23d0ba 10 // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
marcozecchini 0:9fca2b23d0ba 11
marcozecchini 0:9fca2b23d0ba 12 void mbed_stats_stack_get(mbed_stats_stack_t *stats)
marcozecchini 0:9fca2b23d0ba 13 {
marcozecchini 0:9fca2b23d0ba 14 memset(stats, 0, sizeof(mbed_stats_stack_t));
marcozecchini 0:9fca2b23d0ba 15
marcozecchini 0:9fca2b23d0ba 16 #if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
marcozecchini 0:9fca2b23d0ba 17 uint32_t thread_n = osThreadGetCount();
marcozecchini 0:9fca2b23d0ba 18 unsigned i;
marcozecchini 0:9fca2b23d0ba 19 osThreadId_t *threads;
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 threads = malloc(sizeof(osThreadId_t) * thread_n);
marcozecchini 0:9fca2b23d0ba 22 MBED_ASSERT(threads != NULL);
marcozecchini 0:9fca2b23d0ba 23
marcozecchini 0:9fca2b23d0ba 24 osKernelLock();
marcozecchini 0:9fca2b23d0ba 25 thread_n = osThreadEnumerate(threads, thread_n);
marcozecchini 0:9fca2b23d0ba 26
marcozecchini 0:9fca2b23d0ba 27 for(i = 0; i < thread_n; i++) {
marcozecchini 0:9fca2b23d0ba 28 uint32_t stack_size = osThreadGetStackSize(threads[i]);
marcozecchini 0:9fca2b23d0ba 29 stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
marcozecchini 0:9fca2b23d0ba 30 stats->reserved_size += stack_size;
marcozecchini 0:9fca2b23d0ba 31 stats->stack_cnt++;
marcozecchini 0:9fca2b23d0ba 32 }
marcozecchini 0:9fca2b23d0ba 33 osKernelUnlock();
marcozecchini 0:9fca2b23d0ba 34
marcozecchini 0:9fca2b23d0ba 35 free(threads);
marcozecchini 0:9fca2b23d0ba 36 #endif
marcozecchini 0:9fca2b23d0ba 37 }
marcozecchini 0:9fca2b23d0ba 38
marcozecchini 0:9fca2b23d0ba 39 size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
marcozecchini 0:9fca2b23d0ba 40 {
marcozecchini 0:9fca2b23d0ba 41 memset(stats, 0, count*sizeof(mbed_stats_stack_t));
marcozecchini 0:9fca2b23d0ba 42 size_t i = 0;
marcozecchini 0:9fca2b23d0ba 43
marcozecchini 0:9fca2b23d0ba 44 #if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT
marcozecchini 0:9fca2b23d0ba 45 osThreadId_t *threads;
marcozecchini 0:9fca2b23d0ba 46
marcozecchini 0:9fca2b23d0ba 47 threads = malloc(sizeof(osThreadId_t) * count);
marcozecchini 0:9fca2b23d0ba 48 MBED_ASSERT(threads != NULL);
marcozecchini 0:9fca2b23d0ba 49
marcozecchini 0:9fca2b23d0ba 50 osKernelLock();
marcozecchini 0:9fca2b23d0ba 51 count = osThreadEnumerate(threads, count);
marcozecchini 0:9fca2b23d0ba 52
marcozecchini 0:9fca2b23d0ba 53 for(i = 0; i < count; i++) {
marcozecchini 0:9fca2b23d0ba 54 uint32_t stack_size = osThreadGetStackSize(threads[i]);
marcozecchini 0:9fca2b23d0ba 55 stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
marcozecchini 0:9fca2b23d0ba 56 stats[i].reserved_size = stack_size;
marcozecchini 0:9fca2b23d0ba 57 stats[i].thread_id = (uint32_t)threads[i];
marcozecchini 0:9fca2b23d0ba 58 stats[i].stack_cnt = 1;
marcozecchini 0:9fca2b23d0ba 59 }
marcozecchini 0:9fca2b23d0ba 60 osKernelUnlock();
marcozecchini 0:9fca2b23d0ba 61
marcozecchini 0:9fca2b23d0ba 62 free(threads);
marcozecchini 0:9fca2b23d0ba 63 #endif
marcozecchini 0:9fca2b23d0ba 64
marcozecchini 0:9fca2b23d0ba 65 return i;
marcozecchini 0:9fca2b23d0ba 66 }
marcozecchini 0:9fca2b23d0ba 67
marcozecchini 0:9fca2b23d0ba 68 #if MBED_STACK_STATS_ENABLED && !MBED_CONF_RTOS_PRESENT
marcozecchini 0:9fca2b23d0ba 69 #warning Stack statistics are currently not supported without the rtos.
marcozecchini 0:9fca2b23d0ba 70 #endif