test_spinlocks.c 4.5 KB

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