mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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