heap_trace.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "heap_trace.h"
  18. #include <string.h>
  19. #include <core/CHIPError.h>
  20. #include <lib/shell/Engine.h>
  21. #include <lib/shell/commands/Help.h>
  22. #include <lib/shell/streamer.h>
  23. #include <lib/support/CodeUtils.h>
  24. #include "esp_err.h"
  25. #include "esp_heap_caps.h"
  26. #include "esp_heap_task_info.h"
  27. #include "esp_heap_trace.h"
  28. using chip::Shell::Engine;
  29. using chip::Shell::PrintCommandHelp;
  30. using chip::Shell::shell_command_t;
  31. using chip::Shell::streamer_get;
  32. using chip::Shell::streamer_printf;
  33. namespace {
  34. constexpr size_t kNumHeapTraceRecords = 100;
  35. constexpr size_t kNumHeapTasks = 20;
  36. constexpr size_t kNumHeapBlocks = 20;
  37. heap_trace_record_t sTraceRecords[kNumHeapTraceRecords];
  38. Engine sShellHeapSubCommands;
  39. CHIP_ERROR HeapTraceHelpHandler(int argc, char ** argv)
  40. {
  41. sShellHeapSubCommands.ForEachCommand(PrintCommandHelp, nullptr);
  42. return CHIP_NO_ERROR;
  43. }
  44. #if CONFIG_HEAP_TRACING_STANDALONE
  45. CHIP_ERROR HeapTraceResetHandler(int argc, char ** argv)
  46. {
  47. ESP_ERROR_CHECK(heap_trace_stop());
  48. ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));
  49. return CHIP_NO_ERROR;
  50. }
  51. CHIP_ERROR HeapTraceDumpHandler(int argc, char ** argv)
  52. {
  53. heap_trace_dump();
  54. streamer_printf(streamer_get(), "Free heap %d/%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT),
  55. heap_caps_get_total_size(MALLOC_CAP_8BIT));
  56. return CHIP_NO_ERROR;
  57. }
  58. #endif // CONFIG_HEAP_TRACING_STANDALONE
  59. #if CONFIG_HEAP_TASK_TRACKING
  60. CHIP_ERROR HeapTraceTaskHandler(int argc, char ** argv)
  61. {
  62. // static storage is required for task memory info;
  63. static size_t numTotals = 0;
  64. static heap_task_totals_t sTotals[kNumHeapTasks];
  65. static heap_task_block_t sBlocks[kNumHeapBlocks];
  66. heap_task_info_params_t heap_info;
  67. memset(&heap_info, 0, sizeof(heap_info));
  68. heap_info.caps[0] = MALLOC_CAP_8BIT; // Gets heap with CAP_8BIT capabilities
  69. heap_info.mask[0] = MALLOC_CAP_8BIT;
  70. heap_info.caps[1] = MALLOC_CAP_32BIT; // Gets heap info with CAP_32BIT capabilities
  71. heap_info.mask[1] = MALLOC_CAP_32BIT;
  72. heap_info.tasks = NULL; // Passing NULL captures heap info for all tasks
  73. heap_info.num_tasks = 0;
  74. heap_info.totals = sTotals; // Gets task wise allocation details
  75. heap_info.num_totals = &numTotals;
  76. heap_info.max_totals = kNumHeapTasks; // Maximum length of "sTotals"
  77. heap_info.blocks = sBlocks; // Gets block wise allocation details. For each block, gets owner task, address and size
  78. heap_info.max_blocks = kNumHeapBlocks; // Maximum length of "sBlocks"
  79. heap_caps_get_per_task_info(&heap_info);
  80. for (int i = 0; i < *heap_info.num_totals; i++)
  81. {
  82. streamer_printf(streamer_get(), "Task: %s -> CAP_8BIT: %zu CAP_32BIT: %zu\n",
  83. heap_info.totals[i].task ? pcTaskGetTaskName(heap_info.totals[i].task) : "Pre-Scheduler allocs",
  84. heap_info.totals[i].size[0], // Heap size with CAP_8BIT capabilities
  85. heap_info.totals[i].size[1]); // Heap size with CAP32_BIT capabilities
  86. }
  87. streamer_printf(streamer_get(), "Free heap %d/%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT),
  88. heap_caps_get_total_size(MALLOC_CAP_8BIT));
  89. return CHIP_NO_ERROR;
  90. }
  91. #endif
  92. CHIP_ERROR HeapTraceDispatch(int argc, char ** argv)
  93. {
  94. if (argc == 0)
  95. {
  96. HeapTraceHelpHandler(argc, argv);
  97. return CHIP_NO_ERROR;
  98. }
  99. return sShellHeapSubCommands.ExecCommand(argc, argv);
  100. }
  101. } // namespace
  102. namespace idf {
  103. namespace chip {
  104. void RegisterHeapTraceCommands()
  105. {
  106. static const shell_command_t sHeapSubCommands[] = {
  107. { &HeapTraceHelpHandler, "help", "Usage: heap-trace <subcommand>" },
  108. #if CONFIG_HEAP_TRACING_STANDALONE
  109. { &HeapTraceResetHandler, "reset", "Reset the heap trace baseline" },
  110. { &HeapTraceDumpHandler, "dump", "Dump the last collected heap trace" },
  111. #endif // CONFIG_HEAP_TRACING_STANDALONE
  112. #if CONFIG_HEAP_TASK_TRACKING
  113. { &HeapTraceTaskHandler, "task", "Dump heap usage of each task" },
  114. #endif // CONFIG_HEAP_TASK_TRACKING
  115. };
  116. sShellHeapSubCommands.RegisterCommands(sHeapSubCommands, ArraySize(sHeapSubCommands));
  117. #if CONFIG_HEAP_TRACING_STANDALONE
  118. ESP_ERROR_CHECK(heap_trace_init_standalone(sTraceRecords, kNumHeapTraceRecords));
  119. ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));
  120. #endif // CONFIG_HEAP_TRACING_STANDALONE
  121. static const shell_command_t sHeapCommand = { &HeapTraceDispatch, "heap-trace", "Heap debug tracing" };
  122. Engine::Root().RegisterCommands(&sHeapCommand, 1);
  123. }
  124. } // namespace chip
  125. } // namespace idf