test_atomic.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "unity.h"
  2. #include <stdatomic.h>
  3. #include "esp_log.h"
  4. #include "esp_attr.h"
  5. #include "hal/cpu_hal.h"
  6. #include "../cache_utils.h"
  7. #define RECORD_TIME_PREPARE() uint32_t __t1, __t2
  8. #define RECORD_TIME_START() do {__t1 = cpu_hal_get_cycle_count();}while(0)
  9. #define RECORD_TIME_END(p_time) do{__t2 = cpu_hal_get_cycle_count(); *p_time = (__t2-__t1);}while(0)
  10. #define TEST_TIMES 11
  11. //Test twice, and only get the result of second time, to avoid influence of cache miss
  12. #define TEST_WRAP_START() \
  13. RECORD_TIME_PREPARE(); \
  14. spi_flash_disable_interrupts_caches_and_other_cpu(); \
  15. for (int i = 0; i < 2; i++) { \
  16. RECORD_TIME_START();
  17. #define TEST_WRAP_END(output) \
  18. RECORD_TIME_END(output); \
  19. } \
  20. spi_flash_enable_interrupts_caches_and_other_cpu();
  21. typedef void (*test_f)(uint32_t* t_op);
  22. static const char TAG[] = "test_atomic";
  23. static uint32_t s_t_ref;
  24. static void sorted_array_insert(uint32_t* array, int* size, uint32_t item)
  25. {
  26. int pos;
  27. for (pos = *size; pos>0; pos--) {
  28. if (array[pos-1] < item) break;
  29. array[pos] = array[pos-1];
  30. }
  31. array[pos]=item;
  32. (*size)++;
  33. }
  34. static void test_flow(const char* name, test_f func)
  35. {
  36. int t_flight_num = 0;
  37. uint32_t t_flight_sorted[TEST_TIMES];
  38. for (int i = 0; i < TEST_TIMES; i++) {
  39. uint32_t t_op;
  40. func(&t_op);
  41. sorted_array_insert(t_flight_sorted, &t_flight_num, t_op);
  42. }
  43. for (int i = 0; i < TEST_TIMES; i++) {
  44. ESP_LOGI(TAG, "%s: %d ops", name, t_flight_sorted[i]-s_t_ref);
  45. }
  46. }
  47. static IRAM_ATTR void test_ref(uint32_t* t_op)
  48. {
  49. TEST_WRAP_START()
  50. TEST_WRAP_END(t_op)
  51. s_t_ref = *t_op;
  52. }
  53. static IRAM_ATTR void test_atomic_load(uint32_t* t_op)
  54. {
  55. atomic_uint_fast32_t var;
  56. uint32_t target = rand();
  57. TEST_WRAP_START()
  58. target = atomic_load(&var);
  59. TEST_WRAP_END(t_op)
  60. (void) target;
  61. }
  62. static IRAM_ATTR void test_atomic_store(uint32_t* t_op)
  63. {
  64. atomic_uint_fast32_t var;
  65. uint32_t src = rand();
  66. TEST_WRAP_START()
  67. atomic_store(&var, src);
  68. TEST_WRAP_END(t_op)
  69. }
  70. static IRAM_ATTR void test_atomic_store_load(uint32_t* t_op)
  71. {
  72. atomic_uint_fast32_t var;
  73. uint32_t src = rand();
  74. TEST_WRAP_START()
  75. atomic_store(&var, src);
  76. src = atomic_load(&var);
  77. TEST_WRAP_END(t_op)
  78. }
  79. static IRAM_ATTR void test_atomic_fetch_and(uint32_t* t_op)
  80. {
  81. atomic_uint_fast32_t var;
  82. uint32_t src = rand();
  83. TEST_WRAP_START()
  84. src = atomic_fetch_and(&var, src);
  85. TEST_WRAP_END(t_op)
  86. }
  87. static IRAM_ATTR void test_atomic_fetch_or(uint32_t* t_op)
  88. {
  89. atomic_uint_fast32_t var;
  90. uint32_t src = rand();
  91. TEST_WRAP_START()
  92. src = atomic_fetch_or(&var, src);
  93. TEST_WRAP_END(t_op)
  94. }
  95. static IRAM_ATTR void test_atomic_compare_exchange(uint32_t* t_op)
  96. {
  97. atomic_uint_fast32_t var;
  98. uint32_t src = rand();
  99. uint32_t cmp = rand();
  100. bool res;
  101. TEST_WRAP_START()
  102. res = atomic_compare_exchange_weak(&var, &src, cmp);
  103. TEST_WRAP_END(t_op)
  104. (void) res;
  105. }
  106. TEST_CASE("test atomic","[atomic]")
  107. {
  108. test_flow("ref", test_ref);
  109. test_flow("atomic_load", test_atomic_load);
  110. test_flow("atomic_store", test_atomic_store);
  111. test_flow("atomic_compare_exchange", test_atomic_compare_exchange);
  112. test_flow("atomic_store + atomic_load", test_atomic_store_load);
  113. test_flow("atomic_and", test_atomic_fetch_and);
  114. test_flow("atomic_or", test_atomic_fetch_or);
  115. }