mbed_stats.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "mbed_assert.h"
  2. #include "mbed_stats.h"
  3. #include "mbed_power_mgmt.h"
  4. #include "mbed_version.h"
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include "device.h"
  8. #ifdef MBED_CONF_RTOS_PRESENT
  9. #include "cmsis_os2.h"
  10. #include "rtos_idle.h"
  11. #elif defined(MBED_STACK_STATS_ENABLED) || defined(MBED_THREAD_STATS_ENABLED) || defined(MBED_CPU_STATS_ENABLED)
  12. #warning Statistics are currently not supported without the rtos.
  13. #endif
  14. #if defined(MBED_CPU_STATS_ENABLED) && (!defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP))
  15. #warning CPU statistics are not supported without low power timer support.
  16. #endif
  17. void mbed_stats_cpu_get(mbed_stats_cpu_t *stats)
  18. {
  19. MBED_ASSERT(stats != NULL);
  20. memset(stats, 0, sizeof(mbed_stats_cpu_t));
  21. #if defined(MBED_CPU_STATS_ENABLED) && defined(DEVICE_LPTICKER) && defined(DEVICE_SLEEP)
  22. stats->uptime = mbed_uptime();
  23. stats->idle_time = mbed_time_idle();
  24. stats->sleep_time = mbed_time_sleep();
  25. stats->deep_sleep_time = mbed_time_deepsleep();
  26. #endif
  27. }
  28. // note: mbed_stats_heap_get defined in mbed_alloc_wrappers.cpp
  29. void mbed_stats_stack_get(mbed_stats_stack_t *stats)
  30. {
  31. MBED_ASSERT(stats != NULL);
  32. memset(stats, 0, sizeof(mbed_stats_stack_t));
  33. #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
  34. uint32_t thread_n = osThreadGetCount();
  35. unsigned i;
  36. osThreadId_t *threads;
  37. threads = malloc(sizeof(osThreadId_t) * thread_n);
  38. MBED_ASSERT(threads != NULL);
  39. osKernelLock();
  40. thread_n = osThreadEnumerate(threads, thread_n);
  41. for (i = 0; i < thread_n; i++) {
  42. uint32_t stack_size = osThreadGetStackSize(threads[i]);
  43. stats->max_size += stack_size - osThreadGetStackSpace(threads[i]);
  44. stats->reserved_size += stack_size;
  45. stats->stack_cnt++;
  46. }
  47. osKernelUnlock();
  48. free(threads);
  49. #endif
  50. }
  51. size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
  52. {
  53. MBED_ASSERT(stats != NULL);
  54. memset(stats, 0, count * sizeof(mbed_stats_stack_t));
  55. size_t i = 0;
  56. #if defined(MBED_STACK_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
  57. osThreadId_t *threads;
  58. threads = malloc(sizeof(osThreadId_t) * count);
  59. MBED_ASSERT(threads != NULL);
  60. osKernelLock();
  61. count = osThreadEnumerate(threads, count);
  62. for (i = 0; i < count; i++) {
  63. uint32_t stack_size = osThreadGetStackSize(threads[i]);
  64. stats[i].max_size = stack_size - osThreadGetStackSpace(threads[i]);
  65. stats[i].reserved_size = stack_size;
  66. stats[i].thread_id = (uint32_t)threads[i];
  67. stats[i].stack_cnt = 1;
  68. }
  69. osKernelUnlock();
  70. free(threads);
  71. #endif
  72. return i;
  73. }
  74. size_t mbed_stats_thread_get_each(mbed_stats_thread_t *stats, size_t count)
  75. {
  76. MBED_ASSERT(stats != NULL);
  77. memset(stats, 0, count * sizeof(mbed_stats_thread_t));
  78. size_t i = 0;
  79. #if defined(MBED_THREAD_STATS_ENABLED) && defined(MBED_CONF_RTOS_PRESENT)
  80. osThreadId_t *threads;
  81. threads = malloc(sizeof(osThreadId_t) * count);
  82. MBED_ASSERT(threads != NULL);
  83. osKernelLock();
  84. count = osThreadEnumerate(threads, count);
  85. for (i = 0; i < count; i++) {
  86. stats[i].id = (uint32_t)threads[i];
  87. stats[i].state = (uint32_t)osThreadGetState(threads[i]);
  88. stats[i].priority = (uint32_t)osThreadGetPriority(threads[i]);
  89. stats[i].stack_size = osThreadGetStackSize(threads[i]);
  90. stats[i].stack_space = osThreadGetStackSpace(threads[i]);
  91. stats[i].name = osThreadGetName(threads[i]);
  92. }
  93. osKernelUnlock();
  94. free(threads);
  95. #endif
  96. return i;
  97. }
  98. void mbed_stats_sys_get(mbed_stats_sys_t *stats)
  99. {
  100. MBED_ASSERT(stats != NULL);
  101. memset(stats, 0, sizeof(mbed_stats_sys_t));
  102. #if defined(MBED_SYS_STATS_ENABLED)
  103. stats->os_version = MBED_VERSION;
  104. #if defined(__CORTEX_M)
  105. stats->cpu_id = SCB->CPUID;
  106. #endif
  107. #if defined(__IAR_SYSTEMS_ICC__)
  108. stats->compiler_id = IAR;
  109. stats->compiler_version = __VER__;
  110. #elif defined(__CC_ARM)
  111. stats->compiler_id = ARM;
  112. stats->compiler_version = __ARMCC_VERSION;
  113. #elif defined(__GNUC__)
  114. stats->compiler_id = GCC_ARM;
  115. stats->compiler_version = (__GNUC__ * 10000 + __GNUC_MINOR__ * 100);
  116. #endif
  117. #endif
  118. return;
  119. }