ccomp_timer_test_inst.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "freertos/FreeRTOS.h"
  8. #include "unity.h"
  9. #include "sdkconfig.h"
  10. #if CONFIG_IDF_TARGET_ESP32
  11. #define CACHE_WAYS 2
  12. #define CACHE_LINE_SIZE 32
  13. #define CACHE_SIZE (1 << 15)
  14. // Only test half due to lack of memory
  15. #elif CONFIG_IDF_TARGET_ESP32S2
  16. // Default cache configuration - no override specified on
  17. // test_utils config
  18. #define CACHE_WAYS 8
  19. #define CACHE_LINE_SIZE 32
  20. #define CACHE_SIZE (1 << 13)
  21. #endif
  22. typedef void (*ccomp_test_func_t)(void);
  23. static const char* TAG = "test_ccomp_timer";
  24. typedef struct {
  25. int64_t wall;
  26. int64_t ccomp;
  27. } ccomp_test_time_t;
  28. typedef struct {
  29. ccomp_test_func_t *funcs;
  30. size_t len;
  31. } ccomp_test_call_t;
  32. #define FUNC() \
  33. do \
  34. { \
  35. volatile int a = 0; \
  36. a++; \
  37. } while (0);
  38. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func1(void)
  39. {
  40. FUNC();
  41. }
  42. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func2(void)
  43. {
  44. FUNC();
  45. }
  46. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func3(void)
  47. {
  48. FUNC();
  49. }
  50. #if TEMPORARY_DISABLED_FOR_TARGETS(ESP32)
  51. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func4(void)
  52. {
  53. FUNC();
  54. }
  55. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func5(void)
  56. {
  57. FUNC();
  58. }
  59. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func6(void)
  60. {
  61. FUNC();
  62. }
  63. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func7(void)
  64. {
  65. FUNC();
  66. }
  67. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func8(void)
  68. {
  69. FUNC();
  70. }
  71. __aligned(CACHE_SIZE / CACHE_WAYS) static void test_func9(void)
  72. {
  73. FUNC();
  74. }
  75. #endif
  76. static void IRAM_ATTR iram_func(void)
  77. {
  78. FUNC();
  79. }
  80. static void IRAM_ATTR perform_calls(ccomp_test_call_t *call)
  81. {
  82. for (int i = 0; i < call->len; i++) {
  83. call->funcs[i]();
  84. }
  85. }
  86. static void IRAM_ATTR prepare_cache(ccomp_test_call_t *call)
  87. {
  88. perform_calls(call);
  89. }
  90. static void IRAM_ATTR prepare_calls(int hit_rate, ccomp_test_func_t *alts, size_t alts_len, size_t len, ccomp_test_call_t *out)
  91. {
  92. assert(hit_rate <= 100);
  93. assert(hit_rate >= 0);
  94. int misses = (100 - hit_rate);
  95. int hits = hit_rate;
  96. ccomp_test_func_t *funcs = calloc(len, sizeof(ccomp_test_func_t));
  97. for (int i = 0, h = 0, i_h = 1, m = -1, i_m = 0, l = 0; i < len; i++, h += i_h, m += i_m) {
  98. funcs[i] = alts[l % alts_len];
  99. if (i_m) {
  100. l++;
  101. }
  102. if (h >= hits) {
  103. h = -1;
  104. i_h = 0;
  105. m = 0;
  106. i_m = 1;
  107. }
  108. if (m >= misses) {
  109. m = -1;
  110. i_m = 0;
  111. h = 0;
  112. i_h = 1;
  113. }
  114. }
  115. out->funcs = funcs;
  116. out->len = len;
  117. }
  118. static ccomp_test_time_t IRAM_ATTR perform_test_at_hit_rate(int hit_rate)
  119. {
  120. static portMUX_TYPE m = portMUX_INITIALIZER_UNLOCKED;
  121. ccomp_test_call_t calls;
  122. ccomp_test_func_t alts[] = {test_func1, test_func2, test_func3,
  123. #if TEMPORARY_DISABLED_FOR_TARGETS(ESP32)
  124. test_func4, test_func5, test_func6, test_func7, test_func8, test_func9,
  125. #endif
  126. };
  127. prepare_calls(hit_rate, alts, sizeof(alts)/sizeof(alts[0]), 10000, &calls);
  128. ccomp_test_func_t f[] = {test_func1, test_func2};
  129. ccomp_test_call_t cache = {
  130. .funcs = f,
  131. .len = sizeof(f) / sizeof(f[0])};
  132. portENTER_CRITICAL(&m);
  133. prepare_cache(&cache);
  134. int64_t start = esp_timer_get_time();
  135. ccomp_timer_start();
  136. perform_calls(&calls);
  137. ccomp_test_time_t t = {
  138. .ccomp = ccomp_timer_stop(),
  139. .wall = esp_timer_get_time() - start
  140. };
  141. portEXIT_CRITICAL(&m);
  142. free(calls.funcs);
  143. return t;
  144. }
  145. static ccomp_test_time_t ccomp_test_ref_time(void)
  146. {
  147. ccomp_test_call_t calls;
  148. ccomp_test_func_t alts[] = {iram_func};
  149. prepare_calls(0, alts, 1, 10000, &calls);
  150. int64_t start = esp_timer_get_time();
  151. ccomp_timer_start();
  152. perform_calls(&calls);
  153. ccomp_test_time_t t = {
  154. .ccomp = ccomp_timer_stop(),
  155. .wall = esp_timer_get_time() - start
  156. };
  157. free(calls.funcs);
  158. return t;
  159. }
  160. TEST_CASE("instruction cache hit rate sweep test", "[test_utils][ccomp_timer]")
  161. {
  162. ccomp_test_time_t t_ref;
  163. ccomp_test_time_t t_hr;
  164. // Perform accesses on RAM. The time recorded here serves as
  165. // reference.
  166. t_ref = ccomp_test_ref_time();
  167. ESP_LOGI(TAG, "Reference Time(us): %lld", (long long)t_ref.ccomp);
  168. // Measure time at particular hit rates
  169. for (int i = 0; i <= 100; i += 5)
  170. {
  171. t_hr = perform_test_at_hit_rate(i);
  172. float error = (abs(t_ref.ccomp - t_hr.ccomp) / (float)t_ref.wall) * 100.0f;
  173. ESP_LOGI(TAG, "Hit Rate(%%): %d Wall Time(us): %lld Compensated Time(us): %lld Error(%%): %f", i, (long long)t_hr.wall, (long long)t_hr.ccomp, error);
  174. // Check if the measured time is at least within some percent of the
  175. // reference.
  176. TEST_ASSERT(error <= 5.0f);
  177. }
  178. }