inport from local
Dependents: Hobbyking_Cheetah_0511
platform/mbed_stats.c@0:85b3fd62ea1a, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:35:48 2020 +0000
- Revision:
- 0:85b3fd62ea1a
reinport to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:85b3fd62ea1a | 1 | #include "mbed_stats.h" |
NYX | 0:85b3fd62ea1a | 2 | #include <string.h> |
NYX | 0:85b3fd62ea1a | 3 | #include <stdlib.h> |
NYX | 0:85b3fd62ea1a | 4 | #include "mbed_assert.h" |
NYX | 0:85b3fd62ea1a | 5 | |
NYX | 0:85b3fd62ea1a | 6 | #if MBED_CONF_RTOS_PRESENT |
NYX | 0:85b3fd62ea1a | 7 | #include "cmsis_os2.h" |
NYX | 0:85b3fd62ea1a | 8 | #endif |
NYX | 0:85b3fd62ea1a | 9 | |
NYX | 0:85b3fd62ea1a | 10 | // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp |
NYX | 0:85b3fd62ea1a | 11 | |
NYX | 0:85b3fd62ea1a | 12 | void mbed_stats_stack_get(mbed_stats_stack_t *stats) |
NYX | 0:85b3fd62ea1a | 13 | { |
NYX | 0:85b3fd62ea1a | 14 | memset(stats, 0, sizeof(mbed_stats_stack_t)); |
NYX | 0:85b3fd62ea1a | 15 | |
NYX | 0:85b3fd62ea1a | 16 | #if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT |
NYX | 0:85b3fd62ea1a | 17 | uint32_t thread_n = osThreadGetCount(); |
NYX | 0:85b3fd62ea1a | 18 | unsigned i; |
NYX | 0:85b3fd62ea1a | 19 | osThreadId_t *threads; |
NYX | 0:85b3fd62ea1a | 20 | |
NYX | 0:85b3fd62ea1a | 21 | threads = malloc(sizeof(osThreadId_t) * thread_n); |
NYX | 0:85b3fd62ea1a | 22 | MBED_ASSERT(threads != NULL); |
NYX | 0:85b3fd62ea1a | 23 | |
NYX | 0:85b3fd62ea1a | 24 | osKernelLock(); |
NYX | 0:85b3fd62ea1a | 25 | thread_n = osThreadEnumerate(threads, thread_n); |
NYX | 0:85b3fd62ea1a | 26 | |
NYX | 0:85b3fd62ea1a | 27 | for(i = 0; i < thread_n; i++) { |
NYX | 0:85b3fd62ea1a | 28 | uint32_t stack_size = osThreadGetStackSize(threads[i]); |
NYX | 0:85b3fd62ea1a | 29 | stats->max_size += stack_size - osThreadGetStackSpace(threads[i]); |
NYX | 0:85b3fd62ea1a | 30 | stats->reserved_size += stack_size; |
NYX | 0:85b3fd62ea1a | 31 | stats->stack_cnt++; |
NYX | 0:85b3fd62ea1a | 32 | } |
NYX | 0:85b3fd62ea1a | 33 | osKernelUnlock(); |
NYX | 0:85b3fd62ea1a | 34 | |
NYX | 0:85b3fd62ea1a | 35 | free(threads); |
NYX | 0:85b3fd62ea1a | 36 | #endif |
NYX | 0:85b3fd62ea1a | 37 | } |
NYX | 0:85b3fd62ea1a | 38 | |
NYX | 0:85b3fd62ea1a | 39 | size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count) |
NYX | 0:85b3fd62ea1a | 40 | { |
NYX | 0:85b3fd62ea1a | 41 | memset(stats, 0, count*sizeof(mbed_stats_stack_t)); |
NYX | 0:85b3fd62ea1a | 42 | size_t i = 0; |
NYX | 0:85b3fd62ea1a | 43 | |
NYX | 0:85b3fd62ea1a | 44 | #if MBED_STACK_STATS_ENABLED && MBED_CONF_RTOS_PRESENT |
NYX | 0:85b3fd62ea1a | 45 | osThreadId_t *threads; |
NYX | 0:85b3fd62ea1a | 46 | |
NYX | 0:85b3fd62ea1a | 47 | threads = malloc(sizeof(osThreadId_t) * count); |
NYX | 0:85b3fd62ea1a | 48 | MBED_ASSERT(threads != NULL); |
NYX | 0:85b3fd62ea1a | 49 | |
NYX | 0:85b3fd62ea1a | 50 | osKernelLock(); |
NYX | 0:85b3fd62ea1a | 51 | count = osThreadEnumerate(threads, count); |
NYX | 0:85b3fd62ea1a | 52 | |
NYX | 0:85b3fd62ea1a | 53 | for(i = 0; i < count; i++) { |
NYX | 0:85b3fd62ea1a | 54 | uint32_t stack_size = osThreadGetStackSize(threads[i]); |
NYX | 0:85b3fd62ea1a | 55 | stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]); |
NYX | 0:85b3fd62ea1a | 56 | stats[i].reserved_size = stack_size; |
NYX | 0:85b3fd62ea1a | 57 | stats[i].thread_id = (uint32_t)threads[i]; |
NYX | 0:85b3fd62ea1a | 58 | stats[i].stack_cnt = 1; |
NYX | 0:85b3fd62ea1a | 59 | } |
NYX | 0:85b3fd62ea1a | 60 | osKernelUnlock(); |
NYX | 0:85b3fd62ea1a | 61 | |
NYX | 0:85b3fd62ea1a | 62 | free(threads); |
NYX | 0:85b3fd62ea1a | 63 | #endif |
NYX | 0:85b3fd62ea1a | 64 | |
NYX | 0:85b3fd62ea1a | 65 | return i; |
NYX | 0:85b3fd62ea1a | 66 | } |
NYX | 0:85b3fd62ea1a | 67 | |
NYX | 0:85b3fd62ea1a | 68 | #if MBED_STACK_STATS_ENABLED && !MBED_CONF_RTOS_PRESENT |
NYX | 0:85b3fd62ea1a | 69 | #warning Stack statistics are currently not supported without the rtos. |
NYX | 0:85b3fd62ea1a | 70 | #endif |