test_spinlocks.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "idf_performance.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(&mux);
  35. portEXIT_CRITICAL(&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. TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP, "%d cycles/op", ((end - start)/REPEAT_OPS));
  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