summaryrefslogtreecommitdiffstats
path: root/testsuites/smptests/smplock01/init.c
blob: 0a379154f50aeb0f0eca688ec62ebbae3ca0322c (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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
 * Copyright (c) 2013, 2016 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * 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 <rtems/score/smplock.h>
#include <rtems/score/smplockmcs.h>
#include <rtems/score/smplockseq.h>
#include <rtems/test.h>
#include <rtems.h>

#define TESTS_USE_PRINTK
#include "tmacros.h"

const char rtems_test_name[] = "SMPLOCK 1";

#define TASK_PRIORITY 1

#define CPU_COUNT 32

#define TEST_COUNT 13

typedef struct {
  rtems_test_parallel_context base;
  unsigned long counter[TEST_COUNT];
  unsigned long local_counter[CPU_COUNT][TEST_COUNT][CPU_COUNT];
  SMP_lock_Control lock RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES);
  Atomic_Uint flag RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES);
  SMP_MCS_lock_Control mcs_lock RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES);
#if defined(RTEMS_PROFILING)
  SMP_lock_Stats mcs_stats;
#endif
  SMP_sequence_lock_Control seq_lock RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES);
  int a RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES);
  int b RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES);
} test_context;

static test_context test_instance = {
  .lock = SMP_LOCK_INITIALIZER("global ticket"),
#if defined(RTEMS_PROFILING)
  .mcs_stats = SMP_LOCK_STATS_INITIALIZER("global MCS"),
#endif
  .flag = ATOMIC_INITIALIZER_UINT(0),
  .mcs_lock = SMP_MCS_LOCK_INITIALIZER,
  .seq_lock = SMP_SEQUENCE_LOCK_INITIALIZER
};

static rtems_interval test_duration(void)
{
  return rtems_clock_get_ticks_per_second();
}

static rtems_interval test_init(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  return test_duration();
}

static void test_fini(
  test_context *ctx,
  const char *name,
  size_t test,
  size_t active_workers
)
{
  unsigned long sum = 0;
  unsigned long n = active_workers;
  unsigned long i;

  printf("  <%s activeWorker=\"%lu\">\n", name, n);

  for (i = 0; i < n; ++i) {
    unsigned long local_counter =
      ctx->local_counter[active_workers - 1][test][i];

    sum += local_counter;

    printf(
      "    <LocalCounter worker=\"%lu\">%lu</LocalCounter>\n",
      i,
      local_counter
    );
  }

  printf(
    "    <GlobalCounter>%lu</GlobalCounter>\n"
    "    <SumOfLocalCounter>%lu</SumOfLocalCounter>\n"
    "  </%s>\n",
    ctx->counter[test],
    sum,
    name
  );
}

static void test_0_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 0;
  unsigned long counter = 0;
  SMP_lock_Context lock_context;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_lock_Acquire(&ctx->lock, &lock_context);
    _SMP_lock_Release(&ctx->lock, &lock_context);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_0_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalTicketLockWithLocalCounter",
    0,
    active_workers
  );
}

static void test_1_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 1;
  unsigned long counter = 0;
  SMP_MCS_lock_Context lock_context;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_MCS_lock_Acquire(&ctx->mcs_lock, &lock_context, &ctx->mcs_stats);
    _SMP_MCS_lock_Release(&ctx->mcs_lock, &lock_context);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_1_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalMCSLockWithLocalCounter",
    1,
    active_workers
  );
}

static void test_2_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 2;
  unsigned long counter = 0;
  SMP_lock_Context lock_context;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_lock_Acquire(&ctx->lock, &lock_context);
    ++ctx->counter[test];
    _SMP_lock_Release(&ctx->lock, &lock_context);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_2_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalTicketLockWithGlobalCounter",
    2,
    active_workers
  );
}

static void test_3_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 3;
  unsigned long counter = 0;
  SMP_MCS_lock_Context lock_context;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_MCS_lock_Acquire(&ctx->mcs_lock, &lock_context, &ctx->mcs_stats);
    ++ctx->counter[test];
    _SMP_MCS_lock_Release(&ctx->mcs_lock, &lock_context);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_3_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalMCSLockWithGlobalCounter",
    3,
    active_workers
  );
}

