ccomp_timer_test_inst.c 5.0 KB

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