ccomp_timer_test_data.c 4.2 KB

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