source: rtems/testsuites/smptests/smpaffinity01/init.c @ 16aaf73b

Last change on this file since 16aaf73b was 16aaf73b, checked in by Sebastian Huber <sebastian.huber@…>, on Oct 11, 2017 at 6:33:19 AM

smpaffinity01: Fix test case

Update #2514.

  • Property mode set to 100644
File size: 7.4 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#include "tmacros.h"
17
18const char rtems_test_name[] = "SMPAFFINITY 1";
19
20rtems_id           Init_id;
21rtems_id           Med_id[NUM_CPUS-1];
22rtems_id           Low_id[NUM_CPUS];
23
24/* forward declarations to avoid warnings */
25void Task_1(rtems_task_argument arg);
26void Validate_setaffinity_errors(void);
27void Validate_getaffinity_errors(void);
28void Validate_affinity(void);
29
30void Task_1(rtems_task_argument arg)
31{
32  while(1);
33}
34
35void Validate_setaffinity_errors(void)
36{
37  int                 sc;
38  cpu_set_t           cpuset;
39
40  /* Verify rtems_task_set_affinity checks that all cpu's exist. */
41  /* Note this check assumes you are running with less than 32 CPUs */
42  CPU_FILL(&cpuset);
43  puts( "Init - rtems_task_set_affinity - Lots of cpus - SUCCESS" );
44  sc = rtems_task_set_affinity( Init_id, sizeof(cpu_set_t), &cpuset );
45  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
46
47  /* Verify rtems_task_set_affinity checks that at least one cpu is set */
48  CPU_ZERO(&cpuset);
49  puts( "Init - rtems_task_set_affinity - no cpu - RTEMS_INVALID_NUMBER" );
50  sc = rtems_task_set_affinity( Init_id, sizeof(cpu_set_t), &cpuset );
51  rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
52
53  /* Verify rtems_task_set_affinity checks that at thread id is valid */
54  CPU_ZERO(&cpuset);
55  CPU_SET(0, &cpuset);
56  puts( "Init - rtems_task_set_affinity - Invalid thread - RTEMS_INVALID_ID" );
57  sc = rtems_task_set_affinity( 999, sizeof(cpu_set_t), &cpuset );
58  rtems_test_assert( sc == RTEMS_INVALID_ID );
59
60  /* Verify rtems_task_set_affinity validates cpusetsize */
61  puts( "Init - rtems_task_set_affinity - Invalid cpusetsize - RTEMS_INVALID_NUMBER" );
62  sc = rtems_task_set_affinity( Init_id,  1, &cpuset );
63  rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
64
65  /* Verifyrtems_task_set_affinity validates cpuset */
66  puts( "Init - rtems_task_set_affinity - Invalid cpuset - RTEMS_INVALID_ADDRESS" );
67  sc = rtems_task_set_affinity( Init_id, sizeof(cpu_set_t), NULL );
68  rtems_test_assert( sc == RTEMS_INVALID_ADDRESS );
69}
70
71void Validate_getaffinity_errors(void)
72{
73  int                 sc;
74  cpu_set_t           cpuset;
75
76  /* Verify rtems_task_get_affinity checks that at thread id is valid */
77  CPU_ZERO(&cpuset);
78  CPU_SET(0, &cpuset);
79  puts( "Init - rtems_task_get_affinity - Invalid thread - RTEMS_INVALID_ID" );
80  sc = rtems_task_get_affinity( 999, sizeof(cpu_set_t), &cpuset );
81  rtems_test_assert( sc == RTEMS_INVALID_ID );
82
83  /* Verify rtems_task_get_affinity validates cpusetsize */
84  puts(
85    "Init - rtems_task_get_affinity - Invalid cpusetsize - RTEMS_INVALID_NUMBER"
86  );
87  sc = rtems_task_get_affinity( Init_id,  1, &cpuset );
88  rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
89
90  /* Verify rtems_task_get_affinity validates cpuset */
91  puts("Init - rtems_task_get_affinity - Invalid cpuset - RTEMS_INVALID_ADDRESS");
92  sc = rtems_task_get_affinity( Init_id, sizeof(cpu_set_t), NULL );
93  rtems_test_assert( sc == RTEMS_INVALID_ADDRESS );
94}
95
96void Validate_affinity(void )
97{
98  cpu_set_t            cpuset0;
99  cpu_set_t            cpuset1;
100  cpu_set_t            cpuset2;
101  uint32_t             i;
102  int                  sc;
103  int                  cpu_count;
104  rtems_task_priority  priority;
105  char                 ch[2];
106
107  puts( "Init - Set Init priority to high");
108  sc = rtems_task_set_priority( Init_id, 1, &priority );
109  directive_failed( sc, "Set Init Priority" );
110
111  sc = rtems_task_get_affinity( Init_id, sizeof(cpu_set_t), &cpuset0 );
112  directive_failed( sc, "Get Affinity of Init Task" );
113
114  /* Get the number of processors that we are using. */
115  cpu_count = rtems_get_processor_count();
116
117  /* Fill the remaining cpus with med priority tasks */
118  puts( "Init - Create Medium priority tasks");
119  for (i=0; i<(cpu_count-1); i++){
120    sprintf(ch, "%01" PRId32, i+1 );
121    sc = rtems_task_create(
122      rtems_build_name( 'C', 'P', 'U', ch[0] ),
123      2,
124      RTEMS_MINIMUM_STACK_SIZE,
125      RTEMS_DEFAULT_MODES,
126      RTEMS_DEFAULT_ATTRIBUTES,
127      &Med_id[i]
128    );
129    directive_failed( sc, "task create" );
130
131    sc = rtems_task_start( Med_id[i], Task_1, i+1 );
132    directive_failed( sc, "task start" );
133
134    sc = rtems_task_get_affinity( Med_id[i], sizeof(cpu_set_t), &cpuset2 );
135    directive_failed( sc, "Get Affinity of Medium Priority Task" );
136    rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
137  }
138
139  /*
140   * Create low priority thread for each remaining cpu with the affinity
141   * set to only run on one cpu.
142   */
143  puts( "Init - Create  Low priority tasks");
144  for (i=0; i<cpu_count; i++){
145    CPU_ZERO(&cpuset1);
146    CPU_SET(i, &cpuset1);
147
148    sprintf(ch, "%01" PRId32, (uint32_t) 0 );
149    sc = rtems_task_create(
150      rtems_build_name( 'X', 'T', 'R', ch[0] ),
151      10,
152      RTEMS_MINIMUM_STACK_SIZE,
153      RTEMS_DEFAULT_MODES,
154      RTEMS_DEFAULT_ATTRIBUTES,
155      &Low_id[i]
156    );
157    directive_failed( sc, "task create" );
158
159    sc = rtems_task_set_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset1 );
160    directive_failed( sc, "Low priority task set affinity" );
161
162    sc = rtems_task_start( Low_id[i], Task_1, i+1 );
163    directive_failed( sc, "task start" );
164  }
165
166
167  /* Verify affinity on low priority tasks */
168  puts("Init - Verify affinity on Low priority tasks");
169  for (i=0; i<cpu_count; i++){
170    CPU_ZERO(&cpuset1);
171    CPU_SET(i, &cpuset1);
172
173    sc = rtems_task_get_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
174    directive_failed( sc, "Low priority task get affinity" );
175    rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
176  }
177
178  /* Change the affinity for each low priority task */
179  puts("Init - Change affinity on Low priority tasks");
180  CPU_COPY(&cpuset0, &cpuset1);
181  for (i=0; i<cpu_count; i++){
182
183    CPU_CLR(i, &cpuset1);
184    sc = rtems_task_set_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset1 );
185
186    /* Verify no cpu's are now set in the cpuset */
187    if (i== (cpu_count-1)) {
188      rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
189      sc = rtems_task_set_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset0 );
190    }
191
192    directive_failed( sc, "Low priority task set affinity" );
193  }
194
195  puts("Init - Validate affinity on Low priority tasks");
196  CPU_COPY(&cpuset0, &cpuset1);
197  for (i=0; i<cpu_count; i++){
198    CPU_CLR(i, &cpuset1);
199
200    sc = rtems_task_get_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
201    directive_failed( sc, "Low priority task get affinity" );
202    if (i== (cpu_count-1))
203      rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
204    else
205      rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
206  }
207}
208
209static void Init(rtems_task_argument arg)
210{
211  int                  sc;
212
213  TEST_BEGIN();
214
215  /* Initialize thread id */
216  sc = rtems_task_ident( RTEMS_SELF, RTEMS_SEARCH_ALL_NODES, &Init_id );
217  directive_failed( sc, "Identify Init Task" );
218
219  Validate_setaffinity_errors();
220  Validate_getaffinity_errors();
221  Validate_affinity();
222
223  TEST_END();
224  rtems_test_exit(0);
225}
226
227#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
228#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
229
230#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
231
232#define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS
233
234#define CONFIGURE_MAXIMUM_TASKS         (NUM_CPUS*2)
235
236#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
237
238#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
239
240#define CONFIGURE_INIT
241
242#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.