summaryrefslogtreecommitdiffstats
path: root/testsuites/validation/tc-score-tq-smp.c
blob: 624ad0e7b69989407d251a84718bab42bdfb115f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup ScoreTqValSmp
 */

/*
 * Copyright (C) 2021 embedded brains GmbH & Co. KG
 *
 * 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/score/smpbarrier.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/threadqimpl.h>

#include "tx-support.h"

#include <rtems/test.h>

/**
 * @defgroup ScoreTqValSmp spec:/score/tq/val/smp
 *
 * @ingroup TestsuitesValidationSmpOnly0
 *
 * @brief Tests SMP-specific thread queue behaviour.
 *
 * This test case performs the following actions:
 *
 * - Create two or three worker threads and a mutex.  Use the mutex and the
 *   worker to do a thread priority change in parallel with a thread queue
 *   extraction.
 *
 *   - Create a mutex and let the runner obtain it.
 *
 *   - Create and start worker A on a second processor. mutex.  Let it wait on
 *     the barrier.
 *
 *   - If there are more than two processors, then create and start also worker
 *     C.  Let it wait on the barrier.
 *
 *   - Create and start worker B.  Let it try to obtain the mutex which is
 *     owned by the runner.  Delete worker B to extract it from the thread
 *     queue. Wrap the thread queue extract operation to do a parallel thread
 *     priority change carried out by worker A (and maybe C).
 *
 *   - Clean up all used resources.
 *
 * - Build a cyclic dependency graph using several worker threads and mutexes.
 *   Use the mutexes and the worker to construct a thread queue deadlock which
 *   is detected on one processor while it uses thread queue links inserted by
 *   another processor.  The runner thread controls the test scenario via the
 *   two thread queue locks.  This is an important test scenario which shows
 *   why the thread queue implementation is a bit more complicated in SMP
 *   configurations.
 *
 *   - Let worker D wait for mutex A.  Let worker C wait for mutex D.  Let
 *     worker B wait for mutex C.
 *
 *   - Let worker A attempt to obtain mutex B.  Let worker A wait on the lock
 *     of mutex C.  Worker A will insert two thread queue links.
 *
 *   - Let worker E try to obtain mutex D.  Worker E will add a thread queue
 *     link which is later used by worker A to detect the deadlock.
 *
 *   - Let worker A continue the obtain sequence.  It will detect a deadlock.
 *
 *   - Clean up all used resources.
 *
 * @{
 */

/**
 * @brief Test context for spec:/score/tq/val/smp test case.
 */
typedef struct {
  /**
   * @brief This member contains the runner identifier.
   */
  rtems_id runner_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 worker D identifier.
   */
  rtems_id worker_d_id;

  /**
   * @brief This member contains the worker E identifier.
   */
  rtems_id worker_e_id;

  /**
   * @brief This member contains the mutex A identifier.
   */
  rtems_id mutex_a_id;

  /**
   * @brief This member contains the mutex B identifier.
   */
  rtems_id mutex_b_id;

  /**
   * @brief This member contains the mutex C identifier.
   */
  rtems_id mutex_c_id;

  /**
   * @brief This member contains the mutex D identifier.
   */
  rtems_id mutex_d_id;

  /**
   * @brief This member contains the count of processors used by the test.
   */
  uint32_t used_cpus;

  /**
   * @brief This member contains the thread queue of the mutex.
   */
  Thread_queue_Queue *thread_queue;

  /**
   * @brief This member contains the context to wrap the thread queue extract.
   */
  WrapThreadQueueContext wrap;

  /**
   * @brief This member contains the barrier to synchronize the runner and the
   *   workers.
   */
  SMP_barrier_Control barrier;

  /**
   * @brief This member contains the barrier state for the runner processor.
   */
  SMP_barrier_State barrier_state;
} ScoreTqValSmp_Context;

static ScoreTqValSmp_Context
  ScoreTqValSmp_Instance;

typedef ScoreTqValSmp_Context Context;

static void Extract( void *arg )
{
  Context *ctx;

  ctx = arg;

  /* PC0 */
  _SMP_barrier_Wait( &ctx->barrier, &ctx->barrier_state, ctx->used_cpus );

  /*
   * Ensure that worker A (and maybe C) acquired the thread wait lock of
   * worker B.
   */
  TicketLockWaitForOthers( &ctx->thread_queue->Lock, ctx->used_cpus - 1 );

  /*
   * Continue with the thread queue extraction.  The thread wait lock of
   * worker B will be changed back to the default thread wait lock.  This
   * will cause worker A (and maybe C) to release the thread queue lock and
   * acquire the default thread wait lock of worker B instead to carry out
   * the priority change.
   *
   * See also _Thread_Wait_acquire_critical().
   */
}

