1 | /* |
---|
2 | * COPYRIGHT (c) 2014. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * Test designed for 2 cores: Init task and TA1 task. |
---|
12 | * of equal priorities. |
---|
13 | * |
---|
14 | * - Set TA1 affinity to core 0 verify |
---|
15 | * - Set TA1 affinity to core 1 verify it does not run because |
---|
16 | * the Init task never blocks |
---|
17 | * - Set Init affinity to core 0 verify both tasks are on the correct cores. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <rtems.h> |
---|
25 | |
---|
26 | #include "tmacros.h" |
---|
27 | |
---|
28 | const char rtems_test_name[] = "SMPSCHEDAFFINITY 2"; |
---|
29 | |
---|
30 | #define NUM_CPUS 2 |
---|
31 | |
---|
32 | struct task_data_t { |
---|
33 | rtems_id id; |
---|
34 | int expected_cpu; |
---|
35 | cpu_set_t cpuset; |
---|
36 | bool ran; |
---|
37 | int actual_cpu; |
---|
38 | }; |
---|
39 | |
---|
40 | struct task_data_t task_data = { |
---|
41 | 0x0, 0, {{0x3}}, false, -1 |
---|
42 | }; |
---|
43 | |
---|
44 | rtems_id task_sem; |
---|
45 | |
---|
46 | static void task(rtems_task_argument arg); |
---|
47 | static void show_threads(void); |
---|
48 | static void task_verify( bool ran, bool change_affinity, int cpu ); |
---|
49 | static void init_verify( int expect ); |
---|
50 | |
---|
51 | static void test_delay(int ticks) |
---|
52 | { |
---|
53 | rtems_interval start, stop; |
---|
54 | start = rtems_clock_get_ticks_since_boot(); |
---|
55 | do { |
---|
56 | stop = rtems_clock_get_ticks_since_boot(); |
---|
57 | } while ( (stop - start) < ticks ); |
---|
58 | } |
---|
59 | |
---|
60 | static void task_verify( bool ran, bool change_affinity, int cpu ) |
---|
61 | { |
---|
62 | rtems_status_code sc; |
---|
63 | size_t size = sizeof(cpu_set_t); |
---|
64 | |
---|
65 | /* Obtain the semaphore without blocking */ |
---|
66 | while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL ); |
---|
67 | |
---|
68 | /* print the expected and actual values */ |
---|
69 | printf( "TA01: expected=%d actual=%d ran=%d\n", |
---|
70 | task_data.expected_cpu, |
---|
71 | task_data.actual_cpu, |
---|
72 | task_data.ran |
---|
73 | ); |
---|
74 | |
---|
75 | /* Verify expected results */ |
---|
76 | rtems_test_assert( task_data.ran == ran ); |
---|
77 | if (ran) |
---|
78 | rtems_test_assert( task_data.expected_cpu == task_data.actual_cpu ); |
---|
79 | |
---|
80 | if (change_affinity) { |
---|
81 | printf("Set TA01 to cpu %d\n", cpu); |
---|
82 | CPU_ZERO(&task_data.cpuset); |
---|
83 | CPU_SET(cpu, &task_data.cpuset); |
---|
84 | sc = rtems_task_set_affinity( task_data.id, size, &task_data.cpuset ); |
---|
85 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
86 | } |
---|
87 | |
---|
88 | /* Reset the states */ |
---|
89 | task_data.ran = false; |
---|
90 | task_data.expected_cpu = cpu; |
---|
91 | |
---|
92 | /* Release the semaphore */ |
---|
93 | rtems_semaphore_release(task_sem); |
---|
94 | } |
---|
95 | |
---|
96 | static void init_verify( int expect ) |
---|
97 | { |
---|
98 | int cpu; |
---|
99 | |
---|
100 | |
---|
101 | test_delay(20); |
---|
102 | |
---|
103 | cpu = rtems_get_current_processor(); |
---|
104 | printf( "Init: expected=%d actual=%d\n", expect, cpu); |
---|
105 | rtems_test_assert( expect == cpu ); |
---|
106 | } |
---|
107 | |
---|
108 | static void task(rtems_task_argument arg) |
---|
109 | { |
---|
110 | rtems_status_code sc; |
---|
111 | |
---|
112 | /* Never block and continually get core id */ |
---|
113 | while (true) { |
---|
114 | sc = rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0); |
---|
115 | if (sc == RTEMS_SUCCESSFUL) { |
---|
116 | task_data.actual_cpu = rtems_get_current_processor(); |
---|
117 | task_data.ran = true; |
---|
118 | rtems_semaphore_release(task_sem); |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | static void test(void) |
---|
124 | { |
---|
125 | rtems_status_code sc; |
---|
126 | uint32_t cpu_count; |
---|
127 | rtems_id id_self; |
---|
128 | cpu_set_t cpuset; |
---|
129 | |
---|
130 | /* Get the number of processors that we are using. */ |
---|
131 | cpu_count = rtems_get_processor_count(); |
---|
132 | if (cpu_count < NUM_CPUS) { |
---|
133 | printf("Error: Test requires at least 2 cpus\n"); |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | id_self = rtems_task_self(); |
---|
138 | |
---|
139 | printf("Create Semaphore\n"); |
---|
140 | sc = rtems_semaphore_create( |
---|
141 | rtems_build_name('S', 'E', 'M', '0'), |
---|
142 | 1, |
---|
143 | RTEMS_LOCAL | |
---|
144 | RTEMS_SIMPLE_BINARY_SEMAPHORE | |
---|
145 | RTEMS_NO_INHERIT_PRIORITY | |
---|
146 | RTEMS_NO_PRIORITY_CEILING | |
---|
147 | RTEMS_FIFO, |
---|
148 | 0, |
---|
149 | &task_sem |
---|
150 | ); |
---|
151 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
152 | |
---|
153 | printf("Create TA1\n"); |
---|
154 | sc = rtems_task_create( |
---|
155 | rtems_build_name('T', 'A', '0', '1'), |
---|
156 | 4, |
---|
157 | RTEMS_MINIMUM_STACK_SIZE, |
---|
158 | RTEMS_DEFAULT_MODES, |
---|
159 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
160 | &task_data.id |
---|
161 | ); |
---|
162 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
163 | |
---|
164 | /* TA1 should start on cpu 0, since init starts on core 1 */ |
---|
165 | sc = rtems_task_start( task_data.id, task, 1 ); |
---|
166 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
167 | |
---|
168 | /* Verify Init task is on cpu 1 */ |
---|
169 | init_verify(1); |
---|
170 | |
---|
171 | /* Verify TA1 on cpu 0 and set the affinity to cpu 0 */ |
---|
172 | task_verify( true, true, 0 ); |
---|
173 | |
---|
174 | /* Verify Init task is on cpu 1 */ |
---|
175 | init_verify(1); |
---|
176 | |
---|
177 | /* Verify TA1 on cpu 0 and change the affinity to cpu 1 */ |
---|
178 | task_verify( true, true, 1 ); |
---|
179 | |
---|
180 | /* Verify Init task is on cpu 1 */ |
---|
181 | init_verify(1); |
---|
182 | |
---|
183 | /* Verify TA1 did not run */ |
---|
184 | task_verify( false, false, 1 ); |
---|
185 | |
---|
186 | /* Set affinity of Init to cpu 0 */ |
---|
187 | printf("Set Affinity of init task to cpu 0\n"); |
---|
188 | CPU_ZERO(&cpuset); |
---|
189 | CPU_SET(0, &cpuset); |
---|
190 | sc = rtems_task_set_affinity( id_self, sizeof(cpuset), &cpuset ); |
---|
191 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
192 | |
---|
193 | /* Verify init task went to cpu 0 */ |
---|
194 | test_delay(50); |
---|
195 | init_verify(0); |
---|
196 | |
---|
197 | /* Verfiy TA1 is now running on cpu 1 */ |
---|
198 | task_verify(true, false, 1); |
---|
199 | } |
---|
200 | |
---|
201 | static void Init(rtems_task_argument arg) |
---|
202 | { |
---|
203 | TEST_BEGIN(); |
---|
204 | |
---|
205 | test(); |
---|
206 | |
---|
207 | TEST_END(); |
---|
208 | rtems_test_exit(0); |
---|
209 | } |
---|
210 | |
---|
211 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
212 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
213 | |
---|
214 | #define CONFIGURE_SMP_APPLICATION |
---|
215 | |
---|
216 | #define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP |
---|
217 | |
---|
218 | #define CONFIGURE_SMP_MAXIMUM_PROCESSORS NUM_CPUS |
---|
219 | |
---|
220 | #define CONFIGURE_MAXIMUM_TASKS NUM_CPUS |
---|
221 | |
---|
222 | #define CONFIGURE_INIT_TASK_PRIORITY 4 |
---|
223 | |
---|
224 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
225 | |
---|
226 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
227 | |
---|
228 | #define CONFIGURE_INIT |
---|
229 | |
---|
230 | #include <rtems/confdefs.h> |
---|