source: rtems/testsuites/smptests/smppsxaffinity02/init.c @ 2ef0328

4.115
Last change on this file since 2ef0328 was 2ef0328, checked in by Jennifer Averett <jennifer.averett@…>, on 02/26/14 at 15:57:16

smptests: Add smppsxaffinity02.

This method exercises the ability to dynamically get and set
the affinity of POSIX threads.

NOTE: There is no scheduler support for affinity. This is
simply a data integrity test.

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