ccomp_timer_test_data.c 4.2 KB

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