source: rtems/testsuites/smptests/smpschedaffinity05/init.c @ ed19002

5
Last change on this file since ed19002 was ed19002, checked in by Sebastian Huber <sebastian.huber@…>, on 10/12/16 at 08:37:07

smpschedaffinity05: Fix configuration

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 *  COPYRIGHT (c) 2014.
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/*
11 * Start 4 tasks with affinity for each of the 4 cpus.
12 * Allow tasks to set their actual cpu value and delete themselves.
13 * Verify the actual cpu values match the expected cpu values.
14 *
15 * Init task is at a lower priority 8 and the threads
16 * with affinity are at priority 4, so the affinity task
17 * on the core init is running on will preempt it.
18 *
19 * Test tasks run and delete themselves.
20 * Init task never blocks.
21 */
22
23#ifdef HAVE_CONFIG_H
24  #include "config.h"
25#endif
26
27#include <rtems.h>
28
29#include "tmacros.h"
30
31const char rtems_test_name[] = "SMPSCHEDAFFINITY 5";
32
33#define NUM_CPUS   4
34#define TASK_COUNT 5
35
36struct task_data_t {
37  rtems_id            id;
38  cpu_set_t           cpuset;
39  rtems_task_priority priority;
40  bool                ran;
41  int                 expected_cpu;
42  int                 actual_cpu;
43  int                 migrate_cpu;
44};
45
46static struct task_data_t task_data[TASK_COUNT] = {
47  {0x0, {{0xc}}, 7, false,  3, -1,  2},
48  {0x0, {{0xf}}, 8, false,  2, -1, -1},
49  {0x0, {{0x3}}, 5, false,  1, -1,  0},
50  {0x0, {{0x9}}, 6, false,  0, -1,  3},
51  {0x0, {{0x2}}, 9, false, -1, -1,  1}
52};
53
54rtems_id           task_sem;
55 
56static void verify_tasks(void);
57
58/*
59 * Spin loop to allow tasks to delay without yeilding the
60 * processor.
61 */
62static void test_delay(int ticks)
63{
64  rtems_interval start, stop;
65  start = rtems_clock_get_ticks_since_boot();
66  do {
67    stop = rtems_clock_get_ticks_since_boot();
68  } while ( (stop - start) < ticks );
69}
70
71static void task(rtems_task_argument arg)
72{
73  rtems_status_code   sc;
74
75  while (true) {
76    sc = rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0);
77    if (sc == RTEMS_SUCCESSFUL) {
78      task_data[arg].ran = true;
79      task_data[arg].actual_cpu = rtems_get_current_processor();
80      rtems_semaphore_release(task_sem);
81    }
82  }
83}
84static void verify_tasks(void)
85{
86  int i;
87
88  printf("Verify Tasks Ran\n");
89 
90  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
91 
92  /* Set Init task data */
93  task_data[0].ran = true;
94  task_data[0].actual_cpu = rtems_get_current_processor();
95
96  /* Verify all tasks */
97  for (i = 0; i < NUM_CPUS; i++) {
98    if (i==0)
99      printf("Init(%d): ran=%d expected=%d actual=%d\n",
100        task_data[i].priority,
101        task_data[i].ran,
102        task_data[i].expected_cpu,
103        task_data[i].actual_cpu
104      );
105    else
106      printf( "TA0%d(%d): ran=%d expected=%d actual=%d\n",
107        i,
108        task_data[i].priority,
109        task_data[i].ran,
110        task_data[i].expected_cpu,
111        task_data[i].actual_cpu
112      );
113
114    /*  Abort test if values are not as expected */
115    if ( task_data[i].expected_cpu == -1 )
116      rtems_test_assert( task_data[i].ran == false );
117    else {
118      rtems_test_assert( task_data[i].ran == true );
119      rtems_test_assert( task_data[i].expected_cpu == task_data[i].actual_cpu );
120    }
121  }
122
123  rtems_semaphore_release(task_sem);
124}
125
126static void test(void)
127{
128  rtems_status_code   sc;
129  rtems_task_argument i;
130  size_t              size;
131  uint32_t            cpu_count;
132  rtems_task_priority priority;
133
134  /* Get the number of processors that we are using. */
135  cpu_count = rtems_get_processor_count();
136  if (cpu_count != 4) {
137    printf("Test requires a minimum of 4 cores\n");
138    return;
139  }
140
141  size = sizeof(cpu_set_t);
142  task_data[0].id = rtems_task_self();
143  printf("Create Semaphore\n");
144
145  sc = rtems_semaphore_create( 
146    rtems_build_name('S', 'E', 'M', '0'),
147    1,                                               /* initial count = 1 */
148    RTEMS_BINARY_SEMAPHORE |
149    RTEMS_PRIORITY |
150    RTEMS_PRIORITY_CEILING,
151    0,
152    &task_sem
153  );
154  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
155
156
157  /* Create and start tasks on each cpu with the appropriate affinity. */
158  for (i = 1; i < TASK_COUNT; i++) {
159
160      sc = rtems_task_create(
161        rtems_build_name('T', 'A', '0', '0'+i),
162        task_data[ i ].priority,
163        RTEMS_MINIMUM_STACK_SIZE,
164        RTEMS_DEFAULT_MODES,
165        RTEMS_DEFAULT_ATTRIBUTES,
166        &task_data[ i ].id
167      );
168      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
169 
170      sc = rtems_task_set_affinity(
171        task_data[ i ].id,
172        size,
173        &task_data[i].cpuset
174      );
175      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
176     
177      printf(
178        "Start TA%d at priority %d on cpu %d\n",
179         i,
180         task_data[i].priority,
181         task_data[i].expected_cpu
182      );
183      sc = rtems_task_start( task_data[ i ].id, task, i );
184      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
185  }
186
187  /* spin for 100 ticks */
188  test_delay(100);
189 
190  verify_tasks();
191
192  i = TASK_COUNT - 1;
193  task_data[ i ].priority = 4;
194  printf("Set TA%d priority %d\n", i,task_data[i].priority );
195  sc = rtems_task_set_priority(
196    task_data[ i ].id,
197    task_data[ i ].priority,
198    &priority
199  );
200  test_delay(25);
201
202  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
203  for (i = 0; i < TASK_COUNT; i++) {
204    task_data[ i ].expected_cpu = task_data[ i ].migrate_cpu;
205    task_data[ i ].actual_cpu = -1;
206    task_data[ i ].ran = false;
207  }
208  rtems_semaphore_release(task_sem);
209  test_delay(25);
210  verify_tasks();
211}
212
213static void Init(rtems_task_argument arg)
214{
215  TEST_BEGIN();
216
217  test();
218
219  TEST_END();
220  rtems_test_exit(0);
221}
222
223#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
224#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
225
226#define CONFIGURE_SMP_APPLICATION
227
228#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
229
230#define CONFIGURE_SMP_MAXIMUM_PROCESSORS NUM_CPUS
231
232#define CONFIGURE_MAXIMUM_TASKS          TASK_COUNT
233
234#define CONFIGURE_MAXIMUM_SEMAPHORES 1
235
236#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
237
238#define CONFIGURE_INIT_TASK_PRIORITY       7
239#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
240
241#define CONFIGURE_INIT
242
243#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.