heap_task_info.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <freertos/FreeRTOS.h>
  14. #include <freertos/task.h>
  15. #include <multi_heap.h>
  16. #include "multi_heap_internal.h"
  17. #include "heap_private.h"
  18. #include "esp_heap_task_info.h"
  19. #ifdef CONFIG_HEAP_TASK_TRACKING
  20. /*
  21. * Return per-task heap allocation totals and lists of blocks.
  22. *
  23. * For each task that has allocated memory from the heap, return totals for
  24. * allocations within regions matching one or more sets of capabilities.
  25. *
  26. * Optionally also return an array of structs providing details about each
  27. * block allocated by one or more requested tasks, or by all tasks.
  28. *
  29. * Returns the number of block detail structs returned.
  30. */
  31. size_t heap_caps_get_per_task_info(heap_task_info_params_t *params)
  32. {
  33. heap_t *reg;
  34. heap_task_block_t *blocks = params->blocks;
  35. size_t count = *params->num_totals;
  36. size_t remaining = params->max_blocks;
  37. // Clear out totals for any prepopulated tasks.
  38. if (params->totals) {
  39. for (size_t i = 0; i < count; ++i) {
  40. for (size_t type = 0; type < NUM_HEAP_TASK_CAPS; ++type) {
  41. params->totals[i].size[type] = 0;
  42. params->totals[i].count[type] = 0;
  43. }
  44. }
  45. }
  46. SLIST_FOREACH(reg, &registered_heaps, next) {
  47. multi_heap_handle_t heap = reg->heap;
  48. if (heap == NULL) {
  49. continue;
  50. }
  51. // Find if the capabilities of this heap region match on of the desired
  52. // sets of capabilities.
  53. uint32_t caps = get_all_caps(reg);
  54. uint32_t type;
  55. for (type = 0; type < NUM_HEAP_TASK_CAPS; ++type) {
  56. if ((caps & params->mask[type]) == params->caps[type]) {
  57. break;
  58. }
  59. }
  60. if (type == NUM_HEAP_TASK_CAPS) {
  61. continue;
  62. }
  63. multi_heap_block_handle_t b = multi_heap_get_first_block(heap);
  64. multi_heap_internal_lock(heap);
  65. for ( ; b ; b = multi_heap_get_next_block(heap, b)) {
  66. if (multi_heap_is_free(b)) {
  67. continue;
  68. }
  69. void *p = multi_heap_get_block_address(b); // Safe, only arithmetic
  70. size_t bsize = multi_heap_get_allocated_size(heap, p); // Validates
  71. TaskHandle_t btask = (TaskHandle_t)multi_heap_get_block_owner(b);
  72. // Accumulate per-task allocation totals.
  73. if (params->totals) {
  74. size_t i;
  75. for (i = 0; i < count; ++i) {
  76. if (params->totals[i].task == btask) {
  77. break;
  78. }
  79. }
  80. if (i < count) {
  81. params->totals[i].size[type] += bsize;
  82. params->totals[i].count[type] += 1;
  83. }
  84. else {
  85. if (count < params->max_totals) {
  86. params->totals[count].task = btask;
  87. params->totals[count].size[type] = bsize;
  88. params->totals[i].count[type] = 1;
  89. ++count;
  90. }
  91. }
  92. }
  93. // Return details about allocated blocks for selected tasks.
  94. if (blocks && remaining > 0) {
  95. if (params->tasks) {
  96. size_t i;
  97. for (i = 0; i < params->num_tasks; ++i) {
  98. if (btask == params->tasks[i]) {
  99. break;
  100. }
  101. }
  102. if (i == params->num_tasks) {
  103. continue;
  104. }
  105. }
  106. blocks->task = btask;
  107. blocks->address = p;
  108. blocks->size = bsize;
  109. ++blocks;
  110. --remaining;
  111. }
  112. }
  113. multi_heap_internal_unlock(heap);
  114. }
  115. *params->num_totals = count;
  116. return params->max_blocks - remaining;
  117. }
  118. #endif // CONFIG_HEAP_TASK_TRACKING