static void test_4_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 4;
  unsigned long counter = 0;
  SMP_lock_Control lock;
  SMP_lock_Context lock_context;

  _SMP_lock_Initialize(&lock, "local");

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_lock_Acquire(&lock, &lock_context);
    _SMP_lock_Release(&lock, &lock_context);
    ++counter;
  }

  _SMP_lock_Destroy(&lock);

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_4_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "LocalTicketLockWithLocalCounter",
    4,
    active_workers
  );
}

static void test_5_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 5;
  unsigned long counter = 0;
#if defined(RTEMS_PROFILING)
  SMP_lock_Stats stats;
#endif
  SMP_MCS_lock_Control lock;
  SMP_MCS_lock_Context lock_context;

  _SMP_lock_Stats_initialize(&stats, "local");
  _SMP_MCS_lock_Initialize(&lock);

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_MCS_lock_Acquire(&lock, &lock_context, &stats);
    _SMP_MCS_lock_Release(&lock, &lock_context);
    ++counter;
  }

  _SMP_MCS_lock_Destroy(&lock);
  _SMP_lock_Stats_destroy(&stats);

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_5_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "LocalMCSLockWithLocalCounter",
    5,
    active_workers
  );
}

static void test_6_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 6;
  unsigned long counter = 0;
  SMP_lock_Control lock;
  SMP_lock_Context lock_context;

  _SMP_lock_Initialize(&lock, "local");

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_lock_Acquire(&lock, &lock_context);

    /* The counter value is not interesting, only the access to it */
    ++ctx->counter[test];

    _SMP_lock_Release(&lock, &lock_context);
    ++counter;
  }

  _SMP_lock_Destroy(&lock);

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_6_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "LocalTicketLockWithGlobalCounter",
    6,
    active_workers
  );
}

static void test_7_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 7;
  unsigned long counter = 0;
#if defined(RTEMS_PROFILING)
  SMP_lock_Stats stats;
#endif
  SMP_MCS_lock_Control lock;
  SMP_MCS_lock_Context lock_context;

  _SMP_lock_Stats_initialize(&stats, "local");
  _SMP_MCS_lock_Initialize(&lock);

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_MCS_lock_Acquire(&lock, &lock_context, &stats);

    /* The counter value is not interesting, only the access to it */
    ++ctx->counter[test];

    _SMP_MCS_lock_Release(&lock, &lock_context);
    ++counter;
  }

  _SMP_MCS_lock_Destroy(&lock);
  _SMP_lock_Stats_destroy(&stats);

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_7_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "LocalMCSLockWithGlobalCounter",
    7,
    active_workers
  );
}

static void busy_section(void)
{
  int i;

  for (i = 0; i < 101; ++i) {
    RTEMS_COMPILER_MEMORY_BARRIER();
  }
}

static void test_8_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 8;
  unsigned long counter = 0;
  SMP_lock_Context lock_context;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_lock_Acquire(&ctx->lock, &lock_context);
    busy_section();
    _SMP_lock_Release(&ctx->lock, &lock_context);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_8_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalTicketLockWithBusySection",
    8,
    active_workers
  );
}

static void test_9_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 9;
  unsigned long counter = 0;
  SMP_MCS_lock_Context lock_context;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    _SMP_MCS_lock_Acquire(&ctx->mcs_lock, &lock_context, &ctx->mcs_stats);
    busy_section();
    _SMP_MCS_lock_Release(&ctx->mcs_lock, &lock_context);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_9_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalMCSLockWithBusySection",
    9,
    active_workers
  );
}

