Skip to content

Commit

Permalink
Add SMP task delete on target test (FreeRTOS#1176)
Browse files Browse the repository at this point in the history
Add SMP task delete on target test
  • Loading branch information
chinglee-iot committed Aug 16, 2024
1 parent 6ed67f5 commit 520fc22
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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/task_delete)
set(TEST_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../../../tests/smp/task_delete)

add_library(task_delete INTERFACE)
target_sources(task_delete INTERFACE
${BOARD_LIBRARY_DIR}/main.c
${CMAKE_CURRENT_LIST_DIR}/task_delete_test_runner.c
${TEST_SOURCE_DIR}/task_delete.c)

target_include_directories(task_delete INTERFACE
${CMAKE_CURRENT_LIST_DIR}/../../..
${TEST_INCLUDE_PATHS}
)

target_link_libraries(task_delete INTERFACE
FreeRTOS-Kernel
FreeRTOS-Kernel-Heap4
${BOARD_LINK_LIBRARIES})

add_executable(test_task_delete)
enable_board_functions(test_task_delete)
target_link_libraries(test_task_delete task_delete)
target_include_directories(test_task_delete PUBLIC
${BOARD_INCLUDE_PATHS})
target_compile_definitions(test_task_delete PRIVATE
${BOARD_DEFINES}
)
Original file line number Diff line number Diff line change
@@ -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 task_delete_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. */
vRunTaskDeleteTest();

vTaskDelete( NULL );
}
/*-----------------------------------------------------------*/

void vRunTest( void )
{
xTaskCreate( prvTestRunnerTask,
"testRunner",
configMINIMAL_STACK_SIZE,
NULL,
configMAX_PRIORITIES - 1,
NULL );
}
/*-----------------------------------------------------------*/
260 changes: 260 additions & 0 deletions FreeRTOS/Test/Target/tests/smp/task_delete/task_delete.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* 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 tasks_delete.c
* @brief When a n RTOS object is deleted, the associated resources shall be freed.
*
* Procedure:
* - TestRunner records original memory size.
* - Create ( num of cores ) tasks T0 ~ T(n - 1).
* - Tasks T0 ~ T(n - 1) delete themselves.
* - TestRunner checks if memory is freed.
* - Create ( num of cores ) tasks T0 ~ T(n - 1).
* - Task T0 ~ T(n - 1) delay in loop.
* - TestRunner deletes T0 ~ T(n - 1).
* - TestRunner checks if memory freed.
* Expected:
* - Have same remaining memory before creating task and after deleting task.
*/

/* Standard includes. */
#include <stdint.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Unit testing support functions. */
#include "unity.h"

/*-----------------------------------------------------------*/

/**
* @brief Timeout value to stop test.
*/
#define TEST_TIMEOUT_MS ( 1000 )

/*-----------------------------------------------------------*/

#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 ( 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 "Task Delete".
*/
void Test_TaskDelete( void );

/**
* @brief Partial test case in "Task Delete" for "Self deletion".
*/
static void prvTestTaskSelfDelete( void );

/**
* @brief Partial test case in "Task Delete" for "Remote deletion".
*/
static void prvTestTaskRemoteDelete( void );

/**
* @brief Task entry to delete itself.
*/
static void prvSelfDeleteTask( void * pvParameters );

/**
* @brief Task entry to loop in delay.
*/
static void prvDelayTask( void * pvParameters );
/*-----------------------------------------------------------*/

/**
* @brief Handles of the tasks created in this test.
*/
static TaskHandle_t xTaskHandles[ configNUMBER_OF_CORES ];

/**
* @brief Flag to indicate task run status.
*/
static BaseType_t xTaskRunStatus[ configNUMBER_OF_CORES ];

/**
* @brief The heap size before creating tasks T0 ~ T(n - 1).
*/
static uint32_t ulOriginalFreeHeapSize;
/*-----------------------------------------------------------*/

static void prvSelfDeleteTask( void * pvParameters )
{
BaseType_t * pxTaskRunStatus = ( BaseType_t * ) pvParameters;

/* Set the flag to indicate the task has run. */
*pxTaskRunStatus = pdTRUE;

vTaskDelete( NULL );

/* The task delete itself. This line should not be run. */
*pxTaskRunStatus = pdFALSE;
}
/*-----------------------------------------------------------*/

