source: rtems/testsuites/smptests/smpschedaffinity05/init.c @ 7eb3fd47

4.115
Last change on this file since 7eb3fd47 was 7eb3fd47, checked in by Jennifer Averett <jennifer.averett@…>, on 07/10/14 at 16:45:58

smpschedaffinity05: Add test for worst case migration for affintiy scheduler.

This test uses a combination of priority and affinity to cause
the tasks running on all 4 cores to change due to one task priority
change.

  • Property mode set to 100644
File size: 6.0 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_LOCAL                   |
149    RTEMS_SIMPLE_BINARY_SEMAPHORE |
150    RTEMS_NO_INHERIT_PRIORITY     |
151    RTEMS_NO_PRIORITY_CEILING     |
152    RTEMS_FIFO,
153    0,
154    &task_sem
155  ); 
156  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
157
158
159  /* Create and start tasks on each cpu with the appropriate affinity. */
160  for (i = 1; i < TASK_COUNT; i++) {
161
162      sc = rtems_task_create(
163        rtems_build_name('T', 'A', '0', '0'+i),
164        task_data[ i ].priority,
165        RTEMS_MINIMUM_STACK_SIZE,
166        RTEMS_DEFAULT_MODES,
167        RTEMS_DEFAULT_ATTRIBUTES,
168        &task_data[ i ].id
169      );
170      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
171 
172      sc = rtems_task_set_affinity(
173        task_data[ i ].id,
174        size,
175        &task_data[i].cpuset
176      );
177      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
178     
179      printf(
180        "Start TA%d at priority %d on cpu %d\n",
181         i,
182         task_data[i].priority,
183         task_data[i].expected_cpu
184      );
185      sc = rtems_task_start( task_data[ i ].id, task, i );
186      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
187  }
188
189  /* spin for 100 ticks */
190  test_delay(100);
191 
192  verify_tasks();
193
194  i = TASK_COUNT - 1;
195  task_data[ i ].priority = 4;
196  printf("Set TA%d priority %d\n", i,task_data[i].priority );
197  sc = rtems_task_set_priority(
198    task_data[ i ].id,
199    task_data[ i ].priority,
200    &priority
201  );
202  test_delay(25);
203
204  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
205  for (i = 0; i < TASK_COUNT; i++) {
206    task_data[ i ].expected_cpu = task_data[ i ].migrate_cpu;
207    task_data[ i ].actual_cpu = -1;
208    task_data[ i ].ran = false;
209  }
210  rtems_semaphore_release(task_sem);
211  test_delay(25);
212  verify_tasks();
213}
214
215static void Init(rtems_task_argument arg)
216{
217  TEST_BEGIN();
218
219  test();
220
221  TEST_END();
222  rtems_test_exit(0);
223}
224
225#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
226#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
227
228#define CONFIGURE_SMP_APPLICATION
229
230#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
231
232#define CONFIGURE_SMP_MAXIMUM_PROCESSORS NUM_CPUS
233
234#define CONFIGURE_MAXIMUM_TASKS          TASK_COUNT
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.