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 | #define TESTS_USE_PRINTF |
---|
20 | #include "tmacros.h" |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <math.h> |
---|
24 | #include <inttypes.h> |
---|
25 | |
---|
26 | const 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 | |
---|
40 | typedef struct { |
---|
41 | uint32_t counter; |
---|
42 | uint32_t unused_space_for_cache_line_alignment[7]; |
---|
43 | } cache_aligned_counter; |
---|
44 | |
---|
45 | typedef struct { |
---|
46 | cache_aligned_counter tokens_per_cpu[CPU_COUNT]; |
---|
47 | volatile cache_aligned_counter cycles_per_cpu[CPU_COUNT]; |
---|
48 | } test_counters; |
---|
49 | |
---|
50 | typedef struct { |
---|
51 | test_counters counters[RUNNER_COUNT]; |
---|
52 | volatile rtems_task_argument token; |
---|
53 | rtems_id runner_ids[RUNNER_COUNT]; |
---|
54 | } test_context; |
---|
55 | |
---|
56 | CPU_STRUCTURE_ALIGNMENT static test_context ctx_instance; |
---|
57 | |
---|
58 | static 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 | |
---|
67 | static 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 | |
---|
100 | static void stopper(rtems_task_argument arg) |
---|
101 | { |
---|
102 | (void) arg; |
---|
103 | |
---|
104 | while (true) { |
---|
105 | /* Do nothing */ |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | static uint32_t abs_delta(uint32_t a, uint32_t b) |
---|
110 | { |
---|
111 | return a > b ? a - b : b - a; |
---|
112 | } |
---|
113 | |
---|
114 | static 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 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
159 | sc = rtems_task_delete(ctx->runner_ids[runner_index]); |
---|
160 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
161 | } |
---|
162 | |
---|
163 | total_cycles = 0; |
---|
164 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
165 | const test_counters *counters = &ctx->counters[runner_index]; |
---|
166 | size_t cpu; |
---|
167 | |
---|
168 | for (cpu = 0; cpu < CPU_COUNT; ++cpu) { |
---|
169 | total_cycles += counters->cycles_per_cpu[cpu].counter; |
---|
170 | } |
---|
171 | } |
---|
172 | average_cycles = (uint32_t) (total_cycles / (RUNNER_COUNT * CPU_COUNT)); |
---|
173 | |
---|
174 | printf( |
---|
175 | "total cycles %" PRIu64 "\n" |
---|
176 | "average cycles %" PRIu32 "\n", |
---|
177 | total_cycles, |
---|
178 | average_cycles |
---|
179 | ); |
---|
180 | |
---|
181 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
182 | const test_counters *counters = &ctx->counters[runner_index]; |
---|
183 | size_t cpu; |
---|
184 | |
---|
185 | printf("runner %" PRIuPTR "\n", runner_index); |
---|
186 | |
---|
187 | for (cpu = 0; cpu < CPU_COUNT; ++cpu) { |
---|
188 | uint32_t tokens = counters->tokens_per_cpu[cpu].counter; |
---|
189 | uint32_t cycles = counters->cycles_per_cpu[cpu].counter; |
---|
190 | double cycle_deviation = ((double) cycles - average_cycles) |
---|
191 | / average_cycles; |
---|
192 | |
---|
193 | printf( |
---|
194 | "\tcpu %zu tokens %" PRIu32 "\n" |
---|
195 | "\tcpu %zu cycles %" PRIu32 "\n" |
---|
196 | "\tcpu %zu cycle deviation %f\n", |
---|
197 | cpu, |
---|
198 | tokens, |
---|
199 | cpu, |
---|
200 | cycles, |
---|
201 | cpu, |
---|
202 | cycle_deviation |
---|
203 | ); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | expected_tokens = ctx->counters[0].tokens_per_cpu[0].counter; |
---|
208 | total_delta = 0; |
---|
209 | for (runner_index = 0; runner_index < RUNNER_COUNT; ++runner_index) { |
---|
210 | test_counters *counters = &ctx->counters[runner_index]; |
---|
211 | size_t cpu; |
---|
212 | |
---|
213 | for (cpu = 0; cpu < CPU_COUNT; ++cpu) { |
---|
214 | uint32_t tokens = counters->tokens_per_cpu[cpu].counter; |
---|
215 | uint32_t delta = abs_delta(tokens, expected_tokens); |
---|
216 | |
---|
217 | rtems_test_assert(delta <= 1); |
---|
218 | |
---|
219 | total_delta += delta; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | rtems_test_assert(total_delta <= (RUNNER_COUNT * CPU_COUNT - 1)); |
---|
224 | } |
---|
225 | |
---|
226 | static void Init(rtems_task_argument arg) |
---|
227 | { |
---|
228 | TEST_BEGIN(); |
---|
229 | |
---|
230 | if (rtems_get_processor_count() >= 2) { |
---|
231 | test(); |
---|
232 | } |
---|
233 | |
---|
234 | TEST_END(); |
---|
235 | rtems_test_exit(0); |
---|
236 | } |
---|
237 | |
---|
238 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
239 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
240 | |
---|
241 | #define CONFIGURE_MAXIMUM_PROCESSORS CPU_COUNT |
---|
242 | |
---|
243 | #define CONFIGURE_MAXIMUM_TASKS (2 + RUNNER_COUNT) |
---|
244 | |
---|
245 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT |
---|
246 | |
---|
247 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
248 | |
---|
249 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
250 | |
---|
251 | #define CONFIGURE_INIT |
---|
252 | |
---|
253 | #include <rtems/confdefs.h> |
---|