heap_task_tracking_main.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #define MAX_TASK_NUM 20 // Max number of per tasks info that it can store
  13. #define MAX_BLOCK_NUM 20 // Max number of per block info that it can store
  14. static size_t s_prepopulated_num = 0;
  15. static heap_task_totals_t s_totals_arr[MAX_TASK_NUM];
  16. static heap_task_block_t s_block_arr[MAX_BLOCK_NUM];
  17. static void esp_dump_per_task_heap_info(void)
  18. {
  19. heap_task_info_params_t heap_info = {0};
  20. heap_info.caps[0] = MALLOC_CAP_8BIT; // Gets heap with CAP_8BIT capabilities
  21. heap_info.mask[0] = MALLOC_CAP_8BIT;
  22. heap_info.caps[1] = MALLOC_CAP_32BIT; // Gets heap info with CAP_32BIT capabilities
  23. heap_info.mask[1] = MALLOC_CAP_32BIT;
  24. heap_info.tasks = NULL; // Passing NULL captures heap info for all tasks
  25. heap_info.num_tasks = 0;
  26. heap_info.totals = s_totals_arr; // Gets task wise allocation details
  27. heap_info.num_totals = &s_prepopulated_num;
  28. heap_info.max_totals = MAX_TASK_NUM; // Maximum length of "s_totals_arr"
  29. heap_info.blocks = s_block_arr; // Gets block wise allocation details. For each block, gets owner task, address and size
  30. heap_info.max_blocks = MAX_BLOCK_NUM; // Maximum length of "s_block_arr"
  31. heap_caps_get_per_task_info(&heap_info);
  32. for (int i = 0 ; i < *heap_info.num_totals; i++) {
  33. printf("Task: %s -> CAP_8BIT: %d CAP_32BIT: %d\n",
  34. heap_info.totals[i].task ? pcTaskGetTaskName(heap_info.totals[i].task) : "Pre-Scheduler allocs" ,
  35. heap_info.totals[i].size[0], // Heap size with CAP_8BIT capabilities
  36. heap_info.totals[i].size[1]); // Heap size with CAP32_BIT capabilities
  37. }
  38. printf("\n\n");
  39. }
  40. static void example_task(void *args)
  41. {
  42. uint32_t size = 0;
  43. const char *TAG = "example_task";
  44. while (1) {
  45. /*
  46. * Allocate random amount of memory for demonstration
  47. */
  48. size = (esp_random() % 1000);
  49. void *ptr = malloc(size);
  50. if (ptr == NULL) {
  51. ESP_LOGE(TAG, "Could not allocate heap memory");
  52. abort();
  53. }
  54. esp_dump_per_task_heap_info();
  55. free(ptr);
  56. vTaskDelay(pdMS_TO_TICKS(2000));
  57. }
  58. }
  59. void app_main(void)
  60. {
  61. /*
  62. * Create example task to demonstrate heap_task_tracking
  63. */
  64. xTaskCreate(&example_task, "example_task", 3072, NULL, 5, NULL);
  65. }