diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-09-03 18:08:27 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-09-03 20:48:55 +0200 |
commit | 569f27aaaa7a707a65bb49a10ca954e64cb56db1 (patch) | |
tree | 03ef16ba0d5a61c1bc652f1300b7e1583020bd09 | |
parent | af0c9adf872cde70520e32305bd223a0ea878efd (diff) |
validation: Test thread pinning
-rw-r--r-- | spec/build/testsuites/validation/validation-smp-only-0.yml | 1 | ||||
-rw-r--r-- | testsuites/validation/tc-score-smp-thread.c | 245 |
2 files changed, 246 insertions, 0 deletions
diff --git a/spec/build/testsuites/validation/validation-smp-only-0.yml b/spec/build/testsuites/validation/validation-smp-only-0.yml index fcb5dd214d..493a838438 100644 --- a/spec/build/testsuites/validation/validation-smp-only-0.yml +++ b/spec/build/testsuites/validation/validation-smp-only-0.yml @@ -15,6 +15,7 @@ source: - testsuites/validation/tc-bsp-interrupt-spurious.c - testsuites/validation/tc-scheduler-smp-only.c - testsuites/validation/tc-score-smp-per-cpu-jobs.c +- testsuites/validation/tc-score-smp-thread.c - testsuites/validation/ts-validation-smp-only-0.c stlib: [] target: testsuites/validation/ts-validation-smp-only-0.exe diff --git a/testsuites/validation/tc-score-smp-thread.c b/testsuites/validation/tc-score-smp-thread.c new file mode 100644 index 0000000000..4dd544cd73 --- /dev/null +++ b/testsuites/validation/tc-score-smp-thread.c @@ -0,0 +1,245 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup RTEMSTestCaseScoreThreadValSmp + */ + +/* + * Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * This file is part of the RTEMS quality process and was automatically + * generated. If you find something that needs to be fixed or + * worded better please post a report or patch to an RTEMS mailing list + * or raise a bug report: + * + * https://www.rtems.org/bugs.html + * + * For information on updating and regenerating please refer to the How-To + * section in the Software Requirements Engineering chapter of the + * RTEMS Software Engineering manual. The manual is provided as a part of + * a release. For development sources please refer to the online + * documentation at: + * + * https://docs.rtems.org + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <rtems.h> +#include <rtems/score/threadimpl.h> + +#include "ts-config.h" +#include "tx-support.h" + +#include <rtems/test.h> + +/** + * @defgroup RTEMSTestCaseScoreThreadValSmp spec:/score/thread/val/smp + * + * @ingroup RTEMSTestSuiteTestsuitesValidationSmpOnly0 + * + * @brief Tests SMP-specific thread behaviour. + * + * This test case performs the following actions: + * + * - Create two worker threads and a mutex. Use the mutex and the worker to + * move to a helping scheduler. + * + * - Pin the runner thread while it executes on a processor owned by a + * helping scheduler. + * + * - Preempt the pinned runner thread. Unpin the runner thread. + * + * - Clean up all used resources. + * + * @{ + */ + +/** + * @brief Test context for spec:/score/thread/val/smp test case. + */ +typedef struct { + /** + * @brief This member contains the scheduler A identifier. + */ + rtems_id scheduler_a_id; + + /** + * @brief This member contains the scheduler B identifier. + */ + rtems_id scheduler_b_id; + + /** + * @brief This member contains the mutex identifier. + */ + rtems_id mutex_id; + + /** + * @brief If this member is true, then the worker shall busy wait. + */ + volatile bool busy; +} ScoreThreadValSmp_Context; + +static ScoreThreadValSmp_Context + ScoreThreadValSmp_Instance; + +static T_fixture ScoreThreadValSmp_Fixture = { + .setup = NULL, + .stop = NULL, + .teardown = NULL, + .scope = NULL, + .initial_context = &ScoreThreadValSmp_Instance +}; + +typedef ScoreThreadValSmp_Context Context; + +typedef enum { + EVENT_OBTAIN = RTEMS_EVENT_0, + EVENT_RELEASE = RTEMS_EVENT_1, + EVENT_BUSY = RTEMS_EVENT_2, + EVENT_MOVE_TO_SCHEDULER_B = RTEMS_EVENT_3 +} Event; + +static void WorkerTask( rtems_task_argument arg ) +{ + Context *ctx; + + ctx = (Context *) arg; + + while ( true ) { + rtems_event_set events; + + events = ReceiveAnyEvents(); + + if ( ( events & EVENT_OBTAIN ) != 0 ) { + ObtainMutex( ctx->mutex_id ); + } + + if ( ( events & EVENT_RELEASE ) != 0 ) { + ReleaseMutex( ctx->mutex_id ); + } + + if ( ( events & EVENT_BUSY ) != 0 ) { + while ( ctx->busy ) { + /* Do nothing */ + } + } + + if ( ( events & EVENT_MOVE_TO_SCHEDULER_B ) != 0 ) { + SetSelfScheduler( ctx->scheduler_b_id, PRIO_HIGH ); + } + } +} + +/** + * @brief Create two worker threads and a mutex. Use the mutex and the worker + * to move to a helping scheduler. + */ +static void ScoreThreadValSmp_Action_0( ScoreThreadValSmp_Context *ctx ) +{ + rtems_status_code sc; + rtems_id worker_id; + rtems_id worker_2_id; + Per_CPU_Control *cpu_self; + Thread_Control *executing; + + SetSelfPriority( PRIO_NORMAL ); + + sc = rtems_scheduler_ident( TEST_SCHEDULER_A_NAME, &ctx->scheduler_a_id ); + T_rsc_success( sc ); + + sc = rtems_scheduler_ident( TEST_SCHEDULER_B_NAME, &ctx->scheduler_b_id ); + T_rsc_success( sc ); + + ctx->mutex_id = CreateMutex(); + + worker_id = CreateTask( "WORK", PRIO_HIGH ); + SetScheduler( worker_id, ctx->scheduler_b_id, PRIO_HIGH ); + StartTask( worker_id, WorkerTask, ctx ); + + worker_2_id = CreateTask( "WRK2", PRIO_HIGH ); + StartTask( worker_2_id, WorkerTask, ctx ); + + ObtainMutex( ctx->mutex_id ); + SendEvents( worker_id, EVENT_OBTAIN | EVENT_RELEASE ); + + ctx->busy = true; + SendEvents( worker_2_id, EVENT_BUSY ); + + /* + * Pin the runner thread while it executes on a processor owned by a helping + * scheduler. + */ + T_eq_u32( rtems_scheduler_get_processor(), 1 ); + + executing = _Thread_Get_executing(); + _Thread_Pin( executing ); + + /* + * Preempt the pinned runner thread. Unpin the runner thread. + */ + ctx->busy = false; + SendEvents( worker_2_id, EVENT_MOVE_TO_SCHEDULER_B ); + + T_eq_u32( rtems_scheduler_get_processor(), 1 ); + + cpu_self = _Thread_Dispatch_disable(); + _Thread_Unpin( executing, cpu_self ); + _Thread_Dispatch_direct( cpu_self ); + + /* + * Clean up all used resources. + */ + ReleaseMutex( ctx->mutex_id); + T_eq_u32( rtems_scheduler_get_processor(), 0 ); + + /* Make sure the worker released the mutex */ + SetSelfScheduler( ctx->scheduler_b_id, PRIO_NORMAL ); + SetSelfScheduler( ctx->scheduler_a_id, PRIO_NORMAL ); + + DeleteTask( worker_id ); + DeleteTask( worker_2_id ); + DeleteMutex( ctx->mutex_id ); + RestoreRunnerPriority(); +} + +/** + * @fn void T_case_body_ScoreThreadValSmp( void ) + */ +T_TEST_CASE_FIXTURE( ScoreThreadValSmp, &ScoreThreadValSmp_Fixture ) +{ + ScoreThreadValSmp_Context *ctx; + + ctx = T_fixture_context(); + + ScoreThreadValSmp_Action_0( ctx ); +} + +/** @} */ |