test_atomic.c 3.5 KB

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