From 7f577d3a56a63a76fe79bcb54c130c35159faf66 Mon Sep 17 00:00:00 2001 From: Alexander Krutwig Date: Thu, 5 Mar 2015 09:06:44 +0100 Subject: tests: Refactor parallel test execution --- cpukit/libmisc/Makefile.am | 1 + cpukit/libmisc/testsupport/test.h | 113 ++++++++++++++++++++++ cpukit/libmisc/testsupport/testparallel.c | 155 ++++++++++++++++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 cpukit/libmisc/testsupport/testparallel.c (limited to 'cpukit/libmisc') diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index 61def145d2..5f6104276e 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -162,6 +162,7 @@ noinst_LIBRARIES += libtestsupport.a libtestsupport_a_SOURCES = libtestsupport_a_SOURCES += testsupport/testbeginend.c libtestsupport_a_SOURCES += testsupport/testextension.c +libtestsupport_a_SOURCES += testsupport/testparallel.c ## fsmount noinst_LIBRARIES += libfsmount.a diff --git a/cpukit/libmisc/testsupport/test.h b/cpukit/libmisc/testsupport/test.h index 48a33a04e5..afed462a01 100644 --- a/cpukit/libmisc/testsupport/test.h +++ b/cpukit/libmisc/testsupport/test.h @@ -17,6 +17,8 @@ #include #include +#include +#include #ifdef __cplusplus extern "C" { @@ -116,6 +118,117 @@ static inline int rtems_test_endk(void) return rtems_test_end_with_plugin(printk_plugin, NULL); } +/** + * @brief Internal context for parallel job execution. + */ +typedef struct { + Atomic_Ulong stop; + SMP_barrier_Control barrier; + size_t worker_count; + rtems_id stop_worker_timer_id; + rtems_id master_id; +} rtems_test_parallel_context; + +/** + * @brief Basic parallel job description. + */ +typedef struct { + /** + * @brief Job initialization handler. + * + * This handler executes only in the context of the master worker before the + * job body handler. + * + * @param[in] ctx The parallel context. + * @param[in] arg The user specified argument. + * + * @return The desired job body execution time in clock ticks. See + * rtems_test_parallel_stop_job(). + */ + rtems_interval (*init)( + rtems_test_parallel_context *ctx, + void *arg + ); + + /** + * @brief Job body handler. + * + * @param[in] ctx The parallel context. + * @param[in] arg The user specified argument. + * @param[in] worker_index The worker index. It ranges from 0 to the + * processor count minus one. + */ + void (*body)( + rtems_test_parallel_context *ctx, + void *arg, + size_t worker_index + ); + + /** + * @brief Job finalization handler. + * + * This handler executes only in the context of the master worker after the + * job body handler. + * + * @param[in] ctx The parallel context. + * @param[in] arg The user specified argument. + */ + void (*fini)( + rtems_test_parallel_context *ctx, + void *arg + ); + + void *arg; +} rtems_test_parallel_job; + +/** + * @brief Indicates if a job body should stop its work loop. + * + * @param[in] ctx The parallel context. + * + * @retval true The job body should stop its work loop and return to the caller. + * @retval false Otherwise. + */ +static inline bool rtems_test_parallel_stop_job( + rtems_test_parallel_context *ctx +) +{ + return _Atomic_Load_ulong(&ctx->stop, ATOMIC_ORDER_RELAXED) != 0; +} + +/** + * @brief Indicates if a worker is the master worker. + * + * The master worker is the thread that called rtems_test_parallel(). + * + * @param[in] worker_index The worker index. + * + * @retval true This is the master worker. + * @retval false Otherwise. + */ +static inline bool rtems_test_parallel_is_master_worker(size_t worker_index) +{ + return worker_index == 0; +} + +/** + * @brief Runs a bunch of jobs in parallel on all processors of the system. + * + * There are SMP barriers before and after the job body. + * + * @param[in] ctx The parallel context. + * @param[in] non_master_worker_priority The task priority for non-master + * workers. + * @param[in] jobs The table of jobs. + * @param[in] job_count The count of jobs in the job table. + */ +void rtems_test_parallel( + rtems_test_parallel_context *ctx, + rtems_task_priority non_master_worker_priority, + const rtems_test_parallel_job *jobs, + size_t job_count +); + /** @} */ #ifdef __cplusplus diff --git a/cpukit/libmisc/testsupport/testparallel.c b/cpukit/libmisc/testsupport/testparallel.c new file mode 100644 index 0000000000..681f769124 --- /dev/null +++ b/cpukit/libmisc/testsupport/testparallel.c @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2013-2015 embedded brains GmbH. All rights reserved. + * + * embedded brains GmbH + * Dornierstr. 4 + * 82178 Puchheim + * Germany + * + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include +#include +#include + +static void stop_worker_timer(rtems_id timer_id, void *arg) +{ + rtems_test_parallel_context *ctx = arg; + + _Atomic_Store_ulong(&ctx->stop, 1, ATOMIC_ORDER_RELAXED); +} + +static void start_worker_stop_timer( + rtems_test_parallel_context *ctx, + rtems_interval duration +) +{ + rtems_status_code sc; + + _Atomic_Store_ulong(&ctx->stop, 0, ATOMIC_ORDER_RELEASE); + + sc = rtems_timer_fire_after( + ctx->stop_worker_timer_id, + duration, + stop_worker_timer, + ctx + ); + _Assert(sc == RTEMS_SUCCESSFUL); +} + +static void run_tests( + rtems_test_parallel_context *ctx, + const rtems_test_parallel_job *jobs, + size_t job_count, + size_t worker_index +) +{ + SMP_barrier_State bs = SMP_BARRIER_STATE_INITIALIZER; + size_t i; + + for (i = 0; i < job_count; ++i) { + const rtems_test_parallel_job *job = &jobs[i]; + + if (rtems_test_parallel_is_master_worker(worker_index)) { + rtems_interval duration = (*job->init)(ctx, job->arg); + + start_worker_stop_timer(ctx, duration); + } + + _SMP_barrier_Wait(&ctx->barrier, &bs, ctx->worker_count); + + (*job->body)(ctx, job->arg, worker_index); + + _SMP_barrier_Wait(&ctx->barrier, &bs, ctx->worker_count); + + if (rtems_test_parallel_is_master_worker(worker_index)) { + (*job->fini)(ctx, job->arg); + } + } +} + +typedef struct { + rtems_test_parallel_context *ctx; + const rtems_test_parallel_job *jobs; + size_t job_count; + size_t worker_index; +} worker_arg; + +static void worker_task(rtems_task_argument arg) +{ + worker_arg warg = *(worker_arg *) arg; + rtems_status_code sc; + + sc = rtems_event_transient_send(warg.ctx->master_id); + _Assert(sc == RTEMS_SUCCESSFUL); + + run_tests(warg.ctx, warg.jobs, warg.job_count, warg.worker_index); + + rtems_task_delete(RTEMS_SELF); +} + +void rtems_test_parallel( + rtems_test_parallel_context *ctx, + rtems_task_priority non_master_worker_priority, + const rtems_test_parallel_job *jobs, + size_t job_count +) +{ + rtems_status_code sc; + size_t worker_index; + + _Atomic_Init_ulong(&ctx->stop, 0); + _SMP_barrier_Control_initialize(&ctx->barrier); + ctx->worker_count = rtems_get_processor_count(); + ctx->master_id = rtems_task_self(); + + sc = rtems_timer_create( + rtems_build_name('S', 'T', 'O', 'P'), + &ctx->stop_worker_timer_id + ); + if (sc != RTEMS_SUCCESSFUL) { + rtems_fatal_error_occurred(sc); + } + + for (worker_index = 1; worker_index < ctx->worker_count; ++worker_index) { + worker_arg warg = { + .ctx = ctx, + .jobs = jobs, + .job_count = job_count, + .worker_index = worker_index + }; + rtems_id worker_id; + + sc = rtems_task_create( + rtems_build_name('W', 'O', 'R', 'K'), + non_master_worker_priority, + RTEMS_MINIMUM_STACK_SIZE, + RTEMS_DEFAULT_MODES, + RTEMS_DEFAULT_ATTRIBUTES, + &worker_id + ); + if (sc != RTEMS_SUCCESSFUL) { + rtems_fatal_error_occurred(sc); + } + + sc = rtems_task_start(worker_id, worker_task, (rtems_task_argument) &warg); + _Assert(sc == RTEMS_SUCCESSFUL); + + sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT); + _Assert(sc == RTEMS_SUCCESSFUL); + } + + run_tests(ctx, jobs, job_count, 0); + + sc = rtems_timer_delete(ctx->stop_worker_timer_id); + _Assert(sc == RTEMS_SUCCESSFUL); +} -- cgit v1.2.3