heap_task_tracking_main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Heap Task Tracking Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_heap_task_info.h"
  11. #include "esp_log.h"
  12. #include "esp_random.h"
  13. #define MAX_TASK_NUM 20 // Max number of per tasks info that it can store
  14. #define MAX_BLOCK_NUM 20 // Max number of per block info that it can store
  15. static size_t s_prepopulated_num = 0;
  16. static heap_task_totals_t s_totals_arr[MAX_TASK_NUM];
  17. static heap_task_block_t s_block_arr[MAX_BLOCK_NUM];
  18. static void esp_dump_per_task_heap_info(void)
  19. {
  20. heap_task_info_params_t heap_info = {0};
  21. heap_info.caps[0] = MALLOC_CAP_8BIT; // Gets heap with CAP_8BIT capabilities
  22. heap_info.mask[0] = MALLOC_CAP_8BIT;
  23. heap_info.caps[1] = MALLOC_CAP_32BIT; // Gets heap info with CAP_32BIT capabilities
  24. heap_info.mask[1] = MALLOC_CAP_32BIT;
  25. heap_info.tasks = NULL; // Passing NULL captures heap info for all tasks
  26. heap_info.num_tasks = 0;
  27. heap_info.totals = s_totals_arr; // Gets task wise allocation details
  28. heap_info.num_totals = &s_prepopulated_num;
  29. heap_info.max_totals = MAX_TASK_NUM; // Maximum length of "s_totals_arr"
  30. heap_info.blocks = s_block_arr; // Gets block wise allocation details. For each block, gets owner task, address and size
  31. heap_info.max_blocks = MAX_BLOCK_NUM; // Maximum length of "s_block_arr"
  32. heap_caps_get_per_task_info(&heap_info);
  33. for (int i = 0 ; i < *heap_info.num_totals; i++) {
  34. printf("Task: %s -> CAP_8BIT: %d CAP_32BIT: %d\n",
  35. heap_info.totals[i].task ? pcTaskGetName(heap_info.totals[i].task) : "Pre-Scheduler allocs" ,
  36. heap_info.totals[i].size[0], // Heap size with CAP_8BIT capabilities
  37. heap_info.totals[i].size[1]); // Heap size with CAP32_BIT capabilities
  38. }
  39. printf("\n\n");
  40. }
  41. static void example_task(void *args)
  42. {
  43. uint32_t size = 0;
  44. const char *TAG = "example_task";
  45. while (1) {
  46. /*
  47. * Allocate random amount of memory for demonstration
  48. */
  49. size = (esp_random() % 1000);
  50. void *ptr = malloc(size);
  51. if (ptr == NULL) {
  52. ESP_LOGE(TAG, "Could not allocate heap memory");
  53. abort();
  54. }
  55. esp_dump_per_task_heap_info();
  56. free(ptr);
  57. vTaskDelay(pdMS_TO_TICKS(2000));
  58. }
  59. }
  60. void app_main(void)
  61. {
  62. /*
  63. * Create example task to demonstrate heap_task_tracking
  64. */
  65. xTaskCreate(&example_task, "example_task", 3072, NULL, 5, NULL);
  66. }