summaryrefslogtreecommitdiffstats
path: root/testsuites/smptests/smppsxaffinity02/init.c
blob: 12f91124b34c78e06d4beed442fc46909f99a017 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define NUM_CPUS   4

#define  _GNU_SOURCE

#include <tmacros.h>
#include <errno.h>
#include <pthread.h>
#include <sched.h>

const char rtems_test_name[] = "SMPPSXAFFINITY 2";

#if HAVE_DECL_PTHREAD_GETAFFINITY_NP

pthread_t           Init_id;
pthread_t           Med_id[NUM_CPUS-1];
pthread_t           Low_id[NUM_CPUS];

/* forward declarations to avoid warnings */
void *POSIX_Init(void *argument);
void Validate_setaffinity_errors(void);
void Validate_getaffinity_errors(void);
void Validate_affinity(void);
void *Thread_1(void *unused);

void *Thread_1(void *unused)
{
  while(1);
}

void Validate_setaffinity_errors(void)
{
  int                 sc;
  cpu_set_t           cpuset;

  /* Verify pthread_setaffinity_np checks that more cpu's don't hurt. */
  CPU_FILL(&cpuset);
  puts( "Init - pthread_setaffinity_np - Lots of cpus - SUCCESS" );
  sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset );
  rtems_test_assert( sc == 0 );

  /* Verify pthread_setaffinity_np checks that at least one cpu is set */
  CPU_ZERO(&cpuset);
  puts( "Init - pthread_setaffinity_np - no cpu - EINVAL" );
  sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset );
  rtems_test_assert( sc == EINVAL );

  /* Verify pthread_setaffinity_np checks that at thread id is valid */
  CPU_ZERO(&cpuset);
  CPU_SET(0, &cpuset);
  puts( "Init - pthread_setaffinity_np - Invalid thread - ESRCH" );
  sc = pthread_setaffinity_np( 999, sizeof(cpu_set_t), &cpuset );
  rtems_test_assert( sc == ESRCH );

  /* Verify pthread_setaffinity_np validates cpusetsize */
  puts( "Init - pthread_setaffinity_np - Invalid cpusetsize - EINVAL" );
  sc = pthread_setaffinity_np( Init_id,  1, &cpuset );
  rtems_test_assert( sc == EINVAL );

  /* Verify pthread_setaffinity_np validates cpuset */
  puts( "Init - pthread_setaffinity_np - Invalid cpuset - EFAULT" );
  sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), NULL );
  rtems_test_assert( sc == EFAULT );
}

void Validate_getaffinity_errors(void)
{
  int                 sc;
  cpu_set_t           cpuset;

  /* Verify pthread_getaffinity_np checks that at thread id is valid */
  CPU_ZERO(&cpuset);
  CPU_SET(0, &cpuset);
  puts( "Init - pthread_getaffinity_np - Invalid thread - ESRCH" );
  sc = pthread_getaffinity_np( 999, sizeof(cpu_set_t), &cpuset );
  rtems_test_assert( sc == ESRCH );

  /* Verify pthread_getaffinity_np validates cpusetsize */
  puts( "Init - pthread_getaffinity_np - Invalid cpusetsize - EINVAL" );
  sc = pthread_getaffinity_np( Init_id,  1, &cpuset );
  rtems_test_assert( sc == EINVAL );

  /* Verify pthread_getaffinity_np validates cpuset */
  puts("Init - pthread_getaffinity_np - Invalid cpuset - EFAULT");
  sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), NULL );
  rtems_test_assert( sc == EFAULT );
}

void Validate_affinity(void )
{
  pthread_attr_t       attr;
  cpu_set_t            cpuset0;
  cpu_set_t            cpuset1;
  cpu_set_t            cpuset2;
  uint32_t             i;
  int                  sc;
  int                  cpu_count;
  struct sched_param   param;


  puts( "Init - Set Init priority to high");
  sc = pthread_getattr_np( Init_id, &attr );
  rtems_test_assert( sc == 0 );

  sc = pthread_attr_setstack( &attr, NULL, 0 );
  rtems_test_assert( sc == 0 );

  sc = pthread_attr_getschedparam( &attr, &param );
  rtems_test_assert( sc == 0 );
  param.sched_priority = sched_get_priority_max( SCHED_FIFO );
  sc = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
  rtems_test_assert( !sc );

  sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset0 );
  rtems_test_assert( !sc );

  /* Get the number of processors that we are using. */
  cpu_count = rtems_get_processor_count();

  /* Fill the remaining cpus with med priority tasks */
  puts( "Init - Create Medium priority tasks");
  for (i=0; i<(cpu_count-1); i++){
    sc = pthread_create( &Med_id[i], &attr, Thread_1, NULL );
    rtems_test_assert( !sc );
  }

  puts( "Init - Verify Medium priority tasks");
  for (i=0; i<(cpu_count-1); i++){
    sc = pthread_getaffinity_np( Med_id[i], sizeof(cpu_set_t), &cpuset2 );
    rtems_test_assert( !sc );
    rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
  }

  /*
   * Create low priority thread for each remaining cpu with the affinity
   * set to only run on one cpu.
   */
  puts( "Init - Create  Low priority tasks");
  for (i=0; i<cpu_count; i++){
    CPU_ZERO(&cpuset1);
    CPU_SET(i, &cpuset1);

    sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &cpuset1 );
    rtems_test_assert( !sc );

    sc = pthread_create( &Low_id[i], &attr, Thread_1, NULL );
    rtems_test_assert( !sc );
  }

  /* Verify affinity on low priority tasks */
  puts( "Init - Verify Low priority tasks");
  for (i=0; i<(cpu_count-1); i++){
    CPU_ZERO(&cpuset1);
    CPU_SET(i, &cpuset1);

    sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
    rtems_test_assert( !sc );
    rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
  }

  /* Change the affinity for each low priority task */
  puts("Init - Change affinity on Low priority tasks");
  CPU_COPY(&cpuset0, &cpuset1);
  for (i=0; i<cpu_count; i++){

    CPU_CLR(i, &cpuset1);
    sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset1 );

    /* Verify no cpu's are now set in the cpuset */
    if (i== (cpu_count-1)) {
      rtems_test_assert( sc == EINVAL );
      sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset0 );
    }
    rtems_test_assert( !sc );
  }

  puts("Init - Validate affinity on Low priority tasks");
  CPU_COPY(&cpuset0, &cpuset1);
  for (i=0; i<cpu_count; i++){
    CPU_CLR(i, &cpuset1);

    sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
    rtems_test_assert( !sc );
    if (i== (cpu_count-1))
      rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
    else
      rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
  }
}

void *POSIX_Init(
  void *ignored
)
{
  TEST_BEGIN();

  /* Initialize thread id */
  Init_id = pthread_self();

  Validate_setaffinity_errors();
  Validate_getaffinity_errors();
  Validate_affinity();

  TEST_END();
  rtems_test_exit(0);
}

#else
void *POSIX_Init(
  void *ignored
)
{
  TEST_BEGIN();
  puts( " Affinity NOT Supported");
  TEST_END();
  rtems_test_exit(0);
}

#endif
/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER

#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP

#define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS

#define CONFIGURE_MAXIMUM_POSIX_THREADS (NUM_CPUS*2)

#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_INIT
#include <rtems/confdefs.h>

/* global variables */