static void PriorityChangeWorker( rtems_task_argument arg )
{
  Context          *ctx;
  SMP_barrier_State state;

  ctx = (Context *) arg;
  _SMP_barrier_State_initialize( &state );

  /* PC0 */
  _SMP_barrier_Wait( &ctx->barrier, &state, ctx->used_cpus );

  SetPriority( ctx->worker_b_id, PRIO_VERY_HIGH );

  /* PC1 */
  _SMP_barrier_Wait( &ctx->barrier, &state, ctx->used_cpus );

  (void) ReceiveAnyEvents();
}

static void MutexObtainWorker( rtems_task_argument arg )
{
  Context *ctx;

  ctx = (Context *) arg;

  ObtainMutex( ctx->mutex_a_id );
}

static void DeadlockWorkerA( rtems_task_argument arg )
{
  Context          *ctx;
  SMP_barrier_State state;

  ctx = (Context *) arg;
  _SMP_barrier_State_initialize( &state );

  ObtainMutex( ctx->mutex_a_id );

  /* D0 */
  _SMP_barrier_Wait( &ctx->barrier, &state, 2 );

  /* D1 */
  _SMP_barrier_Wait( &ctx->barrier, &state, 2 );

  ObtainMutexDeadlock( ctx->mutex_b_id );

  ReleaseMutex( ctx->mutex_a_id );
  SendEvents( ctx->runner_id, RTEMS_EVENT_0 );
  (void) ReceiveAnyEvents();
}

static void DeadlockWorkerB( rtems_task_argument arg )
{
  Context *ctx;

  ctx = (Context *) arg;

  ObtainMutex( ctx->mutex_b_id );
  SendEvents( ctx->runner_id, RTEMS_EVENT_5 );
  ObtainMutex( ctx->mutex_c_id );
  ReleaseMutex( ctx->mutex_c_id );
  ReleaseMutex( ctx->mutex_b_id );
  SendEvents( ctx->runner_id, RTEMS_EVENT_1 );
  (void) ReceiveAnyEvents();
}

static void DeadlockWorkerC( rtems_task_argument arg )
{
  Context *ctx;

  ctx = (Context *) arg;

  ObtainMutex( ctx->mutex_c_id );
  ObtainMutex( ctx->mutex_d_id );
  ReleaseMutex( ctx->mutex_d_id );
  ReleaseMutex( ctx->mutex_c_id );
  SendEvents( ctx->runner_id, RTEMS_EVENT_2 );
  (void) ReceiveAnyEvents();
}

static void DeadlockWorkerD( rtems_task_argument arg )
{
  Context *ctx;

  ctx = (Context *) arg;

  ObtainMutex( ctx->mutex_d_id );
  ObtainMutex( ctx->mutex_a_id );
  ReleaseMutex( ctx->mutex_a_id );
  ReleaseMutex( ctx->mutex_d_id );
  SendEvents( ctx->runner_id, RTEMS_EVENT_3 );
  (void) ReceiveAnyEvents();
}

static void DeadlockWorkerE( rtems_task_argument arg )
{
  Context *ctx;

  ctx = (Context *) arg;

  ObtainMutex( ctx->mutex_d_id );
  ReleaseMutex( ctx->mutex_d_id );
  SendEvents( ctx->runner_id, RTEMS_EVENT_4 );
  (void) ReceiveAnyEvents();
}

static void ScoreTqValSmp_Setup( ScoreTqValSmp_Context *ctx )
{
  SetSelfPriority( PRIO_NORMAL );
}

static void ScoreTqValSmp_Setup_Wrap( void *arg )
{
  ScoreTqValSmp_Context *ctx;

  ctx = arg;
  ScoreTqValSmp_Setup( ctx );
}

static void ScoreTqValSmp_Teardown( ScoreTqValSmp_Context *ctx )
{
  RestoreRunnerPriority();
}

static void ScoreTqValSmp_Teardown_Wrap( void *arg )
{
  ScoreTqValSmp_Context *ctx;

  ctx = arg;
  ScoreTqValSmp_Teardown( ctx );
}

static T_fixture ScoreTqValSmp_Fixture = {
  .setup = ScoreTqValSmp_Setup_Wrap,
  .stop = NULL,
  .teardown = ScoreTqValSmp_Teardown_Wrap,
  .scope = NULL,
  .initial_context = &ScoreTqValSmp_Instance
};

