ccomp_timer_test_data.c 4.3 KB

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