From 2f85ed9bf63aa52d162004ddc619f70e039762ff Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Thu, 8 Feb 2024 13:37:39 +0800 Subject: [PATCH] Add SMP schedule equal priority on target test (#1170) * Add SMP schedule equal priority on target test * Remove unnecessary config * Fix spelling format and header * Code review suggestions Signed-off-by: Gaurav Aggarwal --------- Signed-off-by: Gaurav Aggarwal Co-authored-by: Gaurav Aggarwal Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> Co-authored-by: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com> --- .../schedule_equal_priority/CMakeLists.txt | 33 +++ .../schedule_equal_priority_test_runner.c | 73 +++++++ .../schedule_equal_priority.c | 189 ++++++++++++++++++ .../smp/schedule_equal_priority/test_config.h | 53 +++++ 4 files changed, 348 insertions(+) create mode 100644 FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/CMakeLists.txt create mode 100644 FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/schedule_equal_priority_test_runner.c create mode 100644 FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/schedule_equal_priority.c create mode 100644 FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/test_config.h diff --git a/FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/CMakeLists.txt b/FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/CMakeLists.txt new file mode 100644 index 0000000000..ae70e497d4 --- /dev/null +++ b/FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.13) + +project(example C CXX ASM) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) + +set(TEST_INCLUDE_PATHS ${CMAKE_CURRENT_LIST_DIR}/../../../../../tests/smp/schedule_equal_priority) +set(TEST_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../../../tests/smp/schedule_equal_priority) + +add_library(schedule_equal_priority INTERFACE) +target_sources(schedule_equal_priority INTERFACE + ${BOARD_LIBRARY_DIR}/main.c + ${CMAKE_CURRENT_LIST_DIR}/schedule_equal_priority_test_runner.c + ${TEST_SOURCE_DIR}/schedule_equal_priority.c) + +target_include_directories(schedule_equal_priority INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/../../.. + ${TEST_INCLUDE_PATHS} + ) + +target_link_libraries(schedule_equal_priority INTERFACE + FreeRTOS-Kernel + FreeRTOS-Kernel-Heap4 + ${BOARD_LINK_LIBRARIES}) + +add_executable(test_schedule_equal_priority) +enable_board_functions(test_schedule_equal_priority) +target_link_libraries(test_schedule_equal_priority schedule_equal_priority) +target_include_directories(test_schedule_equal_priority PUBLIC + ${BOARD_INCLUDE_PATHS}) +target_compile_definitions(test_schedule_equal_priority PRIVATE + ${BOARD_DEFINES} +) diff --git a/FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/schedule_equal_priority_test_runner.c b/FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/schedule_equal_priority_test_runner.c new file mode 100644 index 0000000000..cc5e28543e --- /dev/null +++ b/FreeRTOS/Test/Target/boards/pico/tests/smp/schedule_equal_priority/schedule_equal_priority_test_runner.c @@ -0,0 +1,73 @@ +/* + * FreeRTOS V202212.00 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +/** + * @file schedule_equal_priority_test_runner.c + * @brief The implementation of main function to start test runner task. + * + * Procedure: + * - Initialize environment. + * - Run the test case. + */ + +/* Kernel includes. */ +#include "FreeRTOS.h" +#include "task.h" + +/* Unit testing support functions. */ +#include "unity.h" + +/* Pico includes. */ +#include "pico/multicore.h" +#include "pico/stdlib.h" + +/*-----------------------------------------------------------*/ + +static void prvTestRunnerTask( void * pvParameters ); + +/*-----------------------------------------------------------*/ + +static void prvTestRunnerTask( void * pvParameters ) +{ + ( void ) pvParameters; + + /* Run test case. */ + vRunScheduleEqualPriorityTest(); + + vTaskDelete( NULL ); +} +/*-----------------------------------------------------------*/ + +void vRunTest( void ) +{ + xTaskCreate( prvTestRunnerTask, + "testRunner", + configMINIMAL_STACK_SIZE, + NULL, + configMAX_PRIORITIES - 1, + NULL ); +} +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/schedule_equal_priority.c b/FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/schedule_equal_priority.c new file mode 100644 index 0000000000..785950b7a1 --- /dev/null +++ b/FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/schedule_equal_priority.c @@ -0,0 +1,189 @@ +/* + * FreeRTOS V202212.00 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +/** + * @file schedule_equal_priority.c + * @brief The scheduler shall schedule tasks of equal priority in a round robin fashion. + * + * Procedure: + * - Create ( num of cores + 1 ) tasks ( T0~Tn ). Priority T0 = T1 = ... = Tn-1 = Tn. + * - All tasks are running in busy loop. + * Expected: + * - Equal priority tasks are scheduled in a round robin fashion when configUSE_TIME_SLICING + * is set to 1. Verify that all the test tasks get chance to run. + */ + +/* Standard includes. */ +#include + +/* Kernel includes. */ +#include "FreeRTOS.h" +#include "task.h" + +/* Unit testing support functions. */ +#include "unity.h" +/*-----------------------------------------------------------*/ + +/** + * @brief Timeout value to stop test. + */ +#define TEST_TIMEOUT_TICKS ( ( configNUMBER_OF_CORES + 1U ) * 2U ) + +/** + * @brief Nop operation for busy looping. + */ +#ifndef portNOP + #define TEST_NOP() __asm volatile ( "nop" ) +#else + #define TEST_NOP portNOP +#endif +/*-----------------------------------------------------------*/ + +#if ( configNUMBER_OF_CORES < 2 ) + #error This test is for FreeRTOS SMP and therefore, requires at least 2 cores. +#endif /* if configNUMBER_OF_CORES != 2 */ + +#if ( configUSE_TIME_SLICING != 1 ) + #error configUSE_TIME_SLICING must be enabled by including test_config.h in FreeRTOSConfig.h. +#endif /* if configUSE_TIME_SLICING != 1 */ + +#if ( configMAX_PRIORITIES <= 2 ) + #error configMAX_PRIORITIES must be larger than 2 to avoid scheduling idle tasks unexpectedly. +#endif /* if ( configMAX_PRIORITIES <= 2 ) */ +/*-----------------------------------------------------------*/ + +/** + * @brief Test case "schedule equal priority tasks.". + */ +void Test_ScheduleEqualPriority( void ); + +/** + * @brief Function that implements a never blocking FreeRTOS task. + */ +static void prvEverRunningTask( void * pvParameters ); +/*-----------------------------------------------------------*/ + +/** + * @brief Handles of the tasks created in this test. + */ +static TaskHandle_t xTaskHandles[ configNUMBER_OF_CORES + 1 ]; + +/** + * @brief Flags to indicate that all ever running tasks get a chance to run. + */ +static BaseType_t xTaskRun[ configNUMBER_OF_CORES + 1 ]; +/*-----------------------------------------------------------*/ + +static void prvEverRunningTask( void * pvParameters ) +{ + BaseType_t * pxTaskRun = ( ( BaseType_t * ) pvParameters ); + + /* Set the flag for testRunner to check whether all tasks have run. */ + if( pxTaskRun != NULL ) + { + *pxTaskRun = pdTRUE; + } + + for( ; ; ) + { + /* Always running, put asm here to avoid optimization by compiler. */ + TEST_NOP(); + } +} +/*-----------------------------------------------------------*/ + +void Test_ScheduleEqualPriority( void ) +{ + uint32_t i; + BaseType_t xTaskCreationResult; + + /* Create ( configNUMBER_OF_CORES + 1 ) low priority tasks. */ + for( i = 0; i < ( configNUMBER_OF_CORES + 1 ); i++ ) + { + xTaskCreationResult = xTaskCreate( prvEverRunningTask, + "EverRun", + configMINIMAL_STACK_SIZE, + &( xTaskRun[ i ] ), + configMAX_PRIORITIES - 2, + &( xTaskHandles[ i ] ) ); + + TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskCreationResult, "Task creation failed." ); + } + + /* TEST_TIMEOUT_TICKS is long enough to run each task. */ + vTaskDelay( TEST_TIMEOUT_TICKS ); + + for( i = 0; i < ( configNUMBER_OF_CORES + 1 ); i++ ) + { + /* After timeout, all tasks must have been scheduled at least once and + * have set their corresponding flag to pdTRUE. */ + TEST_ASSERT_EQUAL( pdTRUE, xTaskRun[ i ] ); + } +} +/*-----------------------------------------------------------*/ + +/* Runs before every test, put init calls here. */ +void setUp( void ) +{ + uint32_t i; + + for( i = 0; i < ( configNUMBER_OF_CORES + 1 ); i++ ) + { + xTaskRun[ i ] = pdFALSE; + xTaskHandles[ i ] = NULL; + } +} +/*-----------------------------------------------------------*/ + +/* Runs after every test, put clean-up calls here. */ +void tearDown( void ) +{ + uint32_t i; + + /* Delete all the tasks. */ + for( i = 0; i < ( configNUMBER_OF_CORES + 1 ); i++ ) + { + if( xTaskHandles[ i ] != NULL ) + { + vTaskDelete( xTaskHandles[ i ] ); + xTaskHandles[ i ] = NULL; + } + } +} +/*-----------------------------------------------------------*/ + +/** + * @brief Entry point for test runner to run schedule equal priority test. + */ +void vRunScheduleEqualPriorityTest( void ) +{ + UNITY_BEGIN(); + + RUN_TEST( Test_ScheduleEqualPriority ); + + UNITY_END(); +} +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/test_config.h b/FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/test_config.h new file mode 100644 index 0000000000..28f8dbe708 --- /dev/null +++ b/FreeRTOS/Test/Target/tests/smp/schedule_equal_priority/test_config.h @@ -0,0 +1,53 @@ +/* + * FreeRTOS V202212.00 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef TEST_CONFIG_H +#define TEST_CONFIG_H + +/* This file must be included at the end of the FreeRTOSConfig.h. It contains + * any FreeRTOS specific configurations that the test requires. */ + +#ifdef configUSE_TIME_SLICING + #undef configUSE_TIME_SLICING +#endif /* ifdef configUSE_TIME_SLICING */ + +#ifdef configUSE_PREEMPTION + #undef configUSE_PREEMPTION +#endif /* ifdef configUSE_PREEMPTION */ + +#define configUSE_TIME_SLICING 1 +#define configUSE_PREEMPTION 1 + +/*-----------------------------------------------------------*/ + +/** + * @brief Entry point for test runner to run schedule equal priority test. + */ +void vRunScheduleEqualPriorityTest( void ); + +/*-----------------------------------------------------------*/ + +#endif /* ifndef TEST_CONFIG_H */