static void test_10_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 10;
  unsigned long counter = 0;
  unsigned long seq;

  if (rtems_test_parallel_is_master_worker(worker_index)) {
    while (!rtems_test_parallel_stop_job(&ctx->base)) {
      seq = _SMP_sequence_lock_Write_begin(&ctx->seq_lock);

      ctx->a = counter;
      ctx->b = counter;

      _SMP_sequence_lock_Write_end(&ctx->seq_lock, seq);

      ++counter;
    }
  } else {
    while (!rtems_test_parallel_stop_job(&ctx->base)) {
      unsigned long a;
      unsigned long b;

      do {
        seq = _SMP_sequence_lock_Read_begin(&ctx->seq_lock);

        a = ctx->a;
        b = ctx->b;

      } while (_SMP_sequence_lock_Read_retry(&ctx->seq_lock, seq));

      ++counter;
      rtems_test_assert(a == b);
    }
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_10_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "SequenceLock",
    10,
    active_workers
  );
}

static void test_11_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 11;
  unsigned long counter = 0;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    while (_Atomic_Exchange_uint(&ctx->flag, 1, ATOMIC_ORDER_ACQUIRE) != 0) {
      /* Wait */
    }

    _Atomic_Store_uint(&ctx->flag, 0, ATOMIC_ORDER_RELEASE);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_11_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalTASLockWithLocalCounter",
    11,
    active_workers
  );
}

static void test_12_body(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers,
  size_t worker_index
)
{
  test_context *ctx = (test_context *) base;
  size_t test = 12;
  unsigned long counter = 0;

  while (!rtems_test_parallel_stop_job(&ctx->base)) {
    while (_Atomic_Exchange_uint(&ctx->flag, 1, ATOMIC_ORDER_ACQUIRE) != 0) {
      while (_Atomic_Load_uint(&ctx->flag, ATOMIC_ORDER_RELAXED) != 0) {
        /* Wait */
      }
    }

    _Atomic_Store_uint(&ctx->flag, 0, ATOMIC_ORDER_RELEASE);
    ++counter;
  }

  ctx->local_counter[active_workers - 1][test][worker_index] = counter;
}

static void test_12_fini(
  rtems_test_parallel_context *base,
  void *arg,
  size_t active_workers
)
{
  test_context *ctx = (test_context *) base;

  test_fini(
    ctx,
    "GlobalTTASLockWithLocalCounter",
    12,
    active_workers
  );
}

static const rtems_test_parallel_job test_jobs[TEST_COUNT] = {
  {
    .init = test_init,
    .body = test_0_body,
    .fini = test_0_fini,
    .cascade = true
  }, {
    .init = test_init,
    .body = test_1_body,
    .fini = test_1_fini,
    .cascade = true
  }, {
    .init = test_init,
    .body = test_2_body,
    .fini = test_2_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_3_body,
    .fini = test_3_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_4_body,
    .fini = test_4_fini,
    .cascade = true
  }, {
    .init = test_init,
    .body = test_5_body,
    .fini = test_5_fini,
    .cascade = true
  }, {
    .init = test_init,
    .body = test_6_body,
    .fini = test_6_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_7_body,
    .fini = test_7_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_8_body,
    .fini = test_8_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_9_body,
    .fini = test_9_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_10_body,
    .fini = test_10_fini,
    .cascade = false
  }, {
    .init = test_init,
    .body = test_11_body,
    .fini = test_11_fini,
    .cascade = true
  }, {
    .init = test_init,
    .body = test_12_body,
    .fini = test_12_fini,
    .cascade = true
  }
};

static void test(void)
{
  test_context *ctx = &test_instance;
  const char *test = "SMPLock01";

  printf("<%s>\n", test);
  rtems_test_parallel(&ctx->base, NULL, &test_jobs[0], TEST_COUNT);
  printf("</%s>\n", test);
}

static void Init(rtems_task_argument arg)
{
  TEST_BEGIN();

  test();

  TEST_END();
  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MAXIMUM_PROCESSORS CPU_COUNT

#define CONFIGURE_MAXIMUM_TASKS CPU_COUNT

#define CONFIGURE_MAXIMUM_SEMAPHORES 1

#define CONFIGURE_MAXIMUM_TIMERS 1

#define CONFIGURE_INIT_TASK_PRIORITY TASK_PRIORITY
#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>