2022-04-20 07:31:49 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-11-06 03:59:16 -05:00
|
|
|
#include <esp_types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "esp_intr_alloc.h"
|
|
|
|
#include "unity.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
|
2024-07-25 10:47:02 -04:00
|
|
|
#define NUMBER_OF_ITERATIONS 1023
|
|
|
|
|
|
|
|
static int compare_uint32(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return (*(uint32_t *)a - * (uint32_t *)b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t calculate_median(uint32_t *values, int size)
|
|
|
|
{
|
|
|
|
/* Sort the array of values containing the scheduling time of each iteration */
|
|
|
|
qsort(values, size, sizeof(uint32_t), compare_uint32);
|
|
|
|
|
|
|
|
/* Return the median value. (size is an odd number) */
|
|
|
|
return values[size / 2];
|
|
|
|
}
|
2020-04-03 15:57:57 -04:00
|
|
|
|
2019-11-06 03:59:16 -05:00
|
|
|
typedef struct {
|
|
|
|
SemaphoreHandle_t end_sema;
|
|
|
|
uint32_t before_sched;
|
2024-07-25 10:47:02 -04:00
|
|
|
uint32_t cycles_to_sched[NUMBER_OF_ITERATIONS];
|
2019-11-08 00:27:02 -05:00
|
|
|
TaskHandle_t t1_handle;
|
2019-11-06 03:59:16 -05:00
|
|
|
} test_context_t;
|
|
|
|
|
|
|
|
static void test_task_1(void *arg) {
|
|
|
|
test_context_t *context = (test_context_t *)arg;
|
|
|
|
|
2019-11-08 00:27:02 -05:00
|
|
|
for( ;; ) {
|
2019-11-06 03:59:16 -05:00
|
|
|
context->before_sched = portGET_RUN_TIME_COUNTER_VALUE();
|
|
|
|
vPortYield();
|
|
|
|
}
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
vTaskDelete(NULL);
|
2019-11-06 03:59:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_task_2(void *arg) {
|
|
|
|
test_context_t *context = (test_context_t *)arg;
|
2020-04-03 15:57:57 -04:00
|
|
|
|
|
|
|
vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
|
|
|
|
vTaskPrioritySet(context->t1_handle, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
|
|
|
|
vPortYield();
|
2019-11-06 03:59:16 -05:00
|
|
|
|
2024-07-25 10:47:02 -04:00
|
|
|
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
|
|
|
|
context->cycles_to_sched[i] = portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched;
|
2019-11-06 03:59:16 -05:00
|
|
|
vPortYield();
|
|
|
|
}
|
|
|
|
|
|
|
|
xSemaphoreGive(context->end_sema);
|
2024-07-25 10:47:02 -04:00
|
|
|
vTaskDelete(context->t1_handle);
|
2019-11-06 03:59:16 -05:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("scheduling time test", "[freertos]")
|
|
|
|
{
|
2020-11-10 02:40:01 -05:00
|
|
|
test_context_t context;
|
2019-11-06 03:59:16 -05:00
|
|
|
|
|
|
|
context.end_sema = xSemaphoreCreateBinary();
|
|
|
|
TEST_ASSERT(context.end_sema != NULL);
|
|
|
|
|
2019-11-08 00:27:02 -05:00
|
|
|
#if !CONFIG_FREERTOS_UNICORE
|
|
|
|
xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, 1, &context.t1_handle,1);
|
2019-11-06 03:59:16 -05:00
|
|
|
xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, 1, NULL,1);
|
2019-11-08 00:27:02 -05:00
|
|
|
#else
|
2020-04-03 15:57:57 -04:00
|
|
|
xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, &context.t1_handle,0);
|
|
|
|
xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, NULL,0);
|
2019-11-08 00:27:02 -05:00
|
|
|
#endif
|
2019-11-06 03:59:16 -05:00
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
BaseType_t result = xSemaphoreTake(context.end_sema, portMAX_DELAY);
|
2019-11-06 03:59:16 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX32(pdTRUE, result);
|
2024-07-25 10:47:02 -04:00
|
|
|
|
|
|
|
/* We calculate the median instead of the average to avoid outliers which may occur due to cache speed variabilities */
|
|
|
|
uint32_t median_cycles = calculate_median(context.cycles_to_sched, NUMBER_OF_ITERATIONS);
|
|
|
|
TEST_PERFORMANCE_LESS_THAN(SCHEDULING_TIME, "%d cycles", median_cycles);
|
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
vSemaphoreDelete(context.end_sema);
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|