test_freertos_scheduling_time.c 2.2 KB

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