system_info.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "system_info.h"
  2. #include <freertos/task.h>
  3. #include <esp_log.h>
  4. #include <esp_flash.h>
  5. #include <esp_mac.h>
  6. #include <esp_system.h>
  7. #include <esp_partition.h>
  8. #include <esp_app_desc.h>
  9. #include <esp_ota_ops.h>
  10. #if CONFIG_IDF_TARGET_ESP32P4
  11. #include "esp_wifi_remote.h"
  12. #endif
  13. #define TAG "SystemInfo"
  14. size_t SystemInfo::GetFlashSize() {
  15. uint32_t flash_size;
  16. if (esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
  17. ESP_LOGE(TAG, "Failed to get flash size");
  18. return 0;
  19. }
  20. return (size_t)flash_size;
  21. }
  22. size_t SystemInfo::GetMinimumFreeHeapSize() {
  23. return esp_get_minimum_free_heap_size();
  24. }
  25. size_t SystemInfo::GetFreeHeapSize() {
  26. return esp_get_free_heap_size();
  27. }
  28. std::string SystemInfo::GetMacAddress() {
  29. uint8_t mac[6];
  30. #if CONFIG_IDF_TARGET_ESP32P4
  31. esp_wifi_get_mac(WIFI_IF_STA, mac);
  32. #else
  33. esp_read_mac(mac, ESP_MAC_BT);
  34. #endif
  35. char mac_str[18];
  36. snprintf(mac_str, sizeof(mac_str), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  37. return std::string(mac_str);
  38. }
  39. std::string SystemInfo::GetChipModelName() {
  40. return std::string(CONFIG_IDF_TARGET);
  41. }
  42. esp_err_t SystemInfo::PrintTaskCpuUsage(TickType_t xTicksToWait) {
  43. #define ARRAY_SIZE_OFFSET 5
  44. TaskStatus_t *start_array = NULL, *end_array = NULL;
  45. UBaseType_t start_array_size, end_array_size;
  46. configRUN_TIME_COUNTER_TYPE start_run_time, end_run_time;
  47. esp_err_t ret;
  48. uint32_t total_elapsed_time;
  49. //Allocate array to store current task states
  50. start_array_size = uxTaskGetNumberOfTasks() + ARRAY_SIZE_OFFSET;
  51. start_array = (TaskStatus_t*)malloc(sizeof(TaskStatus_t) * start_array_size);
  52. if (start_array == NULL) {
  53. ret = ESP_ERR_NO_MEM;
  54. goto exit;
  55. }
  56. //Get current task states
  57. start_array_size = uxTaskGetSystemState(start_array, start_array_size, &start_run_time);
  58. if (start_array_size == 0) {
  59. ret = ESP_ERR_INVALID_SIZE;
  60. goto exit;
  61. }
  62. vTaskDelay(xTicksToWait);
  63. //Allocate array to store tasks states post delay
  64. end_array_size = uxTaskGetNumberOfTasks() + ARRAY_SIZE_OFFSET;
  65. end_array = (TaskStatus_t*)malloc(sizeof(TaskStatus_t) * end_array_size);
  66. if (end_array == NULL) {
  67. ret = ESP_ERR_NO_MEM;
  68. goto exit;
  69. }
  70. //Get post delay task states
  71. end_array_size = uxTaskGetSystemState(end_array, end_array_size, &end_run_time);
  72. if (end_array_size == 0) {
  73. ret = ESP_ERR_INVALID_SIZE;
  74. goto exit;
  75. }
  76. //Calculate total_elapsed_time in units of run time stats clock period.
  77. total_elapsed_time = (end_run_time - start_run_time);
  78. if (total_elapsed_time == 0) {
  79. ret = ESP_ERR_INVALID_STATE;
  80. goto exit;
  81. }
  82. printf("| Task | Run Time | Percentage\n");
  83. //Match each task in start_array to those in the end_array
  84. for (int i = 0; i < start_array_size; i++) {
  85. int k = -1;
  86. for (int j = 0; j < end_array_size; j++) {
  87. if (start_array[i].xHandle == end_array[j].xHandle) {
  88. k = j;
  89. //Mark that task have been matched by overwriting their handles
  90. start_array[i].xHandle = NULL;
  91. end_array[j].xHandle = NULL;
  92. break;
  93. }
  94. }
  95. //Check if matching task found
  96. if (k >= 0) {
  97. uint32_t task_elapsed_time = end_array[k].ulRunTimeCounter - start_array[i].ulRunTimeCounter;
  98. uint32_t percentage_time = (task_elapsed_time * 100UL) / (total_elapsed_time * CONFIG_FREERTOS_NUMBER_OF_CORES);
  99. printf("| %-16s | %8lu | %4lu%%\n", start_array[i].pcTaskName, task_elapsed_time, percentage_time);
  100. }
  101. }
  102. //Print unmatched tasks
  103. for (int i = 0; i < start_array_size; i++) {
  104. if (start_array[i].xHandle != NULL) {
  105. printf("| %s | Deleted\n", start_array[i].pcTaskName);
  106. }
  107. }
  108. for (int i = 0; i < end_array_size; i++) {
  109. if (end_array[i].xHandle != NULL) {
  110. printf("| %s | Created\n", end_array[i].pcTaskName);
  111. }
  112. }
  113. ret = ESP_OK;
  114. exit: //Common return path
  115. free(start_array);
  116. free(end_array);
  117. return ret;
  118. }
  119. void SystemInfo::PrintTaskList() {
  120. char buffer[1000];
  121. vTaskList(buffer);
  122. ESP_LOGI(TAG, "Task list: \n%s", buffer);
  123. }
  124. void SystemInfo::PrintHeapStats() {
  125. int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  126. int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
  127. ESP_LOGI(TAG, "free sram: %u minimal sram: %u", free_sram, min_free_sram);
  128. }