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.com/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 <inttypes.h> |
---|
23 | |
---|
24 | #define CPU_COUNT 2 |
---|
25 | |
---|
26 | #define RUNNER_COUNT (CPU_COUNT + 1) |
---|
27 | |
---|
28 | #define PRIO_STOP 2 |
---|
29 | |
---|
30 | #define PRIO_HIGH 3 |
---|
31 | |
---|
32 | #define PRIO_NORMAL 4 |
---|
33 | |
---|
34 | /* FIXME: Use atomic operations instead of volatile */ |
---|
35 | |
---|
36 | typedef struct { |
---|
37 | uint32_t counter; |
---|
38 | uint32_t unused_space_for_cache_line_alignment[7]; |
---|
39 | } cache_aligned_counter; |
---|
40 | |
---|
41 | typedef struct { |
---|
42 | cache_aligned_counter tokens_per_cpu[CPU_COUNT]; |
---|
43 | volatile cache_aligned_counter cycles_per_cpu[CPU_COUNT]; |
---|
44 | } test_counters; |
---|
45 | |
---|
46 | typedef struct { |
---|
47 | test_counters counters[RUNNER_COUNT]; |
---|
48 | volatile rtems_task_argument token; |
---|
49 | rtems_id runner_ids[RUNNER_COUNT]; |
---|
50 | } test_context; |
---|
51 | |
---|
52 | CPU_STRUCTURE_ALIGNMENT static test_context ctx_instance; |
---|
53 | |
---|
54 | static void change_prio(rtems_id task, rtems_task_priority prio) |
---|
55 | { |
---|
56 | rtems_status_code sc; |
---|
57 | rtems_task_priority unused; |
---|
58 | |
---|
59 | sc = rtems_task_set_priority(task, prio, &unused); |
---|
60 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
61 | } |
---|
62 | |
---|
63 | static void runner(rtems_task_argument self) |
---|
64 | { |
---|
65 | test_context *ctx = &ctx_instance; |
---|
66 | rtems_task_argument next = (self + 1) % RUNNER_COUNT; |
---|
67 | rtems_id next_runner = ctx->runner_ids[next]; |
---|
68 | test_counters *counters = &ctx->counters[self]; |
---|
69 | test_counters *next_counters = &ctx->counters[next]; |
---|
70 | |
---|
71 | while (true) { |
---|
72 | uint32_t current_cpu = rtems_smp_get_current_processor(); |
---|
73 | |
---|
74 | ++counters->cycles_per_cpu[current_cpu].counter; |
---|
75 | |
---|
76 | if (ctx->token == self) { |
---|
77 | uint32_t other_cpu = (current_cpu + 1) % CPU_COUNT; |
---|
78 | uint32_t snapshot; |
---|
79 | |
---|
80 | ++counters->tokens_per_cpu[current_cpu].counter; |
---|
81 | |
---|
82 | change_prio(next_runner, PRIO_HIGH); |
---|
83 | |
---|
84 | snapshot = next_counters->cycles_per_cpu[other_cpu].counter; |
---|
85 | while (next_counters->cycles_per_cpu[other_cpu].counter == snapshot) { |
---|
86 | /* Wait for other thread to resume execution */ |
---|
87 | } |
---|
88 | |
---|
89 | ctx->token = next; |
---|
90 | |
---|
91 | change_prio(RTEMS_SELF, PRIO_NORMAL); |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | static void stopper(rtems_task_argument arg) |
---|
97 | { |
---|
98 | (void) arg; |
---|
99 | |
---|
100 | while (true) { |
---|
101 | /* Do nothing */ |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | static void test(void) |
---|
106 | { |
---|
107 | test_context *ctx = &ctx_instance; |
---|
108 | rtems_status_code sc; |
---|
109 | rtems_task_argument runner_index; |
---|
110 | rtems_id stopper_id; |
---|
111 | uint32_t expected_tokens; |
---|
112 | uint32_t total_delta; |
---|
113 | |
---|
114 | sc = rtems_task_create( |
---|
115 | rtems_build_name('S', 'T', 'O', 'P'), |
---|
116 | PRIO_STOP, |
---|
117 | RTEMS_MINIMUM_STACK_SIZE, |
---|
118 | RTEMS_DEFAULT_MODES, |
---|
119 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
120 | &stopper_id |
---|
121 | ); |
---|
122 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
123 | |
---|
124 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
125 | sc = rtems_task_create( |
---|
126 | rtems_build_name('R', 'U', 'N', (char) ('0' + runner_index)), |
---|
127 | PRIO_HIGH + runner_index, |
---|
128 | RTEMS_MINIMUM_STACK_SIZE, |
---|
129 | RTEMS_DEFAULT_MODES, |
---|
130 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
131 | &ctx->runner_ids[runner_index] |
---|
132 | ); |
---|
133 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
134 | } |
---|
135 | |
---|
136 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
137 | sc = rtems_task_start(ctx->runner_ids[runner_index], runner, runner_index); |
---|
138 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
139 | } |
---|
140 | |
---|
141 | sc = rtems_task_wake_after(10 * rtems_clock_get_ticks_per_second()); |
---|
142 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
143 | |
---|
144 | sc = rtems_task_start(stopper_id, stopper, 0); |
---|
145 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
146 | |
---|
147 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
148 | test_counters *counters = &ctx->counters[runner_index]; |
---|
149 | size_t cpu; |
---|
150 | |
---|
151 | printf("runner %" PRIuPTR "\n", runner_index); |
---|
152 | |
---|
153 | for (cpu = 0; cpu < CPU_COUNT; ++cpu) { |
---|
154 | printf( |
---|
155 | "\tcpu %zu tokens %" PRIu32 "\n" |
---|
156 | "\tcpu %zu cycles %" PRIu32 "\n", |
---|
157 | cpu, |
---|
158 | counters->tokens_per_cpu[cpu].counter, |
---|
159 | cpu, |
---|
160 | counters->cycles_per_cpu[cpu].counter |
---|
161 | ); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | expected_tokens = ctx->counters[0].tokens_per_cpu[0].counter; |
---|
166 | total_delta = 0; |
---|
167 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
168 | test_counters *counters = &ctx->counters[runner_index]; |
---|
169 | size_t cpu; |
---|
170 | |
---|
171 | for (cpu = 0; cpu < CPU_COUNT; ++cpu) { |
---|
172 | uint32_t tokens = counters->tokens_per_cpu[cpu].counter; |
---|
173 | uint32_t delta = tokens > expected_tokens ? |
---|
174 | tokens - expected_tokens : expected_tokens - tokens; |
---|
175 | |
---|
176 | rtems_test_assert(delta <= 1); |
---|
177 | |
---|
178 | total_delta += delta; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | rtems_test_assert(total_delta <= (RUNNER_COUNT * CPU_COUNT - 1)); |
---|
183 | } |
---|
184 | |
---|
185 | static void Init(rtems_task_argument arg) |
---|
186 | { |
---|
187 | puts("\n\n*** TEST SMPMIGRATION 1 ***"); |
---|
188 | |
---|
189 | if (rtems_smp_get_processor_count() >= 2) { |
---|
190 | test(); |
---|
191 | } |
---|
192 | |
---|
193 | puts("*** END OF TEST SMPMIGRATION 1 ***"); |
---|
194 | |
---|
195 | rtems_test_exit(0); |
---|
196 | } |
---|
197 | |
---|
198 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
199 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
200 | |
---|
201 | #define CONFIGURE_SMP_APPLICATION |
---|
202 | |
---|
203 | #define CONFIGURE_SMP_MAXIMUM_PROCESSORS CPU_COUNT |
---|
204 | |
---|
205 | #define CONFIGURE_MAXIMUM_TASKS (2 + RUNNER_COUNT) |
---|
206 | |
---|
207 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
208 | |
---|
209 | #define CONFIGURE_INIT |
---|
210 | |
---|
211 | #include <rtems/confdefs.h> |
---|