source: rtems/testsuites/smptests/smpmigration01/init.c @ cb5eaddf

4.115
Last change on this file since cb5eaddf was cb5eaddf, checked in by Sebastian Huber <sebastian.huber@…>, on 04/10/14 at 09:02:52

rtems: Rename rtems_smp_get_current_processor()

Rename rtems_smp_get_current_processor() in
rtems_get_current_processor(). Make rtems_get_current_processor() a
function in uni-processor configurations to enable ABI compatibility
with SMP configurations.

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