test_spinlocks.c 4.4 KB

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