perfmon.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //source : https://github.com/Carbon225/esp32-perfmon
  2. #include "../../include/defines.h"
  3. #ifdef DEBUG_ENABLE_PERFMON
  4. #include "perfmon.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_freertos_hooks.h"
  8. #include "sdkconfig.h"
  9. #include "esp_log.h"
  10. static const char *TAG = "perfmon";
  11. static uint64_t idle0Calls = 0;
  12. static uint64_t idle1Calls = 0;
  13. #if defined(CONFIG_ESP32_DEFAULT_CPU_FREQ_240)
  14. static const uint64_t MaxIdleCalls = 1855000;
  15. #elif defined(CONFIG_ESP32_DEFAULT_CPU_FREQ_160)
  16. static const uint64_t MaxIdleCalls = 1233100;
  17. #else
  18. #error "Unsupported CPU frequency"
  19. #endif
  20. static bool idle_task_0()
  21. {
  22. idle0Calls += 1;
  23. return false;
  24. }
  25. static bool idle_task_1()
  26. {
  27. idle1Calls += 1;
  28. return false;
  29. }
  30. static void perfmon_task(void *args)
  31. {
  32. while (1)
  33. {
  34. float idle0 = idle0Calls;
  35. float idle1 = idle1Calls;
  36. idle0Calls = 0;
  37. idle1Calls = 0;
  38. int cpu0 = 100.f - idle0 / MaxIdleCalls * 100.f;
  39. int cpu1 = 100.f - idle1 / MaxIdleCalls * 100.f;
  40. ESP_LOGI(TAG, "Core 0 at %d%%", cpu0);
  41. ESP_LOGI(TAG, "Core 1 at %d%%", cpu1);
  42. // TODO configurable delay
  43. vTaskDelay(5000 / portTICK_PERIOD_MS);
  44. }
  45. vTaskDelete(NULL);
  46. }
  47. esp_err_t perfmon_start()
  48. {
  49. ESP_ERROR_CHECK(esp_register_freertos_idle_hook_for_cpu(idle_task_0, 0));
  50. ESP_ERROR_CHECK(esp_register_freertos_idle_hook_for_cpu(idle_task_1, 1));
  51. // TODO calculate optimal stack size
  52. xTaskCreate(perfmon_task, "perfmon", 2048, NULL, 1, NULL);
  53. return ESP_OK;
  54. }
  55. #endif // DEBUG_ENABLE_PERFMON