trace.c 5.0 KB

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