trace.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <stdint.h>
  2. #include "esp_system.h"
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include "esp_log.h"
  6. #include "freertos/xtensa_api.h"
  7. #include "freertos/FreeRTOSConfig.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/queue.h"
  10. #include "freertos/task.h"
  11. #include "esp_event.h"
  12. #include "trace.h"
  13. static const char TAG[] = "TRACE";
  14. // typedef struct mem_usage_trace_for_thread {
  15. // TaskHandle_t task;
  16. // size_t malloc_int_last;
  17. // size_t malloc_spiram_last;
  18. // size_t malloc_dma_last;
  19. // const char *name;
  20. // SLIST_ENTRY(mem_usage_trace_for_thread) next;
  21. // } mem_usage_trace_for_thread_t;
  22. // static EXT_RAM_ATTR SLIST_HEAD(memtrace, mem_usage_trace_for_thread) s_memtrace;
  23. // mem_usage_trace_for_thread_t* memtrace_get_thread_entry(TaskHandle_t task) {
  24. // if(!task) {
  25. // ESP_LOGE(TAG, "memtrace_get_thread_entry: task is NULL");
  26. // return NULL;
  27. // }
  28. // ESP_LOGD(TAG,"Looking for task %s",STR_OR_ALT(pcTaskGetName(task ), "unknown"));
  29. // mem_usage_trace_for_thread_t* it;
  30. // SLIST_FOREACH(it, &s_memtrace, next) {
  31. // if ( it->task == task ) {
  32. // ESP_LOGD(TAG,"Found task %s",STR_OR_ALT(pcTaskGetName(task ), "unknown"));
  33. // return it;
  34. // }
  35. // }
  36. // return NULL;
  37. // }
  38. // void memtrace_add_thread_entry(TaskHandle_t task) {
  39. // if(!task) {
  40. // ESP_LOGE(TAG, "memtrace_get_thread_entry: task is NULL");
  41. // return ;
  42. // }
  43. // mem_usage_trace_for_thread_t* it = memtrace_get_thread_entry(task);
  44. // if (it) {
  45. // ESP_LOGW(TAG, "memtrace_add_thread_entry: thread already in list");
  46. // return;
  47. // }
  48. // it = (mem_usage_trace_for_thread_t*)malloc_init_external(sizeof(mem_usage_trace_for_thread_t));
  49. // if (!it) {
  50. // ESP_LOGE(TAG, "memtrace_add_thread_entry: malloc failed");
  51. // return;
  52. // }
  53. // it->task = task;
  54. // it->malloc_int_last = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  55. // it->malloc_spiram_last = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  56. // it->malloc_dma_last = heap_caps_get_free_size(MALLOC_CAP_DMA);
  57. // it->name = pcTaskGetName(task);
  58. // SLIST_INSERT_HEAD(&s_memtrace, it, next);
  59. // return;
  60. // }
  61. // void memtrace_print_delta(){
  62. // TaskHandle_t task = xTaskGetCurrentTaskHandle();
  63. // mem_usage_trace_for_thread_t* it = memtrace_get_thread_entry(task);
  64. // if (!it) {
  65. // memtrace_add_thread_entry(task);
  66. // ESP_LOGW(TAG, "memtrace_print_delta: added new entry for task %s",STR_OR_ALT(pcTaskGetName(task ), "unknown"));
  67. // return;
  68. // }
  69. // size_t malloc_int_delta = heap_caps_get_free_size(MALLOC_CAP_INTERNAL) - it->malloc_int_last;
  70. // size_t malloc_spiram_delta = heap_caps_get_free_size(MALLOC_CAP_SPIRAM) - it->malloc_spiram_last;
  71. // size_t malloc_dma_delta = heap_caps_get_free_size(MALLOC_CAP_DMA) - it->malloc_dma_last;
  72. // ESP_LOG(TAG, "Heap internal:%zu (min:%zu) external:%zu (min:%zu) dma:%zu (min:%zu)",
  73. // heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  74. // heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  75. // heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  76. // heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  77. // heap_caps_get_free_size(MALLOC_CAP_DMA),
  78. // heap_caps_get_minimum_free_size(MALLOC_CAP_DMA));
  79. // ESP_LOGW(TAG, "memtrace_print_delta: %s: malloc_int_delta=%d, malloc_spiram_delta=%d, malloc_dma_delta=%d",
  80. // STR_OR_ALT(it->name, "unknown"),
  81. // malloc_int_delta,
  82. // malloc_spiram_delta,
  83. // malloc_dma_delta);
  84. // it->malloc_int_last = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  85. // it->malloc_spiram_last = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  86. // it->malloc_dma_last = heap_caps_get_free_size(MALLOC_CAP_DMA);
  87. // }
  88. size_t malloc_int = 0;
  89. size_t malloc_spiram =0;
  90. size_t malloc_dma = 0;
  91. void memtrace_print_delta(const char * msg, const char * tag, const char * function){
  92. size_t malloc_int_delta = heap_caps_get_free_size(MALLOC_CAP_INTERNAL) - malloc_int;
  93. size_t malloc_spiram_delta = heap_caps_get_free_size(MALLOC_CAP_SPIRAM) - malloc_spiram;
  94. size_t malloc_dma_delta = heap_caps_get_free_size(MALLOC_CAP_DMA) - malloc_dma;
  95. ESP_LOGW(TAG, "Heap internal:%zu (min:%zu)(chg:%d)/external:%zu (min:%zu)(chg:%d)/dma:%zu (min:%zu)(chg:%d) : %s%s%s%s%s",
  96. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  97. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  98. malloc_int_delta,
  99. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  100. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  101. malloc_spiram_delta,
  102. heap_caps_get_free_size(MALLOC_CAP_DMA),
  103. heap_caps_get_minimum_free_size(MALLOC_CAP_DMA),
  104. malloc_dma_delta,
  105. STR_OR_BLANK(tag),
  106. tag?" ":"",
  107. STR_OR_BLANK(function),
  108. function?" ":"",
  109. STR_OR_BLANK(msg)
  110. );
  111. malloc_int = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  112. malloc_spiram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  113. malloc_dma = heap_caps_get_free_size(MALLOC_CAP_DMA);
  114. }