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