ccomp_timer_test_data.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/task.h"
  9. #if CONFIG_IDF_TARGET_ESP32
  10. #include "esp32/clk.h"
  11. #elif CONFIG_IDF_TARGET_ESP32S2
  12. #include "esp32s2/clk.h"
  13. #elif CONFIG_IDF_TARTGET_ESP32S3
  14. #include "esp32s3/clk.h"
  15. #elif CONFIG_IDF_TARGET_ESP32C3
  16. #include "esp32c3/clk.h"
  17. #endif
  18. #include "unity.h"
  19. #include "sdkconfig.h"
  20. static const char* TAG = "test_ccomp_timer";
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #define CACHE_WAYS 2
  23. #define CACHE_LINE_SIZE 32
  24. #define CACHE_SIZE (1 << 15)
  25. // Only test half due to lack of memory
  26. #define TEST_SIZE (CACHE_SIZE / 2)
  27. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  28. // Default cache configuration - no override specified on
  29. // test_utils config
  30. #define CACHE_WAYS 8
  31. #define CACHE_LINE_SIZE 32
  32. #define CACHE_SIZE (1 << 13)
  33. #define TEST_SIZE (CACHE_SIZE)
  34. #elif CONFIG_IDF_TARGET_ESP32C3
  35. #define CACHE_WAYS 8
  36. #define CACHE_LINE_SIZE 32
  37. #define CACHE_SIZE (1 << 14)
  38. #define TEST_SIZE (CACHE_SIZE)
  39. #endif
  40. typedef struct {
  41. uint8_t **accesses;
  42. size_t len;
  43. } ccomp_test_access_t;
  44. typedef struct {
  45. int64_t wall;
  46. int64_t ccomp;
  47. } ccomp_test_time_t;
  48. #if CONFIG_ESP32_SPIRAM_SUPPORT
  49. static uint8_t *flash_mem;
  50. #else
  51. static const uint8_t flash_mem[2 * CACHE_SIZE] = {0};
  52. #endif
  53. static IRAM_ATTR void perform_accesses(ccomp_test_access_t *access)
  54. {
  55. volatile int a = 0;
  56. for (int i = 0; i < access->len; i++) {
  57. a += (int)(*(access->accesses[i]));
  58. }
  59. }
  60. static void prepare_cache(const uint8_t *to_cache)
  61. {
  62. volatile int a = 0;
  63. for (int i = 0; i < CACHE_SIZE; i++) {
  64. a += to_cache[i];
  65. }
  66. }
  67. static void prepare_access_pattern(int hit_rate, const uint8_t *cached, ccomp_test_access_t *out)
  68. {
  69. assert(hit_rate <= 100);
  70. assert(hit_rate >= 0);
  71. int misses = (100 - hit_rate) * CACHE_LINE_SIZE;
  72. int hits = hit_rate * CACHE_LINE_SIZE;
  73. uint8_t **accesses = calloc(TEST_SIZE, sizeof(uint8_t *));
  74. for (int i = 0, h = 0, i_h = 1, m = -1, i_m = 0; i < TEST_SIZE; i++, h += i_h, m += i_m) {
  75. if (i_m) {
  76. accesses[i] = (uint8_t*) (cached + CACHE_SIZE + i);
  77. }
  78. else {
  79. accesses[i] = (uint8_t*) (cached + i);
  80. }
  81. if (h >= hits) {
  82. h = -1;
  83. i_h = 0;
  84. m = 0;
  85. i_m = 1;
  86. }
  87. if (m >= misses) {
  88. m = -1;
  89. i_m = 0;
  90. h = 0;
  91. i_h = 1;
  92. }
  93. }
  94. out->accesses = accesses;
  95. out->len = TEST_SIZE;
  96. }
  97. static ccomp_test_time_t perform_test_at_hit_rate(int hit_rate, const uint8_t *mem)
  98. {
  99. ccomp_test_access_t access;
  100. prepare_access_pattern(hit_rate, mem, &access);
  101. prepare_cache(mem);
  102. int64_t start = esp_timer_get_time();
  103. ccomp_timer_start();
  104. perform_accesses(&access);
  105. ccomp_test_time_t t = {
  106. .ccomp = ccomp_timer_stop(),
  107. .wall = esp_timer_get_time() - start
  108. };
  109. free(access.accesses);
  110. return t;
  111. }
  112. static ccomp_test_time_t ccomp_test_ref_time(void)
  113. {
  114. #if CONFIG_ESP32_SPIRAM_SUPPORT
  115. uint8_t *mem = heap_caps_malloc(2 * CACHE_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_DEFAULT);
  116. #else
  117. uint8_t *mem = heap_caps_malloc(sizeof(flash_mem), MALLOC_CAP_INTERNAL | MALLOC_CAP_DEFAULT);
  118. #endif
  119. ccomp_test_time_t t = perform_test_at_hit_rate(0, mem);
  120. free(mem);
  121. return t;
  122. }
  123. TEST_CASE("data cache hit rate sweep", "[test_utils][ccomp_timer]")
  124. {
  125. ccomp_test_time_t t_ref;
  126. ccomp_test_time_t t_hr;
  127. #if CONFIG_ESP32_SPIRAM_SUPPORT
  128. flash_mem = heap_caps_malloc(2 * CACHE_SIZE, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  129. #endif
  130. // Perform accesses on RAM. The time recorded here serves as
  131. // reference.
  132. t_ref = ccomp_test_ref_time();
  133. ESP_LOGI(TAG, "Reference Time(us): %lld", (long long)t_ref.ccomp);
  134. // Measure time at particular hit rates
  135. for (int i = 0; i <= 100; i += 5)
  136. {
  137. t_hr = perform_test_at_hit_rate(i, flash_mem);
  138. float error = (abs(t_ref.ccomp - t_hr.ccomp) / (float)t_ref.ccomp) * 100.0f;
  139. 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);
  140. // Check if the measured time is at least within some percent of the
  141. // reference.
  142. TEST_ASSERT(error <= 5.0f);
  143. }
  144. #if CONFIG_ESP32_SPIRAM_SUPPORT
  145. free(flash_mem);
  146. #endif
  147. }