ccomp_timer_test_data.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 || CONFIG_IDF_TARGET_ESP32C6
  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 !__riscv
  43. //IDF-5052
  44. static const char* TAG = "test_ccomp_timer";
  45. #if CONFIG_SPIRAM
  46. static uint8_t *flash_mem;
  47. #else
  48. static const uint8_t flash_mem[2 * CACHE_SIZE] = {0};
  49. #endif
  50. static IRAM_ATTR void perform_accesses(ccomp_test_access_t *access)
  51. {
  52. volatile int a = 0;
  53. for (int i = 0; i < access->len; i++) {
  54. a += (int)(*(access->accesses[i]));
  55. }
  56. }
  57. static void prepare_cache(const uint8_t *to_cache)
  58. {
  59. volatile int a = 0;
  60. for (int i = 0; i < CACHE_SIZE; i++) {
  61. a += to_cache[i];
  62. }
  63. }
  64. static void prepare_access_pattern(int hit_rate, const uint8_t *cached, ccomp_test_access_t *out)
  65. {
  66. assert(hit_rate <= 100);
  67. assert(hit_rate >= 0);
  68. int misses = (100 - hit_rate) * CACHE_LINE_SIZE;
  69. int hits = hit_rate * CACHE_LINE_SIZE;
  70. uint8_t **accesses = calloc(TEST_SIZE, sizeof(uint8_t *));
  71. for (int i = 0, h = 0, i_h = 1, m = -1, i_m = 0; i < TEST_SIZE; i++, h += i_h, m += i_m) {
  72. if (i_m) {
  73. accesses[i] = (uint8_t*) (cached + CACHE_SIZE + i);
  74. }
  75. else {
  76. accesses[i] = (uint8_t*) (cached + i);
  77. }
  78. if (h >= hits) {
  79. h = -1;
  80. i_h = 0;
  81. m = 0;
  82. i_m = 1;
  83. }
  84. if (m >= misses) {
  85. m = -1;
  86. i_m = 0;
  87. h = 0;
  88. i_h = 1;
  89. }
  90. }
  91. out->accesses = accesses;
  92. out->len = TEST_SIZE;
  93. }
  94. static ccomp_test_time_t perform_test_at_hit_rate(int hit_rate, const uint8_t *mem)
  95. {
  96. ccomp_test_access_t access;
  97. prepare_access_pattern(hit_rate, mem, &access);
  98. prepare_cache(mem);
  99. int64_t start = esp_timer_get_time();
  100. ccomp_timer_start();
  101. perform_accesses(&access);
  102. ccomp_test_time_t t = {
  103. .ccomp = ccomp_timer_stop(),
  104. .wall = esp_timer_get_time() - start
  105. };
  106. free(access.accesses);
  107. return t;
  108. }
  109. static ccomp_test_time_t ccomp_test_ref_time(void)
  110. {
  111. #if CONFIG_SPIRAM
  112. uint8_t *mem = heap_caps_malloc(2 * CACHE_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_DEFAULT);
  113. #else
  114. uint8_t *mem = heap_caps_malloc(sizeof(flash_mem), MALLOC_CAP_INTERNAL | MALLOC_CAP_DEFAULT);
  115. #endif
  116. ccomp_test_time_t t = perform_test_at_hit_rate(0, mem);
  117. free(mem);
  118. return t;
  119. }
  120. TEST_CASE("data cache hit rate sweep", "[test_utils][ccomp_timer]")
  121. {
  122. ccomp_test_time_t t_ref;
  123. ccomp_test_time_t t_hr;
  124. #if CONFIG_SPIRAM
  125. flash_mem = heap_caps_malloc(2 * CACHE_SIZE, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  126. #endif
  127. // Perform accesses on RAM. The time recorded here serves as
  128. // reference.
  129. t_ref = ccomp_test_ref_time();
  130. ESP_LOGI(TAG, "Reference Time(us): %lld", (long long)t_ref.ccomp);
  131. // Measure time at particular hit rates
  132. for (int i = 0; i <= 100; i += 5)
  133. {
  134. t_hr = perform_test_at_hit_rate(i, flash_mem);
  135. float error = (llabs(t_ref.ccomp - t_hr.ccomp) / (float)t_ref.ccomp) * 100.0f;
  136. 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);
  137. // Check if the measured time is at least within some percent of the
  138. // reference.
  139. TEST_ASSERT(error <= 5.0f);
  140. }
  141. #if CONFIG_SPIRAM
  142. free(flash_mem);
  143. #endif
  144. }
  145. #endif // __riscv