source: rtems/testsuites/smptests/smpmigration01/init.c @ 0d796d6

Last change on this file since 0d796d6 was 0d796d6, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 2, 2017 at 1:08:02 PM

tests: Delete obsolete TESTS_USE_PRINTF

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2013-2015 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include "tmacros.h"
20
21#include <stdio.h>
22#include <math.h>
23#include <inttypes.h>
24
25const char rtems_test_name[] = "SMPMIGRATION 1";
26
27#define CPU_COUNT 2
28
29#define RUNNER_COUNT (CPU_COUNT + 1)
30
31#define PRIO_STOP 2
32
33#define PRIO_HIGH 3
34
35#define PRIO_NORMAL 4
36
37/* FIXME: Use atomic operations instead of volatile */
38
39typedef struct {
40  uint32_t counter;
41  uint32_t unused_space_for_cache_line_alignment[7];
42} cache_aligned_counter;
43
44typedef struct {
45  cache_aligned_counter tokens_per_cpu[CPU_COUNT];
46  volatile cache_aligned_counter cycles_per_cpu[CPU_COUNT];
47} test_counters;
48
49typedef struct {
50  test_counters counters[RUNNER_COUNT];
51  volatile rtems_task_argument token;
52  rtems_id runner_ids[RUNNER_COUNT];
53} test_context;
54
55CPU_STRUCTURE_ALIGNMENT static test_context ctx_instance;
56
57static void change_prio(rtems_id task, rtems_task_priority prio)
58{
59  rtems_status_code sc;
60  rtems_task_priority unused;
61
62  sc = rtems_task_set_priority(task, prio, &unused);
63  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
64}
65
66static void runner(rtems_task_argument self)
67{
68  test_context *ctx = &ctx_instance;
69  rtems_task_argument next = (self + 1) % RUNNER_COUNT;
70  rtems_id next_runner = ctx->runner_ids[next];
71  test_counters *counters = &ctx->counters[self];
72  test_counters *next_counters = &ctx->counters[next];
73
74  while (true) {
75    uint32_t current_cpu = rtems_get_current_processor();
76
77    ++counters->cycles_per_cpu[current_cpu].counter;
78
79    if (ctx->token == self) {
80      uint32_t other_cpu = (current_cpu + 1) % CPU_COUNT;
81      uint32_t snapshot;
82
83      ++counters->tokens_per_cpu[current_cpu].counter;
84
85      change_prio(next_runner, PRIO_HIGH);
86
87      snapshot = next_counters->cycles_per_cpu[other_cpu].counter;
88      while (next_counters->cycles_per_cpu[other_cpu].counter == snapshot) {
89        /* Wait for other thread to resume execution */
90      }
91
92      ctx->token = next;
93
94      change_prio(RTEMS_SELF, PRIO_NORMAL);
95    }
96  }
97}
98
99static void stopper(rtems_task_argument arg)
100{
101  (void) arg;
102
103  while (true) {
104    /* Do nothing */
105  }
106}
107
108static uint32_t abs_delta(uint32_t a, uint32_t b)
109{
110  return a > b ?  a - b : b - a;
111}
112
113static void test(void)
114{
115  test_context *ctx = &ctx_instance;
116  rtems_status_code sc;
117  rtems_task_argument runner_index;
118  rtems_id stopper_id;
119  uint32_t expected_tokens;
120  uint32_t total_delta;
121  uint64_t total_cycles;
122  uint32_t average_cycles;
123
124  sc = rtems_task_create(
125    rtems_build_name('S', 'T', 'O', 'P'),
126    PRIO_STOP,
127    RTEMS_MINIMUM_STACK_SIZE,
128    RTEMS_DEFAULT_MODES,
129    RTEMS_DEFAULT_ATTRIBUTES,
130    &stopper_id
131  );
132  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
133
134  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
135    sc = rtems_task_create(
136      rtems_build_name('R', 'U', 'N', (char) ('0' + runner_index)),
137      PRIO_HIGH + runner_index,
138      RTEMS_MINIMUM_STACK_SIZE,
139      RTEMS_DEFAULT_MODES,
140      RTEMS_DEFAULT_ATTRIBUTES,
141      &ctx->runner_ids[runner_index]
142    );
143    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
144  }
145
146  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
147    sc = rtems_task_start(ctx->runner_ids[runner_index], runner, runner_index);
148    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
149  }
150
151  sc = rtems_task_wake_after(10 * rtems_clock_get_ticks_per_second());
152  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
153
154  sc = rtems_task_start(stopper_id, stopper, 0);
155  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
156
157  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
158    sc = rtems_task_delete(ctx->runner_ids[runner_index]);
159    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
160  }
161
162  total_cycles = 0;
163  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
164    const test_counters *counters = &ctx->counters[runner_index];
165    size_t cpu;
166
167    for (cpu = 0; cpu < CPU_COUNT; ++cpu) {
168      total_cycles += counters->cycles_per_cpu[cpu].counter;
169    }
170  }
171  average_cycles = (uint32_t) (total_cycles / (RUNNER_COUNT * CPU_COUNT));
172
173  printf(
174    "total cycles %" PRIu64 "\n"
175    "average cycles %" PRIu32 "\n",
176    total_cycles,
177    average_cycles
178  );
179
180  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
181    const test_counters *counters = &ctx->counters[runner_index];
182    size_t cpu;
183
184    printf("runner %" PRIuPTR "\n", runner_index);
185
186    for (cpu = 0; cpu < CPU_COUNT; ++cpu) {
187      uint32_t tokens = counters->tokens_per_cpu[cpu].counter;
188      uint32_t cycles = counters->cycles_per_cpu[cpu].counter;
189      double cycle_deviation = ((double) cycles - average_cycles)
190        / average_cycles;
191
192      printf(
193        "\tcpu %zu tokens %" PRIu32 "\n"
194        "\tcpu %zu cycles %" PRIu32 "\n"
195        "\tcpu %zu cycle deviation %f\n",
196        cpu,
197        tokens,
198        cpu,
199        cycles,
200        cpu,
201        cycle_deviation
202      );
203    }
204  }
205
206  expected_tokens = ctx->counters[0].tokens_per_cpu[0].counter;
207  total_delta = 0;
208  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
209    test_counters *counters = &ctx->counters[runner_index];
210    size_t cpu;
211
212    for (cpu = 0; cpu < CPU_COUNT; ++cpu) {
213      uint32_t tokens = counters->tokens_per_cpu[cpu].counter;
214      uint32_t delta = abs_delta(tokens, expected_tokens);
215
216      rtems_test_assert(delta <= 1);
217
218      total_delta += delta;
219    }
220  }
221
222  rtems_test_assert(total_delta <= (RUNNER_COUNT * CPU_COUNT - 1));
223}
224
225static void Init(rtems_task_argument arg)
226{
227  TEST_BEGIN();
228
229  if (rtems_get_processor_count() >= 2) {
230    test();
231  }
232
233  TEST_END();
234  rtems_test_exit(0);
235}
236
237#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
238#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
239
240#define CONFIGURE_MAXIMUM_PROCESSORS CPU_COUNT
241
242#define CONFIGURE_MAXIMUM_TASKS (2 + RUNNER_COUNT)
243
244#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
245
246#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
247
248#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
249
250#define CONFIGURE_INIT
251
252#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.