ccomp_timer_test_api.c 4.2 KB

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