test_freertos_scheduling_time.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <esp_types.h>
  2. #include <stdio.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/semphr.h"
  6. #include "freertos/queue.h"
  7. #include "esp_intr_alloc.h"
  8. #include "unity.h"
  9. #include "soc/cpu.h"
  10. #include "test_utils.h"
  11. #define NUMBER_OF_ITERATIONS 10
  12. typedef struct {
  13. SemaphoreHandle_t end_sema;
  14. uint32_t before_sched;
  15. uint32_t cycles_to_sched;
  16. TaskHandle_t t1_handle;
  17. } test_context_t;
  18. static void test_task_1(void *arg) {
  19. test_context_t *context = (test_context_t *)arg;
  20. for( ;; ) {
  21. context->before_sched = portGET_RUN_TIME_COUNTER_VALUE();
  22. vPortYield();
  23. }
  24. vTaskDelete(NULL);
  25. }
  26. static void test_task_2(void *arg) {
  27. test_context_t *context = (test_context_t *)arg;
  28. uint64_t accumulator = 0;
  29. vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
  30. vTaskPrioritySet(context->t1_handle, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
  31. vPortYield();
  32. for(int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
  33. accumulator += (portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched);
  34. vPortYield();
  35. }
  36. context->cycles_to_sched = accumulator / NUMBER_OF_ITERATIONS;
  37. vTaskDelete(context->t1_handle);
  38. xSemaphoreGive(context->end_sema);
  39. vTaskDelete(NULL);
  40. }
  41. TEST_CASE("scheduling time test", "[freertos]")
  42. {
  43. test_context_t context;
  44. context.end_sema = xSemaphoreCreateBinary();
  45. TEST_ASSERT(context.end_sema != NULL);
  46. #if !CONFIG_FREERTOS_UNICORE
  47. xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, 1, &context.t1_handle,1);
  48. xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, 1, NULL,1);
  49. #else
  50. xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, &context.t1_handle,0);
  51. xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, NULL,0);
  52. #endif
  53. BaseType_t result = xSemaphoreTake(context.end_sema, portMAX_DELAY);
  54. TEST_ASSERT_EQUAL_HEX32(pdTRUE, result);
  55. TEST_PERFORMANCE_LESS_THAN(SCHEDULING_TIME , "%d cycles" ,context.cycles_to_sched);
  56. }