test_freertos_scheduling_time.c 2.1 KB

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