source: rtems/testsuites/smptests/smppsxaffinity02/init.c @ a3ad4af

5
Last change on this file since a3ad4af was a3ad4af, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/17 at 09:22:21

posix: Validate affinity sets by the scheduler

Update #2514.

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