ccomp_timer_test_api.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include "esp_timer.h"
  4. #include "esp_log.h"
  5. #include "esp_attr.h"
  6. #include "ccomp_timer.h"
  7. #include "eri.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_ipc.h"
  11. #include "unity.h"
  12. #ifndef CONFIG_FREERTOS_UNICORE
  13. static void start_timer(void *param)
  14. {
  15. esp_err_t *err = (esp_err_t *)param;
  16. *err = ccomp_timer_start();
  17. }
  18. static void stop_timer(void *param)
  19. {
  20. int64_t *t = (int64_t *)param;
  21. *t = ccomp_timer_stop();
  22. }
  23. #endif
  24. static void computation(void *param)
  25. {
  26. int *l = (int *)param;
  27. for (volatile int i = 0, a = 0; i < *l; i++)
  28. {
  29. a += i;
  30. }
  31. }
  32. TEST_CASE("starting and stopping works", "[test_utils][ccomp_timer]")
  33. {
  34. esp_err_t err;
  35. int64_t t;
  36. /*
  37. * Test on the same task
  38. */
  39. err = ccomp_timer_start();
  40. TEST_ASSERT_EQUAL(ESP_OK, err);
  41. // Start an already started timer
  42. err = ccomp_timer_start();
  43. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, err);
  44. t = ccomp_timer_stop();
  45. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  46. // Stopping a non started timer
  47. t = ccomp_timer_stop();
  48. TEST_ASSERT_EQUAL(-1, t);
  49. #ifndef CONFIG_FREERTOS_UNICORE
  50. /*
  51. * Test on different task on same core
  52. */
  53. err = ccomp_timer_start();
  54. TEST_ASSERT_EQUAL(ESP_OK, err);
  55. esp_ipc_call_blocking(xPortGetCoreID(), start_timer, &err);
  56. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, err);
  57. t = ccomp_timer_stop();
  58. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  59. esp_ipc_call_blocking(xPortGetCoreID(), stop_timer, &t);
  60. TEST_ASSERT_EQUAL(-1, t);
  61. /*
  62. * Timer being stopped from another task on the same core
  63. */
  64. err = ccomp_timer_start();
  65. TEST_ASSERT_EQUAL(ESP_OK, err);
  66. esp_ipc_call_blocking(xPortGetCoreID(), stop_timer, &t);
  67. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  68. /*
  69. * Test on different task on same core
  70. */
  71. err = ccomp_timer_start();
  72. TEST_ASSERT_EQUAL(ESP_OK, err);
  73. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, start_timer, &err);
  74. TEST_ASSERT_EQUAL(ESP_OK, err);
  75. t = ccomp_timer_stop();
  76. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  77. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, stop_timer, &t);
  78. TEST_ASSERT_GREATER_OR_EQUAL(0, t);
  79. #endif
  80. }
  81. TEST_CASE("getting the time works", "[test_utils][ccomp_timer]")
  82. {
  83. // Get wall time and start ccomp timer
  84. int64_t start = esp_timer_get_time();
  85. ccomp_timer_start();
  86. int64_t t_a = ccomp_timer_get_time();
  87. int temp = 10000;
  88. computation(&temp);
  89. int64_t t_b = ccomp_timer_get_time();
  90. // Check that ccomp time after computation is more than
  91. // ccomp time before computation.
  92. TEST_ASSERT_LESS_THAN(t_b, t_a);
  93. // Get time diff between wall time and ccomp time
  94. int64_t t_1 = ccomp_timer_stop();
  95. int64_t t_2 = esp_timer_get_time() - start;
  96. // The times should at least be in the same ballpark (at least within 10%)
  97. float diff = (abs(t_1 - t_2)) / ((float)t_2);
  98. TEST_ASSERT(diff <= 10.0f);
  99. // Since the timer was already stopped, test that ccomp_timer_get_time
  100. // returns the same time as ccomp_timer_stop
  101. int64_t t_c = ccomp_timer_get_time();
  102. TEST_ASSERT_EQUAL(t_1, t_c);
  103. }
  104. #ifndef CONFIG_FREERTOS_UNICORE
  105. TEST_CASE("timers for each core counts independently", "[test_utils][ccomp_timer]")
  106. {
  107. esp_err_t err;
  108. // Start a timer on this core
  109. err = ccomp_timer_start();
  110. TEST_ASSERT_EQUAL(ESP_OK, err);
  111. // Do some work on this core
  112. int temp = 10000;
  113. computation(&temp);
  114. // Start a timer on the other core
  115. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, start_timer, &err);
  116. TEST_ASSERT_EQUAL(ESP_OK, err);
  117. // Do some work on other core (less work than this core did)
  118. temp = 5000;
  119. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, computation, &temp);
  120. // Stop timers from both cores
  121. int64_t t_1 = ccomp_timer_stop();
  122. TEST_ASSERT_GREATER_OR_EQUAL(0, t_1);
  123. int64_t t_2;
  124. esp_ipc_call_blocking(xPortGetCoreID() == 0 ? 1 : 0, stop_timer, &t_2);
  125. TEST_ASSERT_GREATER_OR_EQUAL(0, t_2);
  126. // Since this core did more work, it probably has longer measured time
  127. TEST_ASSERT_GREATER_THAN(t_2, t_1);
  128. }
  129. #endif