ccomp_timer_test_inst.c 5.2 KB

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