/**
 * @brief Create two or three worker threads and a mutex.  Use the mutex and
 *   the worker to do a thread priority change in parallel with a thread queue
 *   extraction.
 */
static void ScoreTqValSmp_Action_0( ScoreTqValSmp_Context *ctx )
{
  _SMP_barrier_Control_initialize( &ctx->barrier );
  _SMP_barrier_State_initialize( &ctx->barrier_state );
  WrapThreadQueueInitialize( &ctx->wrap, Extract, ctx );

  if ( rtems_scheduler_get_processor_maximum() > 2 ) {
    ctx->used_cpus = 3;
  } else {
    ctx->used_cpus = 2;
  }

  /*
   * Create a mutex and let the runner obtain it.
   */
  ctx->mutex_a_id = CreateMutexNoProtocol();
  ctx->thread_queue = GetMutexThreadQueue( ctx->mutex_a_id );
  ObtainMutex( ctx->mutex_a_id );

  /*
   * Create and start worker A on a second processor. mutex.  Let it wait on
   * the barrier.
   */
  ctx->worker_a_id = CreateTask( "WRKA", PRIO_NORMAL );
  SetScheduler( ctx->worker_a_id, SCHEDULER_B_ID, PRIO_NORMAL );
  StartTask( ctx->worker_a_id, PriorityChangeWorker, ctx );

  /*
   * If there are more than two processors, then create and start also worker
   * C.  Let it wait on the barrier.
   */
  if ( ctx->used_cpus > 2 ) {
    ctx->worker_c_id = CreateTask( "WRKC", PRIO_NORMAL );
    SetScheduler( ctx->worker_c_id, SCHEDULER_C_ID, PRIO_NORMAL );
    StartTask( ctx->worker_c_id, PriorityChangeWorker, ctx );
  }

  /*
   * Create and start worker B.  Let it try to obtain the mutex which is owned
   * by the runner.  Delete worker B to extract it from the thread queue. Wrap
   * the thread queue extract operation to do a parallel thread priority change
   * carried out by worker A (and maybe C).
   */
  ctx->worker_b_id = CreateTask( "WRKB", PRIO_HIGH );
  StartTask( ctx->worker_b_id, MutexObtainWorker, ctx );
  WrapThreadQueueExtractDirect( &ctx->wrap, GetThread( ctx->worker_b_id ) );
  DeleteTask( ctx->worker_b_id );

  /*
   * Clean up all used resources.
   */
  /* PC1 */
  _SMP_barrier_Wait( &ctx->barrier, &ctx->barrier_state, ctx->used_cpus );

  WaitForExecutionStop( ctx->worker_a_id );
  DeleteTask( ctx->worker_a_id );

  if ( ctx->used_cpus > 2 ) {
    WaitForExecutionStop( ctx->worker_c_id );
    DeleteTask( ctx->worker_c_id );
  }

  ReleaseMutex( ctx->mutex_a_id );
  DeleteMutex( ctx->mutex_a_id );
  WrapThreadQueueDestroy( &ctx->wrap );
}

/**
 * @brief Build a cyclic dependency graph using several worker threads and
 *   mutexes. Use the mutexes and the worker to construct a thread queue
 *   deadlock which is detected on one processor while it uses thread queue
 *   links inserted by another processor.  The runner thread controls the test
 *   scenario via the two thread queue locks.  This is an important test
 *   scenario which shows why the thread queue implementation is a bit more
 *   complicated in SMP configurations.
 */
