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 | pthread_t Init_id; |
---|
26 | pthread_t Med_id[NUM_CPUS-1]; |
---|
27 | pthread_t Low_id[NUM_CPUS]; |
---|
28 | |
---|
29 | /* forward declarations to avoid warnings */ |
---|
30 | void *POSIX_Init(void *argument); |
---|
31 | void Validate_setaffinity_errors(void); |
---|
32 | void Validate_getaffinity_errors(void); |
---|
33 | void Validate_affinity(void); |
---|
34 | void *Thread_1(void *unused); |
---|
35 | |
---|
36 | void *Thread_1(void *unused) |
---|
37 | { |
---|
38 | while(1); |
---|
39 | } |
---|
40 | |
---|
41 | void Validate_setaffinity_errors(void) |
---|
42 | { |
---|
43 | int sc; |
---|
44 | cpu_set_t cpuset; |
---|
45 | |
---|
46 | /* Verify pthread_setaffinity_np checks that more cpu's don't hurt. */ |
---|
47 | CPU_FILL(&cpuset); |
---|
48 | puts( "Init - pthread_setaffinity_np - Lots of cpus - SUCCESS" ); |
---|
49 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset ); |
---|
50 | rtems_test_assert( sc == 0 ); |
---|
51 | |
---|
52 | /* Verify pthread_setaffinity_np checks that at least one cpu is set */ |
---|
53 | CPU_ZERO(&cpuset); |
---|
54 | puts( "Init - pthread_setaffinity_np - no cpu - EINVAL" ); |
---|
55 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset ); |
---|
56 | rtems_test_assert( sc == EINVAL ); |
---|
57 | |
---|
58 | /* Verify pthread_setaffinity_np checks that at thread id is valid */ |
---|
59 | CPU_ZERO(&cpuset); |
---|
60 | CPU_SET(0, &cpuset); |
---|
61 | puts( "Init - pthread_setaffinity_np - Invalid thread - ESRCH" ); |
---|
62 | sc = pthread_setaffinity_np( 999, sizeof(cpu_set_t), &cpuset ); |
---|
63 | rtems_test_assert( sc == ESRCH ); |
---|
64 | |
---|
65 | /* Verify pthread_setaffinity_np validates cpusetsize */ |
---|
66 | puts( "Init - pthread_setaffinity_np - Invalid cpusetsize - EINVAL" ); |
---|
67 | sc = pthread_setaffinity_np( Init_id, 1, &cpuset ); |
---|
68 | rtems_test_assert( sc == EINVAL ); |
---|
69 | |
---|
70 | /* Verify pthread_setaffinity_np validates cpuset */ |
---|
71 | puts( "Init - pthread_setaffinity_np - Invalid cpuset - EFAULT" ); |
---|
72 | sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), NULL ); |
---|
73 | rtems_test_assert( sc == EFAULT ); |
---|
74 | } |
---|
75 | |
---|
76 | void Validate_getaffinity_errors(void) |
---|
77 | { |
---|
78 | int sc; |
---|
79 | cpu_set_t cpuset; |
---|
80 | |
---|
81 | /* Verify pthread_getaffinity_np checks that at thread id is valid */ |
---|
82 | CPU_ZERO(&cpuset); |
---|
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, 1, &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 | |
---|
115 | sc = pthread_attr_setstack( &attr, NULL, 0 ); |
---|
116 | rtems_test_assert( sc == 0 ); |
---|
117 | |
---|
118 | sc = pthread_attr_getschedparam( &attr, ¶m ); |
---|
119 | rtems_test_assert( sc == 0 ); |
---|
120 | param.sched_priority = sched_get_priority_max( SCHED_FIFO ); |
---|
121 | sc = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); |
---|
122 | rtems_test_assert( !sc ); |
---|
123 | |
---|
124 | sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset0 ); |
---|
125 | rtems_test_assert( !sc ); |
---|
126 | |
---|
127 | /* Get the number of processors that we are using. */ |
---|
128 | cpu_count = rtems_get_processor_count(); |
---|
129 | |
---|
130 | /* Fill the remaining cpus with med priority tasks */ |
---|
131 | puts( "Init - Create Medium priority tasks"); |
---|
132 | for (i=0; i<(cpu_count-1); i++){ |
---|
133 | sc = pthread_create( &Med_id[i], &attr, Thread_1, NULL ); |
---|
134 | rtems_test_assert( !sc ); |
---|
135 | } |
---|
136 | |
---|
137 | puts( "Init - Verify Medium priority tasks"); |
---|
138 | for (i=0; i<(cpu_count-1); i++){ |
---|
139 | sc = pthread_getaffinity_np( Med_id[i], sizeof(cpu_set_t), &cpuset2 ); |
---|
140 | rtems_test_assert( !sc ); |
---|
141 | rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) ); |
---|
142 | } |
---|
143 | |
---|
144 | /* |
---|
145 | * Create low priority thread for each remaining cpu with the affinity |
---|
146 | * set to only run on one cpu. |
---|
147 | */ |
---|
148 | puts( "Init - Create Low priority tasks"); |
---|
149 | for (i=0; i<cpu_count; i++){ |
---|
150 | CPU_ZERO(&cpuset1); |
---|
151 | CPU_SET(i, &cpuset1); |
---|
152 | |
---|
153 | sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &cpuset1 ); |
---|
154 | rtems_test_assert( !sc ); |
---|
155 | |
---|
156 | sc = pthread_create( &Low_id[i], &attr, Thread_1, NULL ); |
---|
157 | rtems_test_assert( !sc ); |
---|
158 | } |
---|
159 | |
---|
160 | /* Verify affinity on low priority tasks */ |
---|
161 | puts( "Init - Verify Low priority tasks"); |
---|
162 | for (i=0; i<(cpu_count-1); i++){ |
---|
163 | CPU_ZERO(&cpuset1); |
---|
164 | CPU_SET(i, &cpuset1); |
---|
165 | |
---|
166 | sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 ); |
---|
167 | rtems_test_assert( !sc ); |
---|
168 | rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) ); |
---|
169 | } |
---|
170 | |
---|
171 | /* Change the affinity for each low priority task */ |
---|
172 | puts("Init - Change affinity on Low priority tasks"); |
---|
173 | CPU_COPY(&cpuset0, &cpuset1); |
---|
174 | for (i=0; i<cpu_count; i++){ |
---|
175 | |
---|
176 | CPU_CLR(i, &cpuset1); |
---|
177 | sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset1 ); |
---|
178 | |
---|
179 | /* Verify no cpu's are now set in the cpuset */ |
---|
180 | if (i== (cpu_count-1)) { |
---|
181 | rtems_test_assert( sc == EINVAL ); |
---|
182 | sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset0 ); |
---|
183 | } |
---|
184 | rtems_test_assert( !sc ); |
---|
185 | } |
---|
186 | |
---|
187 | puts("Init - Validate affinity on Low priority tasks"); |
---|
188 | CPU_COPY(&cpuset0, &cpuset1); |
---|
189 | for (i=0; i<cpu_count; i++){ |
---|
190 | CPU_CLR(i, &cpuset1); |
---|
191 | |
---|
192 | sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 ); |
---|
193 | rtems_test_assert( !sc ); |
---|
194 | if (i== (cpu_count-1)) |
---|
195 | rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) ); |
---|
196 | else |
---|
197 | rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) ); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | void *POSIX_Init( |
---|
202 | void *ignored |
---|
203 | ) |
---|
204 | { |
---|
205 | TEST_BEGIN(); |
---|
206 | |
---|
207 | /* Initialize thread id */ |
---|
208 | Init_id = pthread_self(); |
---|
209 | |
---|
210 | Validate_setaffinity_errors(); |
---|
211 | Validate_getaffinity_errors(); |
---|
212 | Validate_affinity(); |
---|
213 | |
---|
214 | TEST_END(); |
---|
215 | rtems_test_exit(0); |
---|
216 | } |
---|
217 | |
---|
218 | /* configuration information */ |
---|
219 | |
---|
220 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
221 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
222 | |
---|
223 | #define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP |
---|
224 | |
---|
225 | #define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS |
---|
226 | |
---|
227 | #define CONFIGURE_MAXIMUM_POSIX_THREADS (NUM_CPUS*2) |
---|
228 | |
---|
229 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
230 | |
---|
231 | #define CONFIGURE_INIT |
---|
232 | #include <rtems/confdefs.h> |
---|
233 | |
---|
234 | /* global variables */ |
---|