test_spinlocks.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Combined unit tests & benchmarking for spinlock "portMUX" functionality
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/semphr.h"
  9. #include "freertos/queue.h"
  10. #include "unity.h"
  11. #include "hal/cpu_hal.h"
  12. #include "test_utils.h"
  13. #define REPEAT_OPS 10000
  14. static uint32_t start, end;
  15. #define BENCHMARK_START() do { \
  16. start = cpu_hal_get_cycle_count(); \
  17. } while(0)
  18. #define BENCHMARK_END(OPERATION) do { \
  19. end = cpu_hal_get_cycle_count(); \
  20. printf("%s took %d cycles/op (%d cycles for %d ops)\n", \
  21. OPERATION, (end - start)/REPEAT_OPS, \
  22. (end - start), REPEAT_OPS); \
  23. } while(0)
  24. TEST_CASE("portMUX spinlocks (no contention)", "[freertos]")
  25. {
  26. portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
  27. BENCHMARK_START();
  28. for (int i = 0; i < REPEAT_OPS; i++) {
  29. portENTER_CRITICAL_ISR(&mux);
  30. portEXIT_CRITICAL_ISR(&mux);
  31. }
  32. BENCHMARK_END("no contention lock");
  33. #ifdef CONFIG_FREERTOS_UNICORE
  34. TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP_UNICORE, "%d cycles/op", ((end - start)/REPEAT_OPS));
  35. #else
  36. #if CONFIG_SPIRAM
  37. TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP_PSRAM, "%d cycles/op", ((end - start)/REPEAT_OPS));
  38. #else
  39. TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP, "%d cycles/op", ((end - start)/REPEAT_OPS));
  40. #endif
  41. #endif
  42. }
  43. TEST_CASE("portMUX recursive locks (no contention)", "[freertos]")
  44. {
  45. portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
  46. BENCHMARK_START();
  47. const int RECURSE_COUNT = 25;
  48. for (int i = 0; i < REPEAT_OPS / RECURSE_COUNT; i++) {
  49. for (int j = 0; j < RECURSE_COUNT; j++) {
  50. portENTER_CRITICAL(&mux);
  51. }
  52. for (int j = 0; j < RECURSE_COUNT; j++) {
  53. portEXIT_CRITICAL(&mux);
  54. }
  55. }
  56. BENCHMARK_END("no contention recursive");
  57. }
  58. #if portNUM_PROCESSORS == 2
  59. static volatile int shared_value;
  60. static portMUX_TYPE shared_mux;
  61. static xSemaphoreHandle done_sem;
  62. static void task_shared_value_increment(void *ignore)
  63. {
  64. for (int i = 0; i < REPEAT_OPS; i++) {
  65. portENTER_CRITICAL(&shared_mux);
  66. shared_value++;
  67. portEXIT_CRITICAL(&shared_mux);
  68. }
  69. xSemaphoreGive(done_sem);
  70. vTaskDelete(NULL);
  71. }
  72. TEST_CASE("portMUX cross-core locking", "[freertos]")
  73. {
  74. done_sem = xSemaphoreCreateCounting(2, 0);
  75. portMUX_INITIALIZE(&shared_mux);
  76. shared_value = 0;
  77. BENCHMARK_START();
  78. xTaskCreatePinnedToCore(task_shared_value_increment, "INC0", 2048, NULL, UNITY_FREERTOS_PRIORITY + 1, NULL, UNITY_FREERTOS_CPU ? 0 : 1);
  79. xTaskCreatePinnedToCore(task_shared_value_increment, "INC1", 2048, NULL, UNITY_FREERTOS_PRIORITY + 1, NULL, UNITY_FREERTOS_CPU);
  80. for(int i = 0; i < 2; i++) {
  81. if(!xSemaphoreTake(done_sem, 10000/portTICK_PERIOD_MS)) {
  82. TEST_FAIL_MESSAGE("done_sem not released by test task");
  83. }
  84. }
  85. BENCHMARK_END("cross-core incrementing");
  86. vSemaphoreDelete(done_sem);
  87. TEST_ASSERT_EQUAL_INT(REPEAT_OPS * 2, shared_value);
  88. }
  89. TEST_CASE("portMUX high contention", "[freertos]")
  90. {
  91. const int TOTAL_TASKS = 8; /* half on each core */
  92. done_sem = xSemaphoreCreateCounting(TOTAL_TASKS, 0);
  93. portMUX_INITIALIZE(&shared_mux);
  94. shared_value = 0;
  95. BENCHMARK_START();
  96. for (int i = 0; i < TOTAL_TASKS / 2; i++) {
  97. /* as each task has a higher priority than previous, expect
  98. them to preempt the earlier created task, at least on the
  99. other core (this core has the unity task, until that
  100. blocks)... */
  101. xTaskCreatePinnedToCore(task_shared_value_increment, "INC0", 2048, NULL, tskIDLE_PRIORITY + 1 + i, NULL, UNITY_FREERTOS_CPU ? 0 : 1);
  102. xTaskCreatePinnedToCore(task_shared_value_increment, "INC1", 2048, NULL, tskIDLE_PRIORITY + 1 + i, NULL, UNITY_FREERTOS_CPU);
  103. }
  104. for(int i = 0; i < TOTAL_TASKS; i++) {
  105. if(!xSemaphoreTake(done_sem, 10000/portTICK_PERIOD_MS)) {
  106. TEST_FAIL_MESSAGE("done_sem not released by test task");
  107. }
  108. }
  109. BENCHMARK_END("cross-core high contention");
  110. vSemaphoreDelete(done_sem);
  111. TEST_ASSERT_EQUAL_INT(REPEAT_OPS * TOTAL_TASKS, shared_value);
  112. }
  113. #endif // portNUM_PROCESSORS == 2