summaryrefslogtreecommitdiffstats
path: root/testsuites/smptests/smpmigration01/init.c
blob: e4929119b8fda3dc985ff15bf3b2d7cf68820fca (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
/*
 * Copyright (c) 2013 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

#define TESTS_USE_PRINTF
#include "tmacros.h"

#include <stdio.h>
#include <math.h>
#include <inttypes.h>

const char rtems_test_name[] = "SMPMIGRATION 1";

#define CPU_COUNT 2

#define RUNNER_COUNT (CPU_COUNT + 1)

#define PRIO_STOP 2

#define PRIO_HIGH 3

#define PRIO_NORMAL 4

/* FIXME: Use atomic operations instead of volatile */

typedef struct {
  uint32_t counter;
  uint32_t unused_space_for_cache_line_alignment[7];
} cache_aligned_counter;

typedef struct {
  cache_aligned_counter tokens_per_cpu[CPU_COUNT];
  volatile cache_aligned_counter cycles_per_cpu[CPU_COUNT];
} test_counters;

typedef struct {
  test_counters counters[RUNNER_COUNT];
  volatile rtems_task_argument token;
  rtems_id runner_ids[RUNNER_COUNT];
} test_context;

CPU_STRUCTURE_ALIGNMENT static test_context ctx_instance;

static void change_prio(rtems_id task, rtems_task_priority prio)
{
  rtems_status_code sc;
  rtems_task_priority unused;

  sc = rtems_task_set_priority(task, prio, &unused);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

static void runner(rtems_task_argument self)
{
  test_context *ctx = &ctx_instance;
  rtems_task_argument next = (self + 1) % RUNNER_COUNT;
  rtems_id next_runner = ctx->runner_ids[next];
  test_counters *counters = &ctx->counters[self];
  test_counters *next_counters = &ctx->counters[next];

  while (true) {
    uint32_t current_cpu = rtems_smp_get_current_processor();

    ++counters->cycles_per_cpu[current_cpu].counter;

    if (ctx->token == self) {
      uint32_t other_cpu = (current_cpu + 1) % CPU_COUNT;
      uint32_t snapshot;

      ++counters->tokens_per_cpu[current_cpu].counter;

      change_prio(next_runner, PRIO_HIGH);

      snapshot = next_counters->cycles_per_cpu[other_cpu].counter;
      while (next_counters->cycles_per_cpu[other_cpu].counter == snapshot) {
        /* Wait for other thread to resume execution */
      }

      ctx->token = next;

      change_prio(RTEMS_SELF, PRIO_NORMAL);
    }
  }
}

static void stopper(rtems_task_argument arg)
{
  (void) arg;

  while (true) {
    /* Do nothing */
  }
}

static uint32_t abs_delta(uint32_t a, uint32_t b)
{
  return a > b ?  a - b : b - a;
}

static void test(void)
{
  test_context *ctx = &ctx_instance;
  rtems_status_code sc;
  rtems_task_argument runner_index;
  rtems_id stopper_id;
  uint32_t expected_tokens;
  uint32_t total_delta;
  uint64_t total_cycles;
  uint32_t average_cycles;

  sc = rtems_task_create(
    rtems_build_name('S', 'T', 'O', 'P'),
    PRIO_STOP,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &stopper_id
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
    sc = rtems_task_create(
      rtems_build_name('R', 'U', 'N', (char) ('0' + runner_index)),
      PRIO_HIGH + runner_index,
      RTEMS_MINIMUM_STACK_SIZE,
      RTEMS_DEFAULT_MODES,
      RTEMS_DEFAULT_ATTRIBUTES,
      &ctx->runner_ids[runner_index]
    );
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
  }

  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
    sc = rtems_task_start(ctx->runner_ids[runner_index], runner, runner_index);
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
  }

  sc = rtems_task_wake_after(10 * rtems_clock_get_ticks_per_second());
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  sc = rtems_task_start(stopper_id, stopper, 0);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  total_cycles = 0;
  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
    const test_counters *counters = &ctx->counters[runner_index];
    size_t cpu;

    for (cpu = 0; cpu < CPU_COUNT; ++cpu) {
      total_cycles += counters->cycles_per_cpu[cpu].counter;
    }
  }
  average_cycles = (uint32_t) (total_cycles / (RUNNER_COUNT * CPU_COUNT));

  printf(
    "total cycles %" PRIu64 "\n"
    "average cycles %" PRIu32 "\n",
    total_cycles,
    average_cycles
  );

  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
    const test_counters *counters = &ctx->counters[runner_index];
    size_t cpu;

    printf("runner %" PRIuPTR "\n", runner_index);

    for (cpu = 0; cpu < CPU_COUNT; ++cpu) {
      uint32_t tokens = counters->tokens_per_cpu[cpu].counter;
      uint32_t cycles = counters->cycles_per_cpu[cpu].counter;
      double cycle_deviation = ((double) cycles - average_cycles)
        / average_cycles;

      printf(
        "\tcpu %zu tokens %" PRIu32 "\n"
        "\tcpu %zu cycles %" PRIu32 "\n"
        "\tcpu %zu cycle deviation %f\n",
        cpu,
        tokens,
        cpu,
        cycles,
        cpu,
        cycle_deviation
      );

      rtems_test_assert(fabs(cycle_deviation) < 0.5);
    }
  }

  expected_tokens = ctx->counters[0].tokens_per_cpu[0].counter;
  total_delta = 0;
  for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) {
    test_counters *counters = &ctx->counters[runner_index];
    size_t cpu;

    for (cpu = 0; cpu < CPU_COUNT; ++cpu) {
      uint32_t tokens = counters->tokens_per_cpu[cpu].counter;
      uint32_t delta = abs_delta(tokens, expected_tokens);

      rtems_test_assert(delta <= 1);

      total_delta += delta;
    }
  }

  rtems_test_assert(total_delta <= (RUNNER_COUNT * CPU_COUNT - 1));
}

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

  if (rtems_smp_get_processor_count() >= 2) {
    test();
  }

  TEST_END();
  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_SMP_APPLICATION

#define CONFIGURE_SMP_MAXIMUM_PROCESSORS CPU_COUNT

#define CONFIGURE_MAXIMUM_TASKS (2 + RUNNER_COUNT)

#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>