static void prvDelayTask( void * pvParameters )
{
BaseType_t * pxTaskRunStatus = ( BaseType_t * ) pvParameters;

/* Set the flag to indicate the task has run. */
*pxTaskRunStatus = pdTRUE;

/* Block this task forever. */
vTaskDelay( portMAX_DELAY );
}
/*-----------------------------------------------------------*/

static void prvTestTaskSelfDelete( void )
{
uint32_t i;
BaseType_t xTaskCreationResult;
uint32_t ulFreeHeapSize;

/* Create configNUMBER_OF_CORES low priority tasks. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
xTaskRunStatus[ i ] = pdFALSE;
xTaskCreationResult = xTaskCreate( prvSelfDeleteTask,
"SelfDel",
configMINIMAL_STACK_SIZE,
( void * ) ( &( xTaskRunStatus[ i ] ) ),
configMAX_PRIORITIES - 2,
&( xTaskHandles[ i ] ) );

TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskCreationResult, "Task creation failed." );
}

/* Wait task to delete itself. */
vTaskDelay( pdMS_TO_TICKS( TEST_TIMEOUT_MS ) );

/* Verify the task run status. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
TEST_ASSERT_EQUAL_MESSAGE( pdTRUE, xTaskRunStatus[ i ], "Task hasn't been run." );
xTaskHandles[ i ] = NULL;
}

/* Verify the memory used for task TCB and stack is freed. */
ulFreeHeapSize = xPortGetFreeHeapSize();
TEST_ASSERT_EQUAL_INT_MESSAGE( ulOriginalFreeHeapSize, ulFreeHeapSize, "Self deleted task test failed." );
}
/*-----------------------------------------------------------*/

static void prvTestTaskRemoteDelete( void )
{
uint32_t i;
BaseType_t xTaskCreationResult;
uint32_t ulFreeHeapSize;

/* Create configNUMBER_OF_CORES low priority tasks. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
xTaskRunStatus[ i ] = pdFALSE;
xTaskCreationResult = xTaskCreate( prvDelayTask,
"KeepDelay",
configMINIMAL_STACK_SIZE,
( void * ) ( &( xTaskRunStatus[ i ] ) ),
configMAX_PRIORITIES - 2,
&( xTaskHandles[ i ] ) );

TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskCreationResult, "Task creation failed." );
}

/* Delay a while for tasks just created to run. */
vTaskDelay( pdMS_TO_TICKS( TEST_TIMEOUT_MS ) );

/* Verify the task run status. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
TEST_ASSERT_EQUAL_MESSAGE( pdTRUE, xTaskRunStatus[ i ], "Task hasn't been run." );
}

/* Delete tasks remotely. */
for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
vTaskDelete( xTaskHandles[ i ] );
xTaskHandles[ i ] = NULL;
}

ulFreeHeapSize = xPortGetFreeHeapSize();
TEST_ASSERT_EQUAL_INT_MESSAGE( ulOriginalFreeHeapSize, ulFreeHeapSize, "Remote deleted task test failed." );
}
/*-----------------------------------------------------------*/

void Test_TaskDelete( void )
{
prvTestTaskSelfDelete();
prvTestTaskRemoteDelete();
}
/*-----------------------------------------------------------*/

/* Runs before every test, put init calls here. */
void setUp( void )
{
/* Get the heap size before creating tasks. */
ulOriginalFreeHeapSize = xPortGetFreeHeapSize();
}
/*-----------------------------------------------------------*/

/* Runs after every test, put clean-up calls here. */
void tearDown( void )
{
uint32_t i;

for( i = 0; i < configNUMBER_OF_CORES; i++ )
{
if( xTaskHandles[ i ] != NULL )
{
vTaskDelete( xTaskHandles[ i ] );
xTaskHandles[ i ] = NULL;
}
}
}
/*-----------------------------------------------------------*/

/**
* @brief Entry point for test runner to task delete test.
*/
void vRunTaskDeleteTest( void )
{
UNITY_BEGIN();

RUN_TEST( Test_TaskDelete );

UNITY_END();
}
/*-----------------------------------------------------------*/
Loading

0 comments on commit 520fc22

Please sign in to comment.