static void ScoreTqValSmp_Action_1( ScoreTqValSmp_Context *ctx )
{
  Thread_queue_Queue *queue_b;
  Thread_queue_Queue *queue_c;
  ISR_lock_Context    lock_context;
  SMP_barrier_State   state;

  if ( rtems_scheduler_get_processor_maximum() <= 2 ) {
    /*
     * We can only run this validation test on systems with three or more
     * processors.  The sequence under test can happen on systems with only two
     * processors, however, we need a third processor to control the other two
     * processors via ISR locks to get a deterministic test scenario.
     */
    return;
  }

  ctx->runner_id = rtems_task_self();

  _SMP_barrier_Control_initialize( &ctx->barrier );
  _SMP_barrier_State_initialize( &state );

  ctx->mutex_a_id = CreateMutexNoProtocol();
  ctx->mutex_b_id = CreateMutexNoProtocol();
  ctx->mutex_c_id = CreateMutexNoProtocol();
  ctx->mutex_d_id = CreateMutexNoProtocol();

  queue_b = GetMutexThreadQueue( ctx->mutex_b_id );
  queue_c = GetMutexThreadQueue( ctx->mutex_c_id );

  ctx->worker_a_id = CreateTask( "WRKA", PRIO_NORMAL );
  ctx->worker_b_id = CreateTask( "WRKB", PRIO_NORMAL );
  ctx->worker_c_id = CreateTask( "WRKC", PRIO_NORMAL );
  ctx->worker_d_id = CreateTask( "WRKD", PRIO_NORMAL );
  ctx->worker_e_id = CreateTask( "WRKE", PRIO_NORMAL );

  SetScheduler( ctx->worker_a_id, SCHEDULER_B_ID, PRIO_NORMAL );
  SetScheduler( ctx->worker_b_id, SCHEDULER_B_ID, PRIO_HIGH );
  SetScheduler( ctx->worker_c_id, SCHEDULER_B_ID, PRIO_HIGH );
  SetScheduler( ctx->worker_d_id, SCHEDULER_B_ID, PRIO_HIGH );
  SetScheduler( ctx->worker_e_id, SCHEDULER_C_ID, PRIO_NORMAL );

  /*
   * Let worker D wait for mutex A.  Let worker C wait for mutex D.  Let worker
   * B wait for mutex C.
   */
  StartTask( ctx->worker_a_id, DeadlockWorkerA, ctx );

  /* D0 */
  _SMP_barrier_Wait( &ctx->barrier, &state, 2 );

  StartTask( ctx->worker_d_id, DeadlockWorkerD, ctx );
  StartTask( ctx->worker_c_id, DeadlockWorkerC, ctx );
  StartTask( ctx->worker_b_id, DeadlockWorkerB, ctx );
  ReceiveAllEvents( RTEMS_EVENT_5 );
  WaitForExecutionStop( ctx->worker_b_id );

  /*
   * Let worker A attempt to obtain mutex B.  Let worker A wait on the lock of
   * mutex C.  Worker A will insert two thread queue links.
   */
  _ISR_lock_ISR_disable( &lock_context );
  _Thread_queue_Queue_acquire_critical(
    queue_c,
    &_Thread_Executing->Potpourri_stats,
    &lock_context
  );
  _ISR_lock_ISR_enable( &lock_context );

  /* D1 */
  _SMP_barrier_Wait( &ctx->barrier, &state, 2 );

  TicketLockWaitForOthers( &queue_c->Lock, 1 );

  /*
   * Let worker E try to obtain mutex D.  Worker E will add a thread queue link
   * which is later used by worker A to detect the deadlock.
   */
  StartTask( ctx->worker_e_id, DeadlockWorkerE, ctx );
  TicketLockWaitForOthers( &queue_b->Lock, 1 );

  /*
   * Let worker A continue the obtain sequence.  It will detect a deadlock.
   */
  _ISR_lock_ISR_disable( &lock_context );
  _Thread_queue_Queue_release( queue_c, &lock_context );

  /*
   * Clean up all used resources.
   */
  ReceiveAllEvents(
    RTEMS_EVENT_0 | RTEMS_EVENT_1 | RTEMS_EVENT_2 | RTEMS_EVENT_3 |
    RTEMS_EVENT_4
  );
  WaitForExecutionStop( ctx->worker_a_id );
  WaitForExecutionStop( ctx->worker_b_id );
  WaitForExecutionStop( ctx->worker_c_id );
  WaitForExecutionStop( ctx->worker_d_id );
  WaitForExecutionStop( ctx->worker_e_id );
  DeleteTask( ctx->worker_a_id );
  DeleteTask( ctx->worker_b_id );
  DeleteTask( ctx->worker_c_id );
  DeleteTask( ctx->worker_d_id );
  DeleteTask( ctx->worker_e_id );
  DeleteMutex( ctx->mutex_a_id );
  DeleteMutex( ctx->mutex_b_id );
  DeleteMutex( ctx->mutex_c_id );
  DeleteMutex( ctx->mutex_d_id );
}

/**
 * @fn void T_case_body_ScoreTqValSmp( void )
 */
T_TEST_CASE_FIXTURE( ScoreTqValSmp, &ScoreTqValSmp_Fixture )
{
  ScoreTqValSmp_Context *ctx;

  ctx = T_fixture_context();

  ScoreTqValSmp_Action_0( ctx );
  ScoreTqValSmp_Action_1( ctx );
}

/** @} */