test_freertos_scheduling_time.c
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 14 #define NUMBER_OF_ITERATIONS 10 15 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 23 static void test_task_1(void *arg) { 24 test_context_t *context = (test_context_t *)arg; 25 26 for( ;; ) { 27 context->before_sched = portGET_RUN_TIME_COUNTER_VALUE(); 28 vPortYield(); 29 } 30 31 vTaskDelete(NULL); 32 } 33 34 static void test_task_2(void *arg) { 35 test_context_t *context = (test_context_t *)arg; 36 uint64_t accumulator = 0; 37 38 vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY + 1); 39 vTaskPrioritySet(context->t1_handle, CONFIG_UNITY_FREERTOS_PRIORITY + 1); 40 vPortYield(); 41 42 for(int i = 0; i < NUMBER_OF_ITERATIONS; i++) { 43 accumulator += (portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched); 44 vPortYield(); 45 } 46 47 context->cycles_to_sched = accumulator / NUMBER_OF_ITERATIONS; 48 vTaskDelete(context->t1_handle); 49 xSemaphoreGive(context->end_sema); 50 vTaskDelete(NULL); 51 } 52 53 TEST_CASE("scheduling time test", "[freertos]") 54 { 55 test_context_t context; 56 57 context.end_sema = xSemaphoreCreateBinary(); 58 TEST_ASSERT(context.end_sema != NULL); 59 60 #if !CONFIG_FREERTOS_UNICORE 61 xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, 1, &context.t1_handle,1); 62 xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, 1, NULL,1); 63 #else 64 xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, &context.t1_handle,0); 65 xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, NULL,0); 66 #endif 67 68 BaseType_t result = xSemaphoreTake(context.end_sema, portMAX_DELAY); 69 TEST_ASSERT_EQUAL_HEX32(pdTRUE, result); 70 TEST_PERFORMANCE_LESS_THAN(SCHEDULING_TIME , "scheduling time %d cycles" ,context.cycles_to_sched); 71 }