summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2021-09-03 18:08:27 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2021-09-15 07:50:49 +0200
commit8b416538de92b08d54bd9304e2274f9f9ebe2399 (patch)
tree4e6880fee3ebf19594da1512d85af6b70cf045b6
parent37b788dea045434de9443c8398f44888a9082516 (diff)
validation: Test thread pinning
-rw-r--r--spec/build/testsuites/validation/validation-smp-only-0.yml1
-rw-r--r--testsuites/validation/tc-score-smp-thread.c344
2 files changed, 345 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..be7e9fd1ee
--- /dev/null
+++ b/testsuites/validation/tc-score-smp-thread.c
@@ -0,0 +1,344 @@
+/* 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 three 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.
+ *
+ * - Pin and unpin the runner thread. This is a nested operation.
+ *
+ * - Preempt the pinned runner thread. Worker B and C execute at the same
+ * time on processor 0 and 1 respectively for some point in time. This
+ * shows that the pinning of the runner thread is maintained.
+ *
+ * - Unpin the runner thread. The runner moves back to its home scheduler.
+ *
+ * - Release the mutex.
+ *
+ * - Pin the runner thread. Unpin the runner thread while it is suspended.
+ *
+ * - Make sure the worker released the mutex.
+ *
+ * - 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 worker A identifier.
+ */
+ rtems_id worker_a_id;
+
+ /**
+ * @brief This member contains the worker B identifier.
+ */
+ rtems_id worker_b_id;
+
+ /**
+ * @brief This member contains the worker C identifier.
+ */
+ rtems_id worker_c_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;
+
+ /**
+ * @brief This member contains a counter for EVENT_COUNT.
+ */
+ volatile uint32_t counter;
+} 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_COUNT = RTEMS_EVENT_3,
+ EVENT_LET_WORKER_C_COUNT = RTEMS_EVENT_4
+} 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_COUNT ) != 0 ) {
+ ++ctx->counter;
+ }
+
+ if ( ( events & EVENT_LET_WORKER_C_COUNT ) != 0 ) {
+ uint32_t counter;
+
+ counter = ctx->counter;
+ SendEvents( ctx->worker_c_id, EVENT_COUNT );
+
+ while ( ctx->counter == counter ) {
+ /* Wait */
+ }
+ }
+ }
+}
+
+static void Suspend( void *arg )
+{
+ Thread_Control *thread;
+
+ thread = arg;
+ SuspendTask( thread->Object.id );
+}
+
+static void Resume( void *arg )
+{
+ Thread_Control *thread;
+
+ thread = arg;
+ ResumeTask( thread->Object.id );
+}
+
+/**
+ * @brief Create three 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;
+ Per_CPU_Control *cpu_self;
+ Thread_Control *executing;
+
+ executing = _Thread_Get_executing();
+ SetSelfPriority( PRIO_NORMAL );
+ ctx->counter = 0;
+
+ 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();
+
+ ctx->worker_a_id = CreateTask( "WRKA", PRIO_NORMAL );
+ SetScheduler( ctx->worker_a_id, ctx->scheduler_b_id, PRIO_NORMAL );
+ StartTask( ctx->worker_a_id, WorkerTask, ctx );
+
+ ctx->worker_b_id = CreateTask( "WRKB", PRIO_HIGH );
+ StartTask( ctx->worker_b_id, WorkerTask, ctx );
+
+ ctx->worker_c_id = CreateTask( "WRKC", PRIO_LOW );
+ StartTask( ctx->worker_c_id, WorkerTask, ctx );
+
+ ObtainMutex( ctx->mutex_id );
+ SendEvents( ctx->worker_a_id, EVENT_OBTAIN | EVENT_RELEASE );
+
+ ctx->busy = true;
+ SendEvents( ctx->worker_b_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 );
+ _Thread_Pin( executing );
+
+ /*
+ * Pin and unpin the runner thread. This is a nested operation.
+ */
+ T_eq_u32( rtems_scheduler_get_processor(), 1 );
+ _Thread_Pin( executing );
+ _Thread_Unpin( executing, _Per_CPU_Get_snapshot() );
+
+ /*
+ * Preempt the pinned runner thread. Worker B and C execute at the same time
+ * on processor 0 and 1 respectively for some point in time. This shows that
+ * the pinning of the runner thread is maintained.
+ */
+ ctx->busy = false;
+ SetScheduler( ctx->worker_b_id, ctx->scheduler_b_id, PRIO_HIGH );
+ SendEvents( ctx->worker_b_id, EVENT_LET_WORKER_C_COUNT );
+
+ T_eq_u32( rtems_scheduler_get_processor(), 1 );
+ T_eq_u32( ctx->counter, 1 );
+
+ /*
+ * Unpin the runner thread. The runner moves back to its home scheduler.
+ */
+ cpu_self = _Thread_Dispatch_disable();
+ _Thread_Unpin( executing, cpu_self );
+ _Thread_Dispatch_direct( cpu_self );
+
+ T_eq_u32( rtems_scheduler_get_processor(), 0 );
+
+ /*
+ * Release the mutex.
+ */
+ ReleaseMutex( ctx->mutex_id);
+ T_eq_u32( rtems_scheduler_get_processor(), 0 );
+
+ /*
+ * Pin the runner thread. Unpin the runner thread while it is suspended.
+ */
+ _Thread_Pin( executing );
+
+ /* We have to preempt the runner to end up in _Thread_Do_unpin() */
+ SetPriority( ctx->worker_c_id, PRIO_HIGH );
+ SendEvents( ctx->worker_c_id, EVENT_COUNT );
+ T_eq_u32( ctx->counter, 2 );
+
+ cpu_self = _Thread_Dispatch_disable();
+ CallWithinISR( Suspend, executing );
+ _Thread_Unpin( executing, cpu_self );
+ CallWithinISR( Resume, executing );
+ _Thread_Dispatch_direct( cpu_self );
+
+ /*
+ * Make sure the worker released the mutex.
+ */
+ SetSelfScheduler( ctx->scheduler_b_id, PRIO_LOW );
+ SetSelfScheduler( ctx->scheduler_a_id, PRIO_NORMAL );
+
+ /*
+ * Clean up all used resources.
+ */
+ DeleteTask( ctx->worker_a_id );
+ DeleteTask( ctx->worker_b_id );
+ DeleteTask( ctx->worker_c_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 );
+}
+
+/** @} */