1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2011. |
---|
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 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #define NUM_CPUS 4 |
---|
15 | |
---|
16 | #define _GNU_SOURCE |
---|
17 | |
---|
18 | #include <tmacros.h> |
---|
19 | #include <errno.h> |
---|
20 | #include <pthread.h> |
---|
21 | #include <sched.h> |
---|
22 | |
---|
23 | const char rtems_test_name[] = "SMPPSXAFFINITY 2"; |
---|
24 | |
---|
25 | #if HAVE_DECL_PTHREAD_GETAFFINITY_NP |
---|
26 | |
---|
27 | pthread_t Init_id; |
---|
28 | pthread_t Med_id[NUM_CPUS-1]; |
---|
29 | pthread_t Low_id[NUM_CPUS]; |
---|
30 | |
---|
31 | /* forward declarations to avoid warnings */ |
---|
32 | void *POSIX_Init(void *argument); |
---|
33 | void Validate_setaffinity_errors(void); |
---|
34 | void Validate_getaffinity_errors(void); |
---|
35 | void Validate_affinity(void); |
---|
36 | void *Thread_1(void *unused); |
---|
37 | |
---|
38 | void *Thread_1(void *unused) |
---|
39 | { |
---|
40 | while(1); |
---|
41 | } |
---|
42 | |
---|
43 | void Validate_setaffinity_errors(void) |
---|
44 | { |
---|
45 | int sc; |
---|
46 | cpu_set_t cpuset; |
---|
47 | |
---|
48 | /* Verify pthread_setaffinity_np checks that all cpu's exist. */ |
---|
49 | CPU_FILL(&cpuset); |
---|
50 | puts( "Init - pthread_setaffinity_np - Invalid cpu - EINVAL" ); |
---|
51 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset ); |
---|
52 | rtems_test_assert( sc == EINVAL ); |
---|
53 | |
---|
54 | /* Verify pthread_setaffinity_np checks that at least one cpu is set */ |
---|
55 | CPU_ZERO(&cpuset); |
---|
56 | puts( "Init - pthread_setaffinity_np - no cpu - EINVAL" ); |
---|
57 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset ); |
---|
58 | rtems_test_assert( sc == EINVAL ); |
---|
59 | |
---|
60 | /* Verify pthread_setaffinity_np checks that at thread id is valid */ |
---|
61 | CPU_SET(0, &cpuset); |
---|
62 | puts( "Init - pthread_setaffinity_np - Invalid thread - ESRCH" ); |
---|
63 | sc = pthread_setaffinity_np( 999, sizeof(cpu_set_t), &cpuset ); |
---|
64 | rtems_test_assert( sc == ESRCH ); |
---|
65 | |
---|
66 | /* Verify pthread_setaffinity_np validates cpusetsize */ |
---|
67 | puts( "Init - pthread_setaffinity_np - Invalid cpusetsize - EINVAL" ); |
---|
68 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t) * 2, &cpuset ); |
---|
69 | rtems_test_assert( sc == EINVAL ); |
---|
70 | |
---|
71 | /* Verify pthread_setaffinity_np validates cpuset */ |
---|
72 | puts( "Init - pthread_setaffinity_np - Invalid cpuset - EFAULT" ); |
---|
73 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), NULL ); |
---|
74 | rtems_test_assert( sc == EFAULT ); |
---|
75 | } |
---|
76 | |
---|
77 | void Validate_getaffinity_errors(void) |
---|
78 | { |
---|
79 | int sc; |
---|
80 | cpu_set_t cpuset; |
---|
81 | |
---|
82 | /* Verify pthread_getaffinity_np checks that at thread id is valid */ |
---|
83 | CPU_SET(0, &cpuset); |
---|
84 | puts( "Init - pthread_getaffinity_np - Invalid thread - ESRCH" ); |
---|
85 | sc = pthread_getaffinity_np( 999, sizeof(cpu_set_t), &cpuset ); |
---|
86 | rtems_test_assert( sc == ESRCH ); |
---|
87 | |
---|
88 | /* Verify pthread_getaffinity_np validates cpusetsize */ |
---|
89 | puts( "Init - pthread_getaffinity_np - Invalid cpusetsize - EINVAL" ); |
---|
90 | sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t) * 2, &cpuset ); |
---|
91 | rtems_test_assert( sc == EINVAL ); |
---|
92 | |
---|
93 | /* Verify pthread_getaffinity_np validates cpuset */ |
---|
94 | puts("Init - pthread_getaffinity_np - Invalid cpuset - EFAULT"); |
---|
95 | sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), NULL ); |
---|
96 | rtems_test_assert( sc == EFAULT ); |
---|
97 | } |
---|
98 | |
---|
99 | void Validate_affinity(void ) |
---|
100 | { |
---|
101 | pthread_attr_t attr; |
---|
102 | cpu_set_t cpuset0; |
---|
103 | cpu_set_t cpuset1; |
---|
104 | cpu_set_t cpuset2; |
---|
105 | uint32_t i; |
---|
106 | int sc; |
---|
107 | int cpu_count; |
---|
108 | struct sched_param param; |
---|
109 | |
---|
110 | |
---|
111 | puts( "Init - Set Init priority to high"); |
---|
112 | sc = pthread_getattr_np( Init_id, &attr ); |
---|
113 | rtems_test_assert( sc == 0 ); |
---|
114 | sc = pthread_attr_getschedparam( &attr, ¶m ); |
---|
115 | rtems_test_assert( sc == 0 ); |
---|
116 | param.sched_priority = sched_get_priority_max( SCHED_FIFO ); |
---|
117 | sc = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); |
---|
118 | rtems_test_assert( !sc ); |
---|
119 | |
---|
120 | sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset0 ); |
---|
121 | rtems_test_assert( !sc ); |
---|
122 | |
---|
123 | /* Get the number of processors that we are using. */ |
---|
124 | cpu_count = rtems_get_processor_count(); |
---|
125 | |
---|
126 | /* Fill the remaining cpus with med priority tasks */ |
---|
127 | puts( "Init - Create Medium priority tasks"); |
---|
128 | for (i=0; i<(cpu_count-1); i++){ |
---|
129 | sc = pthread_create( &Med_id[i], &attr, Thread_1, NULL ); |
---|
130 | rtems_test_assert( !sc ); |
---|
131 | } |
---|
132 | |
---|
133 | puts( "Init - Verify Medium priority tasks"); |
---|
134 | for (i=0; i<(cpu_count-1); i++){ |
---|
135 | sc = pthread_getaffinity_np( Med_id[i], sizeof(cpu_set_t), &cpuset2 ); |
---|
136 | rtems_test_assert( !sc ); |
---|
137 | rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) ); |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | * Create low priority thread for each remaining cpu with the affinity |
---|
142 | * set to only run on one cpu. |
---|
143 | */ |
---|
144 | puts( "Init - Create Low priority tasks"); |
---|
145 | for (i=0; i<cpu_count; i++){ |
---|
146 | CPU_ZERO(&cpuset1); |
---|
147 | CPU_SET(i, &cpuset1); |
---|
148 | |
---|
149 | sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &cpuset1 ); |
---|
150 | rtems_test_assert( !sc ); |
---|
151 | |
---|
152 | sc = pthread_create( &Low_id[i], &attr, Thread_1, NULL ); |
---|
153 | rtems_test_assert( !sc ); |
---|
154 | } |
---|
155 | |
---|
156 | /* Verify affinity on low priority tasks */ |
---|
157 | puts( "Init - Verify Low priority tasks"); |
---|
158 | for (i=0; i<(cpu_count-1); i++){ |
---|
159 | CPU_ZERO(&cpuset1); |
---|
160 | CPU_SET(i, &cpuset1); |
---|
161 | |
---|
162 | sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 ); |
---|
163 | rtems_test_assert( !sc ); |
---|
164 | rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) ); |
---|
165 | } |
---|
166 | |
---|
167 | /* Change the affinity for each low priority task */ |
---|
168 | puts("Init - Change affinity on Low priority tasks"); |
---|
169 | CPU_COPY(&cpuset1, &cpuset0); |
---|
170 | for (i=0; i<cpu_count; i++){ |
---|
171 | |
---|
172 | CPU_CLR(i, &cpuset1); |
---|
173 | sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset1 ); |
---|
174 | |
---|
175 | /* Verify no cpu's are now set in the cpuset */ |
---|
176 | if (i== (cpu_count-1)) { |
---|
177 | rtems_test_assert( sc == EINVAL ); |
---|
178 | sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset0 ); |
---|
179 | } |
---|
180 | rtems_test_assert( !sc ); |
---|
181 | } |
---|
182 | |
---|
183 | puts("Init - Validate affinity on Low priority tasks"); |
---|
184 | CPU_COPY(&cpuset1, &cpuset0); |
---|
185 | for (i=0; i<cpu_count; i++){ |
---|
186 | CPU_CLR(i, &cpuset1); |
---|
187 | |
---|
188 | sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 ); |
---|
189 | rtems_test_assert( !sc ); |
---|
190 | if (i== (cpu_count-1)) |
---|
191 | rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) ); |
---|
192 | else |
---|
193 | rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) ); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | void *POSIX_Init( |
---|
198 | void *ignored |
---|
199 | ) |
---|
200 | { |
---|
201 | TEST_BEGIN(); |
---|
202 | |
---|
203 | /* Initialize thread id */ |
---|
204 | Init_id = pthread_self(); |
---|
205 | |
---|
206 | Validate_setaffinity_errors(); |
---|
207 | Validate_getaffinity_errors(); |
---|
208 | Validate_affinity(); |
---|
209 | |
---|
210 | TEST_END(); |
---|
211 | rtems_test_exit(0); |
---|
212 | } |
---|
213 | |
---|
214 | #else |
---|
215 | void *POSIX_Init( |
---|
216 | void *ignored |
---|
217 | ) |
---|
218 | { |
---|
219 | TEST_BEGIN(); |
---|
220 | puts( " Affinity NOT Supported"); |
---|
221 | TEST_END(); |
---|
222 | rtems_test_exit(0); |
---|
223 | } |
---|
224 | |
---|
225 | #endif |
---|
226 | /* configuration information */ |
---|
227 | |
---|
228 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
229 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
230 | |
---|
231 | #define CONFIGURE_SMP_APPLICATION |
---|
232 | #define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP |
---|
233 | |
---|
234 | #define CONFIGURE_SMP_MAXIMUM_PROCESSORS NUM_CPUS |
---|
235 | |
---|
236 | #define CONFIGURE_MAXIMUM_POSIX_THREADS (NUM_CPUS*2) |
---|
237 | |
---|
238 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
239 | |
---|
240 | #define CONFIGURE_INIT |
---|
241 | #include <rtems/confdefs.h> |
---|
242 | |
---|
243 | /* global variables